From ed50f3332253c1087c993ca90ff8317d452dd152 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Mon, 9 Dec 2024 20:19:10 +0700 Subject: [PATCH] feat: enhance Quiz schema with schedule-based quiz retrieval and error handling - Added logic to require 'scheduleId' for customers when retrieving quizzes, ensuring proper association with schedules. - Implemented error handling for missing 'scheduleId' and non-existent schedules, improving robustness and user feedback. - Refactored quiz retrieval logic for customers and center mentors to utilize the correct center mentor ID from the schedule, enhancing data integrity. - Updated comments for clarity regarding the quiz selection process based on user roles. --- src/Quiz/quiz.schema.ts | 44 +++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Quiz/quiz.schema.ts b/src/Quiz/quiz.schema.ts index d69d440..269487a 100644 --- a/src/Quiz/quiz.schema.ts +++ b/src/Quiz/quiz.schema.ts @@ -163,33 +163,50 @@ export class QuizSchema extends PothosSchema { if (!ctx.http.me) { throw new Error('Unauthorized') } - // use case 1: center mentor or center owner - if (ctx.http.me.role === Role.CENTER_MENTOR || ctx.http.me.role === Role.CENTER_OWNER) { - const centerMentor = await this.prisma.centerMentor.findUnique({ - where: { mentorId: ctx.http.me.id }, - }) + // use case 1: customer + if (ctx.http.me.role === Role.CUSTOMER) { // using pseudo random to get amount of quizzes based on userid as seed const random = getRandomWithSeed( parseInt(crypto.createHash('sha256').update(ctx.http.me.id).digest('hex'), 16), ) - if (!centerMentor) { + if (!args.scheduleId) { + throw new Error('Schedule ID is required') + } + // get schedule from scheduleId + const schedule = await this.prisma.schedule.findUnique({ + where: { id: args.scheduleId }, + include: { + managedService: true, + }, + }) + if (!schedule) { + throw new Error('Schedule not found') + } + // get centerMentorId from schedule + const centerMentorId = schedule.managedService.mentorId + if (!centerMentorId) { throw new Error('Center mentor not found') } const quizzes = await this.prisma.quiz.findMany({ ...query, where: { serviceId: args.serviceId, - centerMentorId: centerMentor.mentorId, + centerMentorId: centerMentorId, }, }) - // get amount of quizzes using nrOfQuestions and random index based on random - const randomIndex = Math.floor(random * quizzes.length) - return quizzes.slice(randomIndex, randomIndex + (quizzes[0].nrOfQuestions ?? 1)) + // get amount of questions using nrOfQuestions and random index based on random + const randomIndex = Math.floor(random * (quizzes[0]?.nrOfQuestions ?? 1)) + return quizzes.slice(randomIndex, randomIndex + (quizzes[0]?.nrOfQuestions ?? 1)) } - // use case 2: Customer - if (ctx.http.me.role === Role.CUSTOMER) { - // if scheduleId is provided, return quizzes for the schedule + // use case 2: center mentor or center owner + if (ctx.http.me.role === Role.CENTER_MENTOR || ctx.http.me.role === Role.CENTER_OWNER) { + const centerMentor = await this.prisma.centerMentor.findUnique({ + where: { mentorId: ctx.http.me.id }, + }) + if (!centerMentor) { + throw new Error('Center mentor not found') + } if (args.scheduleId) { const schedule = await this.prisma.schedule.findUnique({ where: { id: args.scheduleId }, @@ -201,6 +218,7 @@ export class QuizSchema extends PothosSchema { ...query, where: { serviceId: args.serviceId, + centerMentorId: centerMentor.mentorId, }, }) }