30146/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-24 09:59:10 +00:00

674 lines
16 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const AuditLogs = db.audit_logs;
const Modules = db.modules;
const UserRoles = db.user_roles;
const Companies = db.companies;
const AuditLogsData = [
{
action: 'Created new company',
timestamp: new Date('2023-01-15T08:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
action: 'Updated user role',
timestamp: new Date('2023-02-20T08:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
action: 'Deleted policy document',
timestamp: new Date('2022-05-10T08:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
action: 'Reviewed audit log',
timestamp: new Date('2023-03-25T08:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
action: 'Approved training module',
timestamp: new Date('2023-04-30T08:00:00Z'),
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const ModulesData = [
{
name: 'Evidence Collection',
description: 'Automated evidence collection and monitoring.',
// type code here for "relation_one" field
},
{
name: 'Compliance Framework Mapping',
description: 'Mapping and management of compliance frameworks.',
// type code here for "relation_one" field
},
{
name: 'Risk Assessment',
description: 'Assessment and remediation of risks.',
// type code here for "relation_one" field
},
{
name: 'Policy Documentation Management',
description: 'Management of policy documents and approvals.',
// type code here for "relation_one" field
},
{
name: 'Vendor Risk Management',
description: 'Management of vendor risks and assessments.',
// type code here for "relation_one" field
},
];
const UserRolesData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const CompaniesData = [
{
name: 'Tech Innovators',
},
{
name: 'Green Solutions',
},
{
name: 'HealthTech Corp',
},
{
name: 'EduLearn',
},
{
name: 'FinancePros',
},
];
// Similar logic for "relation_many"
async function associateUserWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setCompany) {
await User0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setCompany) {
await User1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setCompany) {
await User2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User3 = await Users.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (User3?.setCompany) {
await User3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const User4 = await Users.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (User4?.setCompany) {
await User4.setCompany(relatedCompany4);
}
}
async function associateAuditLogWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AuditLog0 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (AuditLog0?.setUser) {
await AuditLog0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AuditLog1 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (AuditLog1?.setUser) {
await AuditLog1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AuditLog2 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (AuditLog2?.setUser) {
await AuditLog2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AuditLog3 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (AuditLog3?.setUser) {
await AuditLog3.setUser(relatedUser3);
}
const relatedUser4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const AuditLog4 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (AuditLog4?.setUser) {
await AuditLog4.setUser(relatedUser4);
}
}
async function associateAuditLogWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog0 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (AuditLog0?.setCompany) {
await AuditLog0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog1 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (AuditLog1?.setCompany) {
await AuditLog1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog2 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (AuditLog2?.setCompany) {
await AuditLog2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog3 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (AuditLog3?.setCompany) {
await AuditLog3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog4 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (AuditLog4?.setCompany) {
await AuditLog4.setCompany(relatedCompany4);
}
}
async function associateAuditLogWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog0 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (AuditLog0?.setCompany) {
await AuditLog0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog1 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (AuditLog1?.setCompany) {
await AuditLog1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog2 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (AuditLog2?.setCompany) {
await AuditLog2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog3 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (AuditLog3?.setCompany) {
await AuditLog3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const AuditLog4 = await AuditLogs.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (AuditLog4?.setCompany) {
await AuditLog4.setCompany(relatedCompany4);
}
}
async function associateModuleWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Module0 = await Modules.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Module0?.setCompany) {
await Module0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Module1 = await Modules.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Module1?.setCompany) {
await Module1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Module2 = await Modules.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Module2?.setCompany) {
await Module2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Module3 = await Modules.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Module3?.setCompany) {
await Module3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const Module4 = await Modules.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Module4?.setCompany) {
await Module4.setCompany(relatedCompany4);
}
}
async function associateUserRoleWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const UserRole0 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (UserRole0?.setUser) {
await UserRole0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const UserRole1 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (UserRole1?.setUser) {
await UserRole1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const UserRole2 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (UserRole2?.setUser) {
await UserRole2.setUser(relatedUser2);
}
const relatedUser3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const UserRole3 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (UserRole3?.setUser) {
await UserRole3.setUser(relatedUser3);
}
const relatedUser4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const UserRole4 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (UserRole4?.setUser) {
await UserRole4.setUser(relatedUser4);
}
}
async function associateUserRoleWithModule() {
const relatedModule0 = await Modules.findOne({
offset: Math.floor(Math.random() * (await Modules.count())),
});
const UserRole0 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (UserRole0?.setModule) {
await UserRole0.setModule(relatedModule0);
}
const relatedModule1 = await Modules.findOne({
offset: Math.floor(Math.random() * (await Modules.count())),
});
const UserRole1 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (UserRole1?.setModule) {
await UserRole1.setModule(relatedModule1);
}
const relatedModule2 = await Modules.findOne({
offset: Math.floor(Math.random() * (await Modules.count())),
});
const UserRole2 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (UserRole2?.setModule) {
await UserRole2.setModule(relatedModule2);
}
const relatedModule3 = await Modules.findOne({
offset: Math.floor(Math.random() * (await Modules.count())),
});
const UserRole3 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (UserRole3?.setModule) {
await UserRole3.setModule(relatedModule3);
}
const relatedModule4 = await Modules.findOne({
offset: Math.floor(Math.random() * (await Modules.count())),
});
const UserRole4 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (UserRole4?.setModule) {
await UserRole4.setModule(relatedModule4);
}
}
async function associateUserRoleWithCompany() {
const relatedCompany0 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const UserRole0 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (UserRole0?.setCompany) {
await UserRole0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const UserRole1 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (UserRole1?.setCompany) {
await UserRole1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const UserRole2 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (UserRole2?.setCompany) {
await UserRole2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const UserRole3 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (UserRole3?.setCompany) {
await UserRole3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Companies.findOne({
offset: Math.floor(Math.random() * (await Companies.count())),
});
const UserRole4 = await UserRoles.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (UserRole4?.setCompany) {
await UserRole4.setCompany(relatedCompany4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await AuditLogs.bulkCreate(AuditLogsData);
await Modules.bulkCreate(ModulesData);
await UserRoles.bulkCreate(UserRolesData);
await Companies.bulkCreate(CompaniesData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithCompany(),
await associateAuditLogWithUser(),
await associateAuditLogWithCompany(),
await associateAuditLogWithCompany(),
await associateModuleWithCompany(),
await associateUserRoleWithUser(),
await associateUserRoleWithModule(),
await associateUserRoleWithCompany(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('audit_logs', null, {});
await queryInterface.bulkDelete('modules', null, {});
await queryInterface.bulkDelete('user_roles', null, {});
await queryInterface.bulkDelete('companies', null, {});
},
};