From b5565163596c38ad72c423a67feee2c3c9f81904 Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Tue, 1 Apr 2025 18:38:36 +0200 Subject: [PATCH] fix: merge + lint fixes --- backend/src/controllers/students.ts | 10 ++++---- backend/src/controllers/teachers.ts | 14 +++++------ .../src/data/questions/question-repository.ts | 2 +- backend/src/interfaces/answer.ts | 8 +++---- backend/src/interfaces/student-request.ts | 2 +- backend/src/services/questions.ts | 16 +++++-------- backend/src/services/students.ts | 23 +++++++++++-------- backend/src/services/teachers.ts | 4 ++-- backend/tests/controllers/students.test.ts | 16 ++++++------- backend/tests/controllers/teachers.test.ts | 17 +++++++------- .../test_assets/users/students.testdata.ts | 4 ++-- frontend/src/queries/themes.ts | 4 ++-- 12 files changed, 58 insertions(+), 62 deletions(-) diff --git a/backend/src/controllers/students.ts b/backend/src/controllers/students.ts index 0161ab58..7f00d640 100644 --- a/backend/src/controllers/students.ts +++ b/backend/src/controllers/students.ts @@ -33,7 +33,7 @@ export async function getStudentHandler(req: Request, res: Response): Promise { const username = req.body.username; const firstName = req.body.firstName; const lastName = req.body.lastName; @@ -45,7 +45,7 @@ export async function createStudentHandler(req: Request, res: Response) { res.json({ student }); } -export async function deleteStudentHandler(req: Request, res: Response) { +export async function deleteStudentHandler(req: Request, res: Response): Promise { const username = req.params.username; requireFields({ username }); @@ -125,7 +125,7 @@ export async function getStudentRequestsHandler(req: Request, res: Response): Pr } export async function getStudentRequestHandler(req: Request, res: Response): Promise { - const username = req.params.username as string; + const username = req.params.username; const classId = req.params.classId; requireFields({ username, classId }); @@ -133,8 +133,8 @@ export async function getStudentRequestHandler(req: Request, res: Response): Pro res.json({ request }); } -export async function deleteClassJoinRequestHandler(req: Request, res: Response) { - const username = req.params.username as string; +export async function deleteClassJoinRequestHandler(req: Request, res: Response): Promise { + const username = req.params.username; const classId = req.params.classId; requireFields({ username, classId }); diff --git a/backend/src/controllers/teachers.ts b/backend/src/controllers/teachers.ts index 89fc1a27..c7b05f9f 100644 --- a/backend/src/controllers/teachers.ts +++ b/backend/src/controllers/teachers.ts @@ -33,7 +33,7 @@ export async function getTeacherHandler(req: Request, res: Response): Promise { const username = req.body.username; const firstName = req.body.firstName; const lastName = req.body.lastName; @@ -45,7 +45,7 @@ export async function createTeacherHandler(req: Request, res: Response) { res.json({ teacher }); } -export async function deleteTeacherHandler(req: Request, res: Response) { +export async function deleteTeacherHandler(req: Request, res: Response): Promise { const username = req.params.username; requireFields({ username }); @@ -54,7 +54,7 @@ export async function deleteTeacherHandler(req: Request, res: Response) { } export async function getTeacherClassHandler(req: Request, res: Response): Promise { - const username = req.params.username as string; + const username = req.params.username; const full = req.query.full === 'true'; requireFields({ username }); @@ -64,7 +64,7 @@ export async function getTeacherClassHandler(req: Request, res: Response): Promi } export async function getTeacherStudentHandler(req: Request, res: Response): Promise { - const username = req.params.username as string; + const username = req.params.username; const full = req.query.full === 'true'; requireFields({ username }); @@ -74,7 +74,7 @@ export async function getTeacherStudentHandler(req: Request, res: Response): Pro } export async function getTeacherQuestionHandler(req: Request, res: Response): Promise { - const username = req.params.username as string; + const username = req.params.username; const full = req.query.full === 'true'; requireFields({ username }); @@ -83,7 +83,7 @@ export async function getTeacherQuestionHandler(req: Request, res: Response): Pr res.json({ questions }); } -export async function getStudentJoinRequestHandler(req: Request, res: Response) { +export async function getStudentJoinRequestHandler(req: Request, res: Response): Promise { const username = req.query.username as string; const classId = req.params.classId; requireFields({ username, classId }); @@ -92,7 +92,7 @@ export async function getStudentJoinRequestHandler(req: Request, res: Response) res.json({ joinRequests }); } -export async function updateStudentJoinRequestHandler(req: Request, res: Response) { +export async function updateStudentJoinRequestHandler(req: Request, res: Response): Promise { const studentUsername = req.query.studentUsername as string; const classId = req.params.classId; const accepted = req.body.accepted !== 'false'; // Default = true diff --git a/backend/src/data/questions/question-repository.ts b/backend/src/data/questions/question-repository.ts index 8f5081ca..2d165abc 100644 --- a/backend/src/data/questions/question-repository.ts +++ b/backend/src/data/questions/question-repository.ts @@ -55,7 +55,7 @@ export class QuestionRepository extends DwengoEntityRepository { }); } - public findAllByAuthor(author: Student): Promise { + public async findAllByAuthor(author: Student): Promise { return this.findAll({ where: { author }, orderBy: { timestamp: 'DESC' }, // New to old diff --git a/backend/src/interfaces/answer.ts b/backend/src/interfaces/answer.ts index 493fd3c0..1162a1d3 100644 --- a/backend/src/interfaces/answer.ts +++ b/backend/src/interfaces/answer.ts @@ -1,5 +1,5 @@ import { mapToUserDTO, UserDTO } from './user.js'; -import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from './question.js'; +import {mapToQuestionDTO, mapToQuestionDTOId, QuestionDTO, QuestionId} from './question.js'; import { Answer } from '../entities/questions/answer.entity.js'; export interface AnswerDTO { @@ -29,10 +29,10 @@ export interface AnswerId { sequenceNumber: number; } -export function mapToAnswerId(answer: AnswerDTO): AnswerId { +export function mapToAnswerDTOId(answer: Answer): AnswerId { return { author: answer.author.username, - toQuestion: mapToQuestionId(answer.toQuestion), - sequenceNumber: answer.sequenceNumber, + toQuestion: mapToQuestionDTOId(answer.toQuestion), + sequenceNumber: answer.sequenceNumber!, }; } diff --git a/backend/src/interfaces/student-request.ts b/backend/src/interfaces/student-request.ts index 12e56f0d..ec6dd41e 100644 --- a/backend/src/interfaces/student-request.ts +++ b/backend/src/interfaces/student-request.ts @@ -18,7 +18,7 @@ export function mapToStudentRequestDTO(request: ClassJoinRequest): StudentReques }; } -export function mapToStudentRequest(student: Student, cls: Class) { +export function mapToStudentRequest(student: Student, cls: Class): ClassJoinRequest { return getClassJoinRequestRepository().create({ requester: student, class: cls, diff --git a/backend/src/services/questions.ts b/backend/src/services/questions.ts index ac1095bf..589ae115 100644 --- a/backend/src/services/questions.ts +++ b/backend/src/services/questions.ts @@ -1,8 +1,8 @@ import { getAnswerRepository, getQuestionRepository } from '../data/repositories.js'; -import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js'; +import {mapToQuestionDTO, mapToQuestionDTOId, QuestionDTO, QuestionId} from '../interfaces/question.js'; import { Question } from '../entities/questions/question.entity.js'; import { Answer } from '../entities/questions/answer.entity.js'; -import { AnswerDTO, AnswerId, mapToAnswerDTO, mapToAnswerId } from '../interfaces/answer.js'; +import { AnswerDTO, AnswerId, mapToAnswerDTO, mapToAnswerDTOId } from '../interfaces/answer.js'; import { QuestionRepository } from '../data/questions/question-repository.js'; import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; import { mapToStudent } from '../interfaces/student.js'; @@ -15,13 +15,11 @@ export async function getAllQuestions(id: LearningObjectIdentifier, full: boolea return []; } - const questionsDTO: QuestionDTO[] = questions.map(mapToQuestionDTO); - if (full) { - return questionsDTO; + return questions.map(mapToQuestionDTO); } - return questionsDTO.map(mapToQuestionId); + return questions.map(mapToQuestionDTOId); } async function fetchQuestion(questionId: QuestionId): Promise { @@ -59,13 +57,11 @@ export async function getAnswersByQuestion(questionId: QuestionId, full: boolean return []; } - const answersDTO = answers.map(mapToAnswerDTO); - if (full) { - return answersDTO; + return answers.map(mapToAnswerDTO); } - return answersDTO.map(mapToAnswerId); + return answers.map(mapToAnswerDTOId); } export async function createQuestion(questionDTO: QuestionDTO): Promise { diff --git a/backend/src/services/students.ts b/backend/src/services/students.ts index 3eb93278..dde5bf0d 100644 --- a/backend/src/services/students.ts +++ b/backend/src/services/students.ts @@ -12,8 +12,13 @@ import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js'; import { getAllAssignments } from './assignments.js'; -import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js'; -import { mapToStudentRequest, mapToStudentRequestDTO } from '../interfaces/student-request.js'; +import { + mapToQuestionDTO, + mapToQuestionDTOId, + QuestionDTO, + QuestionId +} from '../interfaces/question.js'; +import {mapToStudentRequest, mapToStudentRequestDTO, StudentRequestDTO} from '../interfaces/student-request.js'; import { Student } from '../entities/users/student.entity.js'; import { NotFoundException } from '../exceptions/not-found-exception.js'; import { fetchClass } from './classes.js'; @@ -116,16 +121,14 @@ export async function getStudentQuestions(username: string, full: boolean): Prom const questionRepository = getQuestionRepository(); const questions = await questionRepository.findAllByAuthor(student); - const questionsDTO = questions.map(mapToQuestionDTO); - if (full) { - return questionsDTO; + return questions.map(mapToQuestionDTO); } - return questionsDTO.map(mapToQuestionId); + return questions.map(mapToQuestionDTOId); } -export async function createClassJoinRequest(username: string, classId: string) { +export async function createClassJoinRequest(username: string, classId: string): Promise { const requestRepo = getClassJoinRequestRepository(); const student = await fetchStudent(username); // Throws error if student not found @@ -136,7 +139,7 @@ export async function createClassJoinRequest(username: string, classId: string) return mapToStudentRequestDTO(request); } -export async function getJoinRequestsByStudent(username: string) { +export async function getJoinRequestsByStudent(username: string): Promise { const requestRepo = getClassJoinRequestRepository(); const student = await fetchStudent(username); @@ -145,7 +148,7 @@ export async function getJoinRequestsByStudent(username: string) { return requests.map(mapToStudentRequestDTO); } -export async function getJoinRequestByStudentClass(username: string, classId: string){ +export async function getJoinRequestByStudentClass(username: string, classId: string): Promise{ const requestRepo = getClassJoinRequestRepository(); const student = await fetchStudent(username); @@ -159,7 +162,7 @@ export async function getJoinRequestByStudentClass(username: string, classId: st return mapToStudentRequestDTO(request); } -export async function deleteClassJoinRequest(username: string, classId: string) { +export async function deleteClassJoinRequest(username: string, classId: string): Promise { const requestRepo = getClassJoinRequestRepository(); const student = await fetchStudent(username); diff --git a/backend/src/services/teachers.ts b/backend/src/services/teachers.ts index dc7f035c..ddd81a02 100644 --- a/backend/src/services/teachers.ts +++ b/backend/src/services/teachers.ts @@ -90,7 +90,7 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom return classes.map((cls) => cls.id); } -export async function getStudentsByTeacher(username: string, full: boolean) { +export async function getStudentsByTeacher(username: string, full: boolean): Promise { const classes: ClassDTO[] = await fetchClassesByTeacher(username); if (!classes || classes.length === 0) { @@ -141,7 +141,7 @@ export async function getJoinRequestsByClass(classId: string): Promise { +export async function updateClassJoinRequestStatus(studentUsername: string, classId: string, accepted = true): Promise { const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository(); const classRepo: ClassRepository = getClassRepository(); diff --git a/backend/tests/controllers/students.test.ts b/backend/tests/controllers/students.test.ts index 9b5858d8..cf72f28c 100644 --- a/backend/tests/controllers/students.test.ts +++ b/backend/tests/controllers/students.test.ts @@ -49,14 +49,14 @@ describe('Student controllers', () => { it('Student not found', async () => { req = { params: { username: 'doesnotexist' } }; - await expect(() => getStudentHandler(req as Request, res as Response)) + await expect(async () => getStudentHandler(req as Request, res as Response)) .rejects.toThrow(NotFoundException); }); it('No username', async () => { req = { params: {} }; - await expect(() => getStudentHandler(req as Request, res as Response)) + await expect(async () => getStudentHandler(req as Request, res as Response)) .rejects.toThrowError(BadRequestException); }); @@ -75,8 +75,6 @@ describe('Student controllers', () => { expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ student: expect.objectContaining(student) })); - console.log(jsonMock) - req = { params: { username: 'coolstudent' } }; await deleteStudentHandler(req as Request, res as Response); @@ -93,14 +91,14 @@ describe('Student controllers', () => { }, }; - await expect(() => createStudentHandler(req as Request, res as Response)) + await expect(async () => createStudentHandler(req as Request, res as Response)) .rejects.toThrowError(EntityAlreadyExistsException); }); it('Create student no body', async () => { req = { body: {} }; - await expect(() => createStudentHandler(req as Request, res as Response)) + await expect(async () => createStudentHandler(req as Request, res as Response)) .rejects.toThrowError(BadRequestException); }); @@ -168,7 +166,7 @@ describe('Student controllers', () => { it('Deleting non-existent student', async () => { req = { params: { username: 'doesnotexist' } }; - await expect(() => deleteStudentHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); + await expect(async () => deleteStudentHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); }); it('Get join requests by student', async () => { @@ -220,7 +218,7 @@ describe('Student controllers', () => { body: { classId: 'id02' }, }; - await expect(() => createStudentRequestHandler(req as Request, res as Response)) + await expect(async () => createStudentRequestHandler(req as Request, res as Response)) .rejects.toThrow(ConflictException); }); @@ -233,7 +231,7 @@ describe('Student controllers', () => { expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); - await expect(() => deleteClassJoinRequestHandler(req as Request, res as Response)) + await expect(async () => deleteClassJoinRequestHandler(req as Request, res as Response)) .rejects.toThrow(NotFoundException); }); }); diff --git a/backend/tests/controllers/teachers.test.ts b/backend/tests/controllers/teachers.test.ts index dc6f05b2..a0b5f648 100644 --- a/backend/tests/controllers/teachers.test.ts +++ b/backend/tests/controllers/teachers.test.ts @@ -9,7 +9,6 @@ import { getStudentJoinRequestHandler, getTeacherClassHandler, getTeacherHandler, - getTeacherQuestionHandler, getTeacherStudentHandler, updateStudentJoinRequestHandler, } from '../../src/controllers/teachers.js'; @@ -46,14 +45,14 @@ describe('Teacher controllers', () => { it('Teacher not found', async () => { req = { params: { username: 'doesnotexist' } }; - await expect(() => getTeacherHandler(req as Request, res as Response)) + await expect(async () => getTeacherHandler(req as Request, res as Response)) .rejects.toThrow(NotFoundException); }); it('No username', async () => { req = { params: {} }; - await expect(() => getTeacherHandler(req as Request, res as Response)) + await expect(async () => getTeacherHandler(req as Request, res as Response)) .rejects.toThrowError(BadRequestException); }); @@ -88,14 +87,14 @@ describe('Teacher controllers', () => { }, }; - await expect(() => createTeacherHandler(req as Request, res as Response)) + await expect(async () => createTeacherHandler(req as Request, res as Response)) .rejects.toThrowError(EntityAlreadyExistsException); }); it('Create teacher no body', async () => { req = { body: {} }; - await expect(() => createTeacherHandler(req as Request, res as Response)) + await expect(async () => createTeacherHandler(req as Request, res as Response)) .rejects.toThrowError(BadRequestException); }); @@ -117,7 +116,7 @@ describe('Teacher controllers', () => { it('Deleting non-existent student', async () => { req = { params: { username: 'doesnotexist' } }; - await expect(() => deleteTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); + await expect(async () => deleteTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); }); it('Get teacher classes', async () => { @@ -193,9 +192,9 @@ describe('Teacher controllers', () => { body: { accepted: 'true' }, }; - const teacher = await updateStudentJoinRequestHandler(req as Request, res as Response); + await updateStudentJoinRequestHandler(req as Request, res as Response); - expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.objectContaining(teacher) })); + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); req = { params: { username: 'PinkFloyd' }, @@ -204,6 +203,6 @@ describe('Teacher controllers', () => { await getStudentRequestsHandler(req as Request, res as Response); const status: boolean = jsonMock.mock.lastCall?.[0].requests[0].status; - expect(status).toBeTruthy; + expect(status).toBeTruthy(); }); }); diff --git a/backend/tests/test_assets/users/students.testdata.ts b/backend/tests/test_assets/users/students.testdata.ts index 8bb3213b..ba34c728 100644 --- a/backend/tests/test_assets/users/students.testdata.ts +++ b/backend/tests/test_assets/users/students.testdata.ts @@ -1,4 +1,4 @@ -import { Connection, EntityManager, IDatabaseDriver } from '@mikro-orm/core'; +import { EntityManager} from '@mikro-orm/core'; import { Student } from '../../../src/entities/users/student.entity'; // 🔓 Ruwe testdata array — herbruikbaar in assertions @@ -14,6 +14,6 @@ export const TEST_STUDENTS = [ ]; // 🏗️ Functie die ORM entities maakt uit de data array -export function makeTestStudents(em: EntityManager>): Student[] { +export function makeTestStudents(em: EntityManager): Student[] { return TEST_STUDENTS.map((data) => em.create(Student, data)); } diff --git a/frontend/src/queries/themes.ts b/frontend/src/queries/themes.ts index fd1851fe..17264a96 100644 --- a/frontend/src/queries/themes.ts +++ b/frontend/src/queries/themes.ts @@ -4,7 +4,7 @@ import { ThemeController } from "@/controllers/themes.ts"; const themeController = new ThemeController(); -export function useThemeQuery(language: MaybeRefOrGetter){ +export function useThemeQuery(language: MaybeRefOrGetter) { return useQuery({ queryKey: ["themes", language], queryFn: () => { @@ -15,7 +15,7 @@ export function useThemeQuery(language: MaybeRefOrGetter){ }); } -export function useThemeHruidsQuery(themeKey: MaybeRefOrGetter){ +export function useThemeHruidsQuery(themeKey: MaybeRefOrGetter) { return useQuery({ queryKey: ["theme-hruids", themeKey], queryFn: () => themeController.getHruidsByKey(toValue(themeKey)!),