2023-02-17 22:00:28 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2023-02-17 22:00:28 +03:00
|
|
|
|
|
|
|
class RolesUsersImporter extends TableImporter {
|
|
|
|
static table = 'roles_users';
|
2023-08-02 16:43:26 +03:00
|
|
|
// No roles imorter, since roles are statically defined in database
|
|
|
|
static dependencies = ['users'];
|
2023-02-17 22:00:28 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(RolesUsersImporter.table, knex, transaction);
|
2023-02-17 22:00:28 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
/**
|
|
|
|
* Ignore overriden quantity for 1:1 relationship
|
|
|
|
*/
|
|
|
|
async import() {
|
|
|
|
const users = await this.transaction.select('id').from('users').whereNot('id', 1);
|
|
|
|
this.roles = await this.transaction.select('id', 'name').from('roles');
|
|
|
|
|
|
|
|
await this.importForEach(users, 1);
|
2023-02-17 22:00:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
const userRoles = ['Editor', 'Contributor', 'Author'];
|
|
|
|
const userRole = userRoles[faker.datatype.number({
|
|
|
|
min: 0,
|
|
|
|
max: userRoles.length - 1
|
|
|
|
})];
|
2023-02-20 14:06:14 +03:00
|
|
|
const actualRole = this.roles.find(role => role.name === userRole);
|
|
|
|
if (!actualRole) {
|
|
|
|
// No roles defined in database, don't bother creating user role
|
|
|
|
return;
|
|
|
|
}
|
2023-02-17 22:00:28 +03:00
|
|
|
return {
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2023-02-20 14:06:14 +03:00
|
|
|
role_id: actualRole.id,
|
2023-02-17 22:00:28 +03:00
|
|
|
user_id: this.model.id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RolesUsersImporter;
|