285 lines
5.8 KiB
JavaScript
285 lines
5.8 KiB
JavaScript
const db = require('../models');
|
|
const Users = db.users;
|
|
|
|
const Categories = db.categories;
|
|
|
|
const Expenses = db.expenses;
|
|
|
|
const Leads = db.leads;
|
|
|
|
const Notes = db.notes;
|
|
|
|
const CategoriesData = [
|
|
{
|
|
name: 'Corporate',
|
|
},
|
|
|
|
{
|
|
name: 'SME',
|
|
},
|
|
|
|
{
|
|
name: 'Startup',
|
|
},
|
|
];
|
|
|
|
const ExpensesData = [
|
|
{
|
|
category: 'Office Supplies',
|
|
|
|
amount: 150.75,
|
|
|
|
date: new Date('2023-10-01T10:00:00Z'),
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
category: 'Travel',
|
|
|
|
amount: 300.5,
|
|
|
|
date: new Date('2023-10-05T15:30:00Z'),
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
category: 'Client Entertainment',
|
|
|
|
amount: 200,
|
|
|
|
date: new Date('2023-10-10T12:00:00Z'),
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
];
|
|
|
|
const LeadsData = [
|
|
{
|
|
name: 'Acme Corp',
|
|
|
|
status: 'new',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
name: 'Globex Inc',
|
|
|
|
status: 'qualified',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
name: 'Initech',
|
|
|
|
status: 'qualified',
|
|
|
|
// type code here for "relation_one" field
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
];
|
|
|
|
const NotesData = [
|
|
{
|
|
content: 'Meeting with Acme Corp went well.',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
content: 'Follow up with Globex Inc next week.',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
|
|
{
|
|
content: 'Initech is interested in our services.',
|
|
|
|
// type code here for "relation_one" field
|
|
},
|
|
];
|
|
|
|
// Similar logic for "relation_many"
|
|
|
|
async function associateExpenseWithUser() {
|
|
const relatedUser0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Expense0 = await Expenses.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Expense0?.setUser) {
|
|
await Expense0.setUser(relatedUser0);
|
|
}
|
|
|
|
const relatedUser1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Expense1 = await Expenses.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Expense1?.setUser) {
|
|
await Expense1.setUser(relatedUser1);
|
|
}
|
|
|
|
const relatedUser2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Expense2 = await Expenses.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Expense2?.setUser) {
|
|
await Expense2.setUser(relatedUser2);
|
|
}
|
|
}
|
|
|
|
async function associateLeadWithCategory() {
|
|
const relatedCategory0 = await Categories.findOne({
|
|
offset: Math.floor(Math.random() * (await Categories.count())),
|
|
});
|
|
const Lead0 = await Leads.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Lead0?.setCategory) {
|
|
await Lead0.setCategory(relatedCategory0);
|
|
}
|
|
|
|
const relatedCategory1 = await Categories.findOne({
|
|
offset: Math.floor(Math.random() * (await Categories.count())),
|
|
});
|
|
const Lead1 = await Leads.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Lead1?.setCategory) {
|
|
await Lead1.setCategory(relatedCategory1);
|
|
}
|
|
|
|
const relatedCategory2 = await Categories.findOne({
|
|
offset: Math.floor(Math.random() * (await Categories.count())),
|
|
});
|
|
const Lead2 = await Leads.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Lead2?.setCategory) {
|
|
await Lead2.setCategory(relatedCategory2);
|
|
}
|
|
}
|
|
|
|
async function associateLeadWithOwner() {
|
|
const relatedOwner0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Lead0 = await Leads.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Lead0?.setOwner) {
|
|
await Lead0.setOwner(relatedOwner0);
|
|
}
|
|
|
|
const relatedOwner1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Lead1 = await Leads.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Lead1?.setOwner) {
|
|
await Lead1.setOwner(relatedOwner1);
|
|
}
|
|
|
|
const relatedOwner2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Lead2 = await Leads.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Lead2?.setOwner) {
|
|
await Lead2.setOwner(relatedOwner2);
|
|
}
|
|
}
|
|
|
|
async function associateNoteWithUser() {
|
|
const relatedUser0 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Note0 = await Notes.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 0,
|
|
});
|
|
if (Note0?.setUser) {
|
|
await Note0.setUser(relatedUser0);
|
|
}
|
|
|
|
const relatedUser1 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Note1 = await Notes.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 1,
|
|
});
|
|
if (Note1?.setUser) {
|
|
await Note1.setUser(relatedUser1);
|
|
}
|
|
|
|
const relatedUser2 = await Users.findOne({
|
|
offset: Math.floor(Math.random() * (await Users.count())),
|
|
});
|
|
const Note2 = await Notes.findOne({
|
|
order: [['id', 'ASC']],
|
|
offset: 2,
|
|
});
|
|
if (Note2?.setUser) {
|
|
await Note2.setUser(relatedUser2);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
up: async (queryInterface, Sequelize) => {
|
|
await Categories.bulkCreate(CategoriesData);
|
|
|
|
await Expenses.bulkCreate(ExpensesData);
|
|
|
|
await Leads.bulkCreate(LeadsData);
|
|
|
|
await Notes.bulkCreate(NotesData);
|
|
|
|
await Promise.all([
|
|
// Similar logic for "relation_many"
|
|
|
|
await associateExpenseWithUser(),
|
|
|
|
await associateLeadWithCategory(),
|
|
|
|
await associateLeadWithOwner(),
|
|
|
|
await associateNoteWithUser(),
|
|
]);
|
|
},
|
|
|
|
down: async (queryInterface, Sequelize) => {
|
|
await queryInterface.bulkDelete('categories', null, {});
|
|
|
|
await queryInterface.bulkDelete('expenses', null, {});
|
|
|
|
await queryInterface.bulkDelete('leads', null, {});
|
|
|
|
await queryInterface.bulkDelete('notes', null, {});
|
|
},
|
|
};
|