31722/backend/src/db/seeders/20231127130745-sample-data.js
2025-05-23 00:34:28 +00:00

387 lines
9.7 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Departments = db.departments;
const Transactions = db.transactions;
const Organizations = db.organizations;
const DepartmentsData = [
{
name: 'Human Resources',
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Research and Development',
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Customer Support',
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Marketing',
// type code here for "relation_one" field
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const TransactionsData = [
{
title: 'Annual Budget Report',
type: 'InternalLetter',
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "relation_one" field
},
{
title: 'New Employee Onboarding',
type: 'InternalLetter',
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "relation_one" field
},
{
title: 'Product Launch Plan',
type: 'InternalLetter',
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "relation_one" field
},
{
title: 'Customer Feedback Analysis',
type: 'Incoming',
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'Tech Innovators',
},
{
name: 'Green Solutions',
},
{
name: 'Health First',
},
{
name: 'EduTech',
},
];
// 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);
}
}
async function associateDepartmentWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department0 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Department0?.setOrganization) {
await Department0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department1 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Department1?.setOrganization) {
await Department1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department2 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Department2?.setOrganization) {
await Department2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department3 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Department3?.setOrganization) {
await Department3.setOrganization(relatedOrganization3);
}
}
// Similar logic for "relation_many"
async function associateDepartmentWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department0 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Department0?.setOrganization) {
await Department0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department1 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Department1?.setOrganization) {
await Department1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department2 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Department2?.setOrganization) {
await Department2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Department3 = await Departments.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Department3?.setOrganization) {
await Department3.setOrganization(relatedOrganization3);
}
}
async function associateTransactionWithDepartment() {
const relatedDepartment0 = await Departments.findOne({
offset: Math.floor(Math.random() * (await Departments.count())),
});
const Transaction0 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Transaction0?.setDepartment) {
await Transaction0.setDepartment(relatedDepartment0);
}
const relatedDepartment1 = await Departments.findOne({
offset: Math.floor(Math.random() * (await Departments.count())),
});
const Transaction1 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Transaction1?.setDepartment) {
await Transaction1.setDepartment(relatedDepartment1);
}
const relatedDepartment2 = await Departments.findOne({
offset: Math.floor(Math.random() * (await Departments.count())),
});
const Transaction2 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Transaction2?.setDepartment) {
await Transaction2.setDepartment(relatedDepartment2);
}
const relatedDepartment3 = await Departments.findOne({
offset: Math.floor(Math.random() * (await Departments.count())),
});
const Transaction3 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Transaction3?.setDepartment) {
await Transaction3.setDepartment(relatedDepartment3);
}
}
async function associateTransactionWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Transaction0 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Transaction0?.setOrganization) {
await Transaction0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Transaction1 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Transaction1?.setOrganization) {
await Transaction1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Transaction2 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Transaction2?.setOrganization) {
await Transaction2.setOrganization(relatedOrganization2);
}
const relatedOrganization3 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Transaction3 = await Transactions.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Transaction3?.setOrganization) {
await Transaction3.setOrganization(relatedOrganization3);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Departments.bulkCreate(DepartmentsData);
await Transactions.bulkCreate(TransactionsData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateDepartmentWithOrganization(),
// Similar logic for "relation_many"
await associateDepartmentWithOrganization(),
await associateTransactionWithDepartment(),
await associateTransactionWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('departments', null, {});
await queryInterface.bulkDelete('transactions', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};