import { Injectable } from '@nestjs/common'; import { TrpcService } from '@server/trpc/trpc.service'; import { Prisma } from '@nice/common'; import { VisitService } from './visit.service'; import { z, ZodType } from 'zod'; import { getClientIp } from './utils'; const VisitCreateArgsSchema: ZodType = z.any(); const VisitDeleteArgsSchema: ZodType = z.any(); const VisitCreateManyInputSchema: ZodType = z.any(); const VisitDeleteManyArgsSchema: ZodType = z.any(); @Injectable() export class VisitRouter { constructor( private readonly trpc: TrpcService, private readonly visitService: VisitService, ) {} router = this.trpc.router({ create: this.trpc.protectProcedure .input(VisitCreateArgsSchema) .mutation(async ({ ctx, input }) => { const { staff, req } = ctx; // 从请求中获取 IP const ip = getClientIp(req); const currentMeta = typeof input.data.meta === 'object' && input.data.meta !== null ? input.data.meta : {}; input.data.meta = { ...currentMeta, ip: ip || '', } as Prisma.InputJsonObject; // 明确指定类型 return await this.visitService.create(input, staff); }), createMany: this.trpc.protectProcedure .input(z.array(VisitCreateManyInputSchema)) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.visitService.createMany({ data: input }, staff); }), deleteMany: this.trpc.procedure .input(VisitDeleteManyArgsSchema) .mutation(async ({ ctx, input }) => { const { staff, req } = ctx; // 从请求中获取 IP if (!input?.where?.visitorId) { const ip = getClientIp(req); // Create a new where clause const newWhere = { ...input.where, meta: { path: ['ip'], equals: ip || '', }, }; // Update the input with the new where clause input = { ...input, where: newWhere, }; } return await this.visitService.deleteMany(input, staff); }), }); }