From d8e8f5ec458fbabf71d4e256f4ecd7027eaa991d Mon Sep 17 00:00:00 2001 From: Junyi Date: Sat, 25 Mar 2023 13:15:03 +0700 Subject: [PATCH] feat(plugin-workflow): add failOnEmpty option for query node (#1599) --- .../workflow/src/client/locale/zh-CN.ts | 1 + .../workflow/src/client/nodes/query.tsx | 9 +++- .../__tests__/instructions/query.test.ts | 43 +++++++++++++++++++ .../workflow/src/server/instructions/query.ts | 10 ++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/plugins/workflow/src/client/locale/zh-CN.ts b/packages/plugins/workflow/src/client/locale/zh-CN.ts index 41bff5440..a45789f89 100644 --- a/packages/plugins/workflow/src/client/locale/zh-CN.ts +++ b/packages/plugins/workflow/src/client/locale/zh-CN.ts @@ -134,6 +134,7 @@ export default { 'Update record': '更新数据', 'Query record': '查询数据', 'Multiple records': '多条数据', + 'Fail on no data': '无数据时报错', 'Please select collection first': '请先选择数据表', 'Only update records matching conditions': '只更新满足条件的数据', 'Please add at least one condition': '请添加至少一个条件', diff --git a/packages/plugins/workflow/src/client/nodes/query.tsx b/packages/plugins/workflow/src/client/nodes/query.tsx index 8450a2eec..65b9a3784 100644 --- a/packages/plugins/workflow/src/client/nodes/query.tsx +++ b/packages/plugins/workflow/src/client/nodes/query.tsx @@ -16,10 +16,9 @@ export default { group: 'collection', fieldset: { collection, - // 'config.multiple': { + // multiple: { // type: 'boolean', // title: `{{t("Multiple records", { ns: "${NAMESPACE}" })}}`, - // name: 'config.multiple', // 'x-decorator': 'FormItem', // 'x-component': 'Checkbox', // 'x-component-props': { @@ -32,6 +31,12 @@ export default { filter, appends } + }, + failOnEmpty: { + type: 'boolean', + title: `{{t("Fail on no data", { ns: "${NAMESPACE}" })}}`, + 'x-decorator': 'FormItem', + 'x-component': 'Checkbox', } }, view: { diff --git a/packages/plugins/workflow/src/server/__tests__/instructions/query.test.ts b/packages/plugins/workflow/src/server/__tests__/instructions/query.test.ts index c03ad364c..0be76d8c3 100644 --- a/packages/plugins/workflow/src/server/__tests__/instructions/query.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/instructions/query.test.ts @@ -1,6 +1,7 @@ import { Application } from '@nocobase/server'; import Database from '@nocobase/database'; import { getApp, sleep } from '..'; +import { EXECUTION_STATUS, JOB_STATUS } from '../../constants'; @@ -315,4 +316,46 @@ describe('workflow > instructions > query', () => { expect(job.result.length).toBe(0); }); }); + + describe('failOnEmpty', () => { + it('failOnEmpty', async () => { + const n1 = await workflow.createNode({ + type: 'query', + config: { + collection: 'categories', + failOnEmpty: true + } + }); + + const post = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const [execution] = await workflow.getExecutions(); + expect(execution.status).toBe(EXECUTION_STATUS.FAILED); + const [job] = await execution.getJobs(); + expect(job.status).toBe(JOB_STATUS.FAILED); + expect(job.result).toBe(null); + }); + + it('failOnEmpty && multiple', async () => { + const n1 = await workflow.createNode({ + type: 'query', + config: { + collection: 'categories', + multiple: true, + failOnEmpty: true + } + }); + + const post = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const [execution] = await workflow.getExecutions(); + expect(execution.status).toBe(EXECUTION_STATUS.FAILED); + const [job] = await execution.getJobs(); + expect(job.result).toMatchObject([]); + }); + }); }); diff --git a/packages/plugins/workflow/src/server/instructions/query.ts b/packages/plugins/workflow/src/server/instructions/query.ts index f79a3bd18..675ad4c90 100644 --- a/packages/plugins/workflow/src/server/instructions/query.ts +++ b/packages/plugins/workflow/src/server/instructions/query.ts @@ -7,7 +7,8 @@ export default { const { collection, multiple, - params = {} + params = {}, + failOnEmpty = false } = node.config; const repo = (node.constructor).database.getRepository(collection); @@ -20,6 +21,13 @@ export default { transaction: processor.transaction }); + if (failOnEmpty && (multiple ? !result.length : !result)) { + return { + result, + status: JOB_STATUS.FAILED + } + } + // NOTE: `toJSON()` to avoid getting undefined value from Proxied model instance (#380) // e.g. Object.prototype.hasOwnProperty.call(result, 'id') // false // so the properties can not be get by json-templates(object-path)