2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2022-10-26 19:55:08 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
|
|
|
|
class MembersPaidSubscriptionEventsImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'members_paid_subscription_events';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['subscriptions', 'members_stripe_customers_subscriptions'];
|
2023-02-16 15:11:00 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(MembersPaidSubscriptionEventsImporter.table, knex, transaction);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
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);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
if (!this.model.currency) {
|
|
|
|
// Not a paid subscription
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// TODO: Implement upgrades
|
|
|
|
const membersStripeCustomersSubscription = this.membersStripeCustomersSubscriptions.find((m) => {
|
2023-02-27 15:40:53 +03:00
|
|
|
return m.ghost_subscription_id === this.model.id;
|
2022-10-26 19:55:08 +03:00
|
|
|
});
|
|
|
|
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;
|