fix(plugin-workflow): fix assignees and aggregate variable (#2725)

This commit is contained in:
Junyi 2023-09-26 23:39:50 +08:00 committed by GitHub
parent 49c2fbf45d
commit 6beae90806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 36 deletions

View File

@ -17,19 +17,20 @@ import { NAMESPACE, lang } from '../locale';
import { collection, filter } from '../schemas/collection'; import { collection, filter } from '../schemas/collection';
import { BaseTypeSets, defaultFieldNames, nodesOptions, triggerOptions } from '../variable'; import { BaseTypeSets, defaultFieldNames, nodesOptions, triggerOptions } from '../variable';
function matchToManyField(field, appends): boolean { function matchToManyField(field): boolean {
const fieldPrefix = `${field.name}.`; // const fieldPrefix = `${field.name}.`;
return ( return ['hasMany', 'belongsToMany'].includes(field.type);
(['hasOne', 'belongsTo'].includes(field.type) &&
(appends ? appends.includes(field.name) || appends.some((item) => item.startsWith(fieldPrefix)) : true)) ||
['hasMany', 'belongsToMany'].includes(field.type)
);
} }
function useAssociatedFields() { function useAssociatedFields() {
const compile = useCompile(); const compile = useCompile();
return [nodesOptions, triggerOptions].map((item) => { return [nodesOptions, triggerOptions].map((item) => {
const children = item.useOptions({ types: [matchToManyField] })?.filter(Boolean); const children = item
.useOptions({
types: [matchToManyField],
appends: null,
})
?.filter(Boolean);
return { return {
label: compile(item.label), label: compile(item.label),
value: item.value, value: item.value,
@ -100,6 +101,9 @@ function AssociatedConfig({ value, onChange, ...props }): JSX.Element {
// const associationFieldName = path.pop(); // const associationFieldName = path.pop();
const { field } = option.pop(); const { field } = option.pop();
if (!field || !['hasMany', 'belongsToMany'].includes(field.type)) {
return;
}
// need to get: // need to get:
// * source collection (from node.config) // * source collection (from node.config)
// * target collection (from field name) // * target collection (from field name)
@ -242,6 +246,9 @@ export default {
title: `{{t("Data of associated collection", { ns: "${NAMESPACE}" })}}`, title: `{{t("Data of associated collection", { ns: "${NAMESPACE}" })}}`,
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'AssociatedConfig', 'x-component': 'AssociatedConfig',
'x-component-props': {
changeOnSelect: true,
},
'x-reactions': [ 'x-reactions': [
{ {
dependencies: ['associated'], dependencies: ['associated'],

View File

@ -53,6 +53,8 @@ export default {
const name = `${id}`; const name = `${id}`;
const [result] = getCollectionFieldOptions({ const [result] = getCollectionFieldOptions({
// collection: config.collection, // collection: config.collection,
// depth: options?.depth ?? depth,
appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])],
...options, ...options,
fields: [ fields: [
{ {
@ -65,8 +67,6 @@ export default {
}, },
}, },
], ],
// depth: options?.depth ?? depth,
appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });

View File

@ -98,6 +98,8 @@ export default {
const name = `${id}`; const name = `${id}`;
const [result] = getCollectionFieldOptions({ const [result] = getCollectionFieldOptions({
// collection: config.collection, // collection: config.collection,
// depth: options?.depth ?? depth,
appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])],
...options, ...options,
fields: [ fields: [
{ {
@ -110,8 +112,6 @@ export default {
}, },
}, },
], ],
// depth: options?.depth ?? depth,
appends: [name, ...(config.params?.appends?.map((item) => `${name}.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });

View File

@ -170,9 +170,9 @@ export default {
// : 1; // : 1;
const result = getCollectionFieldOptions({ const result = getCollectionFieldOptions({
// depth, // depth,
appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])],
...options, ...options,
fields: rootFields, fields: rootFields,
appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });

View File

@ -77,9 +77,9 @@ export default {
]; ];
const result = getCollectionFieldOptions({ const result = getCollectionFieldOptions({
// depth, // depth,
appends: ['data', 'user', ...(config.appends?.map((item) => `data.${item}`) || [])],
...options, ...options,
fields: rootFields, fields: rootFields,
appends: ['data', 'user', ...(config.appends?.map((item) => `data.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });

View File

@ -43,6 +43,7 @@ export default {
if (config.mode === SCHEDULE_MODE.COLLECTION_FIELD) { if (config.mode === SCHEDULE_MODE.COLLECTION_FIELD) {
const [fieldOption] = getCollectionFieldOptions({ const [fieldOption] = getCollectionFieldOptions({
// depth, // depth,
appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])],
...opts, ...opts,
fields: [ fields: [
{ {
@ -55,7 +56,6 @@ export default {
}, },
}, },
], ],
appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])],
compile, compile,
getCollectionFields, getCollectionFields,
}); });

View File

@ -29,6 +29,7 @@ export type OptionsOfUseVariableOptions = {
value?: string; value?: string;
children?: string; children?: string;
}; };
appends?: string[] | null;
}; };
export const defaultFieldNames = { label: 'label', value: 'value', children: 'children' } as const; 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: 'attachments', multiple: false } }
// { type: 'reference', options: { collection: 'myExpressions', entity: false } } // { type: 'reference', options: { collection: 'myExpressions', entity: false } }
function matchFieldType(field, type, appends?: string[]): boolean { function matchFieldType(field, type): boolean {
const inputType = typeof type; const inputType = typeof type;
if (inputType === 'string') { if (inputType === 'string') {
return BaseTypeSets[type]?.has(field.interface); return BaseTypeSets[type]?.has(field.interface);
@ -148,7 +149,7 @@ function matchFieldType(field, type, appends?: string[]): boolean {
} }
if (inputType === 'function') { if (inputType === 'function') {
return type(field, appends); return type(field);
} }
return false; return false;
@ -158,32 +159,51 @@ function isAssociationField(field): boolean {
return ['belongsTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(field.type); 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}.`; const fieldPrefix = `${field.name}.`;
return appends.filter((item) => item.startsWith(fieldPrefix)).map((item) => item.replace(fieldPrefix, '')); return appends.filter((item) => item.startsWith(fieldPrefix)).map((item) => item.replace(fieldPrefix, ''));
} }
function filterTypedFields({ fields, types, appends, compile, getCollectionFields }) { function filterTypedFields({ fields, types, appends, compile, getCollectionFields }) {
return fields.filter((field) => { return fields.filter((field) => {
if (types?.length) { const match = types?.length ? types.some((type) => matchFieldType(field, type)) : true;
return types.some((type) => matchFieldType(field, type, appends));
}
if (isAssociationField(field)) { 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 nextAppends = getNextAppends(field, appends);
const included = appends.includes(field.name); const included = appends.includes(field.name);
return ( if (match) {
(nextAppends.length || included) && return included;
filterTypedFields({ } else {
fields: getNormalizedFields(field.target, { compile, getCollectionFields }), return (
types, (nextAppends?.length || included) &&
// depth: depth - 1, filterTypedFields({
appends: nextAppends, fields: getNormalizedFields(field.target, { compile, getCollectionFields }),
compile, types,
getCollectionFields, // depth: depth - 1,
}).length appends: nextAppends,
); compile,
getCollectionFields,
}).length
);
}
} else {
return match;
} }
return true;
}); });
} }
@ -273,7 +293,7 @@ function loadChildren(option) {
option.children = result; option.children = result;
} else { } else {
option.isLeaf = true; 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) { if (!matchingType) {
option.disabled = true; option.disabled = true;
} }
@ -303,10 +323,11 @@ export function getCollectionFieldOptions(options): VariableOption[] {
getCollectionFields, getCollectionFields,
}).map((field) => { }).map((field) => {
const label = compile(field.uiSchema?.title || field.name); const label = compile(field.uiSchema?.title || field.name);
// console.log('===', label, field);
const nextAppends = getNextAppends(field, appends); const nextAppends = getNextAppends(field, appends);
// TODO: no matching fields in next appends should consider isLeaf as true // 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 { return {
[fieldNames.label]: label, [fieldNames.label]: label,
key: field.name, key: field.name,