33544/backend/src/db/seeders/20231127130745-sample-data.js
2025-08-21 19:26:42 +00:00

343 lines
7.1 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: 'Meeting with Acme Corp',
activity_date: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
description: 'Call with Globex Inc',
activity_date: new Date('2023-10-02T11:30:00Z'),
// type code here for "relation_one" field
},
{
description: 'Email to Initech',
activity_date: new Date('2023-10-03T09:15:00Z'),
// type code here for "relation_one" field
},
{
description: 'Presentation to Umbrella Corp',
activity_date: new Date('2023-10-04T14:45:00Z'),
// 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_many" field
},
{
first_name: 'Jane',
last_name: 'Smith',
email: 'jane.smith@globex.com',
// type code here for "relation_many" field
},
{
first_name: 'Bob',
last_name: 'White',
email: 'bob.white@initech.com',
// type code here for "relation_many" field
},
{
first_name: 'Alice',
last_name: 'Johnson',
email: 'alice.johnson@umbrella.com',
// type code here for "relation_many" field
},
];
const LeadsData = [
{
name: 'Acme Corp',
status: 'new',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Globex Inc',
status: 'qualified',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Initech',
status: 'qualified',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
{
name: 'Umbrella Corp',
status: 'new',
// type code here for "relation_one" field
// type code here for "relation_many" field
},
];
const NotesData = [
{
content: 'Initial meeting notes with Acme Corp',
// type code here for "relation_one" field
created_on: new Date('2023-10-01T10:00:00Z'),
},
{
content: 'Follow-up call with Globex Inc',
// type code here for "relation_one" field
created_on: new Date('2023-10-02T11:30:00Z'),
},
{
content: 'Qualified lead for Initech',
// type code here for "relation_one" field
created_on: new Date('2023-10-03T09:15:00Z'),
},
{
content: 'Lost lead for Umbrella Corp',
// type code here for "relation_one" field
created_on: new Date('2023-10-04T14:45:00Z'),
},
];
// Similar logic for "relation_many"
async function associateActivityWithRelated_lead() {
const relatedRelated_lead0 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity0 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Activity0?.setRelated_lead) {
await Activity0.setRelated_lead(relatedRelated_lead0);
}
const relatedRelated_lead1 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity1 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Activity1?.setRelated_lead) {
await Activity1.setRelated_lead(relatedRelated_lead1);
}
const relatedRelated_lead2 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity2 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Activity2?.setRelated_lead) {
await Activity2.setRelated_lead(relatedRelated_lead2);
}
const relatedRelated_lead3 = await Leads.findOne({
offset: Math.floor(Math.random() * (await Leads.count())),
});
const Activity3 = await Activities.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Activity3?.setRelated_lead) {
await Activity3.setRelated_lead(relatedRelated_lead3);
}
}
// Similar logic for "relation_many"
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);
}
const relatedOwner3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Lead3 = await Leads.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Lead3?.setOwner) {
await Lead3.setOwner(relatedOwner3);
}
}
// Similar logic for "relation_many"
async function associateNoteWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note0 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Note0?.setAuthor) {
await Note0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note1 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Note1?.setAuthor) {
await Note1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note2 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Note2?.setAuthor) {
await Note2.setAuthor(relatedAuthor2);
}
const relatedAuthor3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Note3 = await Notes.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Note3?.setAuthor) {
await Note3.setAuthor(relatedAuthor3);
}
}
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 associateActivityWithRelated_lead(),
// Similar logic for "relation_many"
await associateLeadWithOwner(),
// Similar logic for "relation_many"
await associateNoteWithAuthor(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('activities', null, {});
await queryInterface.bulkDelete('contacts', null, {});
await queryInterface.bulkDelete('leads', null, {});
await queryInterface.bulkDelete('notes', null, {});
},
};