mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 12:26:22 +00:00
fix: call root server hook after insertNewSchema (#282)
This commit is contained in:
parent
8b5ddf4501
commit
571b077840
@ -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: {
|
||||
|
@ -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`;
|
||||
|
Loading…
Reference in New Issue
Block a user