R49 合同台账增强:金额分类、筛选、到期预警、续约与导出
这个提交包含在:
@@ -0,0 +1,453 @@
|
||||
const Fastify = require("fastify");
|
||||
|
||||
const contractsRegistryRoutes = require("../../../src/routes/contractsRegistry");
|
||||
const { AppError } = require("../../../src/utils/AppError");
|
||||
const { currentDateString, parseDateOnlyString } = require("../../../src/utils/date");
|
||||
|
||||
const ACTOR_ID = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const PROJECT_ID = "11111111-1111-4111-8111-111111111111";
|
||||
const OWNER_ID = "22222222-2222-4222-8222-222222222222";
|
||||
const MANAGER_ID = "33333333-3333-4333-8333-333333333333";
|
||||
const MEMBER_ID = "44444444-4444-4444-8444-444444444444";
|
||||
const DEPARTMENT_ID = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
|
||||
function buildFeatureFlagRecord(flagKey) {
|
||||
return {
|
||||
id: `flag-${flagKey}`,
|
||||
flag_key: flagKey,
|
||||
override_mode: "force_on",
|
||||
rollout_type: "global",
|
||||
department_ids: [],
|
||||
note: "unit-test",
|
||||
version: 1,
|
||||
created_at: new Date("2026-04-04T08:00:00.000Z"),
|
||||
created_by: null,
|
||||
updated_at: new Date("2026-04-04T08:00:00.000Z"),
|
||||
updated_by: null,
|
||||
};
|
||||
}
|
||||
|
||||
function buildContractRow(overrides = {}) {
|
||||
return {
|
||||
id: "55555555-5555-4555-8555-555555555551",
|
||||
contract_no: "HT-2026-001",
|
||||
project_id: PROJECT_ID,
|
||||
contract_type: "service",
|
||||
counterparty_name: "测试客户",
|
||||
summary: "合同摘要",
|
||||
amount: "100000.00",
|
||||
currency: "CNY",
|
||||
signed_date: new Date("2026-03-01T00:00:00.000Z"),
|
||||
effective_date: new Date("2026-03-05T00:00:00.000Z"),
|
||||
expiry_date: new Date("2026-03-31T00:00:00.000Z"),
|
||||
confidentiality_level: "internal",
|
||||
archive_location: "/archive/contracts/001",
|
||||
offline_archive_no: "OFF-001",
|
||||
owner_employee_id: OWNER_ID,
|
||||
status: "active",
|
||||
created_at: new Date("2026-03-01T08:00:00.000Z"),
|
||||
updated_at: new Date("2026-04-01T08:00:00.000Z"),
|
||||
owner: {
|
||||
id: OWNER_ID,
|
||||
employee_no: "E001",
|
||||
name: "Owner",
|
||||
},
|
||||
project: {
|
||||
id: PROJECT_ID,
|
||||
project_no: "PRJ-001",
|
||||
name: "试点项目",
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function mapSelectedRow(row, select) {
|
||||
if (!select) {
|
||||
return row;
|
||||
}
|
||||
|
||||
return Object.entries(select).reduce((result, [key, enabled]) => {
|
||||
if (enabled === true) {
|
||||
result[key] = row[key];
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
|
||||
function cloneRow(row) {
|
||||
return {
|
||||
...row,
|
||||
owner: row.owner ? { ...row.owner } : null,
|
||||
project: row.project ? { ...row.project } : null,
|
||||
};
|
||||
}
|
||||
|
||||
function buildStatefulPrisma(initialContracts) {
|
||||
const state = {
|
||||
contracts: initialContracts.map((item) => cloneRow(item)),
|
||||
audits: [],
|
||||
};
|
||||
|
||||
const auditCreate = vi.fn(async ({ data }) => {
|
||||
state.audits.push(data);
|
||||
return data;
|
||||
});
|
||||
|
||||
const contractRegistry = {
|
||||
findMany: vi.fn(async ({ where, select, include }) => {
|
||||
let rows = [...state.contracts];
|
||||
if (where?.contract_no?.startsWith) {
|
||||
rows = rows.filter((item) => item.contract_no.startsWith(where.contract_no.startsWith));
|
||||
}
|
||||
|
||||
if (select) {
|
||||
return rows.map((item) => mapSelectedRow(item, select));
|
||||
}
|
||||
|
||||
if (include) {
|
||||
return rows.map((item) => cloneRow(item));
|
||||
}
|
||||
|
||||
return rows.map((item) => cloneRow(item));
|
||||
}),
|
||||
findUnique: vi.fn(async ({ where, select, include }) => {
|
||||
const row = state.contracts.find((item) => item.id === where.id);
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
if (select) {
|
||||
return mapSelectedRow(row, select);
|
||||
}
|
||||
if (include) {
|
||||
return cloneRow(row);
|
||||
}
|
||||
return cloneRow(row);
|
||||
}),
|
||||
create: vi.fn(async ({ data, include }) => {
|
||||
const created = buildContractRow({
|
||||
id: `66666666-6666-4666-8666-${String(state.contracts.length + 1).padStart(12, "0")}`,
|
||||
contract_no: data.contract_no,
|
||||
project_id: data.project_id ?? null,
|
||||
contract_type: data.contract_type,
|
||||
counterparty_name: data.counterparty_name,
|
||||
summary: data.summary,
|
||||
amount: data.amount ?? null,
|
||||
currency: data.currency,
|
||||
signed_date: data.signed_date,
|
||||
effective_date: data.effective_date,
|
||||
expiry_date: data.expiry_date,
|
||||
confidentiality_level: data.confidentiality_level,
|
||||
archive_location: data.archive_location,
|
||||
offline_archive_no: data.offline_archive_no,
|
||||
owner_employee_id: data.owner_employee_id,
|
||||
status: data.status,
|
||||
});
|
||||
state.contracts.push(created);
|
||||
return include ? cloneRow(created) : created;
|
||||
}),
|
||||
update: vi.fn(async ({ where, data, include }) => {
|
||||
const index = state.contracts.findIndex((item) => item.id === where.id);
|
||||
if (index < 0) {
|
||||
return null;
|
||||
}
|
||||
state.contracts[index] = {
|
||||
...state.contracts[index],
|
||||
...data,
|
||||
updated_at: new Date("2026-04-04T09:30:00.000Z"),
|
||||
};
|
||||
return include ? cloneRow(state.contracts[index]) : state.contracts[index];
|
||||
}),
|
||||
};
|
||||
|
||||
return {
|
||||
state,
|
||||
prisma: {
|
||||
featureFlag: {
|
||||
findUnique: vi.fn().mockImplementation(async ({ where }) => buildFeatureFlagRecord(where.flag_key)),
|
||||
},
|
||||
auditLog: {
|
||||
create: auditCreate,
|
||||
},
|
||||
contractRegistry,
|
||||
$transaction: async (callback) =>
|
||||
callback({
|
||||
auditLog: { create: auditCreate },
|
||||
contractRegistry,
|
||||
}),
|
||||
},
|
||||
auditCreate,
|
||||
};
|
||||
}
|
||||
|
||||
async function buildApp(options = {}) {
|
||||
const app = Fastify();
|
||||
const actor = options.actor ?? {
|
||||
id: ACTOR_ID,
|
||||
department_id: DEPARTMENT_ID,
|
||||
permissionCodes: ["project.manage"],
|
||||
};
|
||||
const prisma =
|
||||
options.prisma ??
|
||||
(() => {
|
||||
const fallback = {
|
||||
featureFlag: {
|
||||
findUnique: vi.fn().mockImplementation(async ({ where }) => buildFeatureFlagRecord(where.flag_key)),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
contractRegistry: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
create: vi.fn().mockResolvedValue(buildContractRow()),
|
||||
update: vi.fn().mockResolvedValue(buildContractRow()),
|
||||
},
|
||||
};
|
||||
fallback.$transaction = async (callback) => callback(fallback);
|
||||
return fallback;
|
||||
})();
|
||||
|
||||
app.decorate("authenticate", async (request) => {
|
||||
request.employee = actor;
|
||||
});
|
||||
app.decorate("hasPermission", (employee, permissionCode) => {
|
||||
const permissionCodes = employee.permissionCodes ?? [];
|
||||
return permissionCodes.includes("*") || permissionCodes.includes(permissionCode);
|
||||
});
|
||||
app.decorate("config", {
|
||||
appTimezone: "Asia/Shanghai",
|
||||
projectLinkingEnabled: true,
|
||||
});
|
||||
app.decorate("prisma", prisma);
|
||||
app.setErrorHandler((error, request, reply) => {
|
||||
if (error instanceof AppError) {
|
||||
return reply.status(error.statusCode).send({
|
||||
requestId: request.id,
|
||||
error: { code: error.code, message: error.message, details: error.details ?? {} },
|
||||
});
|
||||
}
|
||||
return reply.status(500).send({
|
||||
requestId: request.id,
|
||||
error: { code: "SYS_001", message: "服务器内部错误", details: {} },
|
||||
});
|
||||
});
|
||||
|
||||
await app.register(contractsRegistryRoutes, { prefix: "/api" });
|
||||
return { app, prisma };
|
||||
}
|
||||
|
||||
describe("contracts registry routes", () => {
|
||||
it("lists contracts with expired filtering and computed status", async () => {
|
||||
const contractFindMany = vi.fn().mockResolvedValue([buildContractRow()]);
|
||||
const { app } = await buildApp({
|
||||
prisma: (() => {
|
||||
const prisma = {
|
||||
featureFlag: {
|
||||
findUnique: vi.fn().mockImplementation(async ({ where }) => buildFeatureFlagRecord(where.flag_key)),
|
||||
},
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
contractRegistry: {
|
||||
findMany: contractFindMany,
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
};
|
||||
prisma.$transaction = async (callback) => callback(prisma);
|
||||
return prisma;
|
||||
})(),
|
||||
});
|
||||
|
||||
const today = parseDateOnlyString(currentDateString("Asia/Shanghai"));
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/contracts-registry?status=expired&contractType=service&counterpartyName=%E6%B5%8B%E8%AF%95&expiryDateFrom=2026-03-01&expiryDateTo=2026-03-31`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const payload = response.json();
|
||||
expect(payload.data.items[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
status: "expired",
|
||||
persistedStatus: "active",
|
||||
contractType: "service",
|
||||
amount: "100000.00",
|
||||
}),
|
||||
);
|
||||
expect(contractFindMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
AND: expect.arrayContaining([
|
||||
expect.objectContaining({ status: "active", expiry_date: { lt: today } }),
|
||||
expect.objectContaining({ contract_type: "service" }),
|
||||
expect.objectContaining({
|
||||
counterparty_name: expect.objectContaining({ contains: "测试", mode: "insensitive" }),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
expiry_date: expect.objectContaining({
|
||||
gte: new Date("2026-03-01T00:00:00.000Z"),
|
||||
lte: new Date("2026-03-31T00:00:00.000Z"),
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects invalid expiry date range", async () => {
|
||||
const { app } = await buildApp();
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/contracts-registry?expiryDateFrom=2026-04-10&expiryDateTo=2026-04-01",
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.json().error).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "VALID_001",
|
||||
details: expect.objectContaining({
|
||||
expiryDateTo: "must_be_after_or_equal_expiryDateFrom",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("returns expiring contracts within the default window", async () => {
|
||||
const contractFindMany = vi.fn().mockResolvedValue([
|
||||
buildContractRow({
|
||||
id: "contract-2",
|
||||
contract_no: "HT-2026-002",
|
||||
expiry_date: new Date("2026-04-10T00:00:00.000Z"),
|
||||
}),
|
||||
]);
|
||||
const { app } = await buildApp({
|
||||
prisma: (() => {
|
||||
const prisma = {
|
||||
featureFlag: {
|
||||
findUnique: vi.fn().mockImplementation(async ({ where }) => buildFeatureFlagRecord(where.flag_key)),
|
||||
},
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
contractRegistry: {
|
||||
findMany: contractFindMany,
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
};
|
||||
prisma.$transaction = async (callback) => callback(prisma);
|
||||
return prisma;
|
||||
})(),
|
||||
});
|
||||
|
||||
const today = parseDateOnlyString(currentDateString("Asia/Shanghai"));
|
||||
const end = new Date(today.getTime());
|
||||
end.setUTCDate(end.getUTCDate() + 30);
|
||||
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/contracts-registry/expiring",
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data).toEqual(
|
||||
expect.objectContaining({
|
||||
days: 30,
|
||||
total: 1,
|
||||
}),
|
||||
);
|
||||
expect(contractFindMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
AND: expect.arrayContaining([
|
||||
{ status: "active" },
|
||||
expect.objectContaining({
|
||||
expiry_date: expect.objectContaining({
|
||||
gte: today,
|
||||
lte: end,
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("renews an active contract into the next draft revision and archives the source", async () => {
|
||||
const stateful = buildStatefulPrisma([
|
||||
buildContractRow({
|
||||
id: "55555555-5555-4555-8555-555555555551",
|
||||
contract_no: "HT-2026-001",
|
||||
status: "active",
|
||||
}),
|
||||
buildContractRow({
|
||||
id: "55555555-5555-4555-8555-555555555552",
|
||||
contract_no: "HT-2026-001-R1",
|
||||
status: "archived",
|
||||
}),
|
||||
]);
|
||||
const { app } = await buildApp({ prisma: stateful.prisma });
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/contracts-registry/55555555-5555-4555-8555-555555555551/renew",
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data).toEqual(
|
||||
expect.objectContaining({
|
||||
item: expect.objectContaining({
|
||||
contractNo: "HT-2026-001-R2",
|
||||
status: "draft",
|
||||
persistedStatus: "draft",
|
||||
}),
|
||||
sourceItem: expect.objectContaining({
|
||||
contractNo: "HT-2026-001",
|
||||
status: "archived",
|
||||
persistedStatus: "archived",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(stateful.state.contracts.find((item) => item.id === "55555555-5555-4555-8555-555555555551")?.status).toBe("archived");
|
||||
expect(
|
||||
stateful.state.contracts.some((item) => item.contract_no === "HT-2026-001-R2" && item.status === "draft"),
|
||||
).toBe(true);
|
||||
expect(stateful.state.audits.map((item) => item.action)).toEqual(
|
||||
expect.arrayContaining(["CONTRACT_REGISTRY_RENEWED", "CONTRACT_REGISTRY_STATUS_CHANGED"]),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects renewing a draft contract", async () => {
|
||||
const stateful = buildStatefulPrisma([
|
||||
buildContractRow({
|
||||
id: "55555555-5555-4555-8555-555555555553",
|
||||
contract_no: "HT-2026-003",
|
||||
status: "draft",
|
||||
}),
|
||||
]);
|
||||
const { app } = await buildApp({ prisma: stateful.prisma });
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/contracts-registry/55555555-5555-4555-8555-555555555553/renew",
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(409);
|
||||
expect(response.json().error).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "VALID_001",
|
||||
details: expect.objectContaining({ status: "renew_not_allowed" }),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
const Fastify = require("fastify");
|
||||
|
||||
const exportsRoutes = require("../../../src/routes/exports");
|
||||
|
||||
const ACTOR_ID = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const PROJECT_ID = "11111111-1111-4111-8111-111111111111";
|
||||
const OWNER_ID = "22222222-2222-4222-8222-222222222222";
|
||||
const DEPARTMENT_ID = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
|
||||
function buildFeatureFlagRecord(flagKey) {
|
||||
return {
|
||||
id: `flag-${flagKey}`,
|
||||
flag_key: flagKey,
|
||||
override_mode: "force_on",
|
||||
rollout_type: "global",
|
||||
department_ids: [],
|
||||
note: "unit-test",
|
||||
version: 1,
|
||||
created_at: new Date("2026-04-04T08:00:00.000Z"),
|
||||
created_by: null,
|
||||
updated_at: new Date("2026-04-04T08:00:00.000Z"),
|
||||
updated_by: null,
|
||||
};
|
||||
}
|
||||
|
||||
function buildContractRow(overrides = {}) {
|
||||
return {
|
||||
id: "contract-1",
|
||||
contract_no: "HT-2026-001",
|
||||
project_id: PROJECT_ID,
|
||||
contract_type: "service",
|
||||
counterparty_name: "测试客户",
|
||||
summary: "合同摘要",
|
||||
amount: "88000.00",
|
||||
currency: "CNY",
|
||||
signed_date: new Date("2026-03-01T00:00:00.000Z"),
|
||||
effective_date: new Date("2026-03-05T00:00:00.000Z"),
|
||||
expiry_date: new Date("2026-03-31T00:00:00.000Z"),
|
||||
confidentiality_level: "internal",
|
||||
archive_location: "/archive/contracts/001",
|
||||
offline_archive_no: "OFF-001",
|
||||
owner_employee_id: OWNER_ID,
|
||||
status: "active",
|
||||
created_at: new Date("2026-03-01T08:00:00.000Z"),
|
||||
updated_at: new Date("2026-04-01T08:00:00.000Z"),
|
||||
owner: {
|
||||
name: "Owner",
|
||||
},
|
||||
project: {
|
||||
project_no: "PRJ-001",
|
||||
name: "试点项目",
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function buildApp(options = {}) {
|
||||
const app = Fastify();
|
||||
const actor = options.actor ?? {
|
||||
id: ACTOR_ID,
|
||||
department_id: DEPARTMENT_ID,
|
||||
permissionCodes: ["project.manage"],
|
||||
};
|
||||
const count = options.count ?? vi.fn().mockResolvedValue(1);
|
||||
const findMany =
|
||||
options.findMany ??
|
||||
vi.fn().mockImplementation(async ({ where }) => {
|
||||
if (where?.OR) {
|
||||
return [];
|
||||
}
|
||||
return [buildContractRow()];
|
||||
});
|
||||
const auditCreate = options.auditCreate ?? vi.fn().mockResolvedValue({});
|
||||
|
||||
app.decorate("authenticate", async (request) => {
|
||||
request.employee = actor;
|
||||
});
|
||||
app.decorate("requirePermission", (permissionCode) => async (request) => {
|
||||
request.employee = actor;
|
||||
const permissionCodes = actor.permissionCodes ?? [];
|
||||
if (!permissionCodes.includes("*") && !permissionCodes.includes(permissionCode)) {
|
||||
throw new Error(`unexpected permission check: ${permissionCode}`);
|
||||
}
|
||||
});
|
||||
app.decorate("hasPermission", (employee, permissionCode) => {
|
||||
const permissionCodes = employee.permissionCodes ?? [];
|
||||
return permissionCodes.includes("*") || permissionCodes.includes(permissionCode);
|
||||
});
|
||||
app.decorate("config", {
|
||||
appTimezone: "Asia/Shanghai",
|
||||
projectLinkingEnabled: true,
|
||||
});
|
||||
app.decorate("prisma", {
|
||||
featureFlag: {
|
||||
findUnique: vi.fn().mockImplementation(async ({ where }) => buildFeatureFlagRecord(where.flag_key)),
|
||||
},
|
||||
auditLog: {
|
||||
create: auditCreate,
|
||||
},
|
||||
contractRegistry: {
|
||||
count,
|
||||
findMany,
|
||||
},
|
||||
});
|
||||
|
||||
await app.register(exportsRoutes, { prefix: "/api" });
|
||||
return { app, count, findMany, auditCreate };
|
||||
}
|
||||
|
||||
describe("contracts registry export route", () => {
|
||||
it("exports filtered contracts registry as csv", async () => {
|
||||
const { app, count, findMany, auditCreate } = await buildApp();
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/exports/contracts-registry",
|
||||
payload: {
|
||||
format: "csv",
|
||||
status: "expired",
|
||||
contractType: "service",
|
||||
counterpartyName: "测试",
|
||||
expiryDateFrom: "2026-03-01",
|
||||
expiryDateTo: "2026-03-31",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers["content-type"]).toContain("text/csv");
|
||||
expect(response.headers["content-disposition"]).toContain("contracts-registry-");
|
||||
expect(response.body).toContain("合同编号");
|
||||
expect(response.body).toContain("服务合同");
|
||||
expect(response.body).toContain("已过期");
|
||||
expect(count).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
AND: expect.arrayContaining([
|
||||
expect.objectContaining({ contract_type: "service" }),
|
||||
expect.objectContaining({
|
||||
counterparty_name: expect.objectContaining({ contains: "测试", mode: "insensitive" }),
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(findMany).toHaveBeenCalled();
|
||||
expect(auditCreate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
action: "EXPORT_CONTRACTS_REGISTRY",
|
||||
extra: expect.objectContaining({
|
||||
format: "csv",
|
||||
status: "expired",
|
||||
contractType: "service",
|
||||
counterpartyName: "测试",
|
||||
matchedTotal: 1,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("exports contracts registry as xlsx", async () => {
|
||||
const { app, auditCreate } = await buildApp({
|
||||
count: vi.fn().mockResolvedValue(1),
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
buildContractRow({
|
||||
id: "contract-2",
|
||||
contract_no: "HT-2026-002",
|
||||
contract_type: "purchase",
|
||||
status: "archived",
|
||||
expiry_date: new Date("2026-04-15T00:00:00.000Z"),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/exports/contracts-registry",
|
||||
payload: {
|
||||
format: "xlsx",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers["content-type"]).toContain(
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
);
|
||||
expect(response.headers["content-disposition"]).toContain("contracts-registry-");
|
||||
expect(Buffer.from(response.rawPayload).length).toBeGreaterThan(0);
|
||||
expect(auditCreate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
action: "EXPORT_CONTRACTS_REGISTRY",
|
||||
extra: expect.objectContaining({
|
||||
format: "xlsx",
|
||||
matchedTotal: 1,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
在新工单中引用
屏蔽一个用户