const db = require('../models'); const Users = db.users; const BillingRecords = db.billing_records; const Customers = db.customers; const Invoices = db.invoices; const BillingRecordsData = [ { // type code here for "relation_one" field record_date: new Date('2023-10-02T10:00:00Z'), amount: 1500, status: 'Overdue', }, { // type code here for "relation_one" field record_date: new Date('2023-10-06T10:00:00Z'), amount: 2500, status: 'Pending', }, { // type code here for "relation_one" field record_date: new Date('2023-10-11T10:00:00Z'), amount: 3200, status: 'Paid', }, ]; const CustomersData = [ { name: 'John Doe', email: 'john.doe@example.com', phone: '123-456-7890', // type code here for "relation_many" field }, { name: 'Jane Smith', email: 'jane.smith@example.com', phone: '234-567-8901', // type code here for "relation_many" field }, { name: 'Acme Corp', email: 'contact@acmecorp.com', phone: '345-678-9012', // type code here for "relation_many" field }, ]; const InvoicesData = [ { invoice_number: 'INV-001', issue_date: new Date('2023-10-01T10:00:00Z'), due_date: new Date('2023-10-15T10:00:00Z'), amount: 1500, // type code here for "relation_one" field // type code here for "relation_one" field }, { invoice_number: 'INV-002', issue_date: new Date('2023-10-05T10:00:00Z'), due_date: new Date('2023-10-20T10:00:00Z'), amount: 2500, // type code here for "relation_one" field // type code here for "relation_one" field }, { invoice_number: 'INV-003', issue_date: new Date('2023-10-10T10:00:00Z'), due_date: new Date('2023-10-25T10:00:00Z'), amount: 3200, // type code here for "relation_one" field // type code here for "relation_one" field }, ]; // Similar logic for "relation_many" async function associateBillingRecordWithInvoice() { const relatedInvoice0 = await Invoices.findOne({ offset: Math.floor(Math.random() * (await Invoices.count())), }); const BillingRecord0 = await BillingRecords.findOne({ order: [['id', 'ASC']], offset: 0, }); if (BillingRecord0?.setInvoice) { await BillingRecord0.setInvoice(relatedInvoice0); } const relatedInvoice1 = await Invoices.findOne({ offset: Math.floor(Math.random() * (await Invoices.count())), }); const BillingRecord1 = await BillingRecords.findOne({ order: [['id', 'ASC']], offset: 1, }); if (BillingRecord1?.setInvoice) { await BillingRecord1.setInvoice(relatedInvoice1); } const relatedInvoice2 = await Invoices.findOne({ offset: Math.floor(Math.random() * (await Invoices.count())), }); const BillingRecord2 = await BillingRecords.findOne({ order: [['id', 'ASC']], offset: 2, }); if (BillingRecord2?.setInvoice) { await BillingRecord2.setInvoice(relatedInvoice2); } } // Similar logic for "relation_many" async function associateInvoiceWithCustomer() { const relatedCustomer0 = await Customers.findOne({ offset: Math.floor(Math.random() * (await Customers.count())), }); const Invoice0 = await Invoices.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Invoice0?.setCustomer) { await Invoice0.setCustomer(relatedCustomer0); } const relatedCustomer1 = await Customers.findOne({ offset: Math.floor(Math.random() * (await Customers.count())), }); const Invoice1 = await Invoices.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Invoice1?.setCustomer) { await Invoice1.setCustomer(relatedCustomer1); } const relatedCustomer2 = await Customers.findOne({ offset: Math.floor(Math.random() * (await Customers.count())), }); const Invoice2 = await Invoices.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Invoice2?.setCustomer) { await Invoice2.setCustomer(relatedCustomer2); } } async function associateInvoiceWithBilling_manager() { const relatedBilling_manager0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Invoice0 = await Invoices.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Invoice0?.setBilling_manager) { await Invoice0.setBilling_manager(relatedBilling_manager0); } const relatedBilling_manager1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Invoice1 = await Invoices.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Invoice1?.setBilling_manager) { await Invoice1.setBilling_manager(relatedBilling_manager1); } const relatedBilling_manager2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Invoice2 = await Invoices.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Invoice2?.setBilling_manager) { await Invoice2.setBilling_manager(relatedBilling_manager2); } } module.exports = { up: async (queryInterface, Sequelize) => { await BillingRecords.bulkCreate(BillingRecordsData); await Customers.bulkCreate(CustomersData); await Invoices.bulkCreate(InvoicesData); await Promise.all([ // Similar logic for "relation_many" await associateBillingRecordWithInvoice(), // Similar logic for "relation_many" await associateInvoiceWithCustomer(), await associateInvoiceWithBilling_manager(), ]); }, down: async (queryInterface, Sequelize) => { await queryInterface.bulkDelete('billing_records', null, {}); await queryInterface.bulkDelete('customers', null, {}); await queryInterface.bulkDelete('invoices', null, {}); }, };