2022-10-26 19:55:08 +03:00
|
|
|
const TableImporter = require('./base');
|
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
const {luck} = require('../utils/random');
|
2022-11-03 14:08:03 +03:00
|
|
|
const dateToDatabaseString = require('../utils/database-date');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
class MembersSubscribeEventsImporter extends TableImporter {
|
|
|
|
constructor(knex, {newsletters, subscriptions}) {
|
|
|
|
super('members_subscribe_events', knex);
|
|
|
|
this.newsletters = newsletters;
|
|
|
|
this.subscriptions = subscriptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
setImportOptions({model}) {
|
|
|
|
this.model = model;
|
|
|
|
this.count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
const count = this.count;
|
|
|
|
this.count = this.count + 1;
|
|
|
|
|
|
|
|
if (count === 1 && this.model.status === 'free') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:08:03 +03:00
|
|
|
let createdAt = dateToDatabaseString(faker.date.between(new Date(this.model.created_at), new Date()));
|
2022-10-26 19:55:08 +03:00
|
|
|
let subscribed = luck(80);
|
|
|
|
|
|
|
|
// Free newsletter by default
|
2023-02-09 01:12:19 +03:00
|
|
|
let newsletterId = this.newsletters[1].id;
|
2022-10-26 19:55:08 +03:00
|
|
|
if (this.model.status === 'paid' && count === 0) {
|
|
|
|
// Paid newsletter
|
2023-02-09 01:12:19 +03:00
|
|
|
newsletterId = this.newsletters[0].id;
|
2022-10-26 19:55:08 +03:00
|
|
|
createdAt = this.subscriptions.find(s => s.member_id === this.model.id).created_at;
|
|
|
|
subscribed = luck(98);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!subscribed) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: faker.database.mongodbObjectId(),
|
|
|
|
member_id: this.model.id,
|
|
|
|
newsletter_id: newsletterId,
|
|
|
|
subscribed: true,
|
|
|
|
created_at: createdAt,
|
|
|
|
source: 'member'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersSubscribeEventsImporter;
|