From 6beae90806022880dc7719c8a7c2c2c070c6ee75 Mon Sep 17 00:00:00 2001 From: Junyi Date: Tue, 26 Sep 2023 23:39:50 +0800 Subject: [PATCH] fix(plugin-workflow): fix assignees and aggregate variable (#2725) --- .../src/client/nodes/aggregate.tsx | 23 ++++--- .../src/client/nodes/create.tsx | 4 +- .../src/client/nodes/query.tsx | 4 +- .../src/client/triggers/collection.tsx | 2 +- .../src/client/triggers/form.tsx | 2 +- .../src/client/triggers/schedule/index.tsx | 2 +- .../plugin-workflow/src/client/variable.tsx | 63 ++++++++++++------- 7 files changed, 64 insertions(+), 36 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/aggregate.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/aggregate.tsx index 5e19e119c..e5af50f08 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/aggregate.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/aggregate.tsx @@ -17,19 +17,20 @@ import { NAMESPACE, lang } from '../locale'; import { collection, filter } from '../schemas/collection'; import { BaseTypeSets, defaultFieldNames, nodesOptions, triggerOptions } from '../variable'; -function matchToManyField(field, appends): boolean { - const fieldPrefix = `${field.name}.`; - return ( - (['hasOne', 'belongsTo'].includes(field.type) && - (appends ? appends.includes(field.name) || appends.some((item) => item.startsWith(fieldPrefix)) : true)) || - ['hasMany', 'belongsToMany'].includes(field.type) - ); +function matchToManyField(field): boolean { + // const fieldPrefix = `${field.name}.`; + return ['hasMany', 'belongsToMany'].includes(field.type); } function useAssociatedFields() { const compile = useCompile(); return [nodesOptions, triggerOptions].map((item) => { - const children = item.useOptions({ types: [matchToManyField] })?.filter(Boolean); + const children = item + .useOptions({ + types: [matchToManyField], + appends: null, + }) + ?.filter(Boolean); return { label: compile(item.label), value: item.value, @@ -100,6 +101,9 @@ function AssociatedConfig({ value, onChange, ...props }): JSX.Element { // const associationFieldName = path.pop(); const { field } = option.pop(); + if (!field || !['hasMany', 'belongsToMany'].includes(field.type)) { + return; + } // need to get: // * source collection (from node.config) // * target collection (from field name) @@ -242,6 +246,9 @@ export default { title: `{{t("Data of associated collection", { ns: "${NAMESPACE}" })}}`, 'x-decorator': 'FormItem', 'x-component': 'AssociatedConfig', + 'x-component-props': { + changeOnSelect: true, + }, 'x-reactions': [ { dependencies: ['associated'], diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/create.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/create.tsx index 7a9508601..a9467097e 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/create.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/create.tsx @@ -53,6 +53,8 @@ export default { const name = `${id}`; const [result] = getCollectionFieldOptions({ // collection: config.collection, + // depth: options?.depth ?? depth, + appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])], ...options, fields: [ { @@ -65,8 +67,6 @@ export default { }, }, ], - // depth: options?.depth ?? depth, - appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])], compile, getCollectionFields, }); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/query.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/query.tsx index 6c54060f0..f488eed3d 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/query.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/nodes/query.tsx @@ -98,6 +98,8 @@ export default { const name = `${id}`; const [result] = getCollectionFieldOptions({ // collection: config.collection, + // depth: options?.depth ?? depth, + appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])], ...options, fields: [ { @@ -110,8 +112,6 @@ export default { }, }, ], - // depth: options?.depth ?? depth, - appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])], compile, getCollectionFields, }); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/collection.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/collection.tsx index 581c703b9..be380adc9 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/collection.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/collection.tsx @@ -170,9 +170,9 @@ export default { // : 1; const result = getCollectionFieldOptions({ // depth, + appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])], ...options, fields: rootFields, - appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])], compile, getCollectionFields, }); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/form.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/form.tsx index b0d3b88bb..66839c348 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/form.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/form.tsx @@ -77,9 +77,9 @@ export default { ]; const result = getCollectionFieldOptions({ // depth, + appends: ['data', 'user', ...(config.appends?.map((item) => `data.${item}`) || [])], ...options, fields: rootFields, - appends: ['data', 'user', ...(config.appends?.map((item) => `data.${item}`) || [])], compile, getCollectionFields, }); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/schedule/index.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/schedule/index.tsx index 7d1a67c88..7e55937dd 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/schedule/index.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/triggers/schedule/index.tsx @@ -43,6 +43,7 @@ export default { if (config.mode === SCHEDULE_MODE.COLLECTION_FIELD) { const [fieldOption] = getCollectionFieldOptions({ // depth, + appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])], ...opts, fields: [ { @@ -55,7 +56,6 @@ export default { }, }, ], - appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])], compile, getCollectionFields, }); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/client/variable.tsx b/packages/plugins/@nocobase/plugin-workflow/src/client/variable.tsx index a44cdb5be..9cfd37ae6 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/client/variable.tsx +++ b/packages/plugins/@nocobase/plugin-workflow/src/client/variable.tsx @@ -29,6 +29,7 @@ export type OptionsOfUseVariableOptions = { value?: string; children?: string; }; + appends?: string[] | null; }; export const defaultFieldNames = { label: 'label', value: 'value', children: 'children' } as const; @@ -126,7 +127,7 @@ export const BaseTypeSets = { // { type: 'reference', options: { collection: 'attachments', multiple: false } } // { type: 'reference', options: { collection: 'myExpressions', entity: false } } -function matchFieldType(field, type, appends?: string[]): boolean { +function matchFieldType(field, type): boolean { const inputType = typeof type; if (inputType === 'string') { return BaseTypeSets[type]?.has(field.interface); @@ -148,7 +149,7 @@ function matchFieldType(field, type, appends?: string[]): boolean { } if (inputType === 'function') { - return type(field, appends); + return type(field); } return false; @@ -158,32 +159,51 @@ function isAssociationField(field): boolean { return ['belongsTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(field.type); } -function getNextAppends(field, appends: string[]) { +function getNextAppends(field, appends: string[] | null): string[] | null { + if (appends == null) { + return null; + } const fieldPrefix = `${field.name}.`; return appends.filter((item) => item.startsWith(fieldPrefix)).map((item) => item.replace(fieldPrefix, '')); } function filterTypedFields({ fields, types, appends, compile, getCollectionFields }) { return fields.filter((field) => { - if (types?.length) { - return types.some((type) => matchFieldType(field, type, appends)); - } + const match = types?.length ? types.some((type) => matchFieldType(field, type)) : true; if (isAssociationField(field)) { + if (appends === null) { + return ( + match || + filterTypedFields({ + fields: getNormalizedFields(field.target, { compile, getCollectionFields }), + types, + // depth: depth - 1, + appends, + compile, + getCollectionFields, + }) + ); + } const nextAppends = getNextAppends(field, appends); const included = appends.includes(field.name); - return ( - (nextAppends.length || included) && - filterTypedFields({ - fields: getNormalizedFields(field.target, { compile, getCollectionFields }), - types, - // depth: depth - 1, - appends: nextAppends, - compile, - getCollectionFields, - }).length - ); + if (match) { + return included; + } else { + return ( + (nextAppends?.length || included) && + filterTypedFields({ + fields: getNormalizedFields(field.target, { compile, getCollectionFields }), + types, + // depth: depth - 1, + appends: nextAppends, + compile, + getCollectionFields, + }).length + ); + } + } else { + return match; } - return true; }); } @@ -273,7 +293,7 @@ function loadChildren(option) { option.children = result; } else { option.isLeaf = true; - const matchingType = option.types ? option.types.some((type) => matchFieldType(option.field, type, appends)) : true; + const matchingType = option.types ? option.types.some((type) => matchFieldType(option.field, type)) : true; if (!matchingType) { option.disabled = true; } @@ -303,10 +323,11 @@ export function getCollectionFieldOptions(options): VariableOption[] { getCollectionFields, }).map((field) => { const label = compile(field.uiSchema?.title || field.name); - // console.log('===', label, field); const nextAppends = getNextAppends(field, appends); // TODO: no matching fields in next appends should consider isLeaf as true - const isLeaf = !isAssociationField(field) || (!nextAppends.length && !appends.includes(field.name)); + const isLeaf = + !isAssociationField(field) || (nextAppends && !nextAppends.length && !appends.includes(field.name)) || false; + return { [fieldNames.label]: label, key: field.name,