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
+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,
});
}