Added roles to users in data generator
no issue
This commit is contained in:
parent
36ea0cc6d6
commit
37f16b1b17
@ -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)
|
||||
|
@ -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')
|
||||
};
|
||||
|
31
ghost/data-generator/lib/tables/roles-users.js
Normal file
31
ghost/data-generator/lib/tables/roles-users.js
Normal 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;
|
Loading…
Reference in New Issue
Block a user