feat: 重构追溯码及上链出库流程

This commit is contained in:
Nixevol
2026-07-16 11:49:50 +08:00
parent 5f5c158c01
commit ebc2603815
92 changed files with 7089 additions and 1615 deletions
@@ -0,0 +1,65 @@
import { TraceCodeService } from './trace-code.service';
function createHarness(codes: any[]) {
const prisma = {
traceCode: {
findMany: jest.fn().mockResolvedValue(codes),
deleteMany: jest.fn().mockResolvedValue({ count: codes.length }),
},
queryLog: { updateMany: jest.fn().mockResolvedValue({ count: 0 }) },
batch: { update: jest.fn().mockResolvedValue({}) },
$transaction: jest.fn().mockImplementation(async (operations: any[]) => Promise.all(operations)),
};
const service = new TraceCodeService(
prisma as any,
{} as any,
{} as any,
{} as any,
{} as any,
);
return { service, prisma };
}
function code(overrides: Record<string, unknown> = {}) {
return {
id: 1n,
shortCode: '482731000001',
batchId: 10,
outbound: null,
proofs: [],
jobItems: [],
...overrides,
};
}
describe('TraceCodeService.batchDelete', () => {
it.each([
[code({ outbound: { id: 2n } }), '已登记出库'],
[code({ proofs: [{ status: 'ONCHAIN' }] }), '已上链'],
[code({ proofs: [{ status: 'UNKNOWN' }] }), '存在上链记录'],
[code({ jobItems: [{ id: 3n }] }), '存在批量操作记录'],
])('explicitly rejects a referenced trace code', async (blocked, message) => {
const harness = createHarness([blocked]);
await expect(harness.service.batchDelete({ ids: ['1'] })).rejects.toThrow(message);
expect(harness.prisma.traceCode.deleteMany).not.toHaveBeenCalled();
});
it('deduplicates ids and updates batch counters in the same transaction', async () => {
const harness = createHarness([code()]);
await expect(harness.service.batchDelete({ ids: ['1', '1'] })).resolves.toEqual({ success: true, count: 1 });
expect(harness.prisma.traceCode.findMany).toHaveBeenCalledWith({
where: { id: { in: [1n] } },
select: expect.objectContaining({ outbound: expect.anything(), proofs: expect.anything(), jobItems: expect.anything() }),
});
expect(harness.prisma.queryLog.updateMany).toHaveBeenCalledWith({
where: { traceCodeId: { in: [1n] } },
data: { traceCodeId: null },
});
expect(harness.prisma.batch.update).toHaveBeenCalledWith({
where: { id: 10 },
data: { quantity: { decrement: 1 }, generatedCount: { decrement: 1 } },
});
});
});