diff --git a/packages/core/client/src/workflow/triggers/collection.tsx b/packages/core/client/src/workflow/triggers/collection.tsx index 641d8c0b70..4464a7908e 100644 --- a/packages/core/client/src/workflow/triggers/collection.tsx +++ b/packages/core/client/src/workflow/triggers/collection.tsx @@ -49,7 +49,8 @@ export default { { value: 2, label: '{{t("After record updated")}}' }, { value: 3, label: '{{t("After record added or updated")}}' }, { value: 4, label: '{{t("After record deleted")}}' } - ] + ], + placeholder: '{{t("Trigger on")}}' }, required: true }, @@ -62,6 +63,7 @@ export default { 'x-component': 'FieldsSelect', 'x-component-props': { mode: 'multiple', + placeholder: '{{t("Select Field")}}' } }, 'config.condition': { diff --git a/packages/plugins/workflow/src/Plugin.ts b/packages/plugins/workflow/src/Plugin.ts index d3918f4f7b..82d6151e53 100644 --- a/packages/plugins/workflow/src/Plugin.ts +++ b/packages/plugins/workflow/src/Plugin.ts @@ -17,30 +17,26 @@ import { EXECUTION_STATUS } from './constants'; async function setCurrent(instance: WorkflowModel, options) { const updates: { enabled?: boolean, current?: boolean } = {}; - if (!instance.changed('enabled')) { + if (!instance.changed('enabled') || !instance.enabled) { return; } - if (instance.enabled) { - instance.set('current', true); - updates.enabled = false; - } + instance.set('current', true); + updates.enabled = false; - if (instance.current) { - // NOTE: set to `null` but not `false` will not violate the unique index - updates.current = null; - const previous = await (instance.constructor).findOne({ - where: { - key: instance.key, - current: true - } - }); - - if (previous) { - await previous.update(updates, { - transaction: options.transaction - }); + // NOTE: set to `null` but not `false` will not violate the unique index + updates.current = null; + const previous = await (instance.constructor).findOne({ + where: { + key: instance.key, + current: true } + }); + + if (previous) { + await previous.update(updates, { + transaction: options.transaction + }); } } diff --git a/packages/plugins/workflow/src/__tests__/workflow.test.ts b/packages/plugins/workflow/src/__tests__/workflow.test.ts index 57c5438181..6209a09fa9 100644 --- a/packages/plugins/workflow/src/__tests__/workflow.test.ts +++ b/packages/plugins/workflow/src/__tests__/workflow.test.ts @@ -33,12 +33,31 @@ describe('workflow > workflow', () => { collection: 'posts' } }); + expect(workflow.current).toBe(true); + const p1 = await PostRepo.create({ values: { title: 't1' } }); + const executions1 = await workflow.getExecutions(); + expect(executions1.length).toBe(1); + + const w = await WorkflowModel.findOne({ + where: { + current: true + } + }); + expect(w).not.toBeNull(); + expect(w.current).toBe(true); await workflow.update({ enabled: false }); - const post = await PostRepo.create({ values: { title: 't1' } }); + const p2 = await PostRepo.create({ values: { title: 't2' } }); - const executions = await workflow.getExecutions(); - expect(executions.length).toBe(0); + const executions2 = await workflow.getExecutions(); + expect(executions2.length).toBe(1); + + const workflows = await WorkflowModel.findAll({ + where: { + current: true + } + }); + expect(workflows.length).toBe(1); }); });