30031/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-19 11:55:16 +00:00

339 lines
7.2 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Notifications = db.notifications;
const Projects = db.projects;
const Settings = db.settings;
const WorkPackages = db.work_packages;
const NotificationsData = [
{
// type code here for "relation_one" field
notification_date: new Date('2023-10-01T10:05:00Z'),
read: true,
},
{
// type code here for "relation_one" field
notification_date: new Date('2023-10-02T11:05:00Z'),
read: true,
},
{
// type code here for "relation_one" field
notification_date: new Date('2023-10-03T12:05:00Z'),
read: false,
},
{
// type code here for "relation_one" field
notification_date: new Date('2023-10-04T13:05:00Z'),
read: false,
},
{
// type code here for "relation_one" field
notification_date: new Date('2023-10-05T14:05:00Z'),
read: true,
},
];
const ProjectsData = [
{
name: 'Project Alpha',
// type code here for "relation_many" field
},
{
name: 'Project Beta',
// type code here for "relation_many" field
},
{
name: 'Project Gamma',
// type code here for "relation_many" field
},
{
name: 'Project Delta',
// type code here for "relation_many" field
},
{
name: 'Project Epsilon',
// type code here for "relation_many" field
},
];
const SettingsData = [
{
openproject_url: 'https://openproject.example.com',
openproject_api_key: 'abc123',
},
{
openproject_url: 'https://openproject.example.com',
openproject_api_key: 'def456',
},
{
openproject_url: 'https://openproject.example.com',
openproject_api_key: 'ghi789',
},
{
openproject_url: 'https://openproject.example.com',
openproject_api_key: 'jkl012',
},
{
openproject_url: 'https://openproject.example.com',
openproject_api_key: 'mno345',
},
];
const WorkPackagesData = [
{
title: 'Design Homepage',
status: 'Done',
// type code here for "relation_one" field
assigned_to: 'John Doe',
event_type: 'Closed',
event_date: new Date('2023-10-01T10:00:00Z'),
},
{
title: 'Develop API',
status: 'Done',
// type code here for "relation_one" field
assigned_to: 'Jane Smith',
event_type: 'Closed',
event_date: new Date('2023-10-02T11:00:00Z'),
},
{
title: 'Test Application',
status: 'Done',
// type code here for "relation_one" field
assigned_to: 'Michael Brown',
event_type: 'Closed',
event_date: new Date('2023-10-03T12:00:00Z'),
},
{
title: 'Deploy to Production',
status: 'InProgress',
// type code here for "relation_one" field
assigned_to: 'Emily Davis',
event_type: 'Updated',
event_date: new Date('2023-10-04T13:00:00Z'),
},
{
title: 'Update Documentation',
status: 'Done',
// type code here for "relation_one" field
assigned_to: 'David Wilson',
event_type: 'Closed',
event_date: new Date('2023-10-05T14:00:00Z'),
},
];
// Similar logic for "relation_many"
async function associateNotificationWithWork_package() {
const relatedWork_package0 = await WorkPackages.findOne({
offset: Math.floor(Math.random() * (await WorkPackages.count())),
});
const Notification0 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Notification0?.setWork_package) {
await Notification0.setWork_package(relatedWork_package0);
}
const relatedWork_package1 = await WorkPackages.findOne({
offset: Math.floor(Math.random() * (await WorkPackages.count())),
});
const Notification1 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Notification1?.setWork_package) {
await Notification1.setWork_package(relatedWork_package1);
}
const relatedWork_package2 = await WorkPackages.findOne({
offset: Math.floor(Math.random() * (await WorkPackages.count())),
});
const Notification2 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Notification2?.setWork_package) {
await Notification2.setWork_package(relatedWork_package2);
}
const relatedWork_package3 = await WorkPackages.findOne({
offset: Math.floor(Math.random() * (await WorkPackages.count())),
});
const Notification3 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (Notification3?.setWork_package) {
await Notification3.setWork_package(relatedWork_package3);
}
const relatedWork_package4 = await WorkPackages.findOne({
offset: Math.floor(Math.random() * (await WorkPackages.count())),
});
const Notification4 = await Notifications.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (Notification4?.setWork_package) {
await Notification4.setWork_package(relatedWork_package4);
}
}
// Similar logic for "relation_many"
async function associateWorkPackageWithProject() {
const relatedProject0 = await Projects.findOne({
offset: Math.floor(Math.random() * (await Projects.count())),
});
const WorkPackage0 = await WorkPackages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (WorkPackage0?.setProject) {
await WorkPackage0.setProject(relatedProject0);
}
const relatedProject1 = await Projects.findOne({
offset: Math.floor(Math.random() * (await Projects.count())),
});
const WorkPackage1 = await WorkPackages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (WorkPackage1?.setProject) {
await WorkPackage1.setProject(relatedProject1);
}
const relatedProject2 = await Projects.findOne({
offset: Math.floor(Math.random() * (await Projects.count())),
});
const WorkPackage2 = await WorkPackages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (WorkPackage2?.setProject) {
await WorkPackage2.setProject(relatedProject2);
}
const relatedProject3 = await Projects.findOne({
offset: Math.floor(Math.random() * (await Projects.count())),
});
const WorkPackage3 = await WorkPackages.findOne({
order: [['id', 'ASC']],
offset: 3,
});
if (WorkPackage3?.setProject) {
await WorkPackage3.setProject(relatedProject3);
}
const relatedProject4 = await Projects.findOne({
offset: Math.floor(Math.random() * (await Projects.count())),
});
const WorkPackage4 = await WorkPackages.findOne({
order: [['id', 'ASC']],
offset: 4,
});
if (WorkPackage4?.setProject) {
await WorkPackage4.setProject(relatedProject4);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Notifications.bulkCreate(NotificationsData);
await Projects.bulkCreate(ProjectsData);
await Settings.bulkCreate(SettingsData);
await WorkPackages.bulkCreate(WorkPackagesData);
await Promise.all([
// Similar logic for "relation_many"
await associateNotificationWithWork_package(),
// Similar logic for "relation_many"
await associateWorkPackageWithProject(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('notifications', null, {});
await queryInterface.bulkDelete('projects', null, {});
await queryInterface.bulkDelete('settings', null, {});
await queryInterface.bulkDelete('work_packages', null, {});
},
};