feat(G2-1,G2-2,G2-3): 扩展预算编号与工作流跨域桥接

这个提交包含在:
2026-03-07 18:59:44 +08:00
父节点 09b6f4fbb0
当前提交 52558f22c3
共修改 12 个文件,包含 1211 行新增和 175 行删除
@@ -0,0 +1,77 @@
const {
resolveBudgetScope,
resolveBudgetScopeType,
selectBudgetPoolCandidate,
} = require("../../src/services/budgetPool");
describe("budgetPool service", () => {
it("resolves cost center scope with legal entity", () => {
expect(
resolveBudgetScope({
legalEntityId: "le-1",
costCenterId: "cc-1",
}),
).toEqual({
scopeType: "cost_center_scoped",
departmentId: null,
legalEntityId: "le-1",
costCenterId: "cc-1",
});
});
it("rejects mixed department and master-data dimensions", () => {
expect(() =>
resolveBudgetScope({
departmentId: "dept-1",
legalEntityId: "le-1",
}),
).toThrow(/参数校验失败/);
});
it("prefers cost center budget over department and global budgets", () => {
const selected = selectBudgetPoolCandidate(
[
{ id: "global-1", department_id: null, legal_entity_id: null, cost_center_id: null },
{ id: "dept-1", department_id: "dept-a", legal_entity_id: null, cost_center_id: null },
{ id: "scope-1", department_id: null, legal_entity_id: "le-1", cost_center_id: "cc-1" },
],
{
departmentId: "dept-a",
legalEntityId: "le-1",
costCenterId: "cc-1",
},
);
expect(selected?.id).toBe("scope-1");
expect(resolveBudgetScopeType(selected)).toBe("cost_center_scoped");
});
it("falls back to legacy department budget when no scoped budget exists", () => {
const selected = selectBudgetPoolCandidate(
[
{ id: "global-1", department_id: null, legal_entity_id: null, cost_center_id: null },
{ id: "dept-1", department_id: "dept-a", legal_entity_id: null, cost_center_id: null },
],
{
departmentId: "dept-a",
},
);
expect(selected?.id).toBe("dept-1");
});
it("throws when same-priority pools are ambiguous", () => {
expect(() =>
selectBudgetPoolCandidate(
[
{ id: "scope-1", department_id: null, legal_entity_id: "le-1", cost_center_id: "cc-1" },
{ id: "scope-2", department_id: null, legal_entity_id: "le-1", cost_center_id: "cc-1" },
],
{
legalEntityId: "le-1",
costCenterId: "cc-1",
},
),
).toThrow(/参数校验失败/);
});
});
@@ -0,0 +1,133 @@
const {
issueDocumentNumber,
resolveDocumentSequenceConfig,
} = require("../../src/services/documentSequence");
function buildPrisma({ sequenceMap = {}, categoryMap = {} } = {}) {
return {
documentSequence: {
findUnique: async ({ where }) => sequenceMap[where.document_type] ?? null,
upsert: async ({ where, update, create }) => {
const current = sequenceMap[where.document_type];
if (current) {
return {
...current,
prefix: update.prefix,
date_format: update.date_format,
separator: update.separator,
number_length: update.number_length,
reset_period: update.reset_period,
current_number: current.current_number + 1,
updated_by: update.updated_by,
updated_at: new Date("2026-03-07T00:00:00.000Z"),
};
}
return {
document_type: create.document_type,
prefix: create.prefix,
date_format: create.date_format,
separator: create.separator,
number_length: create.number_length,
reset_period: create.reset_period,
current_number: create.current_number,
last_reset_date: null,
updated_at: new Date("2026-03-07T00:00:00.000Z"),
updated_by: create.updated_by,
};
},
},
documentCategory: {
findUnique: async ({ where }) => categoryMap[where.code] ?? null,
findFirst: async ({ where }) =>
Object.values(categoryMap).find(
(item) => item.entity_type === where.entity_type && item.enabled && item.archived_at == null,
) ?? null,
},
};
}
describe("documentSequence service", () => {
it("keeps reimbursement on legacy sequence when sequence row exists", async () => {
const prisma = buildPrisma({
sequenceMap: {
reimbursement: {
document_type: "reimbursement",
prefix: "BX",
date_format: "YYYYMMDD",
separator: "-",
number_length: 4,
reset_period: "none",
current_number: 12,
last_reset_date: null,
updated_at: new Date("2026-03-07T00:00:00.000Z"),
updated_by: "tester",
},
},
categoryMap: {
reimbursement_general: {
code: "reimbursement_general",
entity_type: "reimbursement",
sequence_prefix: "RB",
number_length: 6,
reset_period: "yearly",
enabled: true,
archived_at: null,
},
},
});
const config = await resolveDocumentSequenceConfig(prisma, { documentType: "reimbursement" });
expect(config.source).toBe("document_sequence");
expect(config.prefix).toBe("BX");
expect(config.resetPeriod).toBe("none");
});
it("falls back to default purchase category when sequence row does not exist", async () => {
const prisma = buildPrisma({
categoryMap: {
purchase_general: {
code: "purchase_general",
entity_type: "purchase_request",
sequence_prefix: "CG",
number_length: 4,
reset_period: "yearly",
enabled: true,
archived_at: null,
},
},
});
const config = await resolveDocumentSequenceConfig(prisma, { documentType: "purchase_request" });
expect(config.source).toBe("document_category");
expect(config.documentCategoryCode).toBe("purchase_general");
expect(config.prefix).toBe("CG");
});
it("issues a document number using category-driven config", async () => {
const prisma = buildPrisma({
categoryMap: {
purchase_general: {
code: "purchase_general",
entity_type: "purchase_request",
sequence_prefix: "CG",
number_length: 4,
reset_period: "yearly",
enabled: true,
archived_at: null,
},
},
});
const issued = await issueDocumentNumber(prisma, {
documentType: "purchase_request",
actorId: "tester",
timeZone: "Asia/Shanghai",
});
expect(issued.config.source).toBe("document_category");
expect(issued.documentNumber).toMatch(/^CG-\d{8}-0001$/);
});
});
@@ -0,0 +1,13 @@
const { resolveEntityPermission } = require("../../src/constants/permissions");
describe("resolveEntityPermission", () => {
it("maps purchase_request approvals to procurement permissions", () => {
expect(resolveEntityPermission("purchase_request", "approve")).toBe("procurement.approve");
expect(resolveEntityPermission("purchase_request", "submit")).toBe("procurement.submit");
});
it("supports procurement alias", () => {
expect(resolveEntityPermission("procurement", "approve")).toBe("procurement.approve");
expect(resolveEntityPermission("procurement", "manage")).toBe("procurement.manage");
});
});