30835/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-19 04:50:37 +00:00

240 lines
5.9 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const ServiceRequests = db.service_requests;
const Companies = db.companies;
const ServiceRequestsData = [
{
title: 'Install New Router',
description:
'Customer requested installation of a new router at their residence.',
// type code here for "relation_one" field
// type code here for "relation_one" field
status: 'Pending',
// type code here for "images" field
// type code here for "relation_one" field
},
{
title: 'Upgrade Internet Plan',
description: 'Customer wants to upgrade to a higher speed internet plan.',
// type code here for "relation_one" field
// type code here for "relation_one" field
status: 'Closed',
// type code here for "images" field
// type code here for "relation_one" field
},
{
title: 'Fix Connection Issue',
description: 'Customer reported intermittent connection issues.',
// type code here for "relation_one" field
// type code here for "relation_one" field
status: 'InProgress',
// type code here for "images" field
// type code here for "relation_one" field
},
];
const CompaniesData = [
{
name: 'TechNet Solutions',
},
{
name: 'Broadband Corp',
},
{
name: 'FiberLink Services',
},
];
// 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 associateServiceRequestWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const ServiceRequest0 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ServiceRequest0?.setCompany) {
await ServiceRequest0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const ServiceRequest1 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ServiceRequest1?.setCompany) {
await ServiceRequest1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const ServiceRequest2 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ServiceRequest2?.setCompany) {
await ServiceRequest2.setCompany(relatedCompany2);
}
}
async function associateServiceRequestWithAssigned_to() {
const relatedAssigned_to0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ServiceRequest0 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ServiceRequest0?.setAssigned_to) {
await ServiceRequest0.setAssigned_to(relatedAssigned_to0);
}
const relatedAssigned_to1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ServiceRequest1 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ServiceRequest1?.setAssigned_to) {
await ServiceRequest1.setAssigned_to(relatedAssigned_to1);
}
const relatedAssigned_to2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ServiceRequest2 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ServiceRequest2?.setAssigned_to) {
await ServiceRequest2.setAssigned_to(relatedAssigned_to2);
}
}
async function associateServiceRequestWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const ServiceRequest0 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ServiceRequest0?.setCompany) {
await ServiceRequest0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const ServiceRequest1 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ServiceRequest1?.setCompany) {
await ServiceRequest1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const ServiceRequest2 = await ServiceRequests.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ServiceRequest2?.setCompany) {
await ServiceRequest2.setCompany(relatedCompany2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await ServiceRequests.bulkCreate(ServiceRequestsData);
await Companies.bulkCreate(CompaniesData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithCompany(),
await associateServiceRequestWithCompany(),
await associateServiceRequestWithAssigned_to(),
await associateServiceRequestWithCompany(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('service_requests', null, {});
await queryInterface.bulkDelete('companies', null, {});
},
};