Ghost/ghost/data-generator/lib/importers/MembersPaidSubscriptionEventsImporter.js
Sam Lord 4ff467794f Entirely rewrote data generator to simplify codebase
refs: https://github.com/TryGhost/DevOps/issues/11

This is a pretty huge commit, but the relevant points are:
* Each importer no longer needs to be passed a set of data, it just gets the data it needs
* Each importer specifies its dependencies, so that the order of import can be determined at runtime using a topological sort
* The main data generator function can just tell each importer to import the data it has

This makes working on the data generator much easier.

Some other benefits are:
* Batched importing, massively speeding up the whole process
* `--tables` to set the exact tables you want to import, and specify the quantity of each
2023-08-04 13:36:09 +01:00

45 lines
1.8 KiB
JavaScript

const TableImporter = require('./TableImporter');
const {faker} = require('@faker-js/faker');
class MembersPaidSubscriptionEventsImporter extends TableImporter {
static table = 'members_paid_subscription_events';
static dependencies = ['subscriptions', 'members_stripe_customers_subscriptions'];
constructor(knex, transaction) {
super(MembersPaidSubscriptionEventsImporter.table, knex, transaction);
}
async import(quantity) {
const subscriptions = await this.transaction.select('id', 'member_id', 'currency', 'created_at').from('subscriptions');
this.membersStripeCustomersSubscriptions = await this.transaction.select('id', 'ghost_subscription_id', 'plan_id', 'mrr').from('members_stripe_customers_subscriptions');
await this.importForEach(subscriptions, quantity ? quantity / subscriptions.length : 1);
}
generate() {
if (!this.model.currency) {
// Not a paid subscription
return null;
}
// TODO: Implement upgrades
const membersStripeCustomersSubscription = this.membersStripeCustomersSubscriptions.find((m) => {
return m.ghost_subscription_id === this.model.id;
});
return {
id: faker.database.mongodbObjectId(),
// TODO: Support expired / updated / cancelled events too
type: 'created',
member_id: this.model.member_id,
subscription_id: this.model.id,
from_plan: null,
to_plan: membersStripeCustomersSubscription.plan_id,
currency: this.model.currency,
source: 'stripe',
mrr_delta: membersStripeCustomersSubscription.mrr,
created_at: this.model.created_at
};
}
}
module.exports = MembersPaidSubscriptionEventsImporter;