32252/backend/src/db/seeders/20231127130745-sample-data.js
2025-06-15 07:21:23 +00:00

241 lines
4.9 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Alerts = db.alerts;
const Departments = db.departments;
const Reports = db.reports;
const Rulesets = db.rulesets;
const AlertsData = [
{
message: 'Unauthorized access attempt detected.',
timestamp: new Date('2023-10-03T14:00:00Z'),
// type code here for "relation_one" field
},
{
message: 'New report generated by Analyst.',
timestamp: new Date('2023-10-02T13:00:00Z'),
// type code here for "relation_one" field
},
{
message: 'Ruleset updated by Editor.',
timestamp: new Date('2023-10-01T12:00:00Z'),
// type code here for "relation_one" field
},
];
const DepartmentsData = [
{
name: 'Intelligence',
// type code here for "relation_many" field
},
{
name: 'Operations',
// type code here for "relation_many" field
},
{
name: 'Analysis',
// type code here for "relation_many" field
},
];
const ReportsData = [
{
title: 'Quarterly Threat Assessment',
// type code here for "relation_one" field
created_date: new Date('2023-10-01T10:00:00Z'),
modified_date: new Date('2023-10-02T12:00:00Z'),
status: 'Draft',
// type code here for "relation_many" field
},
{
title: 'Annual Strategic Overview',
// type code here for "relation_one" field
created_date: new Date('2023-09-15T09:00:00Z'),
modified_date: new Date('2023-09-16T11:00:00Z'),
status: 'Review',
// type code here for "relation_many" field
},
{
title: 'Monthly Intelligence Summary',
// type code here for "relation_one" field
created_date: new Date('2023-08-20T08:00:00Z'),
modified_date: new Date('2023-08-21T10:00:00Z'),
status: 'Final',
// type code here for "relation_many" field
},
];
const RulesetsData = [
{
name: 'Standard Operating Procedures',
description: 'Guidelines for standard operations.',
// type code here for "relation_many" field
},
{
name: 'Emergency Protocols',
description: 'Procedures for emergency situations.',
// type code here for "relation_many" field
},
{
name: 'Data Handling Rules',
description: 'Rules for managing sensitive data.',
// type code here for "relation_many" field
},
];
// Similar logic for "relation_many"
async function associateAlertWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Alert0 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Alert0?.setUser) {
await Alert0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Alert1 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Alert1?.setUser) {
await Alert1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Alert2 = await Alerts.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Alert2?.setUser) {
await Alert2.setUser(relatedUser2);
}
}
// Similar logic for "relation_many"
async function associateReportWithAuthor() {
const relatedAuthor0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report0 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Report0?.setAuthor) {
await Report0.setAuthor(relatedAuthor0);
}
const relatedAuthor1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report1 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Report1?.setAuthor) {
await Report1.setAuthor(relatedAuthor1);
}
const relatedAuthor2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Report2 = await Reports.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Report2?.setAuthor) {
await Report2.setAuthor(relatedAuthor2);
}
}
// Similar logic for "relation_many"
// Similar logic for "relation_many"
module.exports = {
up: async (queryInterface, Sequelize) => {
await Alerts.bulkCreate(AlertsData);
await Departments.bulkCreate(DepartmentsData);
await Reports.bulkCreate(ReportsData);
await Rulesets.bulkCreate(RulesetsData);
await Promise.all([
// Similar logic for "relation_many"
await associateAlertWithUser(),
// Similar logic for "relation_many"
await associateReportWithAuthor(),
// Similar logic for "relation_many"
// Similar logic for "relation_many"
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('alerts', null, {});
await queryInterface.bulkDelete('departments', null, {});
await queryInterface.bulkDelete('reports', null, {});
await queryInterface.bulkDelete('rulesets', null, {});
},
};