30312/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-29 15:19:07 +00:00

419 lines
9.2 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Companies = db.companies;
const Tasks = db.tasks;
const Company = db.company;
const CompaniesData = [
{
name: 'Tech Innovators',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Creative Solutions',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Global Enterprises',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'NextGen Tech',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Visionary Labs',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const TasksData = [
{
title: 'Design Homepage',
description: 'Create a wireframe for the new homepage.',
priority: 'medium',
due_date: new Date('2023-11-15T09:00:00Z'),
reminder: true,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Develop API',
description: 'Implement the RESTful API for the mobile app.',
priority: 'medium',
due_date: new Date('2023-11-20T12:00:00Z'),
reminder: false,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Test Application',
description: 'Conduct thorough testing of the application.',
priority: 'medium',
due_date: new Date('2023-11-25T15:00:00Z'),
reminder: true,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Prepare Presentation',
description: 'Prepare slides for the upcoming client meeting.',
priority: 'low',
due_date: new Date('2023-11-18T10:00:00Z'),
reminder: true,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
title: 'Update Documentation',
description: 'Revise the user manual to include new features.',
priority: 'low',
due_date: new Date('2023-11-22T14:00:00Z'),
reminder: false,
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const CompanyData = [
{
name: 'Pierre Simon de Laplace',
},
{
name: 'August Kekule',
},
{
name: 'Theodosius Dobzhansky',
},
{
name: 'Archimedes',
},
{
name: 'Claude Levi-Strauss',
},
];
// Similar logic for "relation_many"
async function associateUserWithCompany() {
const relatedCompany0 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setCompany) {
await User0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setCompany) {
await User1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setCompany) {
await User2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const User3 = await Users.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (User3?.setCompany) {
await User3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const User4 = await Users.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (User4?.setCompany) {
await User4.setCompany(relatedCompany4);
}
}
// Similar logic for "relation_many"
async function associateCompanyWithCompany() {
const relatedCompany0 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Company0 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Company0?.setCompany) {
await Company0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Company1 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Company1?.setCompany) {
await Company1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Company2 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Company2?.setCompany) {
await Company2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Company3 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Company3?.setCompany) {
await Company3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Company4 = await Companies.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Company4?.setCompany) {
await Company4.setCompany(relatedCompany4);
}
}
async function associateTaskWithAssigned_to() {
const relatedAssigned_to0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Task0 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Task0?.setAssigned_to) {
await Task0.setAssigned_to(relatedAssigned_to0);
}
const relatedAssigned_to1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Task1 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Task1?.setAssigned_to) {
await Task1.setAssigned_to(relatedAssigned_to1);
}
const relatedAssigned_to2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Task2 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Task2?.setAssigned_to) {
await Task2.setAssigned_to(relatedAssigned_to2);
}
const relatedAssigned_to3 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Task3 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Task3?.setAssigned_to) {
await Task3.setAssigned_to(relatedAssigned_to3);
}
const relatedAssigned_to4 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Task4 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Task4?.setAssigned_to) {
await Task4.setAssigned_to(relatedAssigned_to4);
}
}
async function associateTaskWithCompany() {
const relatedCompany0 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Task0 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Task0?.setCompany) {
await Task0.setCompany(relatedCompany0);
}
const relatedCompany1 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Task1 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Task1?.setCompany) {
await Task1.setCompany(relatedCompany1);
}
const relatedCompany2 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Task2 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Task2?.setCompany) {
await Task2.setCompany(relatedCompany2);
}
const relatedCompany3 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Task3 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Task3?.setCompany) {
await Task3.setCompany(relatedCompany3);
}
const relatedCompany4 = await Company.findOne({
offset: Math.floor(Math.random() * (await Company.count())),
});
const Task4 = await Tasks.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Task4?.setCompany) {
await Task4.setCompany(relatedCompany4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Companies.bulkCreate(CompaniesData);
await Tasks.bulkCreate(TasksData);
await Company.bulkCreate(CompanyData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithCompany(),
// Similar logic for "relation_many"
await associateCompanyWithCompany(),
await associateTaskWithAssigned_to(),
await associateTaskWithCompany(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('companies', null, {});
await queryInterface.bulkDelete('tasks', null, {});
await queryInterface.bulkDelete('company', null, {});
},
};