feat(approval): 审批闭环 P0/流程图/会签 — 协办转阅+BPMN图+scriptUser会签

承接 WfTemplate 数据驱动审批主链路(approve/reject/reback/agent 已 live),补齐缺口:

P0 协办(assist)/转阅(circulated):
- flowable-service 新增 POST /tasks/{id}/assist、/circulate(对齐老 ProcessCoreServiceImpl
  createSubTask/createNewTask:每人建独立任务,父任务记意见,不阻断主流程)
- gateway FlowableClient.assist/circulate + ApprovalController 把 assist/circulated 从 400 改真实分派
- 前端 ApprovalDrawer 多选人弹窗 MultiUserForm
- 对抗审查修复:独立任务沿 parentTaskId 回溯父任务取 businessKey/formKey(否则协办/转阅人
  看不到业务单据,且 processInstanceId(null) 崩 500);assist 父任务 404 守卫;MAX_FANOUT=50;
  @Transactional;转阅改 setFormKey 而非 description

P1 BPMN 流程图查看:
- flowable-service 新 ProcessDiagramController GET /process/{pi}/diagram
  (bpmnXml + 当前/历史活动节点,运行/历史双路取 procDefId,只读)
- gateway FlowableClient.diagram + TodoController 转发
- 前端 bpmn-js 渲染(中文走浏览器字体,当前节点绿/已走过灰),ApprovalDrawer 按需展开

P3 会签(BPMN 多实例):移植 CandidateUsersHandler 的 scriptUser(JUEL)候选人解析分支
(原"精简边界"WARN 降级,致招投标/总包合同/施工技术交底等用 ${APPLYUSERS} 的会签节点被跳过):
单变量直读流程变量 + 复杂表达式回退 flowable 表达式管理器 + 结果归一为去重 userId 列表

测试:flowable-service 56 + gateway 全模块 全绿;前端 tsc 干净 + vitest 83/83 + build。
live e2e(真 eip4flow):协办/转阅各 count:1 且子任务回溯出 businessKey/进目标用户待办;
流程图真 DI 实例渲染;会签起 3 并行任务/全票聚合推进。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-26 08:09:12 +08:00
parent dccca17314
commit 4f9e4abfc2
13 changed files with 1129 additions and 20 deletions

View File

@@ -0,0 +1,64 @@
package com.dsasp.server.wf.user.assignee;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
* scriptUser 会签办理人解析的纯函数单测(同包以访问包级 helper):
* 单变量名提取 + 求值结果归一为 userId 列表。修复前 scriptUser 分支 WARN 降级返空,
* 致招投标/总包合同/施工技术交底等会签节点(集合用 ${APPLYUSERS})无办理人被跳过。
*/
class CandidateUsersScriptUserTest {
@Test
void singleVarName_提取简单变量() {
assertThat(CandidateUsersHandler.singleVarName("${APPLYUSERS}")).isEqualTo("APPLYUSERS");
assertThat(CandidateUsersHandler.singleVarName(" ${ APPLYUSERS } ")).isEqualTo("APPLYUSERS");
}
@Test
void singleVarName_复杂表达式返回null() {
assertThat(CandidateUsersHandler.singleVarName("${a + b}")).isNull();
assertThat(CandidateUsersHandler.singleVarName("${users[0]}")).isNull();
assertThat(CandidateUsersHandler.singleVarName("ADMIN")).isNull();
assertThat(CandidateUsersHandler.singleVarName(null)).isNull();
}
@Test
void coerceUserList_支持List() {
assertThat(CandidateUsersHandler.coerceUserList(List.of("u1", "u2")))
.containsExactly("u1", "u2");
}
@Test
void coerceUserList_支持数组() {
assertThat(CandidateUsersHandler.coerceUserList(new String[] { "u1", "u2" }))
.containsExactly("u1", "u2");
}
@Test
void coerceUserList_逗号分号全角半角分隔串() {
assertThat(CandidateUsersHandler.coerceUserList("u1,u2; u3,u4;u5"))
.containsExactly("u1", "u2", "u3", "u4", "u5");
}
@Test
void coerceUserList_单值() {
assertThat(CandidateUsersHandler.coerceUserList("ADMIN")).containsExactly("ADMIN");
}
@Test
void coerceUserList_去重去空白() {
assertThat(CandidateUsersHandler.coerceUserList(Arrays.asList(" u1 ", "u1", "", "u2", null)))
.containsExactly("u1", "u2");
}
@Test
void coerceUserList_null返回空() {
assertThat(CandidateUsersHandler.coerceUserList(null)).isEmpty();
}
}

View File

@@ -330,4 +330,129 @@ class FlowControllerTest {
.andExpect(status().isOk())
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content().string("1509"));
}
// ---------------------------------------------------------------- 协办(assist) / 转阅(circulate)
@Test
void 协办缺少目标用户返回400() throws Exception {
mvc.perform(post("/api/flow/tasks/task-9/assist")
.contentType("application/json")
.content("{\"comment\":\"请协助\"}"))
.andExpect(status().isBadRequest());
}
@Test
void 协办为每个目标用户建子任务并记意见() throws Exception {
// 父任务查得(取 processInstanceId 记意见)
when(taskService.createTaskQuery()).thenReturn(taskQuery);
when(taskQuery.taskId("task-9")).thenReturn(taskQuery);
Task parent = org.mockito.Mockito.mock(Task.class);
when(parent.getProcessInstanceId()).thenReturn("pi-9");
when(taskQuery.singleResult()).thenReturn(parent);
// 每次 newTask() 返回一个新子任务桩
Task sub = org.mockito.Mockito.mock(Task.class);
when(taskService.newTask()).thenReturn(sub);
mvc.perform(post("/api/flow/tasks/task-9/assist")
.contentType("application/json")
.content("{\"comment\":\"请协助\",\"targetUserIds\":[\"u-a\",\"u-b\"]}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.assisted").value(true))
.andExpect(jsonPath("$.count").value(2));
// 两个子任务:各设受托人/父任务/名称「协办」后保存
verify(sub).setAssignee("u-a");
verify(sub).setAssignee("u-b");
verify(sub, org.mockito.Mockito.times(2)).setParentTaskId("task-9");
verify(sub, org.mockito.Mockito.times(2)).setName("协办");
verify(taskService, org.mockito.Mockito.times(2)).saveTask(sub);
verify(taskService).addComment("task-9", "pi-9", "请协助");
}
@Test
void 转阅为每个目标用户建转阅任务并存formKey() throws Exception {
when(taskService.createTaskQuery()).thenReturn(taskQuery);
when(taskQuery.taskId("task-10")).thenReturn(taskQuery);
Task parent = org.mockito.Mockito.mock(Task.class);
when(parent.getProcessInstanceId()).thenReturn("pi-10");
when(parent.getFormKey()).thenReturn("wf-tpl-77"); // FormKeyResolver: 原始非空直接返回
when(taskQuery.singleResult()).thenReturn(parent);
Task copy = org.mockito.Mockito.mock(Task.class);
when(taskService.newTask()).thenReturn(copy);
mvc.perform(post("/api/flow/tasks/task-10/circulate")
.contentType("application/json")
.content("{\"comment\":\"请阅\",\"targetUserIds\":[\"u-c\"]}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.circulated").value(true))
.andExpect(jsonPath("$.count").value(1));
verify(copy).setName("转阅");
verify(copy).setAssignee("u-c");
verify(copy).setFormKey("wf-tpl-77"); // 用 setFormKey 而非 description,审批渲染 FormKeyResolver 直接命中
verify(copy).setParentTaskId("task-10");
verify(taskService).saveTask(copy);
}
@Test
void 转阅父任务不存在返回404() throws Exception {
when(taskService.createTaskQuery()).thenReturn(taskQuery);
when(taskQuery.taskId("nope")).thenReturn(taskQuery);
when(taskQuery.singleResult()).thenReturn(null);
mvc.perform(post("/api/flow/tasks/nope/circulate")
.contentType("application/json")
.content("{\"targetUserIds\":[\"u-c\"]}"))
.andExpect(status().isNotFound());
}
@Test
void 协办独立任务approvalContext回溯父任务取业务上下文() throws Exception {
// 协办/转阅建的是独立任务:无 processInstanceId,有 parentTaskId。审批上下文须回溯父任务取 businessKey/formKey,
// 否则协办人/转阅人打开看不到业务单据(code review HIGH 修复点)。
when(taskService.createTaskQuery()).thenReturn(taskQuery);
Task sub = org.mockito.Mockito.mock(Task.class);
when(sub.getName()).thenReturn("协办");
when(sub.getProcessInstanceId()).thenReturn(null);
when(sub.getParentTaskId()).thenReturn("parent-1");
when(sub.getFormKey()).thenReturn(null);
TaskQuery subQuery = org.mockito.Mockito.mock(TaskQuery.class);
when(taskQuery.taskId("sub-1")).thenReturn(subQuery);
when(subQuery.singleResult()).thenReturn(sub);
// 父任务:真实流程任务,带 processInstanceId/formKey
Task parent = org.mockito.Mockito.mock(Task.class);
when(parent.getProcessInstanceId()).thenReturn("pi-1");
when(parent.getFormKey()).thenReturn("wf-77");
when(parent.getProcessDefinitionId()).thenReturn("def-1");
when(parent.getTaskDefinitionKey()).thenReturn("node-a");
TaskQuery parentQuery = org.mockito.Mockito.mock(TaskQuery.class);
when(taskQuery.taskId("parent-1")).thenReturn(parentQuery);
when(parentQuery.singleResult()).thenReturn(parent);
// 父流程实例 businessKey
when(runtimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
when(processInstanceQuery.processInstanceId("pi-1")).thenReturn(processInstanceQuery);
ProcessInstance pi = org.mockito.Mockito.mock(ProcessInstance.class);
when(pi.getBusinessKey()).thenReturn("BK-1");
when(processInstanceQuery.singleResult()).thenReturn(pi);
mvc.perform(get("/api/flow/tasks/sub-1/approval-context"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(true))
.andExpect(jsonPath("$.formKey").value("wf-77"))
.andExpect(jsonPath("$.businessKey").value("BK-1"))
.andExpect(jsonPath("$.processInstanceId").value("pi-1"));
}
@Test
void 协办超过上限返回400() throws Exception {
StringBuilder ids = new StringBuilder("[");
for (int i = 0; i < 51; i++) {
ids.append(i == 0 ? "" : ",").append("\"u").append(i).append("\"");
}
ids.append("]");
mvc.perform(post("/api/flow/tasks/t/assist")
.contentType("application/json")
.content("{\"targetUserIds\":" + ids + "}"))
.andExpect(status().isBadRequest());
}
}

View File

@@ -0,0 +1,120 @@
package com.eip4.flowservice.api;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricActivityInstanceQuery;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceQuery;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
* 流程图端点薄层单测:mock 引擎,验证 BPMN XML 透出 + 当前/历史节点收敛 + 实例不存在降级。
*/
@ExtendWith(MockitoExtension.class)
class ProcessDiagramControllerTest {
@Mock RepositoryService repositoryService;
@Mock RuntimeService runtimeService;
@Mock HistoryService historyService;
@Mock ProcessInstanceQuery processInstanceQuery;
@Mock HistoricProcessInstanceQuery historicProcessInstanceQuery;
@Mock HistoricActivityInstanceQuery historicActivityInstanceQuery;
MockMvc mvc;
@BeforeEach
void setUp() {
ProcessDiagramController controller = new ProcessDiagramController(repositoryService, runtimeService, historyService);
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
void 运行中流程返回XML与当前及历史节点() throws Exception {
ProcessInstance pi = org.mockito.Mockito.mock(ProcessInstance.class);
when(pi.getProcessDefinitionId()).thenReturn("def-1");
when(runtimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
when(processInstanceQuery.processInstanceId("pi-1")).thenReturn(processInstanceQuery);
when(processInstanceQuery.singleResult()).thenReturn(pi);
when(runtimeService.getActiveActivityIds("pi-1")).thenReturn(List.of("task_b"));
String xml = "<?xml version=\"1.0\"?><definitions><process id=\"p\"/></definitions>";
when(repositoryService.getProcessModel("def-1"))
.thenReturn(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
HistoricActivityInstance a1 = org.mockito.Mockito.mock(HistoricActivityInstance.class);
when(a1.getActivityId()).thenReturn("task_a");
when(historyService.createHistoricActivityInstanceQuery()).thenReturn(historicActivityInstanceQuery);
when(historicActivityInstanceQuery.processInstanceId("pi-1")).thenReturn(historicActivityInstanceQuery);
when(historicActivityInstanceQuery.finished()).thenReturn(historicActivityInstanceQuery);
when(historicActivityInstanceQuery.list()).thenReturn(List.<HistoricActivityInstance>of(a1));
mvc.perform(get("/api/flow/process/pi-1/diagram"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(true))
.andExpect(jsonPath("$.bpmnXml").value(xml))
.andExpect(jsonPath("$.activeActivityIds[0]").value("task_b"))
.andExpect(jsonPath("$.finishedActivityIds[0]").value("task_a"));
}
@Test
void 已结束流程走历史取XML无当前节点() throws Exception {
// 运行库查不到
when(runtimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
when(processInstanceQuery.processInstanceId("pi-2")).thenReturn(processInstanceQuery);
when(processInstanceQuery.singleResult()).thenReturn(null);
// 历史库查得 procDefId
HistoricProcessInstance hpi = org.mockito.Mockito.mock(HistoricProcessInstance.class);
when(hpi.getProcessDefinitionId()).thenReturn("def-2");
when(historyService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
when(historicProcessInstanceQuery.processInstanceId("pi-2")).thenReturn(historicProcessInstanceQuery);
when(historicProcessInstanceQuery.singleResult()).thenReturn(hpi);
String xml = "<definitions/>";
when(repositoryService.getProcessModel("def-2"))
.thenReturn(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
when(historyService.createHistoricActivityInstanceQuery()).thenReturn(historicActivityInstanceQuery);
when(historicActivityInstanceQuery.processInstanceId("pi-2")).thenReturn(historicActivityInstanceQuery);
when(historicActivityInstanceQuery.finished()).thenReturn(historicActivityInstanceQuery);
when(historicActivityInstanceQuery.list()).thenReturn(List.<HistoricActivityInstance>of());
mvc.perform(get("/api/flow/process/pi-2/diagram"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(true))
.andExpect(jsonPath("$.bpmnXml").value(xml))
.andExpect(jsonPath("$.activeActivityIds").isEmpty());
}
@Test
void 实例不存在返回found_false不报错() throws Exception {
when(runtimeService.createProcessInstanceQuery()).thenReturn(processInstanceQuery);
when(processInstanceQuery.processInstanceId(eq("nope"))).thenReturn(processInstanceQuery);
when(processInstanceQuery.singleResult()).thenReturn(null);
when(historyService.createHistoricProcessInstanceQuery()).thenReturn(historicProcessInstanceQuery);
when(historicProcessInstanceQuery.processInstanceId("nope")).thenReturn(historicProcessInstanceQuery);
when(historicProcessInstanceQuery.singleResult()).thenReturn(null);
mvc.perform(get("/api/flow/process/nope/diagram"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(false));
}
}