fix: remove truncated management totals

This commit is contained in:
hectorzhao
2026-07-12 10:56:00 +08:00
parent 24cb632a45
commit 390b970032
19 changed files with 107 additions and 80 deletions
+14 -1
View File
@@ -16,7 +16,6 @@ function createPrismaMock() {
interfaceType: 'cmpp20',
queuePriority: 'normal',
tenant: { id: 'tenant-1', name: '租户A', code: 'TENANT-A' },
messageRecords: [{ status: 'delivered' }, { status: 'undelivered' }],
}]),
findUnique: jest.fn().mockResolvedValue({
id: 'app-1',
@@ -106,6 +105,12 @@ function createPrismaMock() {
update: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'downstream-1', ...data })),
updateMany: jest.fn().mockResolvedValue({ count: 0 }),
},
smsMessageRecord: {
groupBy: jest.fn().mockResolvedValue([
{ applicationId: 'app-1', status: 'delivered', _count: { _all: 1 } },
{ applicationId: 'app-1', status: 'undelivered', _count: { _all: 1 } },
]),
},
smsChannel: {
findFirst: jest.fn().mockResolvedValue({
id: 'channel-1',
@@ -177,6 +182,14 @@ describe('SmsConfigService', () => {
cmppConnections: [expect.objectContaining({ connectionId: 'gateway-1-1', account: '100001' })],
}),
]);
expect(prisma.smsApplication.findMany).toHaveBeenCalledWith(expect.objectContaining({
include: { tenant: true, ipAllowlist: true },
}));
expect(prisma.smsApplication.findMany.mock.calls[0][0]).not.toHaveProperty('take');
expect(prisma.smsMessageRecord.groupBy).toHaveBeenCalledWith(expect.objectContaining({
by: ['applicationId', 'status'],
_count: { _all: true },
}));
});
it('returns CMPP params from persisted application and channel config', async () => {
+27 -27
View File
@@ -123,7 +123,7 @@ export class SmsConfigService {
if (query.includeConnections) {
await this.markTimedOutDownstreamConnections();
}
return this.prisma.smsApplication.findMany({
const applications = await this.prisma.smsApplication.findMany({
where: {
tenantId: query.tenantId,
OR: query.keyword ? [
@@ -134,32 +134,36 @@ export class SmsConfigService {
include: {
tenant: true,
ipAllowlist: true,
messageRecords: { where: { queuedAt: { gte: startOfToday() } }, take: 1000 },
},
orderBy: { createdAt: 'desc' },
take: 100,
}).then(async (applications) => {
if (!query.includeConnections) {
return applications;
}
const applicationIds = applications.map((application) => application.id);
const connections = await this.prisma.cmppDownstreamConnection.findMany({
});
if (!query.includeConnections) {
return applications;
}
const applicationIds = applications.map((application) => application.id);
const [connections, messageStats] = await Promise.all([
this.prisma.cmppDownstreamConnection.findMany({
where: { applicationId: { in: applicationIds } },
orderBy: { updatedAt: 'desc' },
take: 500,
});
return applications.map((application) => {
const appConnections = connections.filter((connection) => connection.applicationId === application.id);
const todayTotal = application.messageRecords.length;
const delivered = application.messageRecords.filter((message) => message.status === 'delivered').length;
return {
...application,
cmppConnections: appConnections,
cmppStatus: normalizeApplicationCmppStatus(appConnections, application.status),
sentToday: todayTotal,
deliveryRate: todayTotal > 0 ? Number(((delivered / todayTotal) * 100).toFixed(1)) : 0,
};
});
}),
this.prisma.smsMessageRecord.groupBy({
by: ['applicationId', 'status'],
where: { applicationId: { in: applicationIds }, queuedAt: { gte: startOfToday() } },
_count: { _all: true },
}),
]);
return applications.map((application) => {
const appConnections = connections.filter((connection) => connection.applicationId === application.id);
const appStats = messageStats.filter((item) => item.applicationId === application.id);
const todayTotal = appStats.reduce((sum, item) => sum + item._count._all, 0);
const delivered = appStats.find((item) => item.status === 'delivered')?._count._all ?? 0;
return {
...application,
cmppConnections: appConnections,
cmppStatus: normalizeApplicationCmppStatus(appConnections, application.status),
sentToday: todayTotal,
deliveryRate: todayTotal > 0 ? Number(((delivered / todayTotal) * 100).toFixed(1)) : 0,
};
});
}
@@ -366,7 +370,6 @@ export class SmsConfigService {
const connections = await this.prisma.cmppDownstreamConnection.findMany({
where: { applicationId },
orderBy: { updatedAt: 'desc' },
take: 100,
});
return {
application,
@@ -513,7 +516,6 @@ export class SmsConfigService {
},
include: { materials: true, tenant: true, application: true },
orderBy: { createdAt: 'desc' },
take: 100,
});
}
@@ -596,7 +598,6 @@ export class SmsConfigService {
},
include: { variables: true, application: true, tenant: true, signature: true },
orderBy: { createdAt: 'desc' },
take: 100,
});
}
@@ -696,7 +697,6 @@ export class SmsConfigService {
targetId,
},
orderBy: { createdAt: 'desc' },
take: 100,
});
}