import { Injectable, NotFoundException } from '@nestjs/common'; import type { EventType, Prisma } from '@prisma/client'; import { PrismaService } from '../../common/prisma/prisma.module'; import { serializeBigInt } from '../../common/decorators/current-user.decorator'; const DOMAIN_EVENT_TYPES: EventType[] = [ 'ORDER_STATUS', 'BENEFIT_LEDGER', 'STORE_AUDIT', 'TICKET_COLLAB', 'PROMO_TOUCH', ]; @Injectable() export class AdminDomainEventsService { constructor(private readonly prisma: PrismaService) {} async list(query: { page?: number; pageSize?: number; eventType?: EventType; refType?: string; refId?: string; from?: string; to?: string; }) { const page = query.page ?? 1; const pageSize = query.pageSize ?? 20; const where: Prisma.CommonEventWhereInput = { eventType: query.eventType ?? { in: DOMAIN_EVENT_TYPES }, }; if (query.refType) where.refType = query.refType; if (query.refId) where.refId = BigInt(query.refId); if (query.from || query.to) { where.createdAt = {}; if (query.from) where.createdAt.gte = new Date(query.from); if (query.to) where.createdAt.lte = new Date(query.to); } const [items, total] = await Promise.all([ this.prisma.commonEvent.findMany({ where, orderBy: { createdAt: 'desc' }, skip: (page - 1) * pageSize, take: pageSize, }), this.prisma.commonEvent.count({ where }), ]); return serializeBigInt({ items, total, page, pageSize }); } async detail(id: bigint) { const row = await this.prisma.commonEvent.findUnique({ where: { id } }); if (!row || !DOMAIN_EVENT_TYPES.includes(row.eventType)) { throw new NotFoundException('领域事件不存在'); } return serializeBigInt(row); } }