33903/backend/src/db/seeders/20231127130745-sample-data.js
2025-09-05 19:04:28 +00:00

573 lines
14 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const AccessCodes = db.access_codes;
const EmergencyCalls = db.emergency_calls;
const Organizations = db.organizations;
const AccessCodesData = [
{
code: 'ABC123',
is_active: true,
activation_date: new Date('2023-10-01T09:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
code: 'DEF456',
is_active: false,
activation_date: new Date(Date.now()),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
code: 'GHI789',
is_active: true,
activation_date: new Date('2023-10-02T10:30:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
code: 'JKL012',
is_active: true,
activation_date: new Date('2023-10-03T11:45:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
code: 'MNO345',
is_active: true,
activation_date: new Date(Date.now()),
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const EmergencyCallsData = [
{
// type code here for "relation_one" field
emergency_type: 'Escort',
// type code here for "images" field
location: '123 Main St, Anytown',
status: 'Pending',
// type code here for "relation_one" field
feedback: 'Quick response, very helpful.',
rating: 5,
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
emergency_type: 'Conflict',
// type code here for "images" field
location: '456 Elm St, Othertown',
status: 'Pending',
// type code here for "relation_one" field
feedback: 'Resolved quickly, thank you.',
rating: 4,
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
emergency_type: 'Security',
// type code here for "images" field
location: '789 Oak St, Sometown',
status: 'Cancelled',
// type code here for "relation_one" field
feedback: 'Professional service.',
rating: 5,
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
emergency_type: 'Security',
// type code here for "images" field
location: '321 Pine St, Anothertown',
status: 'Pending',
// type code here for "relation_one" field
feedback: 'Cancelled by client.',
rating: 3,
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
emergency_type: 'Security',
// type code here for "images" field
location: '654 Maple St, Yetanothertown',
status: 'InProgress',
// type code here for "relation_one" field
feedback: 'Awaiting resolution.',
rating: 4,
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'Dmitri Mendeleev',
},
{
name: 'Werner Heisenberg',
},
{
name: 'Joseph J. Thomson',
},
{
name: 'Tycho Brahe',
},
{
name: 'Neils Bohr',
},
];
// Similar logic for "relation_many"
async function associateUserWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setOrganization) {
await User0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setOrganization) {
await User1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setOrganization) {
await User2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User3 = await Users.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (User3?.setOrganization) {
await User3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User4 = await Users.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (User4?.setOrganization) {
await User4.setOrganization(relatedOrganization4);
}
}
async function associateAccessCodeWithActivated_by() {
const relatedActivated_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AccessCode0 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (AccessCode0?.setActivated_by) {
await AccessCode0.setActivated_by(relatedActivated_by0);
}
const relatedActivated_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AccessCode1 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (AccessCode1?.setActivated_by) {
await AccessCode1.setActivated_by(relatedActivated_by1);
}
const relatedActivated_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AccessCode2 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (AccessCode2?.setActivated_by) {
await AccessCode2.setActivated_by(relatedActivated_by2);
}
const relatedActivated_by3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AccessCode3 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (AccessCode3?.setActivated_by) {
await AccessCode3.setActivated_by(relatedActivated_by3);
}
const relatedActivated_by4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AccessCode4 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (AccessCode4?.setActivated_by) {
await AccessCode4.setActivated_by(relatedActivated_by4);
}
}
async function associateAccessCodeWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const AccessCode0 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (AccessCode0?.setOrganization) {
await AccessCode0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const AccessCode1 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (AccessCode1?.setOrganization) {
await AccessCode1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const AccessCode2 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (AccessCode2?.setOrganization) {
await AccessCode2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const AccessCode3 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (AccessCode3?.setOrganization) {
await AccessCode3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const AccessCode4 = await AccessCodes.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (AccessCode4?.setOrganization) {
await AccessCode4.setOrganization(relatedOrganization4);
}
}
async function associateEmergencyCallWithClient() {
const relatedClient0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall0 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (EmergencyCall0?.setClient) {
await EmergencyCall0.setClient(relatedClient0);
}
const relatedClient1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall1 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (EmergencyCall1?.setClient) {
await EmergencyCall1.setClient(relatedClient1);
}
const relatedClient2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall2 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (EmergencyCall2?.setClient) {
await EmergencyCall2.setClient(relatedClient2);
}
const relatedClient3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall3 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (EmergencyCall3?.setClient) {
await EmergencyCall3.setClient(relatedClient3);
}
const relatedClient4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall4 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (EmergencyCall4?.setClient) {
await EmergencyCall4.setClient(relatedClient4);
}
}
async function associateEmergencyCallWithDispatcher() {
const relatedDispatcher0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall0 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (EmergencyCall0?.setDispatcher) {
await EmergencyCall0.setDispatcher(relatedDispatcher0);
}
const relatedDispatcher1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall1 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (EmergencyCall1?.setDispatcher) {
await EmergencyCall1.setDispatcher(relatedDispatcher1);
}
const relatedDispatcher2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall2 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (EmergencyCall2?.setDispatcher) {
await EmergencyCall2.setDispatcher(relatedDispatcher2);
}
const relatedDispatcher3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall3 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (EmergencyCall3?.setDispatcher) {
await EmergencyCall3.setDispatcher(relatedDispatcher3);
}
const relatedDispatcher4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const EmergencyCall4 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (EmergencyCall4?.setDispatcher) {
await EmergencyCall4.setDispatcher(relatedDispatcher4);
}
}
async function associateEmergencyCallWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const EmergencyCall0 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (EmergencyCall0?.setOrganization) {
await EmergencyCall0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const EmergencyCall1 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (EmergencyCall1?.setOrganization) {
await EmergencyCall1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const EmergencyCall2 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (EmergencyCall2?.setOrganization) {
await EmergencyCall2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const EmergencyCall3 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (EmergencyCall3?.setOrganization) {
await EmergencyCall3.setOrganization(relatedOrganization3);
}
const relatedOrganization4 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const EmergencyCall4 = await EmergencyCalls.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (EmergencyCall4?.setOrganization) {
await EmergencyCall4.setOrganization(relatedOrganization4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await AccessCodes.bulkCreate(AccessCodesData);
await EmergencyCalls.bulkCreate(EmergencyCallsData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateAccessCodeWithActivated_by(),
await associateAccessCodeWithOrganization(),
await associateEmergencyCallWithClient(),
await associateEmergencyCallWithDispatcher(),
await associateEmergencyCallWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('access_codes', null, {});
await queryInterface.bulkDelete('emergency_calls', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};