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
@@ -99,6 +99,11 @@ export class AdminOperationsController {
response.send(`\uFEFF${exported.content}`);
}
@Get('messages/:id')
getMessage(@Param('id') id: string) {
return this.operations.getMessage(id);
}
@Get('message-segment-audits')
messageSegmentAudits(
@Query('messageId') messageId?: string,
+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([{
+4
View File
@@ -48,6 +48,10 @@ export class OperationsService {
return this.messagesQueries.listMessagesPage(query);
}
async getMessage(id: string) {
return this.messagesQueries.getMessage(id);
}
async exportMessages(query: MessageQuery) {
return this.messagesQueries.exportMessages(query);
}
+65 -36
View File
@@ -45,44 +45,26 @@ async listMessagesPage(query: MessageQuery) {
const [items, total] = await Promise.all([
this.prisma.smsMessageRecord.findMany({
where,
include: {
select: {
id: true,
tenantId: true,
applicationId: true,
channelId: true,
messageId: true,
phoneNumber: true,
carrier: true,
province: true,
content: true,
hasDrainageContent: true,
drainageDetection: true,
billingUnits: true,
amountCents: true,
status: true,
submitStatus: true,
queuedAt: true,
tenant: { select: { id: true, name: true } },
application: { select: { id: true, name: true } },
channel: { select: { id: true, name: true, srcId: true } },
submitRecords: {
select: {
id: true,
submitId: true,
channelId: true,
channelGroupId: true,
channelGroupName: true,
gatewayMessageId: true,
submitStatus: true,
submittedAt: true,
createdAt: true,
channel: { select: { id: true, name: true } },
channelGroup: { select: { id: true, name: true } },
},
},
receiptRecords: {
select: {
id: true,
messageId: true,
gatewayMessageId: true,
receiptStatus: true,
rawStatus: true,
errorCode: true,
errorMessage: true,
deliveredAt: true,
createdAt: true,
channelId: true,
channel: { select: { id: true, name: true } },
},
},
downstreamDeliveries: {
where: { deliveryType: 'receipt' },
select: { id: true, deliveryType: true, status: true, deliveredAt: true, lastError: true },
},
channel: { select: { id: true, name: true } },
},
orderBy: [{ queuedAt: 'desc' }, { id: 'desc' }],
skip: (page - 1) * pageSize,
@@ -92,6 +74,53 @@ async listMessagesPage(query: MessageQuery) {
]);
return { items, total, page, pageSize };
}
async getMessage(id: string) {
const item = await this.prisma.smsMessageRecord.findUnique({
where: { id },
include: {
tenant: { select: { id: true, name: true } },
application: { select: { id: true, name: true } },
channel: { select: { id: true, name: true, srcId: true } },
submitRecords: {
select: {
id: true,
submitId: true,
channelId: true,
channelGroupId: true,
channelGroupName: true,
gatewayMessageId: true,
submitStatus: true,
submittedAt: true,
createdAt: true,
channel: { select: { id: true, name: true } },
channelGroup: { select: { id: true, name: true } },
},
},
receiptRecords: {
select: {
id: true,
messageId: true,
gatewayMessageId: true,
receiptStatus: true,
rawStatus: true,
errorCode: true,
errorMessage: true,
deliveredAt: true,
createdAt: true,
channelId: true,
channel: { select: { id: true, name: true } },
},
},
downstreamDeliveries: {
where: { deliveryType: 'receipt' },
select: { id: true, deliveryType: true, status: true, deliveredAt: true, lastError: true },
},
},
});
if (!item) throw new NotFoundException('Message record not found');
return item;
}
async exportMessages(query: MessageQuery) {
const items = await this.prisma.smsMessageRecord.findMany({
where: messageWhere(query),