33238/backend/src/db/seeders/20231127130745-sample-data.js
2025-08-05 15:27:03 +00:00

399 lines
9.3 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Campaigns = db.campaigns;
const OnboardingSteps = db.onboarding_steps;
const Products = db.products;
const Organizations = db.organizations;
const CampaignsData = [
{
name: 'Summer Sales Boost',
goal: 'acquire',
// type code here for "relation_one" field
// type code here for "relation_one" field
status: 'approved',
start_date: new Date('2023-06-01T00:00:00Z'),
end_date: new Date('2023-08-31T23:59:59Z'),
// type code here for "relation_one" field
},
{
name: 'Churn Reduction Initiative',
goal: 'acquire',
// type code here for "relation_one" field
// type code here for "relation_one" field
status: 'rejected',
start_date: new Date('2023-07-01T00:00:00Z'),
end_date: new Date('2023-09-30T23:59:59Z'),
// type code here for "relation_one" field
},
{
name: 'New Product Launch',
goal: 'acquire',
// type code here for "relation_one" field
// type code here for "relation_one" field
status: 'rejected',
start_date: new Date('2023-05-15T00:00:00Z'),
end_date: new Date('2023-07-15T23:59:59Z'),
// type code here for "relation_one" field
},
];
const OnboardingStepsData = [
{
step_name: 'Select Product Line',
order: 1,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
step_name: 'Enter User Details',
order: 2,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
{
step_name: 'Review Choices',
order: 3,
// type code here for "relation_many" field
// type code here for "relation_one" field
},
];
const ProductsData = [
{
name: 'Unlimited Data Plan',
product_line: 'handset',
price: 49.99,
// type code here for "relation_one" field
},
{
name: 'Fiber Internet 100Mbps',
product_line: 'fiber',
price: 29.99,
// type code here for "relation_one" field
},
{
name: 'Prepaid SIM Card',
product_line: 'fiber',
price: 9.99,
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'William Herschel',
},
{
name: 'Francis Galton',
},
{
name: 'Archimedes',
},
];
// Similar logic for "relation_many"
async function associateUserWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User0 = await Users.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (User0?.setOrganization) {
await User0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User1 = await Users.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (User1?.setOrganization) {
await User1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const User2 = await Users.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (User2?.setOrganization) {
await User2.setOrganization(relatedOrganization2);
}
}
async function associateCampaignWithCreator() {
const relatedCreator0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Campaign0 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Campaign0?.setCreator) {
await Campaign0.setCreator(relatedCreator0);
}
const relatedCreator1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Campaign1 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Campaign1?.setCreator) {
await Campaign1.setCreator(relatedCreator1);
}
const relatedCreator2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Campaign2 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Campaign2?.setCreator) {
await Campaign2.setCreator(relatedCreator2);
}
}
async function associateCampaignWithReviewer() {
const relatedReviewer0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Campaign0 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Campaign0?.setReviewer) {
await Campaign0.setReviewer(relatedReviewer0);
}
const relatedReviewer1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Campaign1 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Campaign1?.setReviewer) {
await Campaign1.setReviewer(relatedReviewer1);
}
const relatedReviewer2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Campaign2 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Campaign2?.setReviewer) {
await Campaign2.setReviewer(relatedReviewer2);
}
}
async function associateCampaignWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Campaign0 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Campaign0?.setOrganization) {
await Campaign0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Campaign1 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Campaign1?.setOrganization) {
await Campaign1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Campaign2 = await Campaigns.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Campaign2?.setOrganization) {
await Campaign2.setOrganization(relatedOrganization2);
}
}
// Similar logic for "relation_many"
async function associateOnboardingStepWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const OnboardingStep0 = await OnboardingSteps.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (OnboardingStep0?.setOrganization) {
await OnboardingStep0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const OnboardingStep1 = await OnboardingSteps.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (OnboardingStep1?.setOrganization) {
await OnboardingStep1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const OnboardingStep2 = await OnboardingSteps.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (OnboardingStep2?.setOrganization) {
await OnboardingStep2.setOrganization(relatedOrganization2);
}
}
async function associateProductWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Product0 = await Products.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Product0?.setOrganization) {
await Product0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Product1 = await Products.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Product1?.setOrganization) {
await Product1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Product2 = await Products.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Product2?.setOrganization) {
await Product2.setOrganization(relatedOrganization2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Campaigns.bulkCreate(CampaignsData);
await OnboardingSteps.bulkCreate(OnboardingStepsData);
await Products.bulkCreate(ProductsData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateCampaignWithCreator(),
await associateCampaignWithReviewer(),
await associateCampaignWithOrganization(),
// Similar logic for "relation_many"
await associateOnboardingStepWithOrganization(),
await associateProductWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('campaigns', null, {});
await queryInterface.bulkDelete('onboarding_steps', null, {});
await queryInterface.bulkDelete('products', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};