perf(cmpp): reduce inbound database round trips

This commit is contained in:
hectorzhao
2026-08-20 15:29:45 +08:00
parent 67b760a599
commit 14f993c1f8
12 changed files with 153 additions and 24 deletions
+30 -2
View File
@@ -397,6 +397,7 @@ function createService(
);
service['postGatewayControl'] = jest.fn().mockResolvedValue({ delivered: true });
service['publishGatewaySubmitCommand'] = jest.fn().mockResolvedValue(undefined);
service['getSendQueue'] = jest.fn().mockReturnValue({ add: jest.fn().mockResolvedValue(undefined) });
return { service, prisma, billing, riskReview, phoneFrequency };
}
@@ -1627,6 +1628,7 @@ describe('SendChainService', () => {
})).resolves.toEqual(expect.objectContaining({ accepted: true, phoneCount: 2 }));
expect(prisma.smsMessageRecord.create).toHaveBeenCalledTimes(2);
expect(prisma.smsApplication.findFirst).toHaveBeenCalledTimes(1);
expect(prisma.smsMessageRecord.update).toHaveBeenCalledWith({
where: { id: 'record-2' },
data: expect.objectContaining({
@@ -1766,7 +1768,10 @@ describe('SendChainService', () => {
templateId: 'tpl-code',
variables: { code: '715021' },
}));
expect(service.enqueueBatchTask).toHaveBeenCalledWith('task-1');
expect(service.enqueueBatchTask).toHaveBeenCalledWith('task-1', {
messageRecordId: 'record-1',
queuePriority: 'normal',
});
expect(prisma.smsReceiptRecord.create).not.toHaveBeenCalled();
});
@@ -1814,7 +1819,10 @@ describe('SendChainService', () => {
where: { id: 'record-1' },
data: { status: 'queued', signatureId: 'sig-1' },
});
expect(service.enqueueBatchTask).toHaveBeenCalledWith('task-1');
expect(service.enqueueBatchTask).toHaveBeenCalledWith('task-1', {
messageRecordId: 'record-1',
queuePriority: 'normal',
});
expect(prisma.smsReceiptRecord.create).not.toHaveBeenCalled();
});
@@ -2090,6 +2098,26 @@ describe('SendChainService', () => {
});
});
it('enqueues a freshly persisted inbound message without querying the task and message again', async () => {
const { service, prisma } = createService();
const add = jest.fn().mockResolvedValue(undefined);
service['getSendQueue'] = jest.fn().mockReturnValue({ add });
await expect(service.enqueueBatchTask('task-1', {
messageRecordId: 'record-1',
queuePriority: 'priority',
})).resolves.toEqual({ taskId: 'task-1', enqueued: 1 });
expect(prisma.smsBatchTask.findUnique).not.toHaveBeenCalled();
expect(prisma.smsMessageRecord.findMany).not.toHaveBeenCalled();
expect(add).toHaveBeenCalledWith('send-message', { messageRecordId: 'record-1' }, {
jobId: 'record-1',
attempts: 3,
priority: 1,
});
expect(prisma.smsBatchTask.update).toHaveBeenCalledWith({ where: { id: 'task-1' }, data: { status: 'queued' } });
});
it('reuses persisted carrier and province without querying routing dictionaries again', async () => {
const { service, prisma } = createService();
service['identifyCarrier'] = jest.fn();