diff --git a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts index 162b8768e..cc4d82fd3 100644 --- a/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/Plugin.test.ts @@ -156,7 +156,7 @@ describe('workflow > Plugin', () => { }); }); - describe('revisions', () => { + describe('revision', () => { it('create revision', async () => { const w1 = await WorkflowModel.create({ enabled: true, @@ -166,7 +166,6 @@ describe('workflow > Plugin', () => { collection: 'posts' } }); - expect(w1.current).toBe(true); const { body, status } = await agent.resource(`workflows`).revision({ filterByTk: w1.id @@ -207,5 +206,84 @@ describe('workflow > Plugin', () => { expect(e1.workflowId).toBe(w1.id); expect(e2.workflowId).toBe(w2.id); }); + + it('revision with nodes', async () => { + const w1 = await WorkflowModel.create({ + enabled: true, + type: 'collection', + config: { + mode: 1, + collection: 'posts' + } + }); + + const n1 = await w1.createNode({ + type: 'echo' + }); + const n2 = await w1.createNode({ + type: 'calculation', + config: { + calculation: { + calculator: 'add', + operands: [ + { + type: '$jobsMapByNodeId', + options: { + nodeId: n1.id, + path: 'data.read' + } + }, + { + value: `{{$jobsMapByNodeId.${n1.id}.data.read}}` + } + ] + } + }, + upstreamId: n1.id + }); + await n1.setDownstream(n2); + + const { body } = await agent.resource(`workflows`).revision({ + filterByTk: w1.id + }); + const w2 = await WorkflowModel.findByPk(body.data.id, { + include: [ + 'nodes' + ] + }); + + const n1_2 = w2.nodes.find(n => !n.upstreamId); + const n2_2 = w2.nodes.find(n => !n.downstreamId); + + expect(n1_2.type).toBe('echo'); + expect(n2_2.type).toBe('calculation'); + expect(n2_2.config).toMatchObject({ + calculation: { + calculator: 'add', + operands: [ + { + type: '$jobsMapByNodeId', + options: { + nodeId: n1_2.id, + path: 'data.read' + } + }, + { + value: `{{$jobsMapByNodeId.${n1_2.id}.data.read}}` + } + ] + } + }); + + await w2.update({ enabled: true }); + + await PostRepo.create({ + values: { title: 't1', read: 1 } + }); + + const [execution] = await w2.getExecutions(); + const [echo, calculation] = await execution.getJobs({ order: [['id', 'ASC']] }); + expect(calculation.result).toBe(2); + }); }); }); diff --git a/packages/plugins/workflow/src/server/actions/workflows.ts b/packages/plugins/workflow/src/server/actions/workflows.ts index 996143f6d..463d4efba 100644 --- a/packages/plugins/workflow/src/server/actions/workflows.ts +++ b/packages/plugins/workflow/src/server/actions/workflows.ts @@ -18,6 +18,15 @@ function migrateConfig(config, oldToNew) { function migrate(value) { switch (typeOf(value)) { case 'object': + if (value.type === '$jobsMapByNodeId') { + return { + ...value, + options: { + ...value.options, + nodeId: oldToNew.get(value.options?.nodeId)?.id + } + }; + } return Object.keys(value).reduce((result, key) => ({ ...result, [key]: migrate(value[key]) }), {}); case 'array': return value.map(item => migrate(item));