chore: update subproject commit reference and enhance Quiz schema

- Updated the subproject commit reference in epess-database to the latest version.
- Enhanced QuizSchema by adding quizAttempt relation and submitQuiz mutation for improved quiz attempt handling.
- Updated Prisma types to include QuizAttempt, ensuring consistency across the application.
This commit is contained in:
2024-12-07 19:39:16 +07:00
parent 636be21a4c
commit 14a91a1885
4 changed files with 129 additions and 27 deletions

View File

@@ -71,6 +71,28 @@ export class QuizSchema extends PothosSchema {
})
}
@PothosRef()
quizAttempt() {
return this.builder.prismaObject('QuizAttempt', {
fields: (t) => ({
id: t.exposeID('id'),
quizId: t.exposeID('quizId'),
quiz: t.relation('quiz'),
userId: t.exposeID('userId'),
user: t.relation('user'),
score: t.exposeInt('score'),
questions: t.relation('questions'),
answers: t.exposeStringList('answers'),
createdAt: t.expose('createdAt', {
type: 'DateTime',
}),
updatedAt: t.expose('updatedAt', {
type: 'DateTime',
}),
}),
})
}
@PothosRef()
stringType() {
return this.builder.objectRef('StringType')
@@ -180,6 +202,41 @@ export class QuizSchema extends PothosSchema {
})
},
}),
submitQuiz: t.prismaField({
type: this.quizAttempt(),
args: {
data: t.arg({
type: this.builder.generator.getCreateInput('QuizAttempt', [
'id',
'createdAt',
'updatedAt',
'quiz',
'user',
'userId',
]),
}),
},
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Subscription is not allowed')
if (!ctx.http.me) throw new Error('User is not authenticated')
if (!args.data) throw new Error('Data is required')
if (!args.data.quiz?.connect?.id) throw new Error('Quiz ID is required')
// query the quiz to get the questions
const quiz = await this.prisma.quiz.findUnique({
where: { id: args.data.quiz.connect.id },
})
if (!quiz) throw new Error('Quiz not found')
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 } },
},
})
},
}),
}))
}
}

File diff suppressed because one or more lines are too long