30396/backend/src/db/seeders/20231127130745-sample-data.js
2025-04-01 17:39:17 +00:00

303 lines
6.7 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Attendance = db.attendance;
const Cases = db.cases;
const Notifications = db.notifications;
const AttendanceData = [
{
// type code here for "relation_one" field
date: new Date('2023-10-01T09:00:00Z'),
ip_address: '192.168.1.10',
},
{
// type code here for "relation_one" field
date: new Date('2023-10-02T09:15:00Z'),
ip_address: '192.168.1.11',
},
{
// type code here for "relation_one" field
date: new Date('2023-10-03T09:30:00Z'),
ip_address: '192.168.1.12',
},
{
// type code here for "relation_one" field
date: new Date('2023-10-04T09:45:00Z'),
ip_address: '192.168.1.13',
},
];
const CasesData = [
{
title: 'Case 001',
description: 'Investigate data breach incident',
assigned_date: new Date('2023-10-01T09:00:00Z'),
completion_date: new Date('2023-10-05T17:00:00Z'),
status: 'completed',
// type code here for "relation_one" field
},
{
title: 'Case 002',
description: 'Analyze malware sample',
assigned_date: new Date('2023-10-02T10:00:00Z'),
completion_date: new Date('2023-10-06T15:00:00Z'),
status: 'in_progress',
// type code here for "relation_one" field
},
{
title: 'Case 003',
description: 'Recover deleted files',
assigned_date: new Date('2023-10-03T11:00:00Z'),
completion_date: new Date('2023-10-07T16:00:00Z'),
status: 'pending',
// type code here for "relation_one" field
},
{
title: 'Case 004',
description: 'Examine suspicious email',
assigned_date: new Date('2023-10-04T12:00:00Z'),
completion_date: new Date('2023-10-08T14:00:00Z'),
status: 'completed',
// type code here for "relation_one" field
},
];
const NotificationsData = [
{
message: 'New case assigned: Case 001',
// type code here for "relation_one" field
date: new Date('2023-10-01T09:05:00Z'),
},
{
message: 'Case 002 status updated to in_progress',
// type code here for "relation_one" field
date: new Date('2023-10-02T10:05:00Z'),
},
{
message: 'Case 003 status updated to pending',
// type code here for "relation_one" field
date: new Date('2023-10-03T11:05:00Z'),
},
{
message: 'Case 004 completed',
// type code here for "relation_one" field
date: new Date('2023-10-04T12:05:00Z'),
},
];
// Similar logic for "relation_many"
async function associateAttendanceWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Attendance0 = await Attendance.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Attendance0?.setUser) {
await Attendance0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Attendance1 = await Attendance.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Attendance1?.setUser) {
await Attendance1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Attendance2 = await Attendance.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Attendance2?.setUser) {
await Attendance2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Attendance3 = await Attendance.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Attendance3?.setUser) {
await Attendance3.setUser(relatedUser3);
}
}
async function associateCaseWithAssigned_to() {
const relatedAssigned_to0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Case0 = await Cases.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Case0?.setAssigned_to) {
await Case0.setAssigned_to(relatedAssigned_to0);
}
const relatedAssigned_to1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Case1 = await Cases.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Case1?.setAssigned_to) {
await Case1.setAssigned_to(relatedAssigned_to1);
}
const relatedAssigned_to2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Case2 = await Cases.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Case2?.setAssigned_to) {
await Case2.setAssigned_to(relatedAssigned_to2);
}
const relatedAssigned_to3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Case3 = await Cases.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Case3?.setAssigned_to) {
await Case3.setAssigned_to(relatedAssigned_to3);
}
}
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);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Attendance.bulkCreate(AttendanceData);
await Cases.bulkCreate(CasesData);
await Notifications.bulkCreate(NotificationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateAttendanceWithUser(),
await associateCaseWithAssigned_to(),
await associateNotificationWithUser(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('attendance', null, {});
await queryInterface.bulkDelete('cases', null, {});
await queryInterface.bulkDelete('notifications', null, {});
},
};