33068/backend/src/db/seeders/20231127130745-sample-data.js
2025-07-28 16:04:44 +00:00

318 lines
7.4 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const ChatGroups = db.chat_groups;
const FileShares = db.file_shares;
const Messages = db.messages;
const ChatGroupsData = [
{
name: 'Project Alpha',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'HR Team',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
name: 'Development Team',
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const FileSharesData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "files" field
shared_at: new Date('2023-10-01T09:00:00Z'),
status: 'Pending',
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "files" field
shared_at: new Date('2023-10-01T09:30:00Z'),
status: 'Approved',
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
// type code here for "files" field
shared_at: new Date('2023-10-01T10:00:00Z'),
status: 'Rejected',
},
];
const MessagesData = [
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Hello, how are you?',
sent_at: new Date('2023-10-01T10:00:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: "I'm good, thanks! How about you?",
sent_at: new Date('2023-10-01T10:05:00Z'),
},
{
// type code here for "relation_one" field
// type code here for "relation_one" field
content: 'Please review the attached document.',
sent_at: new Date('2023-10-01T11:00:00Z'),
},
];
// Similar logic for "relation_many"
// Similar logic for "relation_many"
async function associateChatGroupWithAdmin() {
const relatedAdmin0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ChatGroup0 = await ChatGroups.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ChatGroup0?.setAdmin) {
await ChatGroup0.setAdmin(relatedAdmin0);
}
const relatedAdmin1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ChatGroup1 = await ChatGroups.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ChatGroup1?.setAdmin) {
await ChatGroup1.setAdmin(relatedAdmin1);
}
const relatedAdmin2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ChatGroup2 = await ChatGroups.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ChatGroup2?.setAdmin) {
await ChatGroup2.setAdmin(relatedAdmin2);
}
}
async function associateFileShareWithShared_by() {
const relatedShared_by0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const FileShare0 = await FileShares.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (FileShare0?.setShared_by) {
await FileShare0.setShared_by(relatedShared_by0);
}
const relatedShared_by1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const FileShare1 = await FileShares.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (FileShare1?.setShared_by) {
await FileShare1.setShared_by(relatedShared_by1);
}
const relatedShared_by2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const FileShare2 = await FileShares.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (FileShare2?.setShared_by) {
await FileShare2.setShared_by(relatedShared_by2);
}
}
async function associateFileShareWithShared_with() {
const relatedShared_with0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const FileShare0 = await FileShares.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (FileShare0?.setShared_with) {
await FileShare0.setShared_with(relatedShared_with0);
}
const relatedShared_with1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const FileShare1 = await FileShares.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (FileShare1?.setShared_with) {
await FileShare1.setShared_with(relatedShared_with1);
}
const relatedShared_with2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const FileShare2 = await FileShares.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (FileShare2?.setShared_with) {
await FileShare2.setShared_with(relatedShared_with2);
}
}
async function associateMessageWithSender() {
const relatedSender0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setSender) {
await Message0.setSender(relatedSender0);
}
const relatedSender1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setSender) {
await Message1.setSender(relatedSender1);
}
const relatedSender2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setSender) {
await Message2.setSender(relatedSender2);
}
}
async function associateMessageWithReceiver() {
const relatedReceiver0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message0 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Message0?.setReceiver) {
await Message0.setReceiver(relatedReceiver0);
}
const relatedReceiver1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message1 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Message1?.setReceiver) {
await Message1.setReceiver(relatedReceiver1);
}
const relatedReceiver2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Message2 = await Messages.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Message2?.setReceiver) {
await Message2.setReceiver(relatedReceiver2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await ChatGroups.bulkCreate(ChatGroupsData);
await FileShares.bulkCreate(FileSharesData);
await Messages.bulkCreate(MessagesData);
await Promise.all([
// Similar logic for "relation_many"
// Similar logic for "relation_many"
await associateChatGroupWithAdmin(),
await associateFileShareWithShared_by(),
await associateFileShareWithShared_with(),
await associateMessageWithSender(),
await associateMessageWithReceiver(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('chat_groups', null, {});
await queryInterface.bulkDelete('file_shares', null, {});
await queryInterface.bulkDelete('messages', null, {});
},
};