2023-02-17 17:03:52 +03:00
|
|
|
const TableImporter = require('./base');
|
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
const {luck} = require('../utils/random');
|
|
|
|
const dateToDatabaseString = require('../utils/database-date');
|
|
|
|
|
|
|
|
class MembersClickEventsImporter extends TableImporter {
|
|
|
|
static table = 'members_click_events';
|
|
|
|
|
|
|
|
constructor(knex, {redirects, emails}) {
|
|
|
|
super(MembersClickEventsImporter.table, knex);
|
|
|
|
|
|
|
|
this.redirects = redirects;
|
|
|
|
this.emails = emails;
|
|
|
|
}
|
|
|
|
|
|
|
|
setImportOptions({model, amount}) {
|
|
|
|
this.model = model;
|
|
|
|
this.amount = model.opened_at === null ? 0 : luck(40) ? faker.datatype.number({
|
|
|
|
min: 0,
|
|
|
|
max: amount
|
|
|
|
}) : 0;
|
|
|
|
const email = this.emails.find(e => e.id === this.model.email_id);
|
|
|
|
this.redirectList = this.redirects.filter(redirect => redirect.post_id === email.post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
if (this.amount <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.amount -= 1;
|
|
|
|
|
|
|
|
const openedAt = new Date(this.model.opened_at);
|
|
|
|
const laterOn = new Date(this.model.opened_at);
|
|
|
|
laterOn.setMinutes(laterOn.getMinutes() + 15);
|
2023-02-20 16:36:40 +03:00
|
|
|
const clickTime = new Date(openedAt.valueOf() + (Math.random() * (laterOn.valueOf() - openedAt.valueOf())));
|
2023-02-17 17:03:52 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: faker.database.mongodbObjectId(),
|
|
|
|
member_id: this.model.member_id,
|
|
|
|
redirect_id: this.redirectList[faker.datatype.number({
|
|
|
|
min: 0,
|
|
|
|
max: this.redirectList.length - 1
|
|
|
|
})].id,
|
2023-02-20 16:36:40 +03:00
|
|
|
created_at: dateToDatabaseString(clickTime)
|
2023-02-17 17:03:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersClickEventsImporter;
|