mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
fix(tree): e2e error (#5542)
This commit is contained in:
parent
738ac2230f
commit
eb5e23f686
@ -31,6 +31,7 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
if (!condition(collection.options)) {
|
if (!condition(collection.options)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const tk = collection.filterTargetKey;
|
||||||
const name = `${dataSource.name}_${collection.name}_path`;
|
const name = `${dataSource.name}_${collection.name}_path`;
|
||||||
const parentForeignKey = collection.treeParentField?.foreignKey || 'parentId';
|
const parentForeignKey = collection.treeParentField?.foreignKey || 'parentId';
|
||||||
|
|
||||||
@ -50,9 +51,8 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
//afterCreate
|
//afterCreate
|
||||||
this.db.on(`${collection.name}.afterCreate`, async (model: Model, options) => {
|
this.db.on(`${collection.name}.afterCreate`, async (model: Model, options) => {
|
||||||
const { transaction } = options;
|
const { transaction } = options;
|
||||||
const tk = collection.filterTargetKey;
|
|
||||||
let path = `/${model.get(tk)}`;
|
let path = `/${model.get(tk)}`;
|
||||||
path = await this.getTreePath(model, path, collection, name, transaction);
|
path = await this.getTreePath(model, path, collection, tk, name, transaction);
|
||||||
const rootPk = path.split('/')[1];
|
const rootPk = path.split('/')[1];
|
||||||
await this.app.db.getRepository(name).create({
|
await this.app.db.getRepository(name).create({
|
||||||
values: {
|
values: {
|
||||||
@ -66,18 +66,16 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
|
|
||||||
//afterUpdate
|
//afterUpdate
|
||||||
this.db.on(`${collection.name}.afterUpdate`, async (model: Model, options) => {
|
this.db.on(`${collection.name}.afterUpdate`, async (model: Model, options) => {
|
||||||
const tk = collection.filterTargetKey;
|
|
||||||
// only update parentId and filterTargetKey
|
// only update parentId and filterTargetKey
|
||||||
if (!(model._changed.has(tk) || model._changed.has(parentForeignKey))) {
|
if (!(model._changed.has(tk) || model._changed.has(parentForeignKey))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { transaction } = options;
|
const { transaction } = options;
|
||||||
await this.updateTreePath(model, collection, name, transaction);
|
await this.updateTreePath(model, collection, tk, name, transaction);
|
||||||
});
|
});
|
||||||
|
|
||||||
// after remove
|
// after remove
|
||||||
this.db.on(`${collection.name}.afterBulkUpdate`, async (options) => {
|
this.db.on(`${collection.name}.afterBulkUpdate`, async (options) => {
|
||||||
const tk = collection.filterTargetKey;
|
|
||||||
if (!(options.where && options.where[tk])) {
|
if (!(options.where && options.where[tk])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -88,13 +86,12 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
transaction: options.transaction,
|
transaction: options.transaction,
|
||||||
});
|
});
|
||||||
for (const model of instances) {
|
for (const model of instances) {
|
||||||
await this.updateTreePath(model, collection, name, options.transaction);
|
await this.updateTreePath(model, collection, tk, name, options.transaction);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//afterDestroy
|
//afterDestroy
|
||||||
this.db.on(`${collection.name}.afterDestroy`, async (model: Model, options: DestroyOptions) => {
|
this.db.on(`${collection.name}.afterDestroy`, async (model: Model, options: DestroyOptions) => {
|
||||||
const tk = collection.filterTargetKey;
|
|
||||||
await this.app.db.getRepository(name).destroy({
|
await this.app.db.getRepository(name).destroy({
|
||||||
filter: {
|
filter: {
|
||||||
nodePk: model.get(tk),
|
nodePk: model.get(tk),
|
||||||
@ -142,10 +139,10 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
model: Model,
|
model: Model,
|
||||||
path: string,
|
path: string,
|
||||||
collection: Collection,
|
collection: Collection,
|
||||||
|
tk: string,
|
||||||
pathCollectionName: string,
|
pathCollectionName: string,
|
||||||
transaction?: Transaction,
|
transaction?: Transaction,
|
||||||
) {
|
) {
|
||||||
const tk = collection.filterTargetKey;
|
|
||||||
const parentForeignKey = collection.treeParentField?.foreignKey || 'parentId';
|
const parentForeignKey = collection.treeParentField?.foreignKey || 'parentId';
|
||||||
if (model.get(parentForeignKey) && model.get(parentForeignKey) !== null) {
|
if (model.get(parentForeignKey) && model.get(parentForeignKey) !== null) {
|
||||||
const parent = await this.app.db.getRepository(collection.name).findOne({
|
const parent = await this.app.db.getRepository(collection.name).findOne({
|
||||||
@ -167,7 +164,7 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
});
|
});
|
||||||
const parentPath = lodash.get(parentPathData, 'path', null);
|
const parentPath = lodash.get(parentPathData, 'path', null);
|
||||||
if (parentPath == null) {
|
if (parentPath == null) {
|
||||||
path = await this.getTreePath(parent, path, collection, pathCollectionName, transaction);
|
path = await this.getTreePath(parent, path, collection, tk, pathCollectionName, transaction);
|
||||||
} else {
|
} else {
|
||||||
path = `${parentPath}/${model.get(tk)}`;
|
path = `${parentPath}/${model.get(tk)}`;
|
||||||
}
|
}
|
||||||
@ -180,12 +177,12 @@ class PluginCollectionTreeServer extends Plugin {
|
|||||||
private async updateTreePath(
|
private async updateTreePath(
|
||||||
model: Model,
|
model: Model,
|
||||||
collection: Collection,
|
collection: Collection,
|
||||||
|
tk: string,
|
||||||
pathCollectionName: string,
|
pathCollectionName: string,
|
||||||
transaction: Transaction,
|
transaction: Transaction,
|
||||||
) {
|
) {
|
||||||
const tk = collection.filterTargetKey;
|
|
||||||
let path = `/${model.get(tk)}`;
|
let path = `/${model.get(tk)}`;
|
||||||
path = await this.getTreePath(model, path, collection, pathCollectionName, transaction);
|
path = await this.getTreePath(model, path, collection, tk, pathCollectionName, transaction);
|
||||||
const collectionTreePath = this.db.getCollection(pathCollectionName);
|
const collectionTreePath = this.db.getCollection(pathCollectionName);
|
||||||
const nodePkColumnName = collectionTreePath.getField('nodePk').columnName();
|
const nodePkColumnName = collectionTreePath.getField('nodePk').columnName();
|
||||||
const pathData = await this.app.db.getRepository(pathCollectionName).findOne({
|
const pathData = await this.app.db.getRepository(pathCollectionName).findOne({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user