2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2023-02-17 17:03:52 +03:00
|
|
|
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';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['email_recipients', 'redirects', 'emails'];
|
2023-02-17 17:03:52 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(MembersClickEventsImporter.table, knex, transaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
async import(quantity) {
|
2024-01-05 16:42:30 +03:00
|
|
|
const emailRecipients = await this.transaction.select('id', 'opened_at', 'email_id', 'member_id').from('email_recipients').whereNotNull('opened_at');
|
|
|
|
const redirects = await this.transaction.select('id', 'post_id').from('redirects');
|
|
|
|
const emails = await this.transaction.select('id', 'post_id').from('emails');
|
2023-08-02 16:43:26 +03:00
|
|
|
this.quantity = quantity ? quantity / emailRecipients.length : 2;
|
2023-02-17 17:03:52 +03:00
|
|
|
|
2024-01-05 16:42:30 +03:00
|
|
|
// Create maps for faster lookups (this does make a difference for large data generation)
|
|
|
|
this.emails = new Map();
|
|
|
|
for (const email of emails) {
|
|
|
|
this.emails.set(email.id, email);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.redirects = new Map();
|
|
|
|
for (const redirect of redirects) {
|
|
|
|
if (!this.redirects.has(redirect.post_id)) {
|
|
|
|
this.redirects.set(redirect.post_id, []);
|
|
|
|
}
|
|
|
|
this.redirects.get(redirect.post_id).push(redirect);
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
await this.importForEach(emailRecipients, this.quantity);
|
2023-02-17 17:03:52 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
setReferencedModel(model) {
|
2023-02-17 17:03:52 +03:00
|
|
|
this.model = model;
|
|
|
|
this.amount = model.opened_at === null ? 0 : luck(40) ? faker.datatype.number({
|
|
|
|
min: 0,
|
2023-08-02 16:43:26 +03:00
|
|
|
max: this.quantity
|
2023-02-17 17:03:52 +03:00
|
|
|
}) : 0;
|
2024-01-05 16:42:30 +03:00
|
|
|
const email = this.emails.get(model.email_id);
|
|
|
|
this.redirectList = this.redirects.get(email.post_id) ?? [];
|
2023-02-17 17:03:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
2024-01-05 16:42:30 +03:00
|
|
|
if (this.amount <= 0 || this.redirectList.length === 0 || !this.model.opened_at) {
|
2023-02-17 17:03:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.amount -= 1;
|
|
|
|
|
|
|
|
const openedAt = new Date(this.model.opened_at);
|
2024-01-05 16:42:30 +03:00
|
|
|
const laterOn = new Date(openedAt.getTime() + 1000 * 60 * 15);
|
|
|
|
const clickTime = faker.date.between(openedAt.getTime(), laterOn.getTime()); //added getTime here because it threw random errors
|
2023-02-17 17:03:52 +03:00
|
|
|
|
|
|
|
return {
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2023-02-17 17:03:52 +03:00
|
|
|
member_id: this.model.member_id,
|
2024-01-05 16:42:30 +03:00
|
|
|
redirect_id: this.redirectList[this.redirectList.length === 1 ? 0 : (faker.datatype.number({
|
2023-02-17 17:03:52 +03:00
|
|
|
min: 0,
|
|
|
|
max: this.redirectList.length - 1
|
2024-01-05 16:42:30 +03:00
|
|
|
}))].id,
|
2023-02-20 16:36:40 +03:00
|
|
|
created_at: dateToDatabaseString(clickTime)
|
2023-02-17 17:03:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersClickEventsImporter;
|