fix: call root server hook after insertNewSchema (#282)

This commit is contained in:
ChengLei Shao 2022-04-11 10:09:25 +08:00 committed by GitHub
parent 8b5ddf4501
commit 571b077840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -264,6 +264,38 @@ describe('server hooks', () => {
expect(jsonTree['properties']['child2']).not.toBeDefined();
});
it('should bind menu to role when insert new menu using insertAdjacent', async () => {
await db.getRepository('roles').create({
values: {
name: 'role1',
allowConfigure: true,
allowNewMenu: true,
},
});
const schema = {
'x-uid': 'root',
name: 'root',
properties: {},
};
await uiSchemaRepository.insert(schema);
await uiSchemaRepository.insertAdjacent('afterBegin', 'root', {
'x-uid': 'child2',
name: 'child2',
'x-server-hooks': [
{
type: 'onSelfCreate',
method: 'bindMenuToRole',
},
],
});
const role1Menus = await db.getRepository<BelongsToManyRepository>('roles.menuUiSchemas', 'role1').find();
expect(role1Menus.length).toEqual(1);
});
it('should bind menu to role when create new menu', async () => {
await db.getRepository('roles').create({
values: {

View File

@ -594,6 +594,7 @@ export class UiSchemaRepository extends Repository {
options.removeParentsIfNoChildren = false;
} else {
const schemaExists = await this.schemaExists(schema, { transaction });
if (schemaExists) {
schema = lodash.isString(schema) ? schema : schema['x-uid'];
} else {
@ -711,6 +712,12 @@ export class UiSchemaRepository extends Repository {
},
);
const rootNode = nodes[0];
if (rootNode['x-server-hooks']) {
const rootModel = await this.findOne({ filter: { 'x-uid': rootNode['x-uid'] }, transaction });
await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, rootModel, options);
}
if (options?.returnNode) {
return nodes;
}