fix(plugin-workflow-manual): fix test cases

This commit is contained in:
mytharcher 2025-04-28 22:30:36 +08:00
parent bf41c10a48
commit 1bfdbdadf1
2 changed files with 122 additions and 30 deletions

View File

@ -19,6 +19,30 @@ import ManualInstruction from './ManualInstruction';
import { MANUAL_TASK_TYPE, TASK_STATUS } from '../common/constants'; import { MANUAL_TASK_TYPE, TASK_STATUS } from '../common/constants';
export default class extends Plugin { 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 }) => { onWorkflowStatusChange = async (workflow, { transaction }) => {
const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin; const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin;
const WorkflowManualTaskModel = this.db.getModel('workflowManualTasks'); const WorkflowManualTaskModel = this.db.getModel('workflowManualTasks');
@ -64,7 +88,7 @@ export default class extends Plugin {
const userId = []; const userId = [];
for (const item of tasksByUser) { for (const item of tasksByUser) {
userId.push(item.userId); 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) { for (const row of pendingCounts) {
if (!userStatsMap.get(row.userId)) { 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 }); userStatsMap.set(row.userId, { ...userStatsMap.get(row.userId), pending: row.count });
} }
for (const row of allCounts) { for (const row of allCounts) {
if (!userStatsMap.get(row.userId)) { 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 }); 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; const workflowPlugin = this.app.pm.get(WorkflowPlugin) as WorkflowPlugin;
workflowPlugin.registerInstruction('manual', ManualInstruction); workflowPlugin.registerInstruction('manual', ManualInstruction);
this.db.on('workflowManualTasks.afterSave', async (task: Model, { transaction }) => { this.db.on('workflowManualTasks.afterSave', this.onTaskSave);
const workflowId = Array.from(workflowPlugin.enabledCache.keys()); this.db.on('workflowManualTasks.afterDestroy', this.onTaskSave);
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('workflows.afterUpdate', this.onWorkflowStatusChange); this.db.on('workflows.afterUpdate', this.onWorkflowStatusChange);
// this.db.on('workflows.afterDestroy', this.onWorkflowStatusChange);
} }
} }

View File

@ -24,7 +24,8 @@ describe('workflow > instructions > manual > tasks', () => {
let workflow; let workflow;
let UserModel; let UserModel;
let users; let users;
let UserJobModel; let ManualTaskModel;
let UserTaskRepo;
beforeEach(async () => { beforeEach(async () => {
app = await getApp({ app = await getApp({
@ -37,7 +38,8 @@ describe('workflow > instructions > manual > tasks', () => {
PostRepo = db.getCollection('posts').repository; PostRepo = db.getCollection('posts').repository;
AnotherPostRepo = app.dataSourceManager.dataSources.get('another').collectionManager.getRepository('posts'); AnotherPostRepo = app.dataSourceManager.dataSources.get('another').collectionManager.getRepository('posts');
UserModel = db.getCollection('users').model; UserModel = db.getCollection('users').model;
UserJobModel = db.getModel('workflowManualTasks'); ManualTaskModel = db.getModel('workflowManualTasks');
UserTaskRepo = db.getRepository('userWorkflowTasks');
users = await UserModel.bulkCreate([ users = await UserModel.bulkCreate([
{ id: 2, nickname: 'a' }, { id: 2, nickname: 'a' },
@ -79,12 +81,18 @@ describe('workflow > instructions > manual > tasks', () => {
await sleep(500); await sleep(500);
const UserJobModel = db.getModel('workflowManualTasks'); const pendingJobs = await ManualTaskModel.findAll({
const pendingJobs = await UserJobModel.findAll({
order: [['userId', 'ASC']], order: [['userId', 'ASC']],
}); });
expect(pendingJobs.length).toBe(1); 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({ const res1 = await userAgents[0].resource('workflowManualTasks').submit({
filterByTk: pendingJobs[0].get('id'), filterByTk: pendingJobs[0].get('id'),
values: { values: {
@ -104,6 +112,87 @@ describe('workflow > instructions > manual > tasks', () => {
const posts = await AnotherPostRepo.find(); const posts = await AnotherPostRepo.find();
expect(posts.length).toBe(1); expect(posts.length).toBe(1);
expect(posts[0]).toMatchObject({ title: 't1' }); 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,
});
}); });
}); });
}); });