30892/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-21 10:48:45 +00:00

778 lines
19 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Emergencies = db.emergencies;
const Notifications = db.notifications;
const Reports = db.reports;
const Agencies = db.agencies;
const EmergenciesData = [
{
type: 'Fire',
location: 'Antananarivo',
description: 'A large fire has broken out in the city center.',
// type code here for "files" field
status: 'Résolu',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
type: 'Flood',
location: 'Toamasina',
description: 'Severe flooding in the coastal areas.',
// type code here for "files" field
status: 'Enattente',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
type: 'Earthquake',
location: 'Fianarantsoa',
description: 'Minor earthquake reported in the region.',
// type code here for "files" field
status: 'Prisencharge',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
type: 'Cyclone',
location: 'Mahajanga',
description: 'Cyclone warning issued for the northwest region.',
// type code here for "files" field
status: 'Enattente',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
type: 'Landslide',
location: 'Antsiranana',
description: 'Landslide blocking the main road.',
// type code here for "files" field
status: 'Enattente',
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const NotificationsData = [
{
message: 'New emergency report submitted.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
message: "Emergency status updated to 'Pris en charge'.",
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
message: 'New message from Central Emergency Agency.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
message: 'Emergency report resolved.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
message: 'New attachment added to emergency report.',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const ReportsData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
submitted_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
submitted_at: new Date('2023-10-02T11:30:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
submitted_at: new Date('2023-10-03T09:15:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
submitted_at: new Date('2023-10-04T14:45:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
submitted_at: new Date('2023-10-05T16:20:00Z'),
// type code here for "relation_one" field
},
];
const AgenciesData = [
{
name: 'Central Emergency Agency',
},
{
name: 'Coastal Response Unit',
},
{
name: 'Highland Safety Department',
},
{
name: 'Northwest Cyclone Center',
},
{
name: 'Northern Landslide Team',
},
];
// Similar logic for "relation_many"
async function associateUserWithAgency() {
const relatedAgency0 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setAgency) {
await User0.setAgency(relatedAgency0);
}
const relatedAgency1 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setAgency) {
await User1.setAgency(relatedAgency1);
}
const relatedAgency2 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setAgency) {
await User2.setAgency(relatedAgency2);
}
const relatedAgency3 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const User3 = await Users.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (User3?.setAgency) {
await User3.setAgency(relatedAgency3);
}
const relatedAgency4 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const User4 = await Users.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (User4?.setAgency) {
await User4.setAgency(relatedAgency4);
}
}
async function associateEmergencyWithReported_by() {
const relatedReported_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency0 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Emergency0?.setReported_by) {
await Emergency0.setReported_by(relatedReported_by0);
}
const relatedReported_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency1 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Emergency1?.setReported_by) {
await Emergency1.setReported_by(relatedReported_by1);
}
const relatedReported_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency2 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Emergency2?.setReported_by) {
await Emergency2.setReported_by(relatedReported_by2);
}
const relatedReported_by3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency3 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Emergency3?.setReported_by) {
await Emergency3.setReported_by(relatedReported_by3);
}
const relatedReported_by4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency4 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Emergency4?.setReported_by) {
await Emergency4.setReported_by(relatedReported_by4);
}
}
async function associateEmergencyWithAssigned_to() {
const relatedAssigned_to0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency0 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Emergency0?.setAssigned_to) {
await Emergency0.setAssigned_to(relatedAssigned_to0);
}
const relatedAssigned_to1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency1 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Emergency1?.setAssigned_to) {
await Emergency1.setAssigned_to(relatedAssigned_to1);
}
const relatedAssigned_to2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency2 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Emergency2?.setAssigned_to) {
await Emergency2.setAssigned_to(relatedAssigned_to2);
}
const relatedAssigned_to3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency3 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Emergency3?.setAssigned_to) {
await Emergency3.setAssigned_to(relatedAssigned_to3);
}
const relatedAssigned_to4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Emergency4 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Emergency4?.setAssigned_to) {
await Emergency4.setAssigned_to(relatedAssigned_to4);
}
}
async function associateEmergencyWithAgency() {
const relatedAgency0 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Emergency0 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Emergency0?.setAgency) {
await Emergency0.setAgency(relatedAgency0);
}
const relatedAgency1 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Emergency1 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Emergency1?.setAgency) {
await Emergency1.setAgency(relatedAgency1);
}
const relatedAgency2 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Emergency2 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Emergency2?.setAgency) {
await Emergency2.setAgency(relatedAgency2);
}
const relatedAgency3 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Emergency3 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Emergency3?.setAgency) {
await Emergency3.setAgency(relatedAgency3);
}
const relatedAgency4 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Emergency4 = await Emergencies.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Emergency4?.setAgency) {
await Emergency4.setAgency(relatedAgency4);
}
}
async function associateNotificationWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification0 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Notification0?.setUser) {
await Notification0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification1 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Notification1?.setUser) {
await Notification1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification2 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Notification2?.setUser) {
await Notification2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification3 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Notification3?.setUser) {
await Notification3.setUser(relatedUser3);
}
const relatedUser4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Notification4 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Notification4?.setUser) {
await Notification4.setUser(relatedUser4);
}
}
async function associateNotificationWithAgency() {
const relatedAgency0 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Notification0 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Notification0?.setAgency) {
await Notification0.setAgency(relatedAgency0);
}
const relatedAgency1 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Notification1 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Notification1?.setAgency) {
await Notification1.setAgency(relatedAgency1);
}
const relatedAgency2 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Notification2 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Notification2?.setAgency) {
await Notification2.setAgency(relatedAgency2);
}
const relatedAgency3 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Notification3 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Notification3?.setAgency) {
await Notification3.setAgency(relatedAgency3);
}
const relatedAgency4 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Notification4 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Notification4?.setAgency) {
await Notification4.setAgency(relatedAgency4);
}
}
async function associateReportWithEmergency() {
const relatedEmergency0 = await Emergencies.findOne({
offset: Math.floor(Math.random() * (await Emergencies.count())),
});
const Report0 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Report0?.setEmergency) {
await Report0.setEmergency(relatedEmergency0);
}
const relatedEmergency1 = await Emergencies.findOne({
offset: Math.floor(Math.random() * (await Emergencies.count())),
});
const Report1 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Report1?.setEmergency) {
await Report1.setEmergency(relatedEmergency1);
}
const relatedEmergency2 = await Emergencies.findOne({
offset: Math.floor(Math.random() * (await Emergencies.count())),
});
const Report2 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Report2?.setEmergency) {
await Report2.setEmergency(relatedEmergency2);
}
const relatedEmergency3 = await Emergencies.findOne({
offset: Math.floor(Math.random() * (await Emergencies.count())),
});
const Report3 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Report3?.setEmergency) {
await Report3.setEmergency(relatedEmergency3);
}
const relatedEmergency4 = await Emergencies.findOne({
offset: Math.floor(Math.random() * (await Emergencies.count())),
});
const Report4 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Report4?.setEmergency) {
await Report4.setEmergency(relatedEmergency4);
}
}
async function associateReportWithSubmitted_by() {
const relatedSubmitted_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report0 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Report0?.setSubmitted_by) {
await Report0.setSubmitted_by(relatedSubmitted_by0);
}
const relatedSubmitted_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report1 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Report1?.setSubmitted_by) {
await Report1.setSubmitted_by(relatedSubmitted_by1);
}
const relatedSubmitted_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report2 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Report2?.setSubmitted_by) {
await Report2.setSubmitted_by(relatedSubmitted_by2);
}
const relatedSubmitted_by3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report3 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Report3?.setSubmitted_by) {
await Report3.setSubmitted_by(relatedSubmitted_by3);
}
const relatedSubmitted_by4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report4 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Report4?.setSubmitted_by) {
await Report4.setSubmitted_by(relatedSubmitted_by4);
}
}
async function associateReportWithAgency() {
const relatedAgency0 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Report0 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Report0?.setAgency) {
await Report0.setAgency(relatedAgency0);
}
const relatedAgency1 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Report1 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Report1?.setAgency) {
await Report1.setAgency(relatedAgency1);
}
const relatedAgency2 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Report2 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Report2?.setAgency) {
await Report2.setAgency(relatedAgency2);
}
const relatedAgency3 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Report3 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Report3?.setAgency) {
await Report3.setAgency(relatedAgency3);
}
const relatedAgency4 = await Agencies.findOne({
offset: Math.floor(Math.random() * (await Agencies.count())),
});
const Report4 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Report4?.setAgency) {
await Report4.setAgency(relatedAgency4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Emergencies.bulkCreate(EmergenciesData);
await Notifications.bulkCreate(NotificationsData);
await Reports.bulkCreate(ReportsData);
await Agencies.bulkCreate(AgenciesData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithAgency(),
await associateEmergencyWithReported_by(),
await associateEmergencyWithAssigned_to(),
await associateEmergencyWithAgency(),
await associateNotificationWithUser(),
await associateNotificationWithAgency(),
await associateReportWithEmergency(),
await associateReportWithSubmitted_by(),
await associateReportWithAgency(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('emergencies', null, {});
await queryInterface.bulkDelete('notifications', null, {});
await queryInterface.bulkDelete('reports', null, {});
await queryInterface.bulkDelete('agencies', null, {});
},
};