2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2023-02-17 20:01:20 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
const {luck} = require('../utils/random');
|
|
|
|
|
|
|
|
class MembersLabelsImporter extends TableImporter {
|
|
|
|
static table = 'members_labels';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['labels', 'members'];
|
2023-02-17 20:01:20 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction, {labels}) {
|
|
|
|
super(MembersLabelsImporter.table, knex, transaction);
|
2023-02-17 20:01:20 +03:00
|
|
|
this.labels = labels;
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
async import(quantity) {
|
|
|
|
const members = await this.transaction.select('id').from('members');
|
|
|
|
this.labels = await this.transaction.select('id').from('labels');
|
|
|
|
|
|
|
|
await this.importForEach(members, quantity ? quantity / members.length : 1);
|
2023-02-17 20:01:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
if (luck(90)) {
|
|
|
|
// 90% of members don't have labels
|
|
|
|
return;
|
|
|
|
}
|
2023-08-02 16:43:26 +03:00
|
|
|
// TODO: Ensure we don't generate the same member label twice
|
2023-02-17 20:01:20 +03:00
|
|
|
return {
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2023-02-17 20:01:20 +03:00
|
|
|
member_id: this.model.id,
|
|
|
|
label_id: this.labels[faker.datatype.number({
|
|
|
|
min: 0,
|
|
|
|
max: this.labels.length - 1
|
|
|
|
})].id,
|
|
|
|
sort_order: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersLabelsImporter;
|