feat: implement AI suggestion

This commit is contained in:
2024-12-12 21:22:50 +07:00
parent 3652fda7d3
commit 68353d8985
7 changed files with 133 additions and 183 deletions

View File

@@ -190,6 +190,9 @@ export class QuizSchema extends PothosSchema {
if (!schedule) {
throw new Error('Schedule not found')
}
if (schedule.customerId !== ctx.http.me.id) {
throw new Error('Unauthorized')
}
// get centerMentorId from schedule
const centerMentorId = schedule.managedService.mentorId
if (!centerMentorId) {
@@ -202,6 +205,16 @@ export class QuizSchema extends PothosSchema {
centerMentorId: centerMentorId,
},
})
// check if user has already taken the quiz
const quizAttempt = await this.prisma.quizAttempt.findFirst({
where: {
userId: ctx.http.me.id,
quizId: quizzes[0]?.id,
},
})
if (quizAttempt) {
throw new Error('User has already taken the quiz')
}
// get amount of questions using nrOfQuestions and random index based on random
const randomIndex = quizzes.length > 0 ? Math.floor(random * quizzes.length) : 0
const nrOfQuestions = quizzes[0]?.nrOfQuestions ?? 1
@@ -426,10 +439,10 @@ export class QuizSchema extends PothosSchema {
try {
return await this.prisma.quizAttempt.create({
...query,
data: {
...args.data,
quiz: { connect: { id: args.data.quiz.connect.id } },
user: { connect: { id: ctx.http.me.id } },
data: {
...args.data,
quiz: { connect: { id: args.data.quiz.connect.id } },
user: { connect: { id: ctx.http.me.id } },
},
})
} catch (_error) {