30062/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-19 18:57:13 +00:00

395 lines
8.5 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Activities = db.activities;
const Contacts = db.contacts;
const Leads = db.leads;
const Notes = db.notes;
const ActivitiesData = [
{
description: 'Initial contact with Acme Corp',
activity_date: new Date('2023-10-01T10:30:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
description: 'Follow-up call with Beta LLC',
activity_date: new Date('2023-10-02T11:30:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
description: 'Meeting with Gamma Inc',
activity_date: new Date('2023-10-03T12:30:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const ContactsData = [
{
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@acme.com',
// type code here for "relation_one" field
},
{
first_name: 'Alice',
last_name: 'Smith',
email: 'alice.smith@beta.com',
// type code here for "relation_one" field
},
{
first_name: 'Bob',
last_name: 'White',
email: 'bob.white@gamma.com',
// type code here for "relation_one" field
},
];
const LeadsData = [
{
name: 'Acme Corp',
status: 'New',
category: 'Individual',
// type code here for "relation_one" field
created_date: new Date('2023-10-01T10:00:00Z'),
follow_up_date: new Date('2023-10-10T10:00:00Z'),
},
{
name: 'Beta LLC',
status: 'Lost',
category: 'Corporate',
// type code here for "relation_one" field
created_date: new Date('2023-10-02T11:00:00Z'),
follow_up_date: new Date('2023-10-12T11:00:00Z'),
},
{
name: 'Gamma Inc',
status: 'Qualified',
category: 'Corporate',
// type code here for "relation_one" field
created_date: new Date('2023-10-03T12:00:00Z'),
follow_up_date: new Date('2023-10-13T12:00:00Z'),
},
];
const NotesData = [
{
content: 'Acme Corp is interested in our premium package',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content: 'Beta LLC requires a custom solution',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
content: 'Gamma Inc has a tight deadline',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associateActivityWithLead() {
const relatedLead0 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity0 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Activity0?.setLead) {
await Activity0.setLead(relatedLead0);
}
const relatedLead1 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity1 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Activity1?.setLead) {
await Activity1.setLead(relatedLead1);
}
const relatedLead2 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity2 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Activity2?.setLead) {
await Activity2.setLead(relatedLead2);
}
}
async function associateActivityWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity0 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Activity0?.setUser) {
await Activity0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity1 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Activity1?.setUser) {
await Activity1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Activity2 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Activity2?.setUser) {
await Activity2.setUser(relatedUser2);
}
}
async function associateContactWithLead() {
const relatedLead0 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Contact0 = await Contacts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Contact0?.setLead) {
await Contact0.setLead(relatedLead0);
}
const relatedLead1 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Contact1 = await Contacts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Contact1?.setLead) {
await Contact1.setLead(relatedLead1);
}
const relatedLead2 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Contact2 = await Contacts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Contact2?.setLead) {
await Contact2.setLead(relatedLead2);
}
}
async function associateLeadWithOwner() {
const relatedOwner0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Lead0 = await Leads.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Lead0?.setOwner) {
await Lead0.setOwner(relatedOwner0);
}
const relatedOwner1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Lead1 = await Leads.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Lead1?.setOwner) {
await Lead1.setOwner(relatedOwner1);
}
const relatedOwner2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Lead2 = await Leads.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Lead2?.setOwner) {
await Lead2.setOwner(relatedOwner2);
}
}
async function associateNoteWithLead() {
const relatedLead0 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Note0 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Note0?.setLead) {
await Note0.setLead(relatedLead0);
}
const relatedLead1 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Note1 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Note1?.setLead) {
await Note1.setLead(relatedLead1);
}
const relatedLead2 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Note2 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Note2?.setLead) {
await Note2.setLead(relatedLead2);
}
}
async function associateNoteWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note0 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Note0?.setUser) {
await Note0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note1 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Note1?.setUser) {
await Note1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note2 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Note2?.setUser) {
await Note2.setUser(relatedUser2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Activities.bulkCreate(ActivitiesData);
await Contacts.bulkCreate(ContactsData);
await Leads.bulkCreate(LeadsData);
await Notes.bulkCreate(NotesData);
await Promise.all([
// Similar logic for "relation_many"
await associateActivityWithLead(),
await associateActivityWithUser(),
await associateContactWithLead(),
await associateLeadWithOwner(),
await associateNoteWithLead(),
await associateNoteWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('activities', null, {});
await queryInterface.bulkDelete('contacts', null, {});
await queryInterface.bulkDelete('leads', null, {});
await queryInterface.bulkDelete('notes', null, {});
},
};