From d6171e09a7d34a0fd3fd6879bc830b929ee50286 Mon Sep 17 00:00:00 2001 From: Bunyawat Naunnak Date: Wed, 14 Aug 2024 02:03:03 +0700 Subject: [PATCH] feat: add key feature and use bun instead of package.lock --- src/app/users/shipping/[id]/page.tsx | 1 + src/server/api/routers/parcel.ts | 30 +++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/app/users/shipping/[id]/page.tsx b/src/app/users/shipping/[id]/page.tsx index 5e9da72..60f1845 100755 --- a/src/app/users/shipping/[id]/page.tsx +++ b/src/app/users/shipping/[id]/page.tsx @@ -17,6 +17,7 @@ const Home = async ({ params }: { params: { id: string } }) => { : (student_id as Student); const mockParcels: Parcel[] = await api.parcel.getRemain({ student_id: student?.student_id ?? STUDENT_ID, + project_id: params.id, }); return ( diff --git a/src/server/api/routers/parcel.ts b/src/server/api/routers/parcel.ts index 52cb6e3..78987bf 100755 --- a/src/server/api/routers/parcel.ts +++ b/src/server/api/routers/parcel.ts @@ -40,10 +40,11 @@ export const parcelRouter = createTRPCRouter({ .input( z.object({ student_id: z.string(), + project_id: z.string(), }), ) .query(async ({ ctx, input }) => { - const studentId = input.student_id; // Assuming you have the student's ID in the session. + const { student_id: studentId, project_id: projectId } = input; // Check if the student is in the project with ID `0000000000`. const isStudentInProject = await ctx.db.project_Student.findFirst({ @@ -53,15 +54,26 @@ export const parcelRouter = createTRPCRouter({ }, }); + // Check if the student is an admin. + const isAdmin = await ctx.db.student.findFirst({ + where: { + student_id: studentId, + isAdmin: true, + }, + }); + return await ctx.db.parcel.findMany({ - where: isStudentInProject - ? { available: true, type: "KEY" } - : { - available: true, - NOT: { - type: "KEY", - }, - }, + where: + isStudentInProject ?? isAdmin + ? { available: true, type: "KEY" } + : projectId === "0000000000" + ? { available: true, type: "KEY" } + : { + available: true, + NOT: { + type: "KEY", + }, + }, }); }),