const db = require('../models'); const Users = db.users; const Activities = db.activities; const Companies = db.companies; const Contacts = db.contacts; const Deals = db.deals; const ActivitiesData = [ { description: 'Call with John Doe', // type code here for "relation_one" field // type code here for "relation_one" field activity_date: new Date('2023-10-01T10:30:00Z'), }, { description: 'Meeting with Jane Smith', // type code here for "relation_one" field // type code here for "relation_one" field activity_date: new Date('2023-10-02T11:30:00Z'), }, { description: 'Email to Alice Johnson', // type code here for "relation_one" field // type code here for "relation_one" field activity_date: new Date('2023-10-03T12:30:00Z'), }, ]; const CompaniesData = [ { name: 'Acme Corp', industry: 'Manufacturing', website: 'https://www.acmecorp.com', }, { name: 'Globex Inc', industry: 'Technology', website: 'https://www.globex.com', }, { name: 'Initech', industry: 'Software', website: 'https://www.initech.com', }, ]; const ContactsData = [ { first_name: 'John', last_name: 'Doe', email: 'john.doe@example.com', phone: '123-456-7890', // type code here for "relation_one" field }, { first_name: 'Jane', last_name: 'Smith', email: 'jane.smith@example.com', phone: '987-654-3210', // type code here for "relation_one" field }, { first_name: 'Alice', last_name: 'Johnson', email: 'alice.johnson@example.com', phone: '555-123-4567', // type code here for "relation_one" field }, ]; const DealsData = [ { title: 'Deal with Acme Corp', amount: 50000, stage: 'closed', // type code here for "relation_one" field // type code here for "relation_one" field start_date: new Date('2023-10-01T10:00:00Z'), end_date: new Date('2023-10-15T10:00:00Z'), }, { title: 'Globex Partnership', amount: 75000, stage: 'prospect', // type code here for "relation_one" field // type code here for "relation_one" field start_date: new Date('2023-10-02T11:00:00Z'), end_date: new Date('2023-10-16T11:00:00Z'), }, { title: 'Initech Software License', amount: 100000, stage: 'closed', // type code here for "relation_one" field // type code here for "relation_one" field start_date: new Date('2023-10-03T12:00:00Z'), end_date: new Date('2023-10-17T12:00:00Z'), }, ]; // Similar logic for "relation_many" async function associateActivityWithDeal() { const relatedDeal0 = await Deals.findOne({ offset: Math.floor(Math.random() * (await Deals.count())), }); const Activity0 = await Activities.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Activity0?.setDeal) { await Activity0.setDeal(relatedDeal0); } const relatedDeal1 = await Deals.findOne({ offset: Math.floor(Math.random() * (await Deals.count())), }); const Activity1 = await Activities.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Activity1?.setDeal) { await Activity1.setDeal(relatedDeal1); } const relatedDeal2 = await Deals.findOne({ offset: Math.floor(Math.random() * (await Deals.count())), }); const Activity2 = await Activities.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Activity2?.setDeal) { await Activity2.setDeal(relatedDeal2); } } 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 associateContactWithCompany() { const relatedCompany0 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const Contact0 = await Contacts.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Contact0?.setCompany) { await Contact0.setCompany(relatedCompany0); } const relatedCompany1 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const Contact1 = await Contacts.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Contact1?.setCompany) { await Contact1.setCompany(relatedCompany1); } const relatedCompany2 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const Contact2 = await Contacts.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Contact2?.setCompany) { await Contact2.setCompany(relatedCompany2); } } async function associateDealWithContact() { const relatedContact0 = await Contacts.findOne({ offset: Math.floor(Math.random() * (await Contacts.count())), }); const Deal0 = await Deals.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Deal0?.setContact) { await Deal0.setContact(relatedContact0); } const relatedContact1 = await Contacts.findOne({ offset: Math.floor(Math.random() * (await Contacts.count())), }); const Deal1 = await Deals.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Deal1?.setContact) { await Deal1.setContact(relatedContact1); } const relatedContact2 = await Contacts.findOne({ offset: Math.floor(Math.random() * (await Contacts.count())), }); const Deal2 = await Deals.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Deal2?.setContact) { await Deal2.setContact(relatedContact2); } } async function associateDealWithOwner() { const relatedOwner0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Deal0 = await Deals.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Deal0?.setOwner) { await Deal0.setOwner(relatedOwner0); } const relatedOwner1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Deal1 = await Deals.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Deal1?.setOwner) { await Deal1.setOwner(relatedOwner1); } const relatedOwner2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Deal2 = await Deals.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Deal2?.setOwner) { await Deal2.setOwner(relatedOwner2); } } module.exports = { up: async (queryInterface, Sequelize) => { await Activities.bulkCreate(ActivitiesData); await Companies.bulkCreate(CompaniesData); await Contacts.bulkCreate(ContactsData); await Deals.bulkCreate(DealsData); await Promise.all([ // Similar logic for "relation_many" await associateActivityWithDeal(), await associateActivityWithUser(), await associateContactWithCompany(), await associateDealWithContact(), await associateDealWithOwner(), ]); }, down: async (queryInterface, Sequelize) => { await queryInterface.bulkDelete('activities', null, {}); await queryInterface.bulkDelete('companies', null, {}); await queryInterface.bulkDelete('contacts', null, {}); await queryInterface.bulkDelete('deals', null, {}); }, };