191 lines
4.1 KiB
JavaScript
191 lines
4.1 KiB
JavaScript
const db = require('../models');
|
|
const Users = db.users;
|
|
|
|
const Properties = db.properties;
|
|
|
|
const RentPayments = db.rent_payments;
|
|
|
|
const PropertiesData = [
|
|
{
|
|
name: 'Home 1',
|
|
|
|
rent_amount: 11000,
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
name: 'Home 2',
|
|
|
|
rent_amount: 10500,
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
name: 'Home 3',
|
|
|
|
rent_amount: 10500,
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
name: 'Home 4',
|
|
|
|
rent_amount: 12000,
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
];
|
|
|
|
const RentPaymentsData = [
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
due_date: new Date('2023-11-02T00:00:00Z'),
|
|
|
|
paid: false,
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
due_date: new Date('2023-11-02T00:00:00Z'),
|
|
|
|
paid: true,
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
due_date: new Date('2023-11-02T00:00:00Z'),
|
|
|
|
paid: true,
|
|
},
|
|
|
|
{
|
|
// type code here for "relation_one" field
|
|
|
|
due_date: new Date('2023-12-02T00:00:00Z'),
|
|
|
|
paid: false,
|
|
},
|
|
];
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
async function associatePropertyWithTenant() {
|
|
const relatedTenant0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Property0 = await Properties.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Property0?.setTenant) {
|
|
await Property0.setTenant(relatedTenant0);
|
|
}
|
|
|
|
const relatedTenant1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Property1 = await Properties.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Property1?.setTenant) {
|
|
await Property1.setTenant(relatedTenant1);
|
|
}
|
|
|
|
const relatedTenant2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Property2 = await Properties.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Property2?.setTenant) {
|
|
await Property2.setTenant(relatedTenant2);
|
|
}
|
|
|
|
const relatedTenant3 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Property3 = await Properties.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Property3?.setTenant) {
|
|
await Property3.setTenant(relatedTenant3);
|
|
}
|
|
}
|
|
|
|
async function associateRentPaymentWithProperty() {
|
|
const relatedProperty0 = await Properties.findOne({
|
|
offset: Math.floor(Math.random() * (await Properties.count())),
|
|
});
|
|
const RentPayment0 = await RentPayments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (RentPayment0?.setProperty) {
|
|
await RentPayment0.setProperty(relatedProperty0);
|
|
}
|
|
|
|
const relatedProperty1 = await Properties.findOne({
|
|
offset: Math.floor(Math.random() * (await Properties.count())),
|
|
});
|
|
const RentPayment1 = await RentPayments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (RentPayment1?.setProperty) {
|
|
await RentPayment1.setProperty(relatedProperty1);
|
|
}
|
|
|
|
const relatedProperty2 = await Properties.findOne({
|
|
offset: Math.floor(Math.random() * (await Properties.count())),
|
|
});
|
|
const RentPayment2 = await RentPayments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (RentPayment2?.setProperty) {
|
|
await RentPayment2.setProperty(relatedProperty2);
|
|
}
|
|
|
|
const relatedProperty3 = await Properties.findOne({
|
|
offset: Math.floor(Math.random() * (await Properties.count())),
|
|
});
|
|
const RentPayment3 = await RentPayments.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (RentPayment3?.setProperty) {
|
|
await RentPayment3.setProperty(relatedProperty3);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
up: async (queryInterface, Sequelize) => {
|
|
await Properties.bulkCreate(PropertiesData);
|
|
|
|
await RentPayments.bulkCreate(RentPaymentsData);
|
|
|
|
await Promise.all([
|
|
// Similar logic for "relation_many"
|
|
|
|
await associatePropertyWithTenant(),
|
|
|
|
await associateRentPaymentWithProperty(),
|
|
]);
|
|
},
|
|
|
|
down: async (queryInterface, Sequelize) => {
|
|
await queryInterface.bulkDelete('properties', null, {});
|
|
|
|
await queryInterface.bulkDelete('rent_payments', null, {});
|
|
},
|
|
};
|