2022-10-26 19:55:08 +03:00
|
|
|
const TableImporter = require('./base');
|
|
|
|
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-03-16 00:50:15 +03:00
|
|
|
constructor(knex, {subscriptions, posts}) {
|
2023-02-16 15:11:00 +03:00
|
|
|
super(MembersSubscriptionCreatedEventsImporter.table, knex);
|
2022-10-26 19:55:08 +03:00
|
|
|
this.subscriptions = subscriptions;
|
2023-03-16 00:50:15 +03:00
|
|
|
|
|
|
|
this.posts = [...posts];
|
|
|
|
// Sort posts in reverse chronologoical order
|
|
|
|
this.posts.sort((a, b) => new Date(b.published_at).valueOf() - new Date(a.published_at).valueOf());
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setImportOptions({model}) {
|
|
|
|
this.model = model;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|