34222/backend/src/db/seeders/20231127130745-sample-data.js
2025-09-19 14:28:59 +00:00

427 lines
9.5 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Alerts = db.alerts;
const MaintenanceTasks = db.maintenance_tasks;
const Platforms = db.platforms;
const Schedules = db.schedules;
const Trains = db.trains;
const AlertsData = [
{
alert_message: 'Train 12345 delayed due to fog',
alert_time: new Date('2023-10-10T07:00:00Z'),
// type code here for "relation_one" field
},
{
alert_message: 'Platform 2 maintenance scheduled',
alert_time: new Date('2023-10-11T08:00:00Z'),
// type code here for "relation_one" field
},
{
alert_message: 'Train 67890 on time',
alert_time: new Date('2023-10-12T09:00:00Z'),
// type code here for "relation_one" field
},
{
alert_message: 'Track inspection required',
alert_time: new Date('2023-10-13T10:00:00Z'),
// type code here for "relation_one" field
},
];
const MaintenanceTasksData = [
{
task_description: 'Check engine oil levels',
status: 'in_progress',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
task_description: 'Inspect brake systems',
status: 'completed',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
task_description: 'Replace worn-out seats',
status: 'completed',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
task_description: 'Clean train interiors',
status: 'pending',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const PlatformsData = [
{
platform_number: '1',
// type code here for "relation_many" field
},
{
platform_number: '2',
// type code here for "relation_many" field
},
{
platform_number: '3',
// type code here for "relation_many" field
},
{
platform_number: '4',
// type code here for "relation_many" field
},
];
const SchedulesData = [
{
departure_time: new Date('2023-10-10T08:00:00Z'),
arrival_time: new Date('2023-10-10T12:00:00Z'),
// type code here for "relation_one" field
},
{
departure_time: new Date('2023-10-11T09:00:00Z'),
arrival_time: new Date('2023-10-11T13:00:00Z'),
// type code here for "relation_one" field
},
{
departure_time: new Date('2023-10-12T10:00:00Z'),
arrival_time: new Date('2023-10-12T14:00:00Z'),
// type code here for "relation_one" field
},
{
departure_time: new Date('2023-10-13T11:00:00Z'),
arrival_time: new Date('2023-10-13T15:00:00Z'),
// type code here for "relation_one" field
},
];
const TrainsData = [
{
train_number: '12345',
train_name: 'Rajdhani Express',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
train_number: '67890',
train_name: 'Shatabdi Express',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
train_number: '11223',
train_name: 'Duronto Express',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
{
train_number: '33445',
train_name: 'Garib Rath Express',
// type code here for "relation_many" field
// type code here for "relation_many" field
},
];
// Similar logic for "relation_many"
async function associateAlertWithTrain() {
const relatedTrain0 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Alert0 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Alert0?.setTrain) {
await Alert0.setTrain(relatedTrain0);
}
const relatedTrain1 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Alert1 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Alert1?.setTrain) {
await Alert1.setTrain(relatedTrain1);
}
const relatedTrain2 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Alert2 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Alert2?.setTrain) {
await Alert2.setTrain(relatedTrain2);
}
const relatedTrain3 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Alert3 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Alert3?.setTrain) {
await Alert3.setTrain(relatedTrain3);
}
}
async function associateMaintenanceTaskWithAssigned_to() {
const relatedAssigned_to0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceTask0 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (MaintenanceTask0?.setAssigned_to) {
await MaintenanceTask0.setAssigned_to(relatedAssigned_to0);
}
const relatedAssigned_to1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceTask1 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (MaintenanceTask1?.setAssigned_to) {
await MaintenanceTask1.setAssigned_to(relatedAssigned_to1);
}
const relatedAssigned_to2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceTask2 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (MaintenanceTask2?.setAssigned_to) {
await MaintenanceTask2.setAssigned_to(relatedAssigned_to2);
}
const relatedAssigned_to3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const MaintenanceTask3 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (MaintenanceTask3?.setAssigned_to) {
await MaintenanceTask3.setAssigned_to(relatedAssigned_to3);
}
}
async function associateMaintenanceTaskWithTrain() {
const relatedTrain0 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const MaintenanceTask0 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (MaintenanceTask0?.setTrain) {
await MaintenanceTask0.setTrain(relatedTrain0);
}
const relatedTrain1 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const MaintenanceTask1 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (MaintenanceTask1?.setTrain) {
await MaintenanceTask1.setTrain(relatedTrain1);
}
const relatedTrain2 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const MaintenanceTask2 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (MaintenanceTask2?.setTrain) {
await MaintenanceTask2.setTrain(relatedTrain2);
}
const relatedTrain3 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const MaintenanceTask3 = await MaintenanceTasks.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (MaintenanceTask3?.setTrain) {
await MaintenanceTask3.setTrain(relatedTrain3);
}
}
// Similar logic for "relation_many"
async function associateScheduleWithTrain() {
const relatedTrain0 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Schedule0 = await Schedules.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Schedule0?.setTrain) {
await Schedule0.setTrain(relatedTrain0);
}
const relatedTrain1 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Schedule1 = await Schedules.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Schedule1?.setTrain) {
await Schedule1.setTrain(relatedTrain1);
}
const relatedTrain2 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Schedule2 = await Schedules.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Schedule2?.setTrain) {
await Schedule2.setTrain(relatedTrain2);
}
const relatedTrain3 = await Trains.findOne({
offset: Math.floor(Math.random() * (await Trains.count())),
});
const Schedule3 = await Schedules.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Schedule3?.setTrain) {
await Schedule3.setTrain(relatedTrain3);
}
}
// Similar logic for "relation_many"
// Similar logic for "relation_many"
module.exports = {
up: async (queryInterface, Sequelize) => {
await Alerts.bulkCreate(AlertsData);
await MaintenanceTasks.bulkCreate(MaintenanceTasksData);
await Platforms.bulkCreate(PlatformsData);
await Schedules.bulkCreate(SchedulesData);
await Trains.bulkCreate(TrainsData);
await Promise.all([
// Similar logic for "relation_many"
await associateAlertWithTrain(),
await associateMaintenanceTaskWithAssigned_to(),
await associateMaintenanceTaskWithTrain(),
// Similar logic for "relation_many"
await associateScheduleWithTrain(),
// Similar logic for "relation_many"
// Similar logic for "relation_many"
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('alerts', null, {});
await queryInterface.bulkDelete('maintenance_tasks', null, {});
await queryInterface.bulkDelete('platforms', null, {});
await queryInterface.bulkDelete('schedules', null, {});
await queryInterface.bulkDelete('trains', null, {});
},
};