From 1bfdbdadf1dca4122de143b13fdb4d24c5043315 Mon Sep 17 00:00:00 2001 From: mytharcher Date: Mon, 28 Apr 2025 22:30:36 +0800 Subject: [PATCH] fix(plugin-workflow-manual): fix test cases --- .../src/server/Plugin.ts | 55 ++++++----- .../src/server/__tests__/tasks.test.ts | 97 ++++++++++++++++++- 2 files changed, 122 insertions(+), 30 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts index a83b47faa0..8d321d9d24 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/Plugin.ts @@ -19,6 +19,30 @@ import ManualInstruction from './ManualInstruction'; import { MANUAL_TASK_TYPE, TASK_STATUS } from '../common/constants'; export default class extends Plugin { + onTaskSave = async (task: Model, { transaction }) => { + const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin; + const workflowId = Array.from(workflowPlugin.enabledCache.keys()); + const ModelClass = task.constructor as unknown as Model; + const pending = await ModelClass.count({ + where: { + userId: task.userId, + workflowId, + status: TASK_STATUS.PENDING, + }, + col: 'id', + transaction, + }); + const all = await ModelClass.count({ + where: { + userId: task.userId, + workflowId, + }, + col: 'id', + transaction, + }); + await workflowPlugin.updateTasksStats(task.userId, MANUAL_TASK_TYPE, { pending, all }, { transaction }); + }; + onWorkflowStatusChange = async (workflow, { transaction }) => { const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin; const WorkflowManualTaskModel = this.db.getModel('workflowManualTasks'); @@ -64,7 +88,7 @@ export default class extends Plugin { const userId = []; for (const item of tasksByUser) { userId.push(item.userId); - userStatsMap.set(item.userId, {}); + userStatsMap.set(item.userId, { pending: 0, all: 0 }); } // 调整所有任务中的负责人的统计数字 @@ -90,13 +114,13 @@ export default class extends Plugin { } for (const row of pendingCounts) { if (!userStatsMap.get(row.userId)) { - userStatsMap.set(row.userId, {}); + userStatsMap.set(row.userId, { pending: 0, all: 0 }); } userStatsMap.set(row.userId, { ...userStatsMap.get(row.userId), pending: row.count }); } for (const row of allCounts) { if (!userStatsMap.get(row.userId)) { - userStatsMap.set(row.userId, {}); + userStatsMap.set(row.userId, { pending: 0, all: 0 }); } userStatsMap.set(row.userId, { ...userStatsMap.get(row.userId), all: row.count }); } @@ -134,30 +158,9 @@ export default class extends Plugin { const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin; workflowPlugin.registerInstruction('manual', ManualInstruction); - this.db.on('workflowManualTasks.afterSave', async (task: Model, { transaction }) => { - const workflowId = Array.from(workflowPlugin.enabledCache.keys()); - const ModelClass = task.constructor as unknown as Model; - const pending = await ModelClass.count({ - where: { - userId: task.userId, - workflowId, - status: TASK_STATUS.PENDING, - }, - col: 'id', - transaction, - }); - const all = await ModelClass.count({ - where: { - userId: task.userId, - workflowId, - }, - col: 'id', - transaction, - }); - await workflowPlugin.updateTasksStats(task.userId, MANUAL_TASK_TYPE, { pending, all }, { transaction }); - }); + this.db.on('workflowManualTasks.afterSave', this.onTaskSave); + this.db.on('workflowManualTasks.afterDestroy', this.onTaskSave); this.db.on('workflows.afterUpdate', this.onWorkflowStatusChange); - // this.db.on('workflows.afterDestroy', this.onWorkflowStatusChange); } } diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/tasks.test.ts b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/tasks.test.ts index bbb1ff32f6..ca036a0bd6 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/tasks.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/server/__tests__/tasks.test.ts @@ -24,7 +24,8 @@ describe('workflow > instructions > manual > tasks', () => { let workflow; let UserModel; let users; - let UserJobModel; + let ManualTaskModel; + let UserTaskRepo; beforeEach(async () => { app = await getApp({ @@ -37,7 +38,8 @@ describe('workflow > instructions > manual > tasks', () => { PostRepo = db.getCollection('posts').repository; AnotherPostRepo = app.dataSourceManager.dataSources.get('another').collectionManager.getRepository('posts'); UserModel = db.getCollection('users').model; - UserJobModel = db.getModel('workflowManualTasks'); + ManualTaskModel = db.getModel('workflowManualTasks'); + UserTaskRepo = db.getRepository('userWorkflowTasks'); users = await UserModel.bulkCreate([ { id: 2, nickname: 'a' }, @@ -79,12 +81,18 @@ describe('workflow > instructions > manual > tasks', () => { await sleep(500); - const UserJobModel = db.getModel('workflowManualTasks'); - const pendingJobs = await UserJobModel.findAll({ + const pendingJobs = await ManualTaskModel.findAll({ order: [['userId', 'ASC']], }); expect(pendingJobs.length).toBe(1); + const s1s = await UserTaskRepo.find(); + expect(s1s.length).toBe(1); + expect(s1s[0].get('stats')).toMatchObject({ + pending: 1, + all: 1, + }); + const res1 = await userAgents[0].resource('workflowManualTasks').submit({ filterByTk: pendingJobs[0].get('id'), values: { @@ -104,6 +112,87 @@ describe('workflow > instructions > manual > tasks', () => { const posts = await AnotherPostRepo.find(); expect(posts.length).toBe(1); expect(posts[0]).toMatchObject({ title: 't1' }); + + const s2s = await UserTaskRepo.find(); + expect(s2s.length).toBe(1); + expect(s2s[0].get('stats')).toMatchObject({ + pending: 0, + all: 1, + }); + }); + }); + + describe('workflow status', () => { + it('workflow disabled', async () => { + const n1 = await workflow.createNode({ + type: 'manual', + config: { + assignees: [users[0].id], + forms: { + f1: { + type: 'create', + actions: [{ status: JOB_STATUS.RESOLVED, key: 'resolve' }], + collection: 'posts', + }, + }, + }, + }); + + const post = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const s1s = await UserTaskRepo.find(); + expect(s1s.length).toBe(1); + expect(s1s[0].get('stats')).toMatchObject({ + pending: 1, + all: 1, + }); + + await workflow.update({ enabled: false }); + + const s2s = await UserTaskRepo.find(); + expect(s2s.length).toBe(1); + expect(s2s[0].get('stats')).toMatchObject({ + pending: 0, + all: 0, + }); + }); + + it('workflow destroy', async () => { + const n1 = await workflow.createNode({ + type: 'manual', + config: { + assignees: [users[0].id], + forms: { + f1: { + type: 'create', + actions: [{ status: JOB_STATUS.RESOLVED, key: 'resolve' }], + collection: 'posts', + }, + }, + }, + }); + + const post = await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const s1s = await UserTaskRepo.find(); + expect(s1s.length).toBe(1); + expect(s1s[0].get('stats')).toMatchObject({ + pending: 1, + all: 1, + }); + + await workflow.destroy(); + + const s2s = await UserTaskRepo.find(); + expect(s2s.length).toBe(1); + expect(s2s[0].get('stats')).toMatchObject({ + pending: 0, + all: 0, + }); }); }); });