From 571b077840d6b5f9e5eb4c19b09fbad607b7aa3b Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 11 Apr 2022 10:09:25 +0800 Subject: [PATCH] fix: call root server hook after insertNewSchema (#282) --- .../src/__tests__/server-hook-impl.test.ts | 32 +++++++++++++++++++ .../src/repository.ts | 9 +++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts index 48a723a2ac..afcf64a68b 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts @@ -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('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: { diff --git a/packages/plugin-ui-schema-storage/src/repository.ts b/packages/plugin-ui-schema-storage/src/repository.ts index 52c3a29593..e35521a030 100644 --- a/packages/plugin-ui-schema-storage/src/repository.ts +++ b/packages/plugin-ui-schema-storage/src/repository.ts @@ -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; } @@ -880,7 +887,7 @@ export class UiSchemaRepository extends Repository { // Compatible with mysql if (this.database.sequelize.getDialect() === 'mysql') { - updateSql = `UPDATE ${treeTable} as TreeTable + updateSql = `UPDATE ${treeTable} as TreeTable JOIN ${treeTable} as NodeInfo ON (NodeInfo.descendant = TreeTable.descendant and NodeInfo.depth = 0) SET TreeTable.sort = TreeTable.sort + 1 WHERE TreeTable.depth = 1 AND TreeTable.ancestor = :ancestor and NodeInfo.type = :type`;