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.
This commit is contained in:
2024-12-09 20:19:10 +07:00
parent aa9ce7a235
commit ed50f33322

View File

@@ -163,33 +163,50 @@ export class QuizSchema extends PothosSchema {
if (!ctx.http.me) { if (!ctx.http.me) {
throw new Error('Unauthorized') throw new Error('Unauthorized')
} }
// use case 1: center mentor or center owner // use case 1: customer
if (ctx.http.me.role === Role.CENTER_MENTOR || ctx.http.me.role === Role.CENTER_OWNER) { if (ctx.http.me.role === Role.CUSTOMER) {
const centerMentor = await this.prisma.centerMentor.findUnique({
where: { mentorId: ctx.http.me.id },
})
// using pseudo random to get amount of quizzes based on userid as seed // using pseudo random to get amount of quizzes based on userid as seed
const random = getRandomWithSeed( const random = getRandomWithSeed(
parseInt(crypto.createHash('sha256').update(ctx.http.me.id).digest('hex'), 16), 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') throw new Error('Center mentor not found')
} }
const quizzes = await this.prisma.quiz.findMany({ const quizzes = await this.prisma.quiz.findMany({
...query, ...query,
where: { where: {
serviceId: args.serviceId, serviceId: args.serviceId,
centerMentorId: centerMentor.mentorId, centerMentorId: centerMentorId,
}, },
}) })
// get amount of quizzes using nrOfQuestions and random index based on random // get amount of questions using nrOfQuestions and random index based on random
const randomIndex = Math.floor(random * quizzes.length) const randomIndex = Math.floor(random * (quizzes[0]?.nrOfQuestions ?? 1))
return quizzes.slice(randomIndex, randomIndex + (quizzes[0].nrOfQuestions ?? 1)) return quizzes.slice(randomIndex, randomIndex + (quizzes[0]?.nrOfQuestions ?? 1))
} }
// use case 2: Customer // use case 2: center mentor or center owner
if (ctx.http.me.role === Role.CUSTOMER) { if (ctx.http.me.role === Role.CENTER_MENTOR || ctx.http.me.role === Role.CENTER_OWNER) {
// if scheduleId is provided, return quizzes for the schedule 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) { if (args.scheduleId) {
const schedule = await this.prisma.schedule.findUnique({ const schedule = await this.prisma.schedule.findUnique({
where: { id: args.scheduleId }, where: { id: args.scheduleId },
@@ -201,6 +218,7 @@ export class QuizSchema extends PothosSchema {
...query, ...query,
where: { where: {
serviceId: args.serviceId, serviceId: args.serviceId,
centerMentorId: centerMentor.mentorId,
}, },
}) })
} }