This commit is contained in:
ditiqi 2025-03-02 17:01:01 +08:00
parent d0a5571b50
commit 41a06f21ae
7 changed files with 72 additions and 217 deletions

View File

@ -1,11 +0,0 @@
import { z } from 'zod';
export const EnrollSchema = z.object({
studentId: z.string(),
postId: z.string(),
});
export const UnenrollSchema = z.object({
studentId: z.string(),
postId: z.string(),
});

View File

@ -1,9 +0,0 @@
import { Module } from '@nestjs/common';
import { EnrollmentRouter } from './enrollment.router';
import { EnrollmentService } from './enrollment.service';
@Module({
exports: [EnrollmentRouter, EnrollmentService],
providers: [EnrollmentRouter, EnrollmentService],
})
export class EnrollmentModule {}

View File

@ -1,54 +0,0 @@
import { Injectable } from '@nestjs/common';
import { TrpcService } from '@server/trpc/trpc.service';
import { Prisma, UpdateOrderSchema } from '@nice/common';
import { EnrollmentService } from './enrollment.service';
import { z, ZodType } from 'zod';
import { EnrollSchema, UnenrollSchema } from './enroll.schema';
const EnrollmentCreateArgsSchema: ZodType<Prisma.EnrollmentCreateArgs> = z.any()
const EnrollmentCreateManyInputSchema: ZodType<Prisma.EnrollmentCreateManyInput> = z.any()
const EnrollmentDeleteManyArgsSchema: ZodType<Prisma.EnrollmentDeleteManyArgs> = z.any()
const EnrollmentFindManyArgsSchema: ZodType<Prisma.EnrollmentFindManyArgs> = z.any()
const EnrollmentFindFirstArgsSchema: ZodType<Prisma.EnrollmentFindFirstArgs> = z.any()
const EnrollmentWhereInputSchema: ZodType<Prisma.EnrollmentWhereInput> = z.any()
const EnrollmentSelectSchema: ZodType<Prisma.EnrollmentSelect> = z.any()
@Injectable()
export class EnrollmentRouter {
constructor(
private readonly trpc: TrpcService,
private readonly enrollmentService: EnrollmentService,
) { }
router = this.trpc.router({
findFirst: this.trpc.procedure
.input(EnrollmentFindFirstArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword
.query(async ({ input }) => {
return await this.enrollmentService.findFirst(input);
}),
findMany: this.trpc.procedure
.input(EnrollmentFindManyArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword
.query(async ({ input }) => {
return await this.enrollmentService.findMany(input);
}),
findManyWithCursor: this.trpc.protectProcedure
.input(z.object({
cursor: z.any().nullish(),
take: z.number().nullish(),
where: EnrollmentWhereInputSchema.nullish(),
select: EnrollmentSelectSchema.nullish()
}))
.query(async ({ ctx, input }) => {
const { staff } = ctx;
return await this.enrollmentService.findManyWithCursor(input);
}),
enroll: this.trpc.protectProcedure
.input(EnrollSchema)
.mutation(async ({ input }) => {
return await this.enrollmentService.enroll(input);
}),
unenroll: this.trpc.protectProcedure
.input(UnenrollSchema)
.mutation(async ({ input }) => {
return await this.enrollmentService.unenroll(input);
}),
});
}

View File

@ -1,78 +0,0 @@
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { BaseService } from '../base/base.service';
import {
UserProfile,
db,
ObjectType,
Prisma,
EnrollmentStatus,
} from '@nice/common';
import { z } from 'zod';
import { EnrollSchema, UnenrollSchema } from './enroll.schema';
import EventBus, { CrudOperation } from '@server/utils/event-bus';
@Injectable()
export class EnrollmentService extends BaseService<Prisma.EnrollmentDelegate> {
constructor() {
super(db, ObjectType.COURSE);
}
async enroll(params: z.infer<typeof EnrollSchema>) {
const { studentId, postId } = params;
const result = await db.$transaction(async (tx) => {
// 检查是否已经报名
const existing = await tx.enrollment.findUnique({
where: {
studentId_postId: {
studentId: studentId,
postId: postId,
},
},
});
if (existing) {
throw new ConflictException('Already enrolled in this post');
}
// 创建报名记录
const enrollment = await tx.enrollment.create({
data: {
studentId: studentId,
postId: postId,
status: EnrollmentStatus.ACTIVE,
},
});
return enrollment;
});
EventBus.emit('dataChanged', {
type: ObjectType.ENROLLMENT,
operation: CrudOperation.CREATED,
data: result,
});
return result;
}
async unenroll(params: z.infer<typeof UnenrollSchema>) {
const { studentId, postId } = params;
const result = await db.enrollment.update({
where: {
studentId_postId: {
studentId,
postId,
},
},
data: {
status: EnrollmentStatus.CANCELLED,
},
});
EventBus.emit('dataChanged', {
type: ObjectType.ENROLLMENT,
operation: CrudOperation.UPDATED,
data: result,
});
return result;
}
}

View File

@ -183,7 +183,7 @@ export class PostService extends BaseTreeService<Prisma.PostDelegate> {
pageSize?: number; pageSize?: number;
where?: Prisma.PostWhereInput; where?: Prisma.PostWhereInput;
orderBy?: OrderByArgs<(typeof db.post)['findMany']>; orderBy?: OrderByArgs<(typeof db.post)['findMany']>;
select?: Prisma.PostSelect<DefaultArgs>; select?: Prisma.PostSelect;
}): Promise<{ }): Promise<{
items: { items: {
id: string; id: string;

View File

@ -3,68 +3,76 @@ import { TrpcService } from '@server/trpc/trpc.service';
import { Prisma, UpdateOrderSchema } from '@nice/common'; import { Prisma, UpdateOrderSchema } from '@nice/common';
import { ResourceService } from './resource.service'; import { ResourceService } from './resource.service';
import { z, ZodType } from 'zod'; import { z, ZodType } from 'zod';
const ResourceCreateArgsSchema: ZodType<Prisma.ResourceCreateArgs> = z.any()
const ResourceCreateManyInputSchema: ZodType<Prisma.ResourceCreateManyInput> = z.any() const ResourceCreateArgsSchema: ZodType<Prisma.ResourceCreateArgs> = z.any();
const ResourceDeleteManyArgsSchema: ZodType<Prisma.ResourceDeleteManyArgs> = z.any() const ResourceCreateManyInputSchema: ZodType<Prisma.ResourceCreateManyInput> =
const ResourceFindManyArgsSchema: ZodType<Prisma.ResourceFindManyArgs> = z.any() z.any();
const ResourceFindFirstArgsSchema: ZodType<Prisma.ResourceFindFirstArgs> = z.any() const ResourceDeleteManyArgsSchema: ZodType<Prisma.ResourceDeleteManyArgs> =
const ResourceWhereInputSchema: ZodType<Prisma.ResourceWhereInput> = z.any() z.any();
const ResourceSelectSchema: ZodType<Prisma.ResourceSelect> = z.any() const ResourceFindManyArgsSchema: ZodType<Prisma.ResourceFindManyArgs> =
z.any();
const ResourceFindFirstArgsSchema: ZodType<Prisma.ResourceFindFirstArgs> =
z.any();
const ResourceWhereInputSchema: ZodType<Prisma.ResourceWhereInput> = z.any();
const ResourceSelectSchema: ZodType<Prisma.ResourceSelect> = z.any();
@Injectable() @Injectable()
export class ResourceRouter { export class ResourceRouter {
constructor( constructor(
private readonly trpc: TrpcService, private readonly trpc: TrpcService,
private readonly resourceService: ResourceService, private readonly resourceService: ResourceService,
) { } ) {}
router = this.trpc.router({ router = this.trpc.router({
create: this.trpc.protectProcedure create: this.trpc.protectProcedure
.input(ResourceCreateArgsSchema) .input(ResourceCreateArgsSchema)
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
const { staff } = ctx; const { staff } = ctx;
return await this.resourceService.create(input, { staff }); return await this.resourceService.create(input, { staff });
}), }),
createMany: this.trpc.protectProcedure.input(z.array(ResourceCreateManyInputSchema)) createMany: this.trpc.protectProcedure
.mutation(async ({ ctx, input }) => { .input(z.array(ResourceCreateManyInputSchema))
const { staff } = ctx; .mutation(async ({ ctx, input }) => {
const { staff } = ctx;
return await this.resourceService.createMany({ data: input }, staff); return await this.resourceService.createMany({ data: input }, staff);
}), }),
deleteMany: this.trpc.procedure deleteMany: this.trpc.procedure
.input(ResourceDeleteManyArgsSchema) .input(ResourceDeleteManyArgsSchema)
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
return await this.resourceService.deleteMany(input); return await this.resourceService.deleteMany(input);
}), }),
findFirst: this.trpc.procedure findFirst: this.trpc.procedure
.input(ResourceFindFirstArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword .input(ResourceFindFirstArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword
.query(async ({ input }) => { .query(async ({ input }) => {
return await this.resourceService.findFirst(input); return await this.resourceService.findFirst(input);
}), }),
softDeleteByIds: this.trpc.protectProcedure softDeleteByIds: this.trpc.protectProcedure
.input(z.object({ ids: z.array(z.string()) })) // expect input according to the schema .input(z.object({ ids: z.array(z.string()) })) // expect input according to the schema
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
return this.resourceService.softDeleteByIds(input.ids); return this.resourceService.softDeleteByIds(input.ids);
}), }),
updateOrder: this.trpc.protectProcedure updateOrder: this.trpc.protectProcedure
.input(UpdateOrderSchema) .input(UpdateOrderSchema)
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
return this.resourceService.updateOrder(input); return this.resourceService.updateOrder(input);
}), }),
findMany: this.trpc.procedure findMany: this.trpc.procedure
.input(ResourceFindManyArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword .input(ResourceFindManyArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword
.query(async ({ input }) => { .query(async ({ input }) => {
return await this.resourceService.findMany(input); return await this.resourceService.findMany(input);
}), }),
findManyWithCursor: this.trpc.protectProcedure findManyWithCursor: this.trpc.protectProcedure
.input(z.object({ .input(
cursor: z.any().nullish(), z.object({
take: z.number().nullish(), cursor: z.any().nullish(),
where: ResourceWhereInputSchema.nullish(), take: z.number().nullish(),
select: ResourceSelectSchema.nullish() where: ResourceWhereInputSchema.nullish(),
})) select: ResourceSelectSchema.nullish(),
.query(async ({ ctx, input }) => { }),
const { staff } = ctx; )
return await this.resourceService.findManyWithCursor(input); .query(async ({ ctx, input }) => {
}), const { staff } = ctx;
}); return await this.resourceService.findManyWithCursor(input);
}),
});
} }

View File

@ -12,7 +12,6 @@
"outDir": "./dist", "outDir": "./dist",
"strict": true, "strict": true,
"esModuleInterop": true, "esModuleInterop": true,
"incremental": true, "incremental": true
// "skipLibCheck": true, }
}, }
}