32264/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-16 00:45:58 +00:00

282 lines
5.7 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const PricingModels = db.pricing_models;
const Quotes = db.quotes;
const RoomTypes = db.room_types;
const Rooms = db.rooms;
const WorkItems = db.work_items;
const PricingModelsData = [
{
pricing_type: 'psf',
rate: 5,
// type code here for "relation_one" field
},
{
pricing_type: 'psf',
rate: 10,
// type code here for "relation_one" field
},
{
pricing_type: 'lump_sum',
rate: 500,
// type code here for "relation_one" field
},
];
const QuotesData = [
{
property_type: 'Condo',
total_sqft: 1000,
// type code here for "relation_many" field
},
{
property_type: 'HDB',
total_sqft: 1200,
// type code here for "relation_many" field
},
{
property_type: 'Office',
total_sqft: 1500,
// type code here for "relation_many" field
},
];
const RoomTypesData = [
{
name: 'Kitchen',
standard_size_percentage: 20,
},
{
name: 'Living Room',
standard_size_percentage: 30,
},
{
name: 'Master Bath',
standard_size_percentage: 10,
},
];
const RoomsData = [
{
// type code here for "relation_one" field
allocated_sqft: 200,
// type code here for "relation_many" field
},
{
// type code here for "relation_one" field
allocated_sqft: 300,
// type code here for "relation_many" field
},
{
// type code here for "relation_one" field
allocated_sqft: 100,
// type code here for "relation_many" field
},
];
const WorkItemsData = [
{
name: 'Tiling',
// type code here for "relation_one" field
},
{
name: 'Carpentry',
// type code here for "relation_one" field
},
{
name: 'Polishing',
// type code here for "relation_one" field
},
];
// Similar logic for "relation_many"
async function associatePricingModelWithWork_item() {
const relatedWork_item0 = await WorkItems.findOne({
offset: Math.floor(Math.random() * (await WorkItems.count())),
});
const PricingModel0 = await PricingModels.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (PricingModel0?.setWork_item) {
await PricingModel0.setWork_item(relatedWork_item0);
}
const relatedWork_item1 = await WorkItems.findOne({
offset: Math.floor(Math.random() * (await WorkItems.count())),
});
const PricingModel1 = await PricingModels.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (PricingModel1?.setWork_item) {
await PricingModel1.setWork_item(relatedWork_item1);
}
const relatedWork_item2 = await WorkItems.findOne({
offset: Math.floor(Math.random() * (await WorkItems.count())),
});
const PricingModel2 = await PricingModels.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (PricingModel2?.setWork_item) {
await PricingModel2.setWork_item(relatedWork_item2);
}
}
// Similar logic for "relation_many"
async function associateRoomWithRoom_type() {
const relatedRoom_type0 = await RoomTypes.findOne({
offset: Math.floor(Math.random() * (await RoomTypes.count())),
});
const Room0 = await Rooms.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Room0?.setRoom_type) {
await Room0.setRoom_type(relatedRoom_type0);
}
const relatedRoom_type1 = await RoomTypes.findOne({
offset: Math.floor(Math.random() * (await RoomTypes.count())),
});
const Room1 = await Rooms.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Room1?.setRoom_type) {
await Room1.setRoom_type(relatedRoom_type1);
}
const relatedRoom_type2 = await RoomTypes.findOne({
offset: Math.floor(Math.random() * (await RoomTypes.count())),
});
const Room2 = await Rooms.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Room2?.setRoom_type) {
await Room2.setRoom_type(relatedRoom_type2);
}
}
// Similar logic for "relation_many"
async function associateWorkItemWithRoom_type() {
const relatedRoom_type0 = await RoomTypes.findOne({
offset: Math.floor(Math.random() * (await RoomTypes.count())),
});
const WorkItem0 = await WorkItems.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (WorkItem0?.setRoom_type) {
await WorkItem0.setRoom_type(relatedRoom_type0);
}
const relatedRoom_type1 = await RoomTypes.findOne({
offset: Math.floor(Math.random() * (await RoomTypes.count())),
});
const WorkItem1 = await WorkItems.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (WorkItem1?.setRoom_type) {
await WorkItem1.setRoom_type(relatedRoom_type1);
}
const relatedRoom_type2 = await RoomTypes.findOne({
offset: Math.floor(Math.random() * (await RoomTypes.count())),
});
const WorkItem2 = await WorkItems.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (WorkItem2?.setRoom_type) {
await WorkItem2.setRoom_type(relatedRoom_type2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await PricingModels.bulkCreate(PricingModelsData);
await Quotes.bulkCreate(QuotesData);
await RoomTypes.bulkCreate(RoomTypesData);
await Rooms.bulkCreate(RoomsData);
await WorkItems.bulkCreate(WorkItemsData);
await Promise.all([
// Similar logic for "relation_many"
await associatePricingModelWithWork_item(),
// Similar logic for "relation_many"
await associateRoomWithRoom_type(),
// Similar logic for "relation_many"
await associateWorkItemWithRoom_type(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('pricing_models', null, {});
await queryInterface.bulkDelete('quotes', null, {});
await queryInterface.bulkDelete('room_types', null, {});
await queryInterface.bulkDelete('rooms', null, {});
await queryInterface.bulkDelete('work_items', null, {});
},
};