72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import type { ActorType, EventType, ResourceBizType, ResourceMediaType, ResourceOwnerType } from '@prisma/client';
|
|
import { Prisma } from '@prisma/client';
|
|
import { PrismaService } from '../../common/prisma/prisma.module';
|
|
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
|
import type { EventListQueryDto, EventTimelineQueryDto } from './dto/common-query.dto';
|
|
import type { CreateEventDto } from './dto/common-mutate.dto';
|
|
|
|
@Injectable()
|
|
export class EventService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async create(dto: CreateEventDto) {
|
|
const event = await this.prisma.commonEvent.create({
|
|
data: {
|
|
eventType: dto.eventType as EventType,
|
|
refType: dto.refType,
|
|
refId: BigInt(dto.refId),
|
|
actorType: dto.actorType as ActorType | undefined,
|
|
actorId: dto.actorId ? BigInt(dto.actorId) : undefined,
|
|
status: dto.status,
|
|
param1: dto.param1,
|
|
param1Desc: dto.param1Desc,
|
|
param2: dto.param2,
|
|
param2Desc: dto.param2Desc,
|
|
param3: dto.param3,
|
|
param3Desc: dto.param3Desc,
|
|
amount1: dto.amount1,
|
|
amount2: dto.amount2,
|
|
remark: dto.remark,
|
|
extraJson: dto.extraJson as Prisma.InputJsonValue | undefined,
|
|
},
|
|
});
|
|
return serializeBigInt(event);
|
|
}
|
|
|
|
async list(query: EventListQueryDto) {
|
|
const page = query.page ?? 1;
|
|
const pageSize = query.pageSize ?? 20;
|
|
const where: Prisma.CommonEventWhereInput = {};
|
|
if (query.refType) where.refType = query.refType;
|
|
if (query.refId) where.refId = BigInt(query.refId);
|
|
if (query.eventType) where.eventType = query.eventType as Prisma.EnumEventTypeFilter['equals'];
|
|
|
|
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 timeline(query: EventTimelineQueryDto) {
|
|
const items = await this.prisma.commonEvent.findMany({
|
|
where: { refType: query.refType, refId: BigInt(query.refId) },
|
|
orderBy: { createdAt: 'asc' },
|
|
take: 200,
|
|
});
|
|
return serializeBigInt(items);
|
|
}
|
|
|
|
async detail(id: bigint) {
|
|
const event = await this.prisma.commonEvent.findUnique({ where: { id } });
|
|
if (!event) throw new NotFoundException('事件不存在');
|
|
return serializeBigInt(event);
|
|
}
|
|
}
|