233 lines
5.3 KiB
JavaScript
233 lines
5.3 KiB
JavaScript
const db = require('../models');
|
|
const Users = db.users;
|
|
|
|
const Conversations = db.conversations;
|
|
|
|
const Messages = db.messages;
|
|
|
|
const ConversationsData = [
|
|
{
|
|
title: 'Project Kickoff',
|
|
|
|
// type code here for "relation_many" field
|
|
},
|
|
|
|
{
|
|
title: 'Weekly Sync',
|
|
|
|
// type code here for "relation_many" field
|
|
},
|
|
|
|
{
|
|
title: 'Design Review',
|
|
|
|
// type code here for "relation_many" field
|
|
},
|
|
|
|
{
|
|
title: 'Client Feedback',
|
|
|
|
// type code here for "relation_many" field
|
|
},
|
|
|
|
{
|
|
title: 'Retrospective',
|
|
|
|
// type code here for "relation_many" field
|
|
},
|
|
];
|
|
|
|
const MessagesData = [
|
|
{
|
|
content: "Let's start the project kickoff meeting.",
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
sent_at: new Date('2023-10-01T10:00:00Z'),
|
|
},
|
|
|
|
{
|
|
content: 'Please review the design documents.',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
sent_at: new Date('2023-10-02T11:00:00Z'),
|
|
},
|
|
|
|
{
|
|
content: 'Here is the client feedback from last week.',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
sent_at: new Date('2023-10-03T12:00:00Z'),
|
|
},
|
|
|
|
{
|
|
content: 'What are the action items from the retrospective?',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
sent_at: new Date('2023-10-04T13:00:00Z'),
|
|
},
|
|
|
|
{
|
|
content: 'Weekly sync is scheduled for tomorrow.',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
sent_at: new Date('2023-10-05T14:00:00Z'),
|
|
},
|
|
];
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
async function associateMessageWithUser() {
|
|
const relatedUser0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Message0 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Message0?.setUser) {
|
|
await Message0.setUser(relatedUser0);
|
|
}
|
|
|
|
const relatedUser1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Message1 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Message1?.setUser) {
|
|
await Message1.setUser(relatedUser1);
|
|
}
|
|
|
|
const relatedUser2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Message2 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Message2?.setUser) {
|
|
await Message2.setUser(relatedUser2);
|
|
}
|
|
|
|
const relatedUser3 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Message3 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Message3?.setUser) {
|
|
await Message3.setUser(relatedUser3);
|
|
}
|
|
|
|
const relatedUser4 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Message4 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 4,
|
|
});
|
|
if (Message4?.setUser) {
|
|
await Message4.setUser(relatedUser4);
|
|
}
|
|
}
|
|
|
|
async function associateMessageWithConversation() {
|
|
const relatedConversation0 = await Conversations.findOne({
|
|
offset: Math.floor(Math.random() * (await Conversations.count())),
|
|
});
|
|
const Message0 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Message0?.setConversation) {
|
|
await Message0.setConversation(relatedConversation0);
|
|
}
|
|
|
|
const relatedConversation1 = await Conversations.findOne({
|
|
offset: Math.floor(Math.random() * (await Conversations.count())),
|
|
});
|
|
const Message1 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Message1?.setConversation) {
|
|
await Message1.setConversation(relatedConversation1);
|
|
}
|
|
|
|
const relatedConversation2 = await Conversations.findOne({
|
|
offset: Math.floor(Math.random() * (await Conversations.count())),
|
|
});
|
|
const Message2 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Message2?.setConversation) {
|
|
await Message2.setConversation(relatedConversation2);
|
|
}
|
|
|
|
const relatedConversation3 = await Conversations.findOne({
|
|
offset: Math.floor(Math.random() * (await Conversations.count())),
|
|
});
|
|
const Message3 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 3,
|
|
});
|
|
if (Message3?.setConversation) {
|
|
await Message3.setConversation(relatedConversation3);
|
|
}
|
|
|
|
const relatedConversation4 = await Conversations.findOne({
|
|
offset: Math.floor(Math.random() * (await Conversations.count())),
|
|
});
|
|
const Message4 = await Messages.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 4,
|
|
});
|
|
if (Message4?.setConversation) {
|
|
await Message4.setConversation(relatedConversation4);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
up: async (queryInterface, Sequelize) => {
|
|
await Conversations.bulkCreate(ConversationsData);
|
|
|
|
await Messages.bulkCreate(MessagesData);
|
|
|
|
await Promise.all([
|
|
// Similar logic for "relation_many"
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
await associateMessageWithUser(),
|
|
|
|
await associateMessageWithConversation(),
|
|
]);
|
|
},
|
|
|
|
down: async (queryInterface, Sequelize) => {
|
|
await queryInterface.bulkDelete('conversations', null, {});
|
|
|
|
await queryInterface.bulkDelete('messages', null, {});
|
|
},
|
|
};
|