31702/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-22 08:17:39 +00:00

977 lines
23 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Appointments = db.appointments;
const Contracts = db.contracts;
const MaintenanceRequests = db.maintenance_requests;
const Messages = db.messages;
const News = db.news;
const Payments = db.payments;
const Properties = db.properties;
const Tenant = db.tenant;
const AppointmentsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
appointment_date: new Date('2023-10-10T14:00:00Z'),
confirmed: true,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
appointment_date: new Date('2023-10-11T15:00:00Z'),
confirmed: false,
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
appointment_date: new Date('2023-10-12T16:00:00Z'),
confirmed: true,
},
];
const ContractsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-10-01T00:00:00Z'),
end_date: new Date('2024-10-01T00:00:00Z'),
monthly_rent: 1200,
// type code here for "files" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-09-01T00:00:00Z'),
end_date: new Date('2024-09-01T00:00:00Z'),
monthly_rent: 2500,
// type code here for "files" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_date: new Date('2023-08-01T00:00:00Z'),
end_date: new Date('2024-08-01T00:00:00Z'),
monthly_rent: 800,
// type code here for "files" field
},
];
const MaintenanceRequestsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
description: 'Leaking faucet in the kitchen.',
request_date: new Date('2023-10-01T00:00:00Z'),
status: 'Pending',
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
description: 'Broken window in the living room.',
request_date: new Date('2023-09-15T00:00:00Z'),
status: 'InProgress',
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
description: 'Heating system not working.',
request_date: new Date('2023-09-20T00:00:00Z'),
status: 'InProgress',
},
];
const MessagesData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Is the apartment still available?',
sent_at: new Date('2023-10-01T10:00:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Yes, it is available for viewing.',
sent_at: new Date('2023-10-01T10:05:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Can we schedule a viewing for next week?',
sent_at: new Date('2023-10-02T11:00:00Z'),
// type code here for "relation_one" field
},
];
const NewsData = [
{
title: 'New Rental Regulations',
content: 'New regulations for rental properties have been introduced.',
// type code here for "relation_one" field
published_at: new Date('2023-09-15T09:00:00Z'),
// type code here for "relation_one" field
},
{
title: 'Maintenance Tips for Landlords',
content: 'Here are some tips to maintain your rental property.',
// type code here for "relation_one" field
published_at: new Date('2023-09-20T10:00:00Z'),
// type code here for "relation_one" field
},
{
title: 'Tenant Rights Update',
content: 'Recent updates to tenant rights you should know.',
// type code here for "relation_one" field
published_at: new Date('2023-09-25T11:00:00Z'),
// type code here for "relation_one" field
},
];
const PaymentsData = [
{
// type code here for "relation_one" field
amount: 1200,
payment_date: new Date('2023-10-01T00:00:00Z'),
method: 'Alipay',
confirmed: true,
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
amount: 2500,
payment_date: new Date('2023-09-01T00:00:00Z'),
method: 'WeChat',
confirmed: true,
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
amount: 800,
payment_date: new Date('2023-08-01T00:00:00Z'),
method: 'WeChat',
confirmed: true,
// type code here for "relation_one" field
},
];
const PropertiesData = [
{
address: '123 Main St, Springfield',
type: 'Studio',
area: 85.5,
rent: 1200,
deposit: 1200,
decoration_status: 'Unfurnished',
// type code here for "images" field
// type code here for "files" field
status: 'Rented',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
address: '456 Elm St, Metropolis',
type: 'House',
area: 150,
rent: 2500,
deposit: 2500,
decoration_status: 'Unfurnished',
// type code here for "images" field
// type code here for "files" field
status: 'Rented',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
address: '789 Oak St, Gotham',
type: 'House',
area: 45,
rent: 800,
deposit: 800,
decoration_status: 'PartiallyFurnished',
// type code here for "images" field
// type code here for "files" field
status: 'Vacant',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const TenantData = [
{
name: 'Ernst Haeckel',
},
{
name: 'Louis Pasteur',
},
{
name: 'Dmitri Mendeleev',
},
];
// Similar logic for "relation_many"
async function associateUserWithTenant() {
const relatedTenant0 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setTenant) {
await User0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setTenant) {
await User1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setTenant) {
await User2.setTenant(relatedTenant2);
}
}
async function associateAppointmentWithProperty() {
const relatedProperty0 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Appointment0 = await Appointments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Appointment0?.setProperty) {
await Appointment0.setProperty(relatedProperty0);
}
const relatedProperty1 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Appointment1 = await Appointments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Appointment1?.setProperty) {
await Appointment1.setProperty(relatedProperty1);
}
const relatedProperty2 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Appointment2 = await Appointments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Appointment2?.setProperty) {
await Appointment2.setProperty(relatedProperty2);
}
}
async function associateAppointmentWithTenant() {
const relatedTenant0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Appointment0 = await Appointments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Appointment0?.setTenant) {
await Appointment0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Appointment1 = await Appointments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Appointment1?.setTenant) {
await Appointment1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Appointment2 = await Appointments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Appointment2?.setTenant) {
await Appointment2.setTenant(relatedTenant2);
}
}
async function associateContractWithProperty() {
const relatedProperty0 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Contract0 = await Contracts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Contract0?.setProperty) {
await Contract0.setProperty(relatedProperty0);
}
const relatedProperty1 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Contract1 = await Contracts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Contract1?.setProperty) {
await Contract1.setProperty(relatedProperty1);
}
const relatedProperty2 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const Contract2 = await Contracts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Contract2?.setProperty) {
await Contract2.setProperty(relatedProperty2);
}
}
async function associateContractWithTenant() {
const relatedTenant0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Contract0 = await Contracts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Contract0?.setTenant) {
await Contract0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Contract1 = await Contracts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Contract1?.setTenant) {
await Contract1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Contract2 = await Contracts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Contract2?.setTenant) {
await Contract2.setTenant(relatedTenant2);
}
}
async function associateMaintenanceRequestWithProperty() {
const relatedProperty0 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const MaintenanceRequest0 = await MaintenanceRequests.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (MaintenanceRequest0?.setProperty) {
await MaintenanceRequest0.setProperty(relatedProperty0);
}
const relatedProperty1 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const MaintenanceRequest1 = await MaintenanceRequests.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (MaintenanceRequest1?.setProperty) {
await MaintenanceRequest1.setProperty(relatedProperty1);
}
const relatedProperty2 = await Properties.findOne({
offset: Math.floor(Math.random() * (await Properties.count())),
});
const MaintenanceRequest2 = await MaintenanceRequests.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (MaintenanceRequest2?.setProperty) {
await MaintenanceRequest2.setProperty(relatedProperty2);
}
}
async function associateMaintenanceRequestWithTenant() {
const relatedTenant0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceRequest0 = await MaintenanceRequests.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (MaintenanceRequest0?.setTenant) {
await MaintenanceRequest0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceRequest1 = await MaintenanceRequests.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (MaintenanceRequest1?.setTenant) {
await MaintenanceRequest1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceRequest2 = await MaintenanceRequests.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (MaintenanceRequest2?.setTenant) {
await MaintenanceRequest2.setTenant(relatedTenant2);
}
}
async function associateMessageWithSender() {
const relatedSender0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setSender) {
await Message0.setSender(relatedSender0);
}
const relatedSender1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setSender) {
await Message1.setSender(relatedSender1);
}
const relatedSender2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setSender) {
await Message2.setSender(relatedSender2);
}
}
async function associateMessageWithReceiver() {
const relatedReceiver0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setReceiver) {
await Message0.setReceiver(relatedReceiver0);
}
const relatedReceiver1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setReceiver) {
await Message1.setReceiver(relatedReceiver1);
}
const relatedReceiver2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setReceiver) {
await Message2.setReceiver(relatedReceiver2);
}
}
async function associateMessageWithTenant() {
const relatedTenant0 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setTenant) {
await Message0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setTenant) {
await Message1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setTenant) {
await Message2.setTenant(relatedTenant2);
}
}
async function associateNewsWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const News0 = await News.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (News0?.setAuthor) {
await News0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const News1 = await News.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (News1?.setAuthor) {
await News1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const News2 = await News.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (News2?.setAuthor) {
await News2.setAuthor(relatedAuthor2);
}
}
async function associateNewsWithTenant() {
const relatedTenant0 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const News0 = await News.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (News0?.setTenant) {
await News0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const News1 = await News.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (News1?.setTenant) {
await News1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const News2 = await News.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (News2?.setTenant) {
await News2.setTenant(relatedTenant2);
}
}
async function associatePaymentWithContract() {
const relatedContract0 = await Contracts.findOne({
offset: Math.floor(Math.random() * (await Contracts.count())),
});
const Payment0 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Payment0?.setContract) {
await Payment0.setContract(relatedContract0);
}
const relatedContract1 = await Contracts.findOne({
offset: Math.floor(Math.random() * (await Contracts.count())),
});
const Payment1 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Payment1?.setContract) {
await Payment1.setContract(relatedContract1);
}
const relatedContract2 = await Contracts.findOne({
offset: Math.floor(Math.random() * (await Contracts.count())),
});
const Payment2 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Payment2?.setContract) {
await Payment2.setContract(relatedContract2);
}
}
async function associatePaymentWithTenant() {
const relatedTenant0 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Payment0 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Payment0?.setTenant) {
await Payment0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Payment1 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Payment1?.setTenant) {
await Payment1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Payment2 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Payment2?.setTenant) {
await Payment2.setTenant(relatedTenant2);
}
}
async function associatePropertyWithLandlord() {
const relatedLandlord0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Property0 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Property0?.setLandlord) {
await Property0.setLandlord(relatedLandlord0);
}
const relatedLandlord1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Property1 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Property1?.setLandlord) {
await Property1.setLandlord(relatedLandlord1);
}
const relatedLandlord2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Property2 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Property2?.setLandlord) {
await Property2.setLandlord(relatedLandlord2);
}
}
async function associatePropertyWithTenant() {
const relatedTenant0 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Property0 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Property0?.setTenant) {
await Property0.setTenant(relatedTenant0);
}
const relatedTenant1 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Property1 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Property1?.setTenant) {
await Property1.setTenant(relatedTenant1);
}
const relatedTenant2 = await Tenant.findOne({
offset: Math.floor(Math.random() * (await Tenant.count())),
});
const Property2 = await Properties.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Property2?.setTenant) {
await Property2.setTenant(relatedTenant2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Appointments.bulkCreate(AppointmentsData);
await Contracts.bulkCreate(ContractsData);
await MaintenanceRequests.bulkCreate(MaintenanceRequestsData);
await Messages.bulkCreate(MessagesData);
await News.bulkCreate(NewsData);
await Payments.bulkCreate(PaymentsData);
await Properties.bulkCreate(PropertiesData);
await Tenant.bulkCreate(TenantData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithTenant(),
await associateAppointmentWithProperty(),
await associateAppointmentWithTenant(),
await associateContractWithProperty(),
await associateContractWithTenant(),
await associateMaintenanceRequestWithProperty(),
await associateMaintenanceRequestWithTenant(),
await associateMessageWithSender(),
await associateMessageWithReceiver(),
await associateMessageWithTenant(),
await associateNewsWithAuthor(),
await associateNewsWithTenant(),
await associatePaymentWithContract(),
await associatePaymentWithTenant(),
await associatePropertyWithLandlord(),
await associatePropertyWithTenant(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('appointments', null, {});
await queryInterface.bulkDelete('contracts', null, {});
await queryInterface.bulkDelete('maintenance_requests', null, {});
await queryInterface.bulkDelete('messages', null, {});
await queryInterface.bulkDelete('news', null, {});
await queryInterface.bulkDelete('payments', null, {});
await queryInterface.bulkDelete('properties', null, {});
await queryInterface.bulkDelete('tenant', null, {});
},
};