role-workspace.test.mjs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 } from "../src/lib/customer-workspace.ts";
  5. import { scopedCustomers, scopedContracts, 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 }, { id: "city", parent: "province" }, { id: "outside", parent: null }],
  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("客户主账号只看本公司;分账号的新旧提交记录均只归本人", () => {
  60. const identity = { id: "zhang", role: "sub", customerNo: "customer-a", login: "a-1" };
  61. const accounts = [{ login: "a-1" }, { login: "a-2" }];
  62. const records = [
  63. { id: "new-own", enterpriseId: "customer-a", submitterId: "zhang" },
  64. { id: "new-other", enterpriseId: "customer-a", submitterId: "li" },
  65. { id: "old-own", enterpriseId: "customer-a", submitter: "下属分账号", subAccountId: "分账号 1" },
  66. { id: "old-other", enterpriseId: "customer-a", submitter: "下属分账号", subAccountId: "分账号 2" },
  67. { id: "master", enterpriseId: "customer-a", submitter: "客户主账号" },
  68. { id: "outside", enterpriseId: "customer-b", submitterId: "zhang" },
  69. ];
  70. assert.deepEqual(customerVisibleTickets(records, identity, accounts).map((item) => item.id), ["new-own", "old-own"]);
  71. const master = { id: "master-a", role: "enterprise", customerNo: "customer-a" };
  72. assert.equal(customerVisibleTickets(records, master, accounts).length, 5);
  73. assert.equal(customerOwnsTicket(records[0], master, accounts), false);
  74. assert.equal(customerOwnsTicket(records[4], master, accounts), true);
  75. assert.deepEqual(customerVisibleTickets(records, { ...identity, role: "operator" }, accounts), []);
  76. });
  77. test("只读对账方按绑定与运营商取客户,同名客户的合同不能靠名称越界", () => {
  78. const customers = [{ no: "1", name: "同名", partner: "甲", operator: "上海市 / 联通", teams: [] }, { no: "2", name: "同名", partner: "乙", operator: "广东省 / 联通", teams: [] }, { no: "3", name: "唯一", partner: "甲", operator: "上海市 / 电信", teams: [] }];
  79. const organization = { partners: [{ id: "p", name: "甲" }], operators: [{ id: "o", name: "联通", region: "sh" }], regions: [{ id: "sh", name: "上海市" }] };
  80. const own = scopedCustomers(customers, { role: "partner", partnerId: "p" }, organization);
  81. assert.deepEqual(own.map((item) => item.no), ["1", "3"]);
  82. assert.deepEqual(scopedCustomers(customers, { role: "operator", operatorId: "o" }, organization).map((item) => item.no), ["1"]);
  83. assert.deepEqual(scopedContracts([{ no: "ambiguous", customer: "同名" }, { no: "explicit", customerNo: "1" }, { no: "unique", customer: "唯一" }, { no: "outside", customerNo: "2" }], own, customers).map((item) => item.no), ["explicit", "unique"]);
  84. assert.equal(viewerParty({ role: "operator", operatorId: "o" }, organization).type, "运营商");
  85. assert.equal(viewerParty({ role: "operator", operatorId: "o" }, organization).name, "上海市 / 联通");
  86. });
  87. test("运维主账号看本团队,成员只看本人,缺失归属时不放大", () => {
  88. const { data, team, ticket } = fixture();
  89. const organization = { ...data, maintenanceTeams: [team] };
  90. const records = [ticket("own", { assigneeId: "a" }), ticket("other-person", { assigneeId: "b" }), ticket("fault", { assigneeId: "a", type: "故障" }), ticket("other-team", { teamId: "other", assigneeId: "a" })];
  91. assert.deepEqual(scopedTeamTickets(records, { role: "ops", maintenanceTeamId: team.id, memberId: "a" }, organization).map((item) => item.id), ["own", "fault"]);
  92. assert.deepEqual(scopedTeamTickets(records, { role: "teamLead", maintenanceTeamId: team.id }, organization).map((item) => item.id), ["own", "other-person", "fault"]);
  93. assert.deepEqual(scopedTeamTickets(records, { role: "ops", maintenanceTeamId: "missing" }, organization), []);
  94. });
  95. test("共用工单详情保留客户本人撤回及完成确认,拒绝跨人和错误状态", () => {
  96. const identity = { id: "zhang", name: "张敏", role: "sub", customerNo: "c", login: "c-1" };
  97. const ticket = { id: "test", type: "故障", enterpriseId: "c", submitterId: "zhang", status: "处理中", closeReason: null, handlingRecords: [] };
  98. const withdrawn = customerTicketAction(ticket, identity, [], "withdraw", "问题已解决", "2026-10-27");
  99. assert.equal(withdrawn.status, "已关闭");
  100. assert.equal(withdrawn.closeReason, "问题已解决");
  101. assert.equal(ticket.status, "处理中");
  102. const confirmed = customerTicketAction({ ...ticket, status: "已完成" }, identity, [], "confirm", "", "2026-10-27");
  103. assert.equal(confirmed.status, "已闭环");
  104. assert.equal(confirmed.handlingRecords[0].actor, "张敏");
  105. assert.throws(() => customerTicketAction(ticket, identity, [], "confirm", "", "2026-10-27"));
  106. assert.throws(() => customerTicketAction(ticket, { ...identity, id: "li" }, [], "withdraw", "申请撤回", "2026-10-27"));
  107. assert.throws(() => customerTicketAction(ticket, identity, [], "withdraw", " ", "2026-10-27"));
  108. assert.throws(() => customerTicketAction({ ...ticket, type: "开户" }, identity, [], "withdraw", "申请撤回", "2026-10-27"));
  109. });