31219/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-04 04:32:41 +00:00

307 lines
6.9 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Drivers = db.drivers;
const EmergencyAccess = db.emergency_access;
const UsageHistory = db.usage_history;
const Vehicles = db.vehicles;
const DriversData = [
{
name: 'Alice Green',
// type code here for "images" field
// type code here for "files" field
// type code here for "relation_many" field
},
{
name: 'Bob White',
// type code here for "images" field
// type code here for "files" field
// type code here for "relation_many" field
},
{
name: 'Charlie Black',
// type code here for "images" field
// type code here for "files" field
// type code here for "relation_many" field
},
];
const EmergencyAccessData = [
{
// type code here for "relation_one" field
access_granted: true,
},
{
// type code here for "relation_one" field
access_granted: true,
},
{
// type code here for "relation_one" field
access_granted: true,
},
];
const UsageHistoryData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_time: new Date('2023-10-01T08:00:00Z'),
end_time: new Date('2023-10-01T10:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_time: new Date('2023-10-02T09:00:00Z'),
end_time: new Date('2023-10-02T11:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
start_time: new Date('2023-10-03T07:30:00Z'),
end_time: new Date('2023-10-03T09:30:00Z'),
},
];
const VehiclesData = [
{
license_plate: 'ABC1234',
model: 'Toyota Camry',
// type code here for "relation_one" field
ignition_status: true,
},
{
license_plate: 'XYZ5678',
model: 'Honda Accord',
// type code here for "relation_one" field
ignition_status: true,
},
{
license_plate: 'LMN9101',
model: 'Ford Focus',
// type code here for "relation_one" field
ignition_status: true,
},
];
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateEmergencyAccessWithVehicle() {
const relatedVehicle0 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const EmergencyAccess0 = await EmergencyAccess.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (EmergencyAccess0?.setVehicle) {
await EmergencyAccess0.setVehicle(relatedVehicle0);
}
const relatedVehicle1 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const EmergencyAccess1 = await EmergencyAccess.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (EmergencyAccess1?.setVehicle) {
await EmergencyAccess1.setVehicle(relatedVehicle1);
}
const relatedVehicle2 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const EmergencyAccess2 = await EmergencyAccess.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (EmergencyAccess2?.setVehicle) {
await EmergencyAccess2.setVehicle(relatedVehicle2);
}
}
async function associateUsageHistoryWithVehicle() {
const relatedVehicle0 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const UsageHistory0 = await UsageHistory.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (UsageHistory0?.setVehicle) {
await UsageHistory0.setVehicle(relatedVehicle0);
}
const relatedVehicle1 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const UsageHistory1 = await UsageHistory.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (UsageHistory1?.setVehicle) {
await UsageHistory1.setVehicle(relatedVehicle1);
}
const relatedVehicle2 = await Vehicles.findOne({
offset: Math.floor(Math.random() * (await Vehicles.count())),
});
const UsageHistory2 = await UsageHistory.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (UsageHistory2?.setVehicle) {
await UsageHistory2.setVehicle(relatedVehicle2);
}
}
async function associateUsageHistoryWithDriver() {
const relatedDriver0 = await Drivers.findOne({
offset: Math.floor(Math.random() * (await Drivers.count())),
});
const UsageHistory0 = await UsageHistory.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (UsageHistory0?.setDriver) {
await UsageHistory0.setDriver(relatedDriver0);
}
const relatedDriver1 = await Drivers.findOne({
offset: Math.floor(Math.random() * (await Drivers.count())),
});
const UsageHistory1 = await UsageHistory.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (UsageHistory1?.setDriver) {
await UsageHistory1.setDriver(relatedDriver1);
}
const relatedDriver2 = await Drivers.findOne({
offset: Math.floor(Math.random() * (await Drivers.count())),
});
const UsageHistory2 = await UsageHistory.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (UsageHistory2?.setDriver) {
await UsageHistory2.setDriver(relatedDriver2);
}
}
async function associateVehicleWithOwner() {
const relatedOwner0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle0 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Vehicle0?.setOwner) {
await Vehicle0.setOwner(relatedOwner0);
}
const relatedOwner1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle1 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Vehicle1?.setOwner) {
await Vehicle1.setOwner(relatedOwner1);
}
const relatedOwner2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Vehicle2 = await Vehicles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Vehicle2?.setOwner) {
await Vehicle2.setOwner(relatedOwner2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Drivers.bulkCreate(DriversData);
await EmergencyAccess.bulkCreate(EmergencyAccessData);
await UsageHistory.bulkCreate(UsageHistoryData);
await Vehicles.bulkCreate(VehiclesData);
await Promise.all([
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateEmergencyAccessWithVehicle(),
await associateUsageHistoryWithVehicle(),
await associateUsageHistoryWithDriver(),
await associateVehicleWithOwner(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('drivers', null, {});
await queryInterface.bulkDelete('emergency_access', null, {});
await queryInterface.bulkDelete('usage_history', null, {});
await queryInterface.bulkDelete('vehicles', null, {});
},
};