const db = require('../models'); const Users = db.users; const Licenses = db.licenses; const Notifications = db.notifications; const Companies = db.companies; const LicensesData = [ { license_type: 'Pollution Control License', application_date: new Date('2023-01-15T10:00:00Z'), approval_date: new Date('2023-02-20T10:00:00Z'), status: 'approved', // type code here for "relation_one" field // type code here for "relation_one" field // type code here for "relation_one" field // type code here for "files" field // type code here for "relation_one" field }, { license_type: 'Water Usage License', application_date: new Date('2023-03-10T10:00:00Z'), approval_date: new Date('2023-04-15T10:00:00Z'), status: 'under_review', // type code here for "relation_one" field // type code here for "relation_one" field // type code here for "relation_one" field // type code here for "files" field // type code here for "relation_one" field }, { license_type: 'Air Quality License', application_date: new Date('2023-05-05T10:00:00Z'), approval_date: new Date('2023-06-10T10:00:00Z'), status: 'rejected', // type code here for "relation_one" field // type code here for "relation_one" field // type code here for "relation_one" field // type code here for "files" field // type code here for "relation_one" field }, ]; const NotificationsData = [ { message: 'Your Pollution Control License has been approved.', sent_at: new Date('2023-02-20T12:00:00Z'), // type code here for "relation_one" field // type code here for "relation_one" field }, { message: 'New document required for Water Usage License.', sent_at: new Date('2023-03-15T09:00:00Z'), // type code here for "relation_one" field // type code here for "relation_one" field }, { message: 'Air Quality License status updated to under review.', sent_at: new Date('2023-05-10T11:00:00Z'), // type code here for "relation_one" field // type code here for "relation_one" field }, ]; const CompaniesData = [ { name: 'Green Solutions Ltd', }, { name: 'EcoConsult Inc', }, { name: 'Sustainability Partners', }, ]; // Similar logic for "relation_many" async function associateUserWithCompany() { const relatedCompany0 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const User0 = await Users.findOne({ order: [['id', 'ASC']], offset: 0, }); if (User0?.setCompany) { await User0.setCompany(relatedCompany0); } const relatedCompany1 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const User1 = await Users.findOne({ order: [['id', 'ASC']], offset: 1, }); if (User1?.setCompany) { await User1.setCompany(relatedCompany1); } const relatedCompany2 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const User2 = await Users.findOne({ order: [['id', 'ASC']], offset: 2, }); if (User2?.setCompany) { await User2.setCompany(relatedCompany2); } } async function associateLicenseWithClient() { const relatedClient0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const License0 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 0, }); if (License0?.setClient) { await License0.setClient(relatedClient0); } const relatedClient1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const License1 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 1, }); if (License1?.setClient) { await License1.setClient(relatedClient1); } const relatedClient2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const License2 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 2, }); if (License2?.setClient) { await License2.setClient(relatedClient2); } } async function associateLicenseWithConsultant() { const relatedConsultant0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const License0 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 0, }); if (License0?.setConsultant) { await License0.setConsultant(relatedConsultant0); } const relatedConsultant1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const License1 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 1, }); if (License1?.setConsultant) { await License1.setConsultant(relatedConsultant1); } const relatedConsultant2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const License2 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 2, }); if (License2?.setConsultant) { await License2.setConsultant(relatedConsultant2); } } async function associateLicenseWithCompany() { const relatedCompany0 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const License0 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 0, }); if (License0?.setCompany) { await License0.setCompany(relatedCompany0); } const relatedCompany1 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const License1 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 1, }); if (License1?.setCompany) { await License1.setCompany(relatedCompany1); } const relatedCompany2 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const License2 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 2, }); if (License2?.setCompany) { await License2.setCompany(relatedCompany2); } } async function associateLicenseWithCompany() { const relatedCompany0 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const License0 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 0, }); if (License0?.setCompany) { await License0.setCompany(relatedCompany0); } const relatedCompany1 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const License1 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 1, }); if (License1?.setCompany) { await License1.setCompany(relatedCompany1); } const relatedCompany2 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const License2 = await Licenses.findOne({ order: [['id', 'ASC']], offset: 2, }); if (License2?.setCompany) { await License2.setCompany(relatedCompany2); } } async function associateNotificationWithRecipient() { const relatedRecipient0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Notification0 = await Notifications.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Notification0?.setRecipient) { await Notification0.setRecipient(relatedRecipient0); } const relatedRecipient1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Notification1 = await Notifications.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Notification1?.setRecipient) { await Notification1.setRecipient(relatedRecipient1); } const relatedRecipient2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Notification2 = await Notifications.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Notification2?.setRecipient) { await Notification2.setRecipient(relatedRecipient2); } } async function associateNotificationWithCompany() { const relatedCompany0 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const Notification0 = await Notifications.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Notification0?.setCompany) { await Notification0.setCompany(relatedCompany0); } const relatedCompany1 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const Notification1 = await Notifications.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Notification1?.setCompany) { await Notification1.setCompany(relatedCompany1); } const relatedCompany2 = await Companies.findOne({ offset: Math.floor(Math.random() * (await Companies.count())), }); const Notification2 = await Notifications.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Notification2?.setCompany) { await Notification2.setCompany(relatedCompany2); } } module.exports = { up: async (queryInterface, Sequelize) => { await Licenses.bulkCreate(LicensesData); await Notifications.bulkCreate(NotificationsData); await Companies.bulkCreate(CompaniesData); await Promise.all([ // Similar logic for "relation_many" await associateUserWithCompany(), await associateLicenseWithClient(), await associateLicenseWithConsultant(), await associateLicenseWithCompany(), await associateLicenseWithCompany(), await associateNotificationWithRecipient(), await associateNotificationWithCompany(), ]); }, down: async (queryInterface, Sequelize) => { await queryInterface.bulkDelete('licenses', null, {}); await queryInterface.bulkDelete('notifications', null, {}); await queryInterface.bulkDelete('companies', null, {}); }, };