2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2022-10-26 19:55:08 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
2022-11-03 14:08:03 +03:00
|
|
|
const dateToDatabaseString = require('../utils/database-date');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
class MembersStatusEventsImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'members_status_events';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['members'];
|
2023-02-16 15:11:00 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(MembersStatusEventsImporter.table, knex, transaction);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
async import(quantity) {
|
2024-01-15 18:23:49 +03:00
|
|
|
let offset = 0;
|
|
|
|
let limit = 100000;
|
2023-08-02 16:43:26 +03:00
|
|
|
|
2024-01-15 18:23:49 +03:00
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
|
|
while (true) {
|
|
|
|
const members = await this.transaction.select('id', 'created_at', 'status').from('members').limit(limit).offset(offset);
|
|
|
|
|
|
|
|
if (members.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.importForEach(members, quantity ? quantity / members.length : 2);
|
|
|
|
offset += limit;
|
|
|
|
}
|
2023-08-02 16:43:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setReferencedModel(model) {
|
2022-10-26 19:55:08 +03:00
|
|
|
this.events = [{
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2022-10-26 19:55:08 +03:00
|
|
|
member_id: model.id,
|
|
|
|
from_status: null,
|
|
|
|
to_status: 'free',
|
2024-01-05 16:42:30 +03:00
|
|
|
created_at: dateToDatabaseString(model.created_at)
|
2022-10-26 19:55:08 +03:00
|
|
|
}];
|
|
|
|
if (model.status !== 'free') {
|
|
|
|
this.events.push({
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2022-10-26 19:55:08 +03:00
|
|
|
member_id: model.id,
|
|
|
|
from_status: 'free',
|
|
|
|
to_status: model.status,
|
2022-11-03 14:08:03 +03:00
|
|
|
created_at: dateToDatabaseString(faker.date.between(new Date(model.created_at), new Date()))
|
2022-10-26 19:55:08 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
2024-01-05 16:42:30 +03:00
|
|
|
const event = this.events.pop();
|
2022-10-26 19:55:08 +03:00
|
|
|
if (!event) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersStatusEventsImporter;
|