Added roles to users in data generator

no issue
This commit is contained in:
Sam Lord 2023-02-17 19:00:28 +00:00
parent 36ea0cc6d6
commit 37f16b1b17
3 changed files with 43 additions and 2 deletions

View File

@ -33,7 +33,8 @@ const {
MembersClickEventsImporter,
OffersImporter,
LabelsImporter,
MembersLabelsImporter
MembersLabelsImporter,
RolesUsersImporter
} = tables;
const path = require('path');
const fs = require('fs/promises');
@ -99,6 +100,10 @@ class DataGenerator {
const tableNames = Object.values(tables).map(importer => importer.table).reverse();
for (const table of tableNames) {
this.logger.debug(`Clearing table ${table}`);
if (table === 'roles_users') {
await transaction(table).del().whereNot('user_id', '1');
continue;
}
if (table === 'users') {
// Avoid deleting the admin user
await transaction(table).del().whereNot('id', '1');
@ -392,6 +397,10 @@ class DataGenerator {
amount: 1
});
const roles = await transaction.select('id', 'name').from('roles');
const rolesUsersImporter = new RolesUsersImporter(transaction, {roles});
await rolesUsersImporter.importForEach(users, {amount: 1});
// TODO: Members labels
// TODO: Email clicks - redirect, members_click_events (relies on emails)

View File

@ -32,5 +32,6 @@ module.exports = {
MembersClickEventsImporter: require('./members-click-events'),
OffersImporter: require('./offers'),
LabelsImporter: require('./labels'),
MembersLabelsImporter: require('./members-labels')
MembersLabelsImporter: require('./members-labels'),
RolesUsersImporter: require('./roles-users')
};

View File

@ -0,0 +1,31 @@
const {faker} = require('@faker-js/faker');
const TableImporter = require('./base');
class RolesUsersImporter extends TableImporter {
static table = 'roles_users';
constructor(knex, {roles}) {
super(RolesUsersImporter.table, knex);
this.roles = roles;
this.sortOrder = 0;
}
setImportOptions({model}) {
this.model = model;
}
generate() {
const userRoles = ['Editor', 'Contributor', 'Author'];
const userRole = userRoles[faker.datatype.number({
min: 0,
max: userRoles.length - 1
})];
return {
id: faker.database.mongodbObjectId(),
role_id: this.roles.find(role => role.name === userRole).id,
user_id: this.model.id
};
}
}
module.exports = RolesUsersImporter;