Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new resolver for marking quizzes illegible and update quiz list to allow filtering on this #96

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions prisma/migrations/20240622123027_quiz_notes/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- CreateEnum
CREATE TYPE "QuizNoteType" AS ENUM ('ILLEGIBLE');

-- CreateTable
CREATE TABLE "quiz_note" (
"id" TEXT NOT NULL,
"quiz_id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"note_type" "QuizNoteType" NOT NULL,
"submitted_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "quiz_note_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "quiz_note" ADD CONSTRAINT "quiz_note_quiz_id_fkey" FOREIGN KEY ("quiz_id") REFERENCES "quiz"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "quiz_note" ADD CONSTRAINT "quiz_note_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
19 changes: 19 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ model Quiz {

completions QuizCompletion[]
images QuizImage[]
notes QuizNote[]
uploadedByUser User @relation(fields: [uploadedByUserId], references: [id])

@@unique(fields: [date, type])
Expand Down Expand Up @@ -79,18 +80,36 @@ model QuizCompletionUser {
@@map("quiz_completion_user")
}

model QuizNote {
id String @id
quizId String @map("quiz_id")
userId String @map("user_id")
noteType QuizNoteType @map("note_type")
submittedAt DateTime @map("submitted_at")

quiz Quiz @relation(fields: [quizId], references: [id])
user User @relation(fields: [userId], references: [id])

@@map("quiz_note")
}

model User {
id String @id
email String
name String?

roles UserRole[]
quizCompletions QuizCompletionUser[]
quizNotes QuizNote[]
uploadedQuizzes Quiz[]

@@map("user")
}

enum QuizNoteType {
ILLEGIBLE
}

enum Role {
USER
ADMIN
Expand Down
16 changes: 16 additions & 0 deletions src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,28 @@ const typeDefs = gql`
averageScorePercentage: Float!
}

enum ExcludeIllegibleOptions {
"""
Exclude quizzes that have been marked as illegible by me.
"""
ME
"""
Exclude quizzes that have been marked as illegible by anyone.
"""
ANYONE
}

"Available filters for the quizzes query"
input QuizFilters {
"""
Optional list of user emails.
If provided, only quizzes completed by none of these users will be included in the results.
"""
excludeCompletedBy: [String]
"""
Optional option to exclude quizzes that have been marked as illegible.
"""
excludeIllegible: ExcludeIllegibleOptions
}

type Query {
Expand Down Expand Up @@ -171,6 +186,7 @@ const typeDefs = gql`
type Mutation {
createQuiz(type: QuizType!, date: Date!, files: [CreateQuizFile]): CreateQuizResult
completeQuiz(quizId: String!, completedBy: [String]!, score: Float!): CompleteQuizResult
markQuizIllegible(quizId: String!): Boolean
}
`;

Expand Down
1 change: 1 addition & 0 deletions src/quiz/quiz.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface QuizDetails {

export interface QuizFilters {
excludeCompletedBy?: string[];
excludeIllegible?: 'ME' | 'ANYONE';
}

export interface CreateQuizResult {
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 @@ -58,11 +58,22 @@ async function completeQuiz(
});
}

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

export const quizQueries = {
quizzes,
quiz,
};
export const quizMutations = {
createQuiz,
completeQuiz,
markQuizIllegible,
};
53 changes: 52 additions & 1 deletion src/quiz/quiz.persistence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Quiz, QuizImage } from '@prisma/client';
import { Quiz, QuizImage, QuizNoteType } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';

import { PrismaService } from '../database/prisma.service';
import { QuizFilters } from './quiz.dto';
Expand Down Expand Up @@ -70,6 +71,23 @@ export class QuizPersistence {
},
},
}),
...(filters.excludeIllegible === 'ME' && {
notes: {
none: {
noteType: 'ILLEGIBLE',
user: {
email: userEmail,
},
},
},
}),
...(filters.excludeIllegible === 'ANYONE' && {
notes: {
none: {
noteType: 'ILLEGIBLE',
},
},
}),
},
});

Expand Down Expand Up @@ -205,4 +223,37 @@ export class QuizPersistence {
});
return slicePagedResults(result, limit, afterId !== undefined);
}

async addQuizNote({
quizId,
noteType,
userEmail,
submittedAt,
}: {
quizId: string;
noteType: QuizNoteType;
userEmail: string;
submittedAt: Date;
}) {
const user = await this.#prisma.client().user.findFirst({
where: {
email: userEmail,
},
});

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

const noteId = uuidv4();
await this.#prisma.client().quizNote.create({
data: {
id: noteId,
noteType,
quizId,
userId: user.id,
submittedAt,
},
});
}
}
8 changes: 8 additions & 0 deletions src/quiz/quiz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,12 @@ export class QuizService {
},
};
}
async markQuizIllegible(quizId: string, userEmail: string): Promise<void> {
await this.#persistence.addQuizNote({
quizId,
noteType: 'ILLEGIBLE',
submittedAt: new Date(),
userEmail,
});
}
}
Loading