doctor-mail/apps/server/src/models/visit/visit.router.ts

69 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-12-30 08:26:40 +08:00
import { Injectable } from '@nestjs/common';
import { TrpcService } from '@server/trpc/trpc.service';
2025-01-06 08:45:23 +08:00
import { Prisma } from '@nice/common';
2024-12-30 08:26:40 +08:00
import { VisitService } from './visit.service';
import { z, ZodType } from 'zod';
2025-01-24 00:19:02 +08:00
import { getClientIp } from './utils';
const VisitCreateArgsSchema: ZodType<Prisma.VisitCreateArgs> = z.any();
2025-01-24 17:39:41 +08:00
const VisitDeleteArgsSchema: ZodType<Prisma.VisitDeleteArgs> = z.any();
2025-01-24 00:19:02 +08:00
const VisitCreateManyInputSchema: ZodType<Prisma.VisitCreateManyInput> =
z.any();
const VisitDeleteManyArgsSchema: ZodType<Prisma.VisitDeleteManyArgs> = z.any();
2024-12-30 08:26:40 +08:00
@Injectable()
export class VisitRouter {
2025-01-24 00:19:02 +08:00
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;
2024-12-30 08:26:40 +08:00
2025-01-24 00:19:02 +08:00
return await this.visitService.createMany({ data: input }, staff);
}),
deleteMany: this.trpc.procedure
.input(VisitDeleteManyArgsSchema)
2025-01-24 17:39:41 +08:00
.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);
2025-01-24 00:19:02 +08:00
}),
});
2024-12-30 08:26:40 +08:00
}