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');
|
2023-03-16 00:50:15 +03:00
|
|
|
const {luck} = require('../utils/random');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
class MembersSubscriptionCreatedEventsImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'members_subscription_created_events';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['members_stripe_customers_subscriptions', 'subscriptions', 'posts'];
|
2023-02-16 15:11:00 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(MembersSubscriptionCreatedEventsImporter.table, knex, transaction);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
async import(quantity) {
|
|
|
|
const membersStripeCustomersSubscriptions = await this.transaction.select('id', 'ghost_subscription_id').from('members_stripe_customers_subscriptions');
|
|
|
|
this.subscriptions = await this.transaction.select('id', 'created_at', 'member_id').from('subscriptions');
|
|
|
|
this.posts = await this.transaction.select('id', 'published_at', 'visibility', 'type', 'slug').from('posts').orderBy('published_at', 'desc');
|
|
|
|
|
|
|
|
await this.importForEach(membersStripeCustomersSubscriptions, quantity ? quantity / membersStripeCustomersSubscriptions.length : 1);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
2023-02-27 15:40:53 +03:00
|
|
|
const subscription = this.subscriptions.find(s => s.id === this.model.ghost_subscription_id);
|
2023-03-16 00:50:15 +03:00
|
|
|
let attribution = {};
|
|
|
|
if (luck(10)) {
|
|
|
|
const post = this.posts.find(p => p.visibility === 'public' && new Date(p.published_at) < new Date(subscription.created_at));
|
|
|
|
if (post) {
|
|
|
|
attribution = {
|
|
|
|
attribution_id: post.id,
|
|
|
|
attribution_type: post.type,
|
|
|
|
attribution_url: post.slug
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Object.assign({}, {
|
2022-10-26 19:55:08 +03:00
|
|
|
id: faker.database.mongodbObjectId(),
|
|
|
|
created_at: subscription.created_at,
|
|
|
|
member_id: subscription.member_id,
|
|
|
|
subscription_id: this.model.id,
|
|
|
|
// TODO: Implement referrers
|
|
|
|
referrer_source: null,
|
|
|
|
referrer_medium: null,
|
|
|
|
referrer_url: null
|
2023-03-16 00:50:15 +03:00
|
|
|
}, attribution);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersSubscriptionCreatedEventsImporter;
|