From 80937bd6dd6a3c6bd73c050741a757db54818858 Mon Sep 17 00:00:00 2001 From: sealday Date: Thu, 11 Jul 2024 19:19:57 +0800 Subject: [PATCH] feat: workflow trigger support blacklist (#1309) Reviewed-on: https://git.daoyoucloud.com/daoyoucloud/tachybase/pulls/1309 --- .../src/server/services/record-service.ts | 24 ------------ .../src/client/triggers/collection.tsx | 37 +++++++++++++++++++ .../src/server/triggers/CollectionTrigger.ts | 13 ++++++- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/packages/plugins/@hera/plugin-rental/src/server/services/record-service.ts b/packages/plugins/@hera/plugin-rental/src/server/services/record-service.ts index ce2ccfd6d..2c9a07060 100644 --- a/packages/plugins/@hera/plugin-rental/src/server/services/record-service.ts +++ b/packages/plugins/@hera/plugin-rental/src/server/services/record-service.ts @@ -242,30 +242,6 @@ export class RecordService { if (!values) return; // 触发打印更新次数跳过 if (Object.keys(values).length === 1 && values?.print_count) return; - if (values?.category === RecordCategory.lease && values.contract) { - const dateObject = values.date; - // 结束时间添加一天,新系统选择时间都是以当天0点开始,但是导入的数据存在不是以0点开始比如2023-12-21:03.000……,提醒结算单的时间可能为2023-12-21:00.000…… - const settlement = await this.db.sequelize.query( - `SELECT * FROM settlements WHERE contract_id = ${values.contract.id} AND start_date <= '${dateObject}' AND end_date >= (TIMESTAMP '${dateObject}' - INTERVAL '1 day')`, - ); - const settlementData: any = settlement[0]; - if (settlementData.length) { - for (const item of settlementData) { - const settlement_id = item.id; - await this.db.getModel('settlements').update( - { - status: settlementStatus.needReCompute, - }, - { - where: { - id: settlement_id, - }, - transaction, - }, - ); - } - } - } if (values?.category === RecordCategory.purchase) { // 定价 const rule = values.price_items; diff --git a/packages/plugins/@tachybase/plugin-workflow/src/client/triggers/collection.tsx b/packages/plugins/@tachybase/plugin-workflow/src/client/triggers/collection.tsx index 071dda034..af6734b9a 100644 --- a/packages/plugins/@tachybase/plugin-workflow/src/client/triggers/collection.tsx +++ b/packages/plugins/@tachybase/plugin-workflow/src/client/triggers/collection.tsx @@ -50,6 +50,15 @@ export default class extends Trigger { }, }, }, + { + target: 'blacklist', + effects: ['onFieldValueChange'], + fulfill: { + state: { + value: [], + }, + }, + }, { target: 'condition', effects: ['onFieldValueChange'], @@ -93,6 +102,34 @@ export default class extends Trigger { }, ], }, + blacklist: { + type: 'array', + title: `{{t("Changed fields - blacklist", { ns: "${NAMESPACE}" })}}`, + description: `{{t("Triggered only if none of the selected fields changes. When record is added or deleted, this field has no effect.", { ns: "${NAMESPACE}" })}}`, + 'x-decorator': 'FormItem', + 'x-component': 'FieldsSelect', + 'x-component-props': { + mode: 'multiple', + placeholder: '{{t("Select field")}}', + filter(field) { + return ( + !field.hidden && + (field.uiSchema ? !field.uiSchema['x-read-pretty'] : true) && + !['linkTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(field.type) + ); + }, + }, + 'x-reactions': [ + { + dependencies: ['collection', 'mode'], + fulfill: { + state: { + visible: `{{!!$deps[0] && ($deps[1] & ${COLLECTION_TRIGGER_MODE.UPDATED})}}`, + }, + }, + }, + ], + }, changed: { type: 'array', title: `{{t("Changed fields", { ns: "${NAMESPACE}" })}}`, diff --git a/packages/plugins/@tachybase/plugin-workflow/src/server/triggers/CollectionTrigger.ts b/packages/plugins/@tachybase/plugin-workflow/src/server/triggers/CollectionTrigger.ts index b14b21935..993ac2aff 100644 --- a/packages/plugins/@tachybase/plugin-workflow/src/server/triggers/CollectionTrigger.ts +++ b/packages/plugins/@tachybase/plugin-workflow/src/server/triggers/CollectionTrigger.ts @@ -37,7 +37,7 @@ function getFieldRawName(collection: ICollection, name: string) { // async function, should return promise async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: Model, options) { - const { condition, changed, mode, appends } = workflow.config; + const { condition, changed, mode, appends, blacklist } = workflow.config; const [dataSourceName, collectionName] = parseCollectionName(workflow.config.collection); const collection = this.workflow.app.dataSourceManager?.dataSources .get(dataSourceName) @@ -57,6 +57,17 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M ) { return; } + if ( + blacklist && + blacklist.length && + blacklist + .filter( + (name) => !['linkTo', 'hasOne', 'hasMany', 'belongsToMany'].includes(collection.getField(name).options.type), + ) + .some((name) => data.changedWithAssociations(getFieldRawName(collection, name))) + ) { + return; + } // NOTE: if no configured condition match, do not trigger if (condition && condition.$and?.length) { // TODO: change to map filter format to calculation format