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 dateToDatabaseString = require('../utils/database-date');
|
|
|
|
|
|
|
|
class EmailBatchesImporter extends TableImporter {
|
|
|
|
static table = 'email_batches';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['emails'];
|
2023-02-17 17:03:52 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(EmailBatchesImporter.table, knex, transaction);
|
2023-02-17 17:03:52 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
async import(quantity) {
|
2023-12-12 19:37:36 +03:00
|
|
|
const emails = await this.transaction.select('id', 'created_at', 'email_count').from('emails');
|
2023-08-02 16:43:26 +03:00
|
|
|
|
2023-12-12 19:37:36 +03:00
|
|
|
// 1 batch per 1000 recipients
|
|
|
|
await this.importForEach(emails, quantity ?? (() => {
|
|
|
|
return Math.ceil(this.model.email_count / 1000);
|
|
|
|
}));
|
2023-02-17 17:03:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
const emailSentDate = new Date(this.model.created_at);
|
|
|
|
const latestUpdatedDate = new Date(this.model.created_at);
|
|
|
|
latestUpdatedDate.setHours(latestUpdatedDate.getHours() + 1);
|
|
|
|
|
|
|
|
return {
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2023-02-17 17:03:52 +03:00
|
|
|
email_id: this.model.id,
|
|
|
|
provider_id: `${new Date().toISOString().split('.')[0].replace(/[^0-9]/g, '')}.${faker.datatype.hexadecimal({length: 16, prefix: '', case: 'lower'})}@m.example.com`,
|
|
|
|
status: 'submitted', // TODO: introduce failures
|
|
|
|
created_at: this.model.created_at,
|
|
|
|
updated_at: dateToDatabaseString(faker.date.between(emailSentDate, latestUpdatedDate))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EmailBatchesImporter;
|