const db = require('../models'); const Users = db.users; const Payments = db.payments; const Translations = db.translations; const Vocabularies = db.vocabularies; const Tenants = db.tenants; const PaymentsData = [ { amount: 10.99, payment_date: new Date('2023-10-01T10:00:00Z'), payment_method: 'M-Pesa', // type code here for "relation_one" field // type code here for "relation_one" field }, { amount: 15.5, payment_date: new Date('2023-10-02T11:30:00Z'), payment_method: 'PayPal', // type code here for "relation_one" field // type code here for "relation_one" field }, { amount: 7.25, payment_date: new Date('2023-10-03T09:45:00Z'), payment_method: 'PayPal', // type code here for "relation_one" field // type code here for "relation_one" field }, { amount: 20, payment_date: new Date('2023-10-04T14:20:00Z'), payment_method: 'M-Pesa', // type code here for "relation_one" field // type code here for "relation_one" field }, { amount: 5.75, payment_date: new Date('2023-10-05T16:10:00Z'), payment_method: 'M-Pesa', // type code here for "relation_one" field // type code here for "relation_one" field }, ]; const TranslationsData = [ { swahili_text: 'habari', translated_text: 'hello', target_language: 'Chinese', // type code here for "relation_one" field // type code here for "relation_one" field }, { swahili_text: 'asante', translated_text: 'thank you', target_language: 'Chinese', // type code here for "relation_one" field // type code here for "relation_one" field }, { swahili_text: 'samahani', translated_text: 'excuse me', target_language: 'Spanish', // type code here for "relation_one" field // type code here for "relation_one" field }, { swahili_text: 'jina lako nani', translated_text: 'what is your name', target_language: 'Spanish', // type code here for "relation_one" field // type code here for "relation_one" field }, { swahili_text: 'chakula kitamu', translated_text: 'delicious food', target_language: 'Chinese', // type code here for "relation_one" field // type code here for "relation_one" field }, ]; const VocabulariesData = [ { word: 'habari', category: 'greetings', // type code here for "relation_many" field // type code here for "relation_one" field }, { word: 'asante', category: 'common phrases', // type code here for "relation_many" field // type code here for "relation_one" field }, { word: 'samahani', category: 'common phrases', // type code here for "relation_many" field // type code here for "relation_one" field }, { word: 'jina lako nani', category: 'general conversations', // type code here for "relation_many" field // type code here for "relation_one" field }, { word: 'chakula kitamu', category: 'eating out', // type code here for "relation_many" field // type code here for "relation_one" field }, ]; const TenantsData = [ { name: 'John Dalton', }, { name: 'Ludwig Boltzmann', }, { name: 'Konrad Lorenz', }, { name: 'Isaac Newton', }, { name: 'Noam Chomsky', }, ]; // Similar logic for "relation_many" async function associateUserWithTenant() { const relatedTenant0 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const User0 = await Users.findOne({ order: [['id', 'ASC']], offset: 0, }); if (User0?.setTenant) { await User0.setTenant(relatedTenant0); } const relatedTenant1 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const User1 = await Users.findOne({ order: [['id', 'ASC']], offset: 1, }); if (User1?.setTenant) { await User1.setTenant(relatedTenant1); } const relatedTenant2 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const User2 = await Users.findOne({ order: [['id', 'ASC']], offset: 2, }); if (User2?.setTenant) { await User2.setTenant(relatedTenant2); } const relatedTenant3 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const User3 = await Users.findOne({ order: [['id', 'ASC']], offset: 3, }); if (User3?.setTenant) { await User3.setTenant(relatedTenant3); } const relatedTenant4 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const User4 = await Users.findOne({ order: [['id', 'ASC']], offset: 4, }); if (User4?.setTenant) { await User4.setTenant(relatedTenant4); } } async function associatePaymentWithUser() { const relatedUser0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Payment0 = await Payments.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Payment0?.setUser) { await Payment0.setUser(relatedUser0); } const relatedUser1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Payment1 = await Payments.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Payment1?.setUser) { await Payment1.setUser(relatedUser1); } const relatedUser2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Payment2 = await Payments.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Payment2?.setUser) { await Payment2.setUser(relatedUser2); } const relatedUser3 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Payment3 = await Payments.findOne({ order: [['id', 'ASC']], offset: 3, }); if (Payment3?.setUser) { await Payment3.setUser(relatedUser3); } const relatedUser4 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Payment4 = await Payments.findOne({ order: [['id', 'ASC']], offset: 4, }); if (Payment4?.setUser) { await Payment4.setUser(relatedUser4); } } async function associatePaymentWithTenant() { const relatedTenant0 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Payment0 = await Payments.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Payment0?.setTenant) { await Payment0.setTenant(relatedTenant0); } const relatedTenant1 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Payment1 = await Payments.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Payment1?.setTenant) { await Payment1.setTenant(relatedTenant1); } const relatedTenant2 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Payment2 = await Payments.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Payment2?.setTenant) { await Payment2.setTenant(relatedTenant2); } const relatedTenant3 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Payment3 = await Payments.findOne({ order: [['id', 'ASC']], offset: 3, }); if (Payment3?.setTenant) { await Payment3.setTenant(relatedTenant3); } const relatedTenant4 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Payment4 = await Payments.findOne({ order: [['id', 'ASC']], offset: 4, }); if (Payment4?.setTenant) { await Payment4.setTenant(relatedTenant4); } } async function associateTranslationWithUser() { const relatedUser0 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Translation0 = await Translations.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Translation0?.setUser) { await Translation0.setUser(relatedUser0); } const relatedUser1 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Translation1 = await Translations.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Translation1?.setUser) { await Translation1.setUser(relatedUser1); } const relatedUser2 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Translation2 = await Translations.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Translation2?.setUser) { await Translation2.setUser(relatedUser2); } const relatedUser3 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Translation3 = await Translations.findOne({ order: [['id', 'ASC']], offset: 3, }); if (Translation3?.setUser) { await Translation3.setUser(relatedUser3); } const relatedUser4 = await Users.findOne({ offset: Math.floor(Math.random() * (await Users.count())), }); const Translation4 = await Translations.findOne({ order: [['id', 'ASC']], offset: 4, }); if (Translation4?.setUser) { await Translation4.setUser(relatedUser4); } } async function associateTranslationWithTenant() { const relatedTenant0 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Translation0 = await Translations.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Translation0?.setTenant) { await Translation0.setTenant(relatedTenant0); } const relatedTenant1 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Translation1 = await Translations.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Translation1?.setTenant) { await Translation1.setTenant(relatedTenant1); } const relatedTenant2 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Translation2 = await Translations.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Translation2?.setTenant) { await Translation2.setTenant(relatedTenant2); } const relatedTenant3 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Translation3 = await Translations.findOne({ order: [['id', 'ASC']], offset: 3, }); if (Translation3?.setTenant) { await Translation3.setTenant(relatedTenant3); } const relatedTenant4 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Translation4 = await Translations.findOne({ order: [['id', 'ASC']], offset: 4, }); if (Translation4?.setTenant) { await Translation4.setTenant(relatedTenant4); } } // Similar logic for "relation_many" async function associateVocabularyWithTenant() { const relatedTenant0 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Vocabulary0 = await Vocabularies.findOne({ order: [['id', 'ASC']], offset: 0, }); if (Vocabulary0?.setTenant) { await Vocabulary0.setTenant(relatedTenant0); } const relatedTenant1 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Vocabulary1 = await Vocabularies.findOne({ order: [['id', 'ASC']], offset: 1, }); if (Vocabulary1?.setTenant) { await Vocabulary1.setTenant(relatedTenant1); } const relatedTenant2 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Vocabulary2 = await Vocabularies.findOne({ order: [['id', 'ASC']], offset: 2, }); if (Vocabulary2?.setTenant) { await Vocabulary2.setTenant(relatedTenant2); } const relatedTenant3 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Vocabulary3 = await Vocabularies.findOne({ order: [['id', 'ASC']], offset: 3, }); if (Vocabulary3?.setTenant) { await Vocabulary3.setTenant(relatedTenant3); } const relatedTenant4 = await Tenants.findOne({ offset: Math.floor(Math.random() * (await Tenants.count())), }); const Vocabulary4 = await Vocabularies.findOne({ order: [['id', 'ASC']], offset: 4, }); if (Vocabulary4?.setTenant) { await Vocabulary4.setTenant(relatedTenant4); } } module.exports = { up: async (queryInterface, Sequelize) => { await Payments.bulkCreate(PaymentsData); await Translations.bulkCreate(TranslationsData); await Vocabularies.bulkCreate(VocabulariesData); await Tenants.bulkCreate(TenantsData); await Promise.all([ // Similar logic for "relation_many" await associateUserWithTenant(), await associatePaymentWithUser(), await associatePaymentWithTenant(), await associateTranslationWithUser(), await associateTranslationWithTenant(), // Similar logic for "relation_many" await associateVocabularyWithTenant(), ]); }, down: async (queryInterface, Sequelize) => { await queryInterface.bulkDelete('payments', null, {}); await queryInterface.bulkDelete('translations', null, {}); await queryInterface.bulkDelete('vocabularies', null, {}); await queryInterface.bulkDelete('tenants', null, {}); }, };