fix(plugin-users): test errors
This commit is contained in:
parent
0fd41c9036
commit
6843bad133
@ -1,14 +1,16 @@
|
|||||||
import Database from '@nocobase/database';
|
import Database from '@nocobase/database';
|
||||||
|
import PluginACL from '@nocobase/plugin-acl';
|
||||||
|
import UsersPlugin from '@nocobase/plugin-users';
|
||||||
import { mockServer, MockServer } from '@nocobase/test';
|
import { mockServer, MockServer } from '@nocobase/test';
|
||||||
import { userPluginConfig } from './utils';
|
import { userPluginConfig } from './utils';
|
||||||
|
|
||||||
describe('createdBy/updatedBy', () => {
|
describe('createdBy/updatedBy', () => {
|
||||||
let api: MockServer;
|
let api: MockServer;
|
||||||
let db: Database;
|
let db: Database;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
api = mockServer();
|
api = mockServer();
|
||||||
api.plugin(require('../server').default, userPluginConfig);
|
api.plugin(UsersPlugin, userPluginConfig);
|
||||||
|
api.plugin(PluginACL);
|
||||||
await api.loadAndInstall();
|
await api.loadAndInstall();
|
||||||
db = api.db;
|
db = api.db;
|
||||||
});
|
});
|
||||||
|
@ -27,7 +27,7 @@ export function setCurrentRole(ctx) {
|
|||||||
} else if (userRoles.length > 1) {
|
} else if (userRoles.length > 1) {
|
||||||
const role = userRoles.find((role) => role.name === currentRole);
|
const role = userRoles.find((role) => role.name === currentRole);
|
||||||
if (!role) {
|
if (!role) {
|
||||||
const defaultRole = userRoles.find((role) => role.rolesUsers?.default);
|
const defaultRole = userRoles.find((role) => role?.rolesUsers?.default);
|
||||||
currentRole = (defaultRole || userRoles[0])?.name;
|
currentRole = (defaultRole || userRoles[0])?.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,14 @@ export class UserModel extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const db = (this.constructor as any).database as Database;
|
const db = (this.constructor as any).database as Database;
|
||||||
|
const repository = db.getRepository('rolesUsers');
|
||||||
|
if (!repository) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const transaction = options.transaction || (await db.sequelize.transaction());
|
const transaction = options.transaction || (await db.sequelize.transaction());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.getRepository('rolesUsers').update({
|
await repository.update({
|
||||||
filter: {
|
filter: {
|
||||||
userId: this.get('id'),
|
userId: this.get('id'),
|
||||||
},
|
},
|
||||||
@ -19,8 +23,7 @@ export class UserModel extends Model {
|
|||||||
},
|
},
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
await repository.update({
|
||||||
await db.getRepository('rolesUsers').update({
|
|
||||||
filter: {
|
filter: {
|
||||||
userId: this.get('id'),
|
userId: this.get('id'),
|
||||||
roleName,
|
roleName,
|
||||||
|
@ -28,19 +28,19 @@ export default class UsersPlugin extends Plugin<UserPluginConfig> {
|
|||||||
this.db.registerModels({ UserModel });
|
this.db.registerModels({ UserModel });
|
||||||
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
||||||
const { transaction } = options;
|
const { transaction } = options;
|
||||||
|
const repository = this.app.db.getRepository('roles');
|
||||||
if (this.app.db.getCollection('roles')) {
|
if (!repository) {
|
||||||
const defaultRole = await this.app.db.getRepository('roles').findOne({
|
return;
|
||||||
|
}
|
||||||
|
const defaultRole = await repository.findOne({
|
||||||
filter: {
|
filter: {
|
||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
||||||
await model.addRoles(defaultRole, { transaction });
|
await model.addRoles(defaultRole, { transaction });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.db.on('afterDefineCollection', (collection: Collection) => {
|
this.db.on('afterDefineCollection', (collection: Collection) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user