From d18258f81ae9c1ccc8a6b5d8a0d653b273553393 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Tue, 10 Dec 2024 16:37:36 +0700 Subject: [PATCH] refactor: improve quiz retrieval logic with schedule validation - Added validation to ensure 'scheduleId' is provided when retrieving quizzes, enhancing error handling. - Implemented a check for the existence of the schedule before proceeding with quiz retrieval, improving robustness. - Reformatted code for better readability and consistency in the Quiz schema. --- src/Quiz/quiz.schema.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Quiz/quiz.schema.ts b/src/Quiz/quiz.schema.ts index 428c89d..a804f19 100644 --- a/src/Quiz/quiz.schema.ts +++ b/src/Quiz/quiz.schema.ts @@ -75,7 +75,7 @@ export class QuizSchema extends PothosSchema { quizAttempt() { return this.builder.prismaObject('QuizAttempt', { fields: (t) => ({ - id: t.exposeID('id'), + id: t.exposeID('id'), quizId: t.exposeID('quizId'), quiz: t.relation('quiz'), userId: t.exposeID('userId'), @@ -211,13 +211,22 @@ export class QuizSchema extends PothosSchema { if (!centerMentor) { throw new Error('Center mentor not found') } - return await this.prisma.quiz.findMany({ - ...query, - where: { - serviceId: args.serviceId, - centerMentorId: centerMentor.mentorId, - }, - }) + if (args.scheduleId) { + const schedule = await this.prisma.schedule.findUnique({ + where: { id: args.scheduleId }, + }) + if (!schedule) { + throw new Error('Schedule not found') + } + return await this.prisma.quiz.findMany({ + ...query, + where: { + serviceId: args.serviceId, + centerMentorId: centerMentor.mentorId, + }, + }) + } + throw new Error('Schedule ID is required') } }, }),