29656/backend/src/db/seeders/20231127130745-sample-data.js
2025-03-06 04:40:37 +00:00

576 lines
14 KiB
JavaScript

const db = require('../models');
const Users = db.users;
const Analytics = db.analytics;
const ApiTokens = db.api_tokens;
const Payments = db.payments;
const SubscriptionPlans = db.subscription_plans;
const Translations = db.translations;
const Organizations = db.organizations;
const AnalyticsData = [
{
translations_processed: 100,
popular_language: 'Spanish',
revenue: 1000,
// type code here for "relation_one" field
},
{
translations_processed: 200,
popular_language: 'French',
revenue: 2000,
// type code here for "relation_one" field
},
{
translations_processed: 150,
popular_language: 'German',
revenue: 1500,
// type code here for "relation_one" field
},
];
const ApiTokensData = [
{
token: 'api_token_1',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
token: 'api_token_2',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
{
token: 'api_token_3',
// type code here for "relation_one" field
// type code here for "relation_one" field
},
];
const PaymentsData = [
{
// type code here for "relation_one" field
amount: 9.99,
payment_date: new Date('2023-10-01T10:00:00Z'),
status: 'Completed',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
amount: 19.99,
payment_date: new Date('2023-10-02T11:00:00Z'),
status: 'Failed',
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
amount: 29.99,
payment_date: new Date('2023-10-03T12:00:00Z'),
status: 'Failed',
// type code here for "relation_one" field
},
];
const SubscriptionPlansData = [
{
name: 'Basic Plan',
price: 9.99,
translation_limit: 1000,
// type code here for "relation_one" field
},
{
name: 'Standard Plan',
price: 19.99,
translation_limit: 5000,
// type code here for "relation_one" field
},
{
name: 'Premium Plan',
price: 29.99,
translation_limit: 10000,
// type code here for "relation_one" field
},
];
const TranslationsData = [
{
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "files" field
status: 'Pending',
requested_at: new Date('2023-10-01T10:00:00Z'),
completed_at: new Date('2023-10-01T12:00:00Z'),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "files" field
status: 'InProgress',
requested_at: new Date('2023-10-02T11:00:00Z'),
completed_at: new Date(),
// type code here for "relation_one" field
},
{
// type code here for "relation_one" field
// type code here for "files" field
// type code here for "files" field
status: 'Pending',
requested_at: new Date('2023-10-03T12:00:00Z'),
completed_at: new Date(),
// type code here for "relation_one" field
},
];
const OrganizationsData = [
{
name: 'Carl Linnaeus',
},
{
name: 'Claude Bernard',
},
{
name: 'Euclid',
},
];
// 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 associateAnalyticWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Analytic0 = await Analytics.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Analytic0?.setOrganization) {
await Analytic0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Analytic1 = await Analytics.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Analytic1?.setOrganization) {
await Analytic1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Analytic2 = await Analytics.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Analytic2?.setOrganization) {
await Analytic2.setOrganization(relatedOrganization2);
}
}
async function associateApiTokenWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ApiToken0 = await ApiTokens.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ApiToken0?.setUser) {
await ApiToken0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ApiToken1 = await ApiTokens.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ApiToken1?.setUser) {
await ApiToken1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const ApiToken2 = await ApiTokens.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ApiToken2?.setUser) {
await ApiToken2.setUser(relatedUser2);
}
}
async function associateApiTokenWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const ApiToken0 = await ApiTokens.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (ApiToken0?.setOrganization) {
await ApiToken0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const ApiToken1 = await ApiTokens.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (ApiToken1?.setOrganization) {
await ApiToken1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const ApiToken2 = await ApiTokens.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (ApiToken2?.setOrganization) {
await ApiToken2.setOrganization(relatedOrganization2);
}
}
async function associatePaymentWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Payment0 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Payment0?.setUser) {
await Payment0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Payment1 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Payment1?.setUser) {
await Payment1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Payment2 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Payment2?.setUser) {
await Payment2.setUser(relatedUser2);
}
}
async function associatePaymentWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Payment0 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Payment0?.setOrganization) {
await Payment0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Payment1 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Payment1?.setOrganization) {
await Payment1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Payment2 = await Payments.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Payment2?.setOrganization) {
await Payment2.setOrganization(relatedOrganization2);
}
}
async function associateSubscriptionPlanWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const SubscriptionPlan0 = await SubscriptionPlans.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (SubscriptionPlan0?.setOrganization) {
await SubscriptionPlan0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const SubscriptionPlan1 = await SubscriptionPlans.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (SubscriptionPlan1?.setOrganization) {
await SubscriptionPlan1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const SubscriptionPlan2 = await SubscriptionPlans.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (SubscriptionPlan2?.setOrganization) {
await SubscriptionPlan2.setOrganization(relatedOrganization2);
}
}
async function associateTranslationWithUser() {
const relatedUser0 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Translation0 = await Translations.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Translation0?.setUser) {
await Translation0.setUser(relatedUser0);
}
const relatedUser1 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Translation1 = await Translations.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Translation1?.setUser) {
await Translation1.setUser(relatedUser1);
}
const relatedUser2 = await Users.findOne({
offset: Math.floor(Math.random() * (await Users.count())),
});
const Translation2 = await Translations.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Translation2?.setUser) {
await Translation2.setUser(relatedUser2);
}
}
async function associateTranslationWithOrganization() {
const relatedOrganization0 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Translation0 = await Translations.findOne({
order: [['id', 'ASC']],
offset: 0,
});
if (Translation0?.setOrganization) {
await Translation0.setOrganization(relatedOrganization0);
}
const relatedOrganization1 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Translation1 = await Translations.findOne({
order: [['id', 'ASC']],
offset: 1,
});
if (Translation1?.setOrganization) {
await Translation1.setOrganization(relatedOrganization1);
}
const relatedOrganization2 = await Organizations.findOne({
offset: Math.floor(Math.random() * (await Organizations.count())),
});
const Translation2 = await Translations.findOne({
order: [['id', 'ASC']],
offset: 2,
});
if (Translation2?.setOrganization) {
await Translation2.setOrganization(relatedOrganization2);
}
}
module.exports = {
up: async (queryInterface, Sequelize) => {
await Analytics.bulkCreate(AnalyticsData);
await ApiTokens.bulkCreate(ApiTokensData);
await Payments.bulkCreate(PaymentsData);
await SubscriptionPlans.bulkCreate(SubscriptionPlansData);
await Translations.bulkCreate(TranslationsData);
await Organizations.bulkCreate(OrganizationsData);
await Promise.all([
// Similar logic for "relation_many"
await associateUserWithOrganization(),
await associateAnalyticWithOrganization(),
await associateApiTokenWithUser(),
await associateApiTokenWithOrganization(),
await associatePaymentWithUser(),
await associatePaymentWithOrganization(),
await associateSubscriptionPlanWithOrganization(),
await associateTranslationWithUser(),
await associateTranslationWithOrganization(),
]);
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('analytics', null, {});
await queryInterface.bulkDelete('api_tokens', null, {});
await queryInterface.bulkDelete('payments', null, {});
await queryInterface.bulkDelete('subscription_plans', null, {});
await queryInterface.bulkDelete('translations', null, {});
await queryInterface.bulkDelete('organizations', null, {});
},
};