role-workspace.test.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { dispatchTeamTickets, eligibleMembers, teamTickets } from "../src/lib/team-workspace.ts";
  4. import { customerOwnsTicket, customerVisibleTickets, customerTicketAction, customerLiveTicketActions } from "../src/lib/customer-workspace.ts";
  5. import { scopedCustomers, scopedTeamTickets, viewerParty } from "../src/lib/workspace-scope.ts";
  6. import { memberCoveredIndexes } from "../src/lib/organization-demo.ts";
  7. function fixture() {
  8. const team = { id: "team-a", ticketTypes: ["开户"], scope: { regions: ["r"], enterprises: [] }, members: [{ id: "a", name: "甲", ticketTypes: ["开户"], account: "在用", enterprises: [0] }, { id: "b", name: "乙", ticketTypes: ["开户"], account: "在用", enterprises: [0] }, { id: "c", name: "丙", ticketTypes: ["开户"], account: "停用", enterprises: [0] }, { id: "d", name: "丁", ticketTypes: ["故障"], account: "在用", enterprises: [0] }] };
  9. const data = { regions: [{ id: "r", parent: null }], enterprises: [{ no: "customer-a", region: "r", teamIds: ["team-a"] }, { no: "customer-b", region: "outside", teamIds: ["team-a"] }] };
  10. const ticket = (id, extra = {}) => ({ id, teamId: "team-a", type: "开户", enterpriseId: "customer-a", assigneeId: "", assigneeName: "", status: "待处理", handlingRecords: [], ...extra });
  11. return { team, data, ticket };
  12. }
  13. test("自动平均逐张重算负荷,固定顺序破同分,并保留历史和输入", () => {
  14. const { team, data, ticket } = fixture();
  15. const records = [ticket("active-a", { assigneeId: "a", assigneeName: "甲", status: "处理中" }), ticket("1"), ticket("2"), ticket("3")];
  16. const before = structuredClone(records);
  17. const result = dispatchTeamTickets(records, data, team, "孙队", "2026-10-27");
  18. assert.equal(result.assigned, 3);
  19. assert.deepEqual(result.records.slice(1).map((item) => item.assigneeId), ["b", "a", "b"]);
  20. assert.equal(result.records[1].handlingRecords[0].actor, "孙队");
  21. assert.ok(result.records.slice(1).every((item) => item.status === "待处理"));
  22. assert.deepEqual(records, before);
  23. });
  24. test("团队队列保留同团队各类型,类型或范围不匹配时留队", () => {
  25. const { team, data, ticket } = fixture();
  26. const records = [ticket("fault", { type: "故障" }), ticket("other-team", { teamId: "other" }), ticket("done", { status: "已完成" }), ticket("assigned", { assigneeId: "b", assigneeName: "乙" }), ticket("out-of-scope", { enterpriseId: "customer-b" }), ticket("unknown", { enterpriseId: "missing" })];
  27. const result = dispatchTeamTickets(records, data, team, "孙队", "2026-10-27");
  28. assert.equal(result.assigned, 0);
  29. assert.equal(result.skipped, 3);
  30. assert.equal(teamTickets(records, team).length, 5);
  31. assert.deepEqual(result.records, records);
  32. assert.deepEqual(eligibleMembers(data, team, ticket("normal")).map((item) => item.id), ["a", "b"]);
  33. });
  34. test("手工分派只修改指定的未分派工单,不能分给不具备资格的成员", () => {
  35. const { team, data, ticket } = fixture();
  36. const records = [ticket("1"), ticket("2")];
  37. const rejected = dispatchTeamTickets(records, data, team, "主账号", "2026-10-27", { ticketId: "1", memberId: "c" });
  38. assert.equal(rejected.assigned, 0);
  39. const result = dispatchTeamTickets(records, data, team, "主账号", "2026-10-27", { ticketId: "1", memberId: "b" });
  40. assert.equal(result.records[0].assigneeId, "b");
  41. assert.equal(result.records[1].assigneeId, "");
  42. assert.equal(result.assigned, 1);
  43. });
  44. test("成员三种负责范围均受团队范围和客户挂接限制", () => {
  45. const data = {
  46. regions: [{ id: "province", parent: null, name: "省", pathName: "全网/省" }, { id: "city", parent: "province", name: "市", pathName: "全网/省/市" }, { id: "outside", parent: null, name: "外", pathName: "全网/外" }],
  47. enterprises: [
  48. { region: "city", teamIds: ["team-a"] },
  49. { region: "city", teamIds: ["team-b"] },
  50. { region: "outside", teamIds: ["team-a"] },
  51. ],
  52. };
  53. const team = { id: "team-a", scope: { regions: ["province"], enterprises: [] } };
  54. assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "follow_team", enterprises: [] }), [0]);
  55. assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "region", scopeRegions: ["province"], enterprises: [] }), [0]);
  56. assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "customer", enterprises: [0, 1, 2] }), [0]);
  57. assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "region", scopeRegions: ["outside"], enterprises: [] }), []);
  58. });
  59. test("树外挂载点按 region_path 前缀落入团队负责区域", () => {
  60. const data = {
  61. regions: [{ id: "province", parent: null, name: "上海", pathName: "全网/上海" }],
  62. enterprises: [
  63. { region: "district-x", regionPath: "全网 / 上海 / 浦东", teamIds: ["team-a"] },
  64. { region: "district-y", regionPath: "全网 / 江苏 / 南京", teamIds: ["team-a"] },
  65. { region: "district-z", regionPath: "全网 / 上海 / 静安", teamIds: ["team-b"] },
  66. ],
  67. };
  68. const team = { id: "team-a", scope: { regions: ["province"], enterprises: [] } };
  69. assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "follow_team", enterprises: [] }), [0]);
  70. });
  71. test("客户主账号只看本公司;分账号的新旧提交记录均只归本人", () => {
  72. const identity = { id: "zhang", role: "sub", customerNo: "customer-a", login: "a-1" };
  73. const accounts = [{ login: "a-1" }, { login: "a-2" }];
  74. const records = [
  75. { id: "new-own", enterpriseId: "customer-a", submitterId: "zhang" },
  76. { id: "new-other", enterpriseId: "customer-a", submitterId: "li" },
  77. { id: "old-own", enterpriseId: "customer-a", submitter: "下属分账号", subAccountId: "分账号 1" },
  78. { id: "old-other", enterpriseId: "customer-a", submitter: "下属分账号", subAccountId: "分账号 2" },
  79. { id: "master", enterpriseId: "customer-a", submitter: "客户主账号" },
  80. { id: "outside", enterpriseId: "customer-b", submitterId: "zhang" },
  81. ];
  82. assert.deepEqual(customerVisibleTickets(records, identity, accounts).map((item) => item.id), ["new-own", "old-own"]);
  83. const master = { id: "master-a", role: "enterprise", customerNo: "customer-a" };
  84. assert.equal(customerVisibleTickets(records, master, accounts).length, 5);
  85. assert.equal(customerOwnsTicket(records[0], master, accounts), false);
  86. assert.equal(customerOwnsTicket(records[4], master, accounts), true);
  87. assert.deepEqual(customerVisibleTickets(records, { ...identity, role: "operator" }, accounts), []);
  88. });
  89. test("只读对账方按绑定与运营商取客户", () => {
  90. const customers = [{ no: "1", name: "同名", partner: "甲", operator: "上海市 / 联通", teams: [] }, { no: "2", name: "同名", partner: "乙", operator: "广东省 / 联通", teams: [] }, { no: "3", name: "唯一", partner: "甲", operator: "上海市 / 电信", teams: [] }];
  91. const organization = { partners: [{ id: "p", name: "甲" }], operators: [{ id: "o", name: "联通", region: "sh" }], regions: [{ id: "sh", name: "上海市" }] };
  92. const own = scopedCustomers(customers, { role: "partner", partnerId: "p" }, organization);
  93. assert.deepEqual(own.map((item) => item.no), ["1", "3"]);
  94. assert.deepEqual(scopedCustomers(customers, { role: "operator", operatorId: "o" }, organization).map((item) => item.no), ["1"]);
  95. assert.equal(viewerParty({ role: "operator", operatorId: "o" }, organization).type, "运营商");
  96. assert.equal(viewerParty({ role: "operator", operatorId: "o" }, organization).name, "上海市 / 联通");
  97. });
  98. test("运维主账号看本团队,成员只看本人,缺失归属时不放大", () => {
  99. const { data, team, ticket } = fixture();
  100. const organization = { ...data, maintenanceTeams: [team] };
  101. const records = [ticket("own", { assigneeId: "a" }), ticket("other-person", { assigneeId: "b" }), ticket("fault", { assigneeId: "a", type: "故障" }), ticket("other-team", { teamId: "other", assigneeId: "a" })];
  102. assert.deepEqual(scopedTeamTickets(records, { role: "ops", maintenanceTeamId: team.id, memberId: "a" }, organization).map((item) => item.id), ["own", "fault"]);
  103. assert.deepEqual(scopedTeamTickets(records, { role: "teamLead", maintenanceTeamId: team.id }, organization).map((item) => item.id), ["own", "other-person", "fault"]);
  104. assert.deepEqual(scopedTeamTickets(records, { role: "ops", maintenanceTeamId: "missing" }, organization), []);
  105. });
  106. test("共用工单详情保留客户本人撤回及完成确认,拒绝跨人和错误状态", () => {
  107. const identity = { id: "zhang", name: "张敏", role: "sub", customerNo: "c", login: "c-1" };
  108. const ticket = { id: "test", type: "故障", enterpriseId: "c", submitterId: "zhang", status: "处理中", closeReason: null, handlingRecords: [] };
  109. const withdrawn = customerTicketAction(ticket, identity, [], "withdraw", "问题已解决", "2026-10-27");
  110. assert.equal(withdrawn.status, "已关闭");
  111. assert.equal(withdrawn.closeReason, "问题已解决");
  112. assert.equal(ticket.status, "处理中");
  113. const confirmed = customerTicketAction({ ...ticket, status: "已完成" }, identity, [], "confirm", "", "2026-10-27");
  114. assert.equal(confirmed.status, "已办结");
  115. assert.equal(confirmed.handlingRecords[0].actor, "张敏");
  116. assert.throws(() => customerTicketAction(ticket, identity, [], "confirm", "", "2026-10-27"));
  117. assert.throws(() => customerTicketAction(ticket, { ...identity, id: "li" }, [], "withdraw", "申请撤回", "2026-10-27"));
  118. assert.throws(() => customerTicketAction(ticket, identity, [], "withdraw", " ", "2026-10-27"));
  119. assert.throws(() => customerTicketAction({ ...ticket, type: "开户" }, identity, [], "withdraw", "申请撤回", "2026-10-27"));
  120. });
  121. /* ── 真实接口口径:客户侧「我的工单」这一行该给哪些操作(缺陷 P-21,判据=finalize.rs)── */
  122. const liveRow = (extra = {}) => ({ status: "processing", ticket_type: "fault", submitter_type: "customer", ...extra });
  123. test("客户自报的非主线单标完成后给「确认完成,关闭工单」,三档在办单给「撤回」", () => {
  124. assert.deepEqual(customerLiveTicketActions(liveRow({ status: "completed" })),
  125. { confirmClose: true, withdraw: false, needsDetail: false, blockedReason: null });
  126. for (const status of ["unassigned", "pending", "processing"])
  127. assert.deepEqual(customerLiveTicketActions(liveRow({ status })),
  128. { confirmClose: false, withdraw: true, needsDetail: false, blockedReason: null });
  129. });
  130. test("主线四类走竣工回执闭环,⛔ 在客户侧确认关闭", () => {
  131. for (const type of ["open", "service_open", "expiry_stop", "service_stop"]) {
  132. const gate = customerLiveTicketActions(liveRow({ status: "completed", ticket_type: type }));
  133. assert.equal(gate.confirmClose, false, type);
  134. assert.equal(gate.withdraw, false, type);
  135. assert.match(gate.blockedReason ?? "", /主线/);
  136. }
  137. // 主线单在办期间仍可撤回(闸门只看 submitter_type+状态,工单§5.1.9)
  138. assert.equal(customerLiveTicketActions(liveRow({ status: "processing", ticket_type: "open" })).withdraw, true);
  139. });
  140. test("运维代建/系统生成/运营商报文的单⛔ 出现确认完成与撤回(标记完成即结束)", () => {
  141. for (const submitter of ["ops", "system", "unicom"]) {
  142. for (const status of ["completed", "processing", "settled", "terminated"]) {
  143. const gate = customerLiveTicketActions(liveRow({ status, submitter_type: submitter }));
  144. assert.equal(gate.confirmClose, false, `${submitter}/${status}`);
  145. assert.equal(gate.withdraw, false, `${submitter}/${status}`);
  146. assert.equal(gate.needsDetail, false, `${submitter}/${status}`);
  147. assert.ok(gate.blockedReason, `${submitter}/${status} 要给可见原因`);
  148. }
  149. }
  150. });
  151. test("提交方未回传时两把⛔ 给,只报 needsDetail 让界面按需取一次详情", () => {
  152. // `read.rs` 的 LIST_COLS 不下发 submitter_type ⇒ 列表行到这里就是 undefined
  153. assert.deepEqual(customerLiveTicketActions({ status: "completed", ticket_type: "fault" }),
  154. { confirmClose: false, withdraw: false, needsDetail: true, blockedReason: null });
  155. assert.deepEqual(customerLiveTicketActions(liveRow({ submitter_type: null })),
  156. { confirmClose: false, withdraw: false, needsDetail: true, blockedReason: null });
  157. });
  158. test("已办结/已终止的单没有客户操作,只给可见原因", () => {
  159. for (const status of ["settled", "terminated"]) {
  160. const gate = customerLiveTicketActions(liveRow({ status }));
  161. assert.equal(gate.confirmClose, false);
  162. assert.equal(gate.withdraw, false);
  163. assert.equal(gate.needsDetail, false);
  164. assert.ok(gate.blockedReason);
  165. }
  166. });