fix(work-record): persist monthly archive rows with stable ids

This commit is contained in:
WuYongJie 2026-09-01 14:31:58 +08:00
parent eead468d4d
commit b3f85d5f5a
2 changed files with 42 additions and 1 deletions

View File

@ -154,5 +154,44 @@ describe("work record monthly archive permissions", () => {
"2026-08",
expect.objectContaining({ recordCount: 3 }),
);
expect(created.id).toMatch(/^archive-[0-9a-f-]+$/);
expect(created.id).not.toMatch(/^archive-draft-/);
});
it("keeps a newly persisted row addressable for a later update", async () => {
mockFindScoreRuleById.mockResolvedValue({
id: "other-rule",
primary: "其他",
secondary: "具体事宜",
unitScore: new Prisma.Decimal(1),
} as never);
mockUpdateArchive.mockResolvedValue(undefined as never);
const created = await workRecordMonthlyArchiveService.createRecord(designer, "2026-08", {
date: "2026-08-03",
scoreRuleId: "other-rule",
approvalOrTask: "新增事项",
spu: "SPU-3",
quantity: "1",
});
mockFindArchive.mockResolvedValueOnce({
...archive,
records: [created],
recordCount: 1,
} as never);
const updated = await workRecordMonthlyArchiveService.updateRecord(
designer,
"2026-08",
created.id,
{ approvalOrTask: "已修改事项" },
);
expect(updated.id).toBe(created.id);
expect(updated.approvalOrTask).toBe("已修改事项");
expect(mockUpdateArchive).toHaveBeenLastCalledWith(
"2026-08",
expect.objectContaining({ recordCount: 1 }),
);
});
});

View File

@ -242,7 +242,9 @@ class WorkRecordMonthlyArchiveService {
const date = existing?.date ?? normalizeText(input.date);
assertDateInArchiveMonth(date, monthKey);
return {
id: existing?.id ?? `archive-draft-${crypto.randomUUID()}`,
// Draft IDs are only used by the client before the first save. Persisted
// archive rows must use a different prefix so later edits call update.
id: existing?.id ?? `archive-${crypto.randomUUID()}`,
date,
designerId: existing?.designerId ?? normalizeText(input.designerId),
designer: existing?.designer ?? normalizeText(input.designer),