114 lines
3.7 KiB
TypeScript
114 lines
3.7 KiB
TypeScript
import { Prisma } from '@prisma/client';
|
|
import { PrismaService } from '../../prisma/prisma.service';
|
|
import { messageWhere, clientUplinkView } from '../operations.helpers';
|
|
|
|
// Uplink record queries and the status-only runtime summary.
|
|
export class OperationsUplinkQueries {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
listUplinkMessages(query: {
|
|
tenantId?: string;
|
|
channelId?: string;
|
|
applicationId?: string;
|
|
phoneNumber?: string;
|
|
keyword?: string;
|
|
startTime?: string;
|
|
endTime?: string;
|
|
page?: number;
|
|
pageSize?: number;
|
|
}) {
|
|
return this.prisma.smsUplinkMessage.findMany({
|
|
where: {
|
|
tenantId: query.tenantId,
|
|
channelId: query.channelId,
|
|
applicationId: query.applicationId,
|
|
phoneNumber: query.phoneNumber ? { contains: query.phoneNumber } : undefined,
|
|
content: query.keyword ? { contains: query.keyword } : undefined,
|
|
receivedAt:
|
|
query.startTime || query.endTime
|
|
? {
|
|
gte: query.startTime ? new Date(query.startTime) : undefined,
|
|
lte: query.endTime ? new Date(query.endTime) : undefined,
|
|
}
|
|
: undefined,
|
|
},
|
|
include: {
|
|
tenant: true,
|
|
application: true,
|
|
channel: true,
|
|
messageRecord: { include: { application: true } },
|
|
matchCandidates: {
|
|
include: {
|
|
tenant: true,
|
|
application: true,
|
|
messageRecord: { include: { application: true, tenant: true, channel: true } },
|
|
},
|
|
orderBy: [{ status: 'asc' }, { confidence: 'desc' }, { createdAt: 'asc' }],
|
|
},
|
|
},
|
|
orderBy: { receivedAt: 'desc' },
|
|
skip: query.page && query.pageSize ? (query.page - 1) * query.pageSize : undefined,
|
|
take: query.pageSize ?? 500,
|
|
});
|
|
}
|
|
async listClientUplinkMessages(query: {
|
|
tenantId?: string;
|
|
applicationId?: string;
|
|
phoneNumber?: string;
|
|
keyword?: string;
|
|
startTime?: string;
|
|
endTime?: string;
|
|
page?: number;
|
|
pageSize?: number;
|
|
}) {
|
|
const items = await this.listUplinkMessages(query);
|
|
return items.map(clientUplinkView);
|
|
}
|
|
async listUplinkMessagesPage(
|
|
query: {
|
|
tenantId?: string;
|
|
channelId?: string;
|
|
applicationId?: string;
|
|
phoneNumber?: string;
|
|
keyword?: string;
|
|
startTime?: string;
|
|
endTime?: string;
|
|
page?: number;
|
|
pageSize?: number;
|
|
},
|
|
clientView = false,
|
|
) {
|
|
const page = Math.max(1, Math.floor(Number(query.page) || 1));
|
|
const pageSize = Math.min(100, Math.max(1, Math.floor(Number(query.pageSize) || 10)));
|
|
const where: Prisma.SmsUplinkMessageWhereInput = {
|
|
tenantId: query.tenantId,
|
|
channelId: query.channelId,
|
|
applicationId: query.applicationId,
|
|
phoneNumber: query.phoneNumber ? { contains: query.phoneNumber } : undefined,
|
|
content: query.keyword ? { contains: query.keyword } : undefined,
|
|
receivedAt:
|
|
query.startTime || query.endTime
|
|
? {
|
|
gte: query.startTime ? new Date(query.startTime) : undefined,
|
|
lte: query.endTime ? new Date(query.endTime) : undefined,
|
|
}
|
|
: undefined,
|
|
};
|
|
const [rawItems, total] = await Promise.all([
|
|
this.listUplinkMessages({ ...query, page, pageSize }),
|
|
this.prisma.smsUplinkMessage.count({ where }),
|
|
]);
|
|
return {
|
|
items: clientView ? rawItems.map(clientUplinkView) : rawItems,
|
|
total,
|
|
page,
|
|
pageSize,
|
|
};
|
|
}
|
|
async monitor(query: { tenantId?: string; channelId?: string }) {
|
|
const where = messageWhere(query);
|
|
const byStatus = await this.prisma.smsMessageRecord.groupBy({ by: ['status'], where, _count: { _all: true } });
|
|
return { byStatus };
|
|
}
|
|
}
|