From 095b50f96421241c1e49dbac1ac4c2c4d40299d0 Mon Sep 17 00:00:00 2001 From: Junyi Date: Sat, 22 Jun 2024 14:44:18 +0800 Subject: [PATCH] fix(plugin-workflow): fix duplicating sync workflow (#4727) --- .../__tests__/actions/workflows.test.ts | 71 +++++++++++++++++++ .../src/server/actions/workflows.ts | 2 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/actions/workflows.test.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/actions/workflows.test.ts index 8a0018c1f1..ca39f3001d 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/actions/workflows.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/actions/workflows.test.ts @@ -381,5 +381,76 @@ describe('workflow > actions > workflows', () => { expect(e1.key).not.toBe(e2.key); expect(e2.workflowId).toBe(w2.id); }); + + it('duplicate sync workflow', async () => { + const w1 = await WorkflowModel.create({ + enabled: true, + type: 'collection', + config: { + mode: 1, + collection: 'posts', + }, + sync: true, + }); + + const p1 = await PostRepo.create({ values: { title: 't1' } }); + + const { body, status } = await agent.resource(`workflows`).revision({ + filterByTk: w1.id, + }); + + expect(status).toBe(200); + const { data: w2 } = body; + expect(w2.config).toMatchObject(w1.config); + expect(w2.key).not.toBe(w1.key); + expect(w2.current).toBeTruthy(); + expect(w2.enabled).toBe(false); + expect(w2.allExecuted).toBe(0); + expect(w2.sync).toBe(true); + + // stop w1 + await WorkflowModel.update( + { + enabled: false, + }, + { + where: { + id: w1.id, + }, + individualHooks: true, + }, + ); + + await WorkflowModel.update( + { + enabled: true, + }, + { + where: { + id: w2.id, + }, + individualHooks: true, + }, + ); + + const p2 = await PostRepo.create({ values: { title: 't2' } }); + + const [w1next, w2next] = await WorkflowModel.findAll({ + order: [['id', 'ASC']], + }); + + expect(w1next.enabled).toBe(false); + expect(w1next.current).toBe(true); + expect(w1next.executed).toBe(1); + expect(w1next.allExecuted).toBe(1); + expect(w2next.enabled).toBe(true); + expect(w2next.executed).toBe(1); + expect(w2next.allExecuted).toBe(1); + + const [e1] = await w1next.getExecutions(); + const [e2] = await w2next.getExecutions(); + expect(e1.key).not.toBe(e2.key); + expect(e2.workflowId).toBe(w2.id); + }); }); }); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/actions/workflows.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/actions/workflows.ts index 27932f44f7..f3784f3557 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/actions/workflows.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/actions/workflows.ts @@ -84,7 +84,6 @@ export async function revision(context: Context, next) { title: origin.title, triggerTitle: origin.triggerTitle, allExecuted: origin.allExecuted, - sync: origin.sync, } : values; @@ -93,6 +92,7 @@ export async function revision(context: Context, next) { title: `${origin.title} copy`, description: origin.description, ...revisionData, + sync: origin.sync, type: origin.type, config: typeof trigger.duplicateConfig === 'function'