Skip to content

Commit

Permalink
#103 Add new mutation to soft delete a quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Aug 14, 2024
1 parent d60f193 commit 98c3616
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ const typeDefs = gql`
type Mutation {
createQuiz(type: QuizType!, date: Date!, files: [CreateQuizFile]): CreateQuizResult
completeQuiz(quizId: String!, completedBy: [String]!, score: Float!): CompleteQuizResult
deleteQuiz(quizId: String!, deletionReason: String!): Boolean
markQuizIllegible(quizId: String!): Boolean
}
`;
Expand Down
11 changes: 11 additions & 0 deletions src/quiz/quiz.gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,23 @@ async function markQuizIllegible(
return true;
}

async function deleteQuiz(
_: unknown,
{ quizId, deletionReason }: { quizId: string; deletionReason: string },
context: QuizlordContext,
): Promise<boolean> {
authorisationService.requireUserRole(context, 'ADMIN');
quizService.deleteQuiz(quizId, deletionReason, context.email);
return true;
}

export const quizQueries = {
quizzes,
quiz,
};
export const quizMutations = {
createQuiz,
completeQuiz,
deleteQuiz,
markQuizIllegible,
};
23 changes: 23 additions & 0 deletions src/quiz/quiz.persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ export class QuizPersistence {
});
}

async softDeleteQuiz(quizId: string, deletionReason: string, email: string) {
const user = await this.#prisma.client().user.findFirst({
where: {
email,
},
});

if (!user) {
throw new Error(`Unable to find user with email ${email}`);
}

return this.#prisma.client().quiz.update({
data: {
deletedAt: new Date(),
deletedByUserId: user.id,
deletionReason,
},
where: {
id: quizId,
},
});
}

async getRecentQuizCompletions({ limit }: { limit: number }) {
return this.#prisma.client().quizCompletion.findMany({
take: limit,
Expand Down
4 changes: 4 additions & 0 deletions src/quiz/quiz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,8 @@ export class QuizService {
userEmail,
});
}

async deleteQuiz(quizId: string, deletionReason: string, email: string) {
await this.#persistence.softDeleteQuiz(quizId, deletionReason, email);
}
}

0 comments on commit 98c3616

Please sign in to comment.