2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2023-03-14 13:32:27 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
const {luck} = require('../utils/random');
|
|
|
|
const dateToDatabaseString = require('../utils/database-date');
|
|
|
|
|
|
|
|
class MembersFeedbackImporter extends TableImporter {
|
|
|
|
static table = 'members_feedback';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['emails', 'email_recipients'];
|
2023-03-14 13:32:27 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction, {emails}) {
|
|
|
|
super(MembersFeedbackImporter.table, knex, transaction);
|
2023-03-14 13:32:27 +03:00
|
|
|
this.emails = emails;
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
async import(quantity) {
|
|
|
|
const emailRecipients = await this.transaction.select('id', 'opened_at', 'email_id', 'member_id').from('email_recipients');
|
|
|
|
this.emails = await this.transaction.select('id', 'post_id').from('emails');
|
|
|
|
|
|
|
|
await this.importForEach(emailRecipients, quantity ? quantity / emailRecipients.length : 1);
|
2023-03-14 13:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
// ~10% of people who opened the email will leave feedback
|
|
|
|
if (!this.model.opened_at || luck(90)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const openedAt = new Date(this.model.opened_at);
|
|
|
|
const laterOn = new Date(this.model.opened_at);
|
|
|
|
laterOn.setMinutes(laterOn.getMinutes() + 60);
|
2024-01-05 16:42:30 +03:00
|
|
|
const feedbackTime = faker.date.between(openedAt, laterOn);
|
2023-03-14 13:32:27 +03:00
|
|
|
|
|
|
|
const postId = this.emails.find(email => email.id === this.model.email_id).post_id;
|
|
|
|
return {
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2023-03-14 13:32:27 +03:00
|
|
|
score: luck(70) ? 1 : 0,
|
|
|
|
member_id: this.model.member_id,
|
|
|
|
post_id: postId,
|
|
|
|
created_at: dateToDatabaseString(feedbackTime),
|
|
|
|
updated_at: dateToDatabaseString(feedbackTime)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersFeedbackImporter;
|