feat: optimize signature workflows and high-frequency queries

This commit is contained in:
hectorzhao
2026-09-02 15:45:48 +08:00
parent 9b8196ecab
commit ad89e8fed7
48 changed files with 1334 additions and 352 deletions
+27 -1
View File
@@ -18,6 +18,7 @@ function createPrismaMock() {
},
smsMessageRecord: {
findMany: jest.fn().mockResolvedValue([{ messageId: 'MSG-1' }]),
findUnique: jest.fn().mockResolvedValue({ id: 'message-1', messageId: 'MSG-1', submitRecords: [], receiptRecords: [], downstreamDeliveries: [] }),
count: jest.fn().mockResolvedValue(51),
groupBy: jest.fn().mockResolvedValue([{ status: 'delivered', _count: { _all: 2 }, _sum: { amountCents: 20, billingUnits: 2 } }]),
aggregate: jest.fn().mockResolvedValue({ _count: { _all: 2 }, _sum: { amountCents: 20, billingUnits: 2 } }),
@@ -311,7 +312,7 @@ describe('OperationsService', () => {
}));
});
it('paginates message records in PostgreSQL and limits heavy relations to the requested page', async () => {
it('paginates message summaries without preloading detail relations', async () => {
const prisma = createPrismaMock();
const service = new OperationsService(prisma as never);
@@ -326,7 +327,17 @@ describe('OperationsService', () => {
skip: 25,
take: 25,
orderBy: [{ queuedAt: 'desc' }, { id: 'desc' }],
select: expect.objectContaining({
id: true,
content: true,
hasDrainageContent: true,
tenant: { select: { id: true, name: true } },
}),
}));
const call = prisma.smsMessageRecord.findMany.mock.calls.at(-1)?.[0];
expect(call.select).not.toHaveProperty('submitRecords');
expect(call.select).not.toHaveProperty('receiptRecords');
expect(call.select).not.toHaveProperty('downstreamDeliveries');
expect(prisma.smsMessageRecord.count).toHaveBeenCalledWith({
where: expect.objectContaining({ tenantId: 'tenant-1' }),
});
@@ -359,6 +370,21 @@ describe('OperationsService', () => {
});
});
it('loads heavy message relations only for one requested detail', async () => {
const prisma = createPrismaMock();
const service = new OperationsService(prisma as never);
await expect(service.getMessage('message-1')).resolves.toEqual(expect.objectContaining({ id: 'message-1' }));
expect(prisma.smsMessageRecord.findUnique).toHaveBeenCalledWith(expect.objectContaining({
where: { id: 'message-1' },
include: expect.objectContaining({
submitRecords: expect.any(Object),
receiptRecords: expect.any(Object),
downstreamDeliveries: expect.any(Object),
}),
}));
});
it('returns the matched message record and the distinct uplink gateway message id to the client view', async () => {
const prisma = createPrismaMock();
prisma.smsUplinkMessage.findMany.mockResolvedValue([{