Merge branch 'dev' into fix/testdata-niet-meer-correct-opgezet
This commit is contained in:
		
						commit
						47e47323d3
					
				
					 24 changed files with 574 additions and 6008 deletions
				
			
		|  | @ -62,6 +62,11 @@ export async function getAllSubmissionsHandler(req: Request, res: Response): Pro | |||
| 
 | ||||
| // TODO: gerald moet nog dingen toevoegen aan de databank voor dat dit gefinaliseerd kan worden
 | ||||
| export async function createSubmissionHandler(req: Request, res: Response): Promise<void> { | ||||
|     const submitter = req.body.submitter; | ||||
|     const usernameSubmitter = req.body.submitter.username; | ||||
|     const group = req.body.group; | ||||
|     requireFields({ group, submitter, usernameSubmitter }); | ||||
| 
 | ||||
|     const submissionDTO = req.body as SubmissionDTO; | ||||
|     const submission = await createSubmission(submissionDTO); | ||||
| 
 | ||||
|  |  | |||
|  | @ -7,7 +7,6 @@ import { | |||
|     getJoinRequestsByClass, | ||||
|     getStudentsByTeacher, | ||||
|     getTeacher, | ||||
|     getTeacherQuestions, | ||||
|     updateClassJoinRequestStatus, | ||||
| } from '../services/teachers.js'; | ||||
| import { requireFields } from './error-helper.js'; | ||||
|  | @ -70,16 +69,6 @@ export async function getTeacherStudentHandler(req: Request, res: Response): Pro | |||
|     res.json({ students }); | ||||
| } | ||||
| 
 | ||||
| export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> { | ||||
|     const username = req.params.username; | ||||
|     const full = req.query.full === 'true'; | ||||
|     requireFields({ username }); | ||||
| 
 | ||||
|     const questions = await getTeacherQuestions(username, full); | ||||
| 
 | ||||
|     res.json({ questions }); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentJoinRequestHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classId; | ||||
|     requireFields({ classId }); | ||||
|  |  | |||
|  | @ -2,7 +2,6 @@ import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; | |||
| import { LearningObject } from '../../entities/content/learning-object.entity.js'; | ||||
| import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js'; | ||||
| import { Language } from '@dwengo-1/common/util/language'; | ||||
| import { Teacher } from '../../entities/users/teacher.entity.js'; | ||||
| 
 | ||||
| export class LearningObjectRepository extends DwengoEntityRepository<LearningObject> { | ||||
|     public async findByIdentifier(identifier: LearningObjectIdentifier): Promise<LearningObject | null> { | ||||
|  | @ -32,11 +31,4 @@ export class LearningObjectRepository extends DwengoEntityRepository<LearningObj | |||
|             } | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     public async findAllByTeacher(teacher: Teacher): Promise<LearningObject[]> { | ||||
|         return this.find( | ||||
|             { admins: teacher }, | ||||
|             { populate: ['admins'] } // Make sure to load admin relations
 | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -26,6 +26,9 @@ export class Assignment { | |||
|     @Property({ type: 'string' }) | ||||
|     learningPathHruid!: string; | ||||
| 
 | ||||
|     @Property({ type: 'datetime', nullable: true }) | ||||
|     deadline?: Date; | ||||
| 
 | ||||
|     @Enum({ | ||||
|         items: () => Language, | ||||
|     }) | ||||
|  |  | |||
|  | @ -20,6 +20,7 @@ export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | |||
|         description: assignment.description, | ||||
|         learningPath: assignment.learningPathHruid, | ||||
|         language: assignment.learningPathLanguage, | ||||
|         deadline: assignment.deadline ?? new Date(), | ||||
|         groups: assignment.groups.map((group) => mapToGroupDTO(group, assignment.within)), | ||||
|     }; | ||||
| } | ||||
|  | @ -31,6 +32,7 @@ export function mapToAssignment(assignmentData: AssignmentDTO, cls: Class): Assi | |||
|         description: assignmentData.description, | ||||
|         learningPathHruid: assignmentData.learningPath, | ||||
|         learningPathLanguage: languageMap[assignmentData.language], | ||||
|         deadline: assignmentData.deadline, | ||||
|         groups: [], | ||||
|     }); | ||||
| } | ||||
|  |  | |||
|  | @ -6,7 +6,6 @@ import { | |||
|     getStudentJoinRequestHandler, | ||||
|     getTeacherClassHandler, | ||||
|     getTeacherHandler, | ||||
|     getTeacherQuestionHandler, | ||||
|     getTeacherStudentHandler, | ||||
|     updateStudentJoinRequestHandler, | ||||
| } from '../controllers/teachers.js'; | ||||
|  | @ -27,8 +26,6 @@ router.get('/:username/classes', getTeacherClassHandler); | |||
| 
 | ||||
| router.get('/:username/students', getTeacherStudentHandler); | ||||
| 
 | ||||
| router.get('/:username/questions', getTeacherQuestionHandler); | ||||
| 
 | ||||
| router.get('/:username/joinRequests/:classId', getStudentJoinRequestHandler); | ||||
| 
 | ||||
| router.put('/:username/joinRequests/:classId/:studentUsername', updateStudentJoinRequestHandler); | ||||
|  |  | |||
|  | @ -42,7 +42,7 @@ export async function fetchStudent(username: string): Promise<Student> { | |||
|     const user = await studentRepository.findByUsername(username); | ||||
| 
 | ||||
|     if (!user) { | ||||
|         throw new NotFoundException('Student with username not found'); | ||||
|         throw new NotFoundException(`Student with username ${username} not found`); | ||||
|     } | ||||
| 
 | ||||
|     return user; | ||||
|  |  | |||
|  | @ -1,12 +1,5 @@ | |||
| import { | ||||
|     getClassJoinRequestRepository, | ||||
|     getClassRepository, | ||||
|     getLearningObjectRepository, | ||||
|     getQuestionRepository, | ||||
|     getTeacherRepository, | ||||
| } from '../data/repositories.js'; | ||||
| import { getClassJoinRequestRepository, getClassRepository, getTeacherRepository } from '../data/repositories.js'; | ||||
| import { mapToClassDTO } from '../interfaces/class.js'; | ||||
| import { mapToQuestionDTO, mapToQuestionDTOId } from '../interfaces/question.js'; | ||||
| import { mapToTeacher, mapToTeacherDTO } from '../interfaces/teacher.js'; | ||||
| import { Teacher } from '../entities/users/teacher.entity.js'; | ||||
| import { fetchStudent } from './students.js'; | ||||
|  | @ -15,10 +8,6 @@ import { mapToStudentRequestDTO } from '../interfaces/student-request.js'; | |||
| import { TeacherRepository } from '../data/users/teacher-repository.js'; | ||||
| import { ClassRepository } from '../data/classes/class-repository.js'; | ||||
| import { Class } from '../entities/classes/class.entity.js'; | ||||
| import { LearningObjectRepository } from '../data/content/learning-object-repository.js'; | ||||
| import { LearningObject } from '../entities/content/learning-object.entity.js'; | ||||
| import { QuestionRepository } from '../data/questions/question-repository.js'; | ||||
| import { Question } from '../entities/questions/question.entity.js'; | ||||
| import { ClassJoinRequestRepository } from '../data/classes/class-join-request-repository.js'; | ||||
| import { Student } from '../entities/users/student.entity.js'; | ||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
|  | @ -26,7 +15,6 @@ import { addClassStudent, fetchClass, getClassStudentsDTO } from './classes.js'; | |||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||
| import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||
| import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | ||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||
| import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request'; | ||||
| import { ClassStatus } from '@dwengo-1/common/util/class-join-request'; | ||||
| import { ConflictException } from '../exceptions/conflict-exception.js'; | ||||
|  | @ -119,28 +107,6 @@ export async function getStudentsByTeacher(username: string, full: boolean): Pro | |||
|     return students.map((student) => student.username); | ||||
| } | ||||
| 
 | ||||
| export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { | ||||
|     const teacher: Teacher = await fetchTeacher(username); | ||||
| 
 | ||||
|     // Find all learning objects that this teacher manages
 | ||||
|     const learningObjectRepository: LearningObjectRepository = getLearningObjectRepository(); | ||||
|     const learningObjects: LearningObject[] = await learningObjectRepository.findAllByTeacher(teacher); | ||||
| 
 | ||||
|     if (!learningObjects || learningObjects.length === 0) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     // Fetch all questions related to these learning objects
 | ||||
|     const questionRepository: QuestionRepository = getQuestionRepository(); | ||||
|     const questions: Question[] = await questionRepository.findAllByLearningObjects(learningObjects); | ||||
| 
 | ||||
|     if (full) { | ||||
|         return questions.map(mapToQuestionDTO); | ||||
|     } | ||||
| 
 | ||||
|     return questions.map(mapToQuestionDTOId); | ||||
| } | ||||
| 
 | ||||
| export async function getJoinRequestsByClass(classId: string): Promise<ClassJoinRequestDTO[]> { | ||||
|     const classRepository: ClassRepository = getClassRepository(); | ||||
|     const cls: Class | null = await classRepository.findById(classId); | ||||
|  |  | |||
							
								
								
									
										76
									
								
								backend/tests/controllers/assignments.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								backend/tests/controllers/assignments.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,76 @@ | |||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { getAssignmentHandler, getAllAssignmentsHandler, getAssignmentsSubmissionsHandler } from '../../src/controllers/assignments.js'; | ||||
| import { NotFoundException } from '../../src/exceptions/not-found-exception'; | ||||
| import { getClass01 } from '../test_assets/classes/classes.testdata'; | ||||
| import { getAssignment01 } from '../test_assets/assignments/assignments.testdata'; | ||||
| 
 | ||||
| function createRequestObject( | ||||
|     classid: string, | ||||
|     assignmentid: string | ||||
| ): { | ||||
|     query: { full: string }; | ||||
|     params: { classid: string; id: string }; | ||||
| } { | ||||
|     return { | ||||
|         params: { | ||||
|             classid: classid, | ||||
|             id: assignmentid, | ||||
|         }, | ||||
|         query: { | ||||
|             full: 'true', | ||||
|         }, | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| describe('Assignment controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
| 
 | ||||
|     let jsonMock: Mock; | ||||
|     let statusMock: Mock; | ||||
| 
 | ||||
|     beforeAll(async () => { | ||||
|         await setupTestApp(); | ||||
|     }); | ||||
| 
 | ||||
|     beforeEach(async () => { | ||||
|         jsonMock = vi.fn(); | ||||
|         statusMock = vi.fn().mockReturnThis(); | ||||
| 
 | ||||
|         res = { | ||||
|             json: jsonMock, | ||||
|             status: statusMock, | ||||
|         }; | ||||
|     }); | ||||
| 
 | ||||
|     it('return error non-existing assignment', async () => { | ||||
|         req = createRequestObject('doesnotexist', '43000'); // Should not exist
 | ||||
| 
 | ||||
|         await expect(async () => getAssignmentHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return an assignment', async () => { | ||||
|         const assignment = getAssignment01(); | ||||
|         req = createRequestObject(assignment.within.classId as string, (assignment.id ?? 1).toString()); | ||||
| 
 | ||||
|         await getAssignmentHandler(req as Request, res as Response); | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ assignment: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of assignments', async () => { | ||||
|         req = createRequestObject(getClass01().classId as string, 'irrelevant'); | ||||
| 
 | ||||
|         await getAllAssignmentsHandler(req as Request, res as Response); | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ assignments: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of submissions for an assignment', async () => { | ||||
|         const assignment = getAssignment01(); | ||||
|         req = createRequestObject(assignment.within.classId as string, (assignment.id ?? 1).toString()); | ||||
| 
 | ||||
|         await getAssignmentsSubmissionsHandler(req as Request, res as Response); | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() })); | ||||
|     }); | ||||
| }); | ||||
|  | @ -1,8 +1,17 @@ | |||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { | ||||
|     createClassHandler, | ||||
|     deleteClassHandler, | ||||
|     getAllClassesHandler, | ||||
|     getClassHandler, | ||||
|     getClassStudentsHandler, | ||||
|     getTeacherInvitationsHandler, | ||||
| } from '../../src/controllers/classes.js'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { createClassHandler, deleteClassHandler } from '../../src/controllers/classes'; | ||||
| 
 | ||||
| import { NotFoundException } from '../../src/exceptions/not-found-exception'; | ||||
| import { BadRequestException } from '../../src/exceptions/bad-request-exception'; | ||||
| import { getClass01 } from '../test_assets/classes/classes.testdata'; | ||||
| describe('Class controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
|  | @ -44,4 +53,71 @@ describe('Class controllers', () => { | |||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Error class not found', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist' }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getClassHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Error create a class without name', async () => { | ||||
|         req = { | ||||
|             body: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => createClassHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     it('return list of students', async () => { | ||||
|         req = { | ||||
|             params: { id: getClass01().classId as string }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassStudentsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ students: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Error students on a non-existent class', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist' }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getClassStudentsHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 200 and a list of teacher-invitations', async () => { | ||||
|         const classId = getClass01().classId as string; | ||||
|         req = { | ||||
|             params: { id: classId }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getTeacherInvitationsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Error teacher-invitations on a non-existent class', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist' }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getTeacherInvitationsHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of classes', async () => { | ||||
|         req = { | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await getAllClassesHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ classes: expect.anything() })); | ||||
|     }); | ||||
| }); | ||||
|  |  | |||
							
								
								
									
										140
									
								
								backend/tests/controllers/groups.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								backend/tests/controllers/groups.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,140 @@ | |||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { | ||||
|     createGroupHandler, | ||||
|     deleteGroupHandler, | ||||
|     getAllGroupsHandler, | ||||
|     getGroupHandler, | ||||
|     getGroupSubmissionsHandler, | ||||
| } from '../../src/controllers/groups.js'; | ||||
| import { NotFoundException } from '../../src/exceptions/not-found-exception'; | ||||
| import { getClass01 } from '../test_assets/classes/classes.testdata'; | ||||
| import { getAssignment01, getAssignment02 } from '../test_assets/assignments/assignments.testdata'; | ||||
| import { getTestGroup01 } from '../test_assets/assignments/groups.testdata'; | ||||
| 
 | ||||
| function createRequestObject( | ||||
|     classid: string, | ||||
|     assignmentid: string, | ||||
|     groupNumber: string | ||||
| ): { | ||||
|     query: { full: string }; | ||||
|     params: { classid: string; groupid: string; assignmentid: string }; | ||||
| } { | ||||
|     return { | ||||
|         params: { | ||||
|             classid: classid, | ||||
|             assignmentid: assignmentid, | ||||
|             groupid: groupNumber, | ||||
|         }, | ||||
|         query: { | ||||
|             full: 'true', | ||||
|         }, | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| describe('Group controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
| 
 | ||||
|     let jsonMock: Mock; | ||||
|     let statusMock: Mock; | ||||
| 
 | ||||
|     beforeAll(async () => { | ||||
|         await setupTestApp(); | ||||
|     }); | ||||
| 
 | ||||
|     beforeEach(async () => { | ||||
|         jsonMock = vi.fn(); | ||||
|         statusMock = vi.fn().mockReturnThis(); | ||||
| 
 | ||||
|         res = { | ||||
|             json: jsonMock, | ||||
|             status: statusMock, | ||||
|         }; | ||||
|     }); | ||||
| 
 | ||||
|     it('Error not found on a non-existing group', async () => { | ||||
|         req = { | ||||
|             params: { | ||||
|                 classid: 'id01', | ||||
|                 assignmentid: '1', | ||||
|                 groupid: '154981', // Should not exist
 | ||||
|             }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getGroupHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 not found on a non-existing assignment', async () => { | ||||
|         req = { | ||||
|             params: { | ||||
|                 classid: 'id01', | ||||
|                 assignmentid: '1000', // Should not exist
 | ||||
|                 groupid: '42000', // Should not exist
 | ||||
|             }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getGroupHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 not found ont a non-existing class', async () => { | ||||
|         req = { | ||||
|             params: { | ||||
|                 classid: 'doesnotexist', // Should not exist
 | ||||
|                 assignmentid: '1000', // Should not exist
 | ||||
|                 groupid: '42000', // Should not exist
 | ||||
|             }, | ||||
|             query: {}, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getGroupHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return an existing group', async () => { | ||||
|         const group = getTestGroup01(); | ||||
|         const classId = getClass01().classId as string; | ||||
|         req = createRequestObject(classId, (group.assignment.id ?? 1).toString(), (group.groupNumber ?? 1).toString()); | ||||
| 
 | ||||
|         await getGroupHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ group: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create and delete', async () => { | ||||
|         const assignment = getAssignment02(); | ||||
|         const classId = assignment.within.classId as string; | ||||
|         req = createRequestObject(classId, (assignment.id ?? 1).toString(), '1'); | ||||
|         req.body = { | ||||
|             members: ['Noordkaap', 'DireStraits'], | ||||
|         }; | ||||
| 
 | ||||
|         await createGroupHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         await deleteGroupHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ group: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return the submissions for a group', async () => { | ||||
|         const group = getTestGroup01(); | ||||
|         const classId = getClass01().classId as string; | ||||
|         req = createRequestObject(classId, (group.assignment.id ?? 1).toString(), (group.groupNumber ?? 1).toString()); | ||||
| 
 | ||||
|         await getGroupSubmissionsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of groups for an assignment', async () => { | ||||
|         const assignment = getAssignment01(); | ||||
|         const classId = assignment.within.classId as string; | ||||
|         req = createRequestObject(classId, (assignment.id ?? 1).toString(), '1'); | ||||
| 
 | ||||
|         await getAllGroupsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ groups: expect.anything() })); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										61
									
								
								backend/tests/controllers/submissions.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								backend/tests/controllers/submissions.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,61 @@ | |||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { getSubmissionHandler, getAllSubmissionsHandler } from '../../src/controllers/submissions.js'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { NotFoundException } from '../../src/exceptions/not-found-exception'; | ||||
| import { getClass02 } from '../test_assets/classes/classes.testdata'; | ||||
| 
 | ||||
| function createRequestObject( | ||||
|     hruid: string, | ||||
|     submissionNumber: string | ||||
| ): { | ||||
|     query: { language: string; version: string }; | ||||
|     params: { hruid: string; id: string }; | ||||
| } { | ||||
|     return { | ||||
|         params: { | ||||
|             hruid: hruid, | ||||
|             id: submissionNumber, | ||||
|         }, | ||||
|         query: { | ||||
|             language: 'en', | ||||
|             version: '1', | ||||
|         }, | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| describe('Submission controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
| 
 | ||||
|     let jsonMock: Mock; | ||||
|     let statusMock: Mock; | ||||
| 
 | ||||
|     beforeAll(async () => { | ||||
|         await setupTestApp(); | ||||
|     }); | ||||
| 
 | ||||
|     beforeEach(async () => { | ||||
|         jsonMock = vi.fn(); | ||||
|         statusMock = vi.fn().mockReturnThis(); | ||||
| 
 | ||||
|         res = { | ||||
|             json: jsonMock, | ||||
|             status: statusMock, | ||||
|         }; | ||||
|     }); | ||||
| 
 | ||||
|     it('error submission is not found', async () => { | ||||
|         req = createRequestObject('id01', '1000000'); | ||||
| 
 | ||||
|         await expect(async () => getSubmissionHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return a list of submissions for a learning object', async () => { | ||||
|         req = createRequestObject(getClass02().classId as string, 'irrelevant'); | ||||
| 
 | ||||
|         await getAllSubmissionsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() })); | ||||
|     }); | ||||
| }); | ||||
|  | @ -5,6 +5,12 @@ import { testLearningPath01, testLearningPath02, testLearningPathWithConditions | |||
| import { getClass01, getClass02, getClassWithTestleerlingAndTestleerkracht } from '../classes/classes.testdata'; | ||||
| 
 | ||||
| export function makeTestAssignemnts(em: EntityManager): Assignment[] { | ||||
|     const futureDate = new Date(); | ||||
|     futureDate.setDate(futureDate.getDate() + 7); | ||||
|     const pastDate = new Date(); | ||||
|     pastDate.setDate(pastDate.getDate() - 7); | ||||
|     const today = new Date(); | ||||
|     today.setHours(23, 59); | ||||
|     assignment01 = em.create(Assignment, { | ||||
|         id: 21000, | ||||
|         within: getClass01(), | ||||
|  | @ -12,6 +18,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] { | |||
|         description: 'reading', | ||||
|         learningPathHruid: testLearningPath02.hruid, | ||||
|         learningPathLanguage: testLearningPath02.language as Language, | ||||
|         deadline: today, | ||||
|         groups: [], | ||||
|     }); | ||||
| 
 | ||||
|  | @ -22,6 +29,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] { | |||
|         description: 'reading', | ||||
|         learningPathHruid: testLearningPath01.hruid, | ||||
|         learningPathLanguage: testLearningPath01.language as Language, | ||||
|         deadline: futureDate, | ||||
|         groups: [], | ||||
|     }); | ||||
| 
 | ||||
|  | @ -32,6 +40,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] { | |||
|         description: 'will be deleted', | ||||
|         learningPathHruid: testLearningPath02.hruid, | ||||
|         learningPathLanguage: testLearningPath02.language as Language, | ||||
|         deadline: pastDate, | ||||
|         groups: [], | ||||
|     }); | ||||
| 
 | ||||
|  | @ -42,6 +51,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] { | |||
|         description: 'with a description', | ||||
|         learningPathHruid: testLearningPath01.hruid, | ||||
|         learningPathLanguage: testLearningPath01.language as Language, | ||||
|         deadline: pastDate, | ||||
|         groups: [], | ||||
|     }); | ||||
| 
 | ||||
|  | @ -52,6 +62,7 @@ export function makeTestAssignemnts(em: EntityManager): Assignment[] { | |||
|         description: 'You have to do the testing learning path with a condition.', | ||||
|         learningPathHruid: testLearningPathWithConditions.hruid, | ||||
|         learningPathLanguage: testLearningPathWithConditions.language as Language, | ||||
|         deadline: futureDate, | ||||
|         groups: [], | ||||
|     }); | ||||
| 
 | ||||
|  |  | |||
|  | @ -7,6 +7,7 @@ export interface AssignmentDTO { | |||
|     description: string; | ||||
|     learningPath: string; | ||||
|     language: string; | ||||
|     deadline: Date; | ||||
|     groups: GroupDTO[] | string[][]; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,49 +1,30 @@ | |||
| <script setup lang="ts"> | ||||
|     import { ref, computed } from "vue"; | ||||
|     import { ref, watch } from "vue"; | ||||
|     import { deadlineRules } from "@/utils/assignment-rules.ts"; | ||||
| 
 | ||||
|     const date = ref(""); | ||||
|     const time = ref("23:59"); | ||||
|     const emit = defineEmits(["update:deadline"]); | ||||
|     const emit = defineEmits<(e: "update:deadline", value: Date) => void>(); | ||||
| 
 | ||||
|     const formattedDeadline = computed(() => { | ||||
|         if (!date.value || !time.value) return ""; | ||||
|         return `${date.value} ${time.value}`; | ||||
|     const datetime = ref(""); | ||||
| 
 | ||||
|     // Watch the datetime value and emit the update | ||||
|     watch(datetime, (val) => { | ||||
|         const newDate = new Date(val); | ||||
|         if (!isNaN(newDate.getTime())) { | ||||
|             emit("update:deadline", newDate); | ||||
|         } | ||||
|     }); | ||||
| 
 | ||||
|     function updateDeadline(): void { | ||||
|         if (date.value && time.value) { | ||||
|             emit("update:deadline", formattedDeadline.value); | ||||
|         } | ||||
|     } | ||||
| </script> | ||||
| 
 | ||||
| <template> | ||||
|     <div> | ||||
|     <v-card-text> | ||||
|         <v-text-field | ||||
|                 v-model="date" | ||||
|                 label="Select Deadline Date" | ||||
|                 type="date" | ||||
|             v-model="datetime" | ||||
|             type="datetime-local" | ||||
|             label="Select Deadline" | ||||
|             variant="outlined" | ||||
|             density="compact" | ||||
|             :rules="deadlineRules" | ||||
|             required | ||||
|                 @update:modelValue="updateDeadline" | ||||
|             ></v-text-field> | ||||
|         /> | ||||
|     </v-card-text> | ||||
| 
 | ||||
|         <v-card-text> | ||||
|             <v-text-field | ||||
|                 v-model="time" | ||||
|                 label="Select Deadline Time" | ||||
|                 type="time" | ||||
|                 variant="outlined" | ||||
|                 density="compact" | ||||
|                 @update:modelValue="updateDeadline" | ||||
|             ></v-text-field> | ||||
|         </v-card-text> | ||||
|     </div> | ||||
| </template> | ||||
| 
 | ||||
| <style scoped></style> | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| import { BaseController } from "@/controllers/base-controller.ts"; | ||||
| import type { JoinRequestResponse, JoinRequestsResponse, StudentsResponse } from "@/controllers/students.ts"; | ||||
| import type { QuestionsResponse } from "@/controllers/questions.ts"; | ||||
| import type { ClassesResponse } from "@/controllers/classes.ts"; | ||||
| import type { TeacherDTO } from "@dwengo-1/common/interfaces/teacher"; | ||||
| 
 | ||||
|  | @ -40,10 +39,6 @@ export class TeacherController extends BaseController { | |||
|         return this.get<StudentsResponse>(`/${username}/students`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     async getQuestions(username: string, full = false): Promise<QuestionsResponse> { | ||||
|         return this.get<QuestionsResponse>(`/${username}/questions`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     async getStudentJoinRequests(username: string, classId: string): Promise<JoinRequestsResponse> { | ||||
|         return this.get<JoinRequestsResponse>(`/${username}/joinRequests/${classId}`); | ||||
|     } | ||||
|  |  | |||
|  | @ -133,5 +133,7 @@ | |||
|     "see-submission": "Einsendung anzeigen", | ||||
|     "view-submissions": "Einsendungen anzeigen", | ||||
|     "valid-username": "Bitte geben Sie einen gültigen Benutzernamen ein", | ||||
|     "creationFailed": "Erstellung fehlgeschlagen, bitte versuchen Sie es erneut" | ||||
|     "creationFailed": "Erstellung fehlgeschlagen, bitte versuchen Sie es erneut", | ||||
|     "no-assignments": "Derzeit gibt es keine Zuweisungen.", | ||||
|     "deadline": "deadline" | ||||
| } | ||||
|  |  | |||
|  | @ -133,5 +133,7 @@ | |||
|     "see-submission": "view submission", | ||||
|     "view-submissions": "view submissions", | ||||
|     "valid-username": "please enter a valid username", | ||||
|     "creationFailed": "creation failed, please try again" | ||||
|     "creationFailed": "creation failed, please try again", | ||||
|     "no-assignments": "There are currently no assignments.", | ||||
|     "deadline": "deadline" | ||||
| } | ||||
|  |  | |||
|  | @ -134,5 +134,7 @@ | |||
|     "see-submission": "voir la soumission", | ||||
|     "view-submissions": "voir les soumissions", | ||||
|     "valid-username": "veuillez entrer un nom d'utilisateur valide", | ||||
|     "creationFailed": "échec de la création, veuillez réessayer" | ||||
|     "creationFailed": "échec de la création, veuillez réessayer", | ||||
|     "no-assignments": "Il n'y a actuellement aucun travail.", | ||||
|     "deadline": "délai" | ||||
| } | ||||
|  |  | |||
|  | @ -133,5 +133,7 @@ | |||
|     "see-submission": "inzending bekijken", | ||||
|     "view-submissions": "inzendingen bekijken", | ||||
|     "valid-username": "voer een geldige gebruikersnaam in", | ||||
|     "creationFailed": "aanmaak mislukt, probeer het opnieuw" | ||||
|     "creationFailed": "aanmaak mislukt, probeer het opnieuw", | ||||
|     "no-assignments": "Er zijn momenteel geen opdrachten.", | ||||
|     "deadline": "deadline" | ||||
| } | ||||
|  |  | |||
|  | @ -10,7 +10,6 @@ import { | |||
| import { TeacherController, type TeacherResponse, type TeachersResponse } from "@/controllers/teachers.ts"; | ||||
| import type { ClassesResponse } from "@/controllers/classes.ts"; | ||||
| import type { JoinRequestResponse, JoinRequestsResponse, StudentsResponse } from "@/controllers/students.ts"; | ||||
| import type { QuestionsResponse } from "@/controllers/questions.ts"; | ||||
| import type { TeacherDTO } from "@dwengo-1/common/interfaces/teacher"; | ||||
| import { studentJoinRequestQueryKey, studentJoinRequestsQueryKey } from "@/queries/students.ts"; | ||||
| 
 | ||||
|  | @ -33,10 +32,6 @@ function teacherStudentsQueryKey(username: string, full: boolean): [string, stri | |||
|     return ["teacher-students", username, full]; | ||||
| } | ||||
| 
 | ||||
| function teacherQuestionsQueryKey(username: string, full: boolean): [string, string, boolean] { | ||||
|     return ["teacher-questions", username, full]; | ||||
| } | ||||
| 
 | ||||
| export function teacherClassJoinRequests(classId: string): [string, string] { | ||||
|     return ["teacher-class-join-requests", classId]; | ||||
| } | ||||
|  | @ -80,17 +75,6 @@ export function useTeacherStudentsQuery( | |||
|     }); | ||||
| } | ||||
| 
 | ||||
| export function useTeacherQuestionsQuery( | ||||
|     username: MaybeRefOrGetter<string | undefined>, | ||||
|     full: MaybeRefOrGetter<boolean> = false, | ||||
| ): UseQueryReturnType<QuestionsResponse, Error> { | ||||
|     return useQuery({ | ||||
|         queryKey: computed(() => teacherQuestionsQueryKey(toValue(username)!, toValue(full))), | ||||
|         queryFn: async () => teacherController.getQuestions(toValue(username)!, toValue(full)), | ||||
|         enabled: () => Boolean(toValue(username)), | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| export function useTeacherJoinRequestsQuery( | ||||
|     username: MaybeRefOrGetter<string | undefined>, | ||||
|     classId: MaybeRefOrGetter<string | undefined>, | ||||
|  |  | |||
|  | @ -48,7 +48,7 @@ | |||
| 
 | ||||
|     // Disable combobox when learningPath prop is passed | ||||
|     const lpIsSelected = route.query.hruid !== undefined; | ||||
|     const deadline = ref(null); | ||||
|     const deadline = ref(new Date()); | ||||
|     const description = ref(""); | ||||
|     const groups = ref<string[][]>([]); | ||||
| 
 | ||||
|  | @ -86,6 +86,7 @@ | |||
|             title: assignmentTitle.value, | ||||
|             description: description.value, | ||||
|             learningPath: lp || "", | ||||
|             deadline: deadline.value, | ||||
|             language: language.value, | ||||
|             groups: groups.value, | ||||
|         }; | ||||
|  |  | |||
|  | @ -11,7 +11,7 @@ | |||
|     import { useDeleteAssignmentMutation } from "@/queries/assignments.ts"; | ||||
|     import "../../assets/common.css"; | ||||
| 
 | ||||
|     const { t } = useI18n(); | ||||
|     const { t, locale } = useI18n(); | ||||
|     const router = useRouter(); | ||||
| 
 | ||||
|     const role = ref(auth.authState.activeRole); | ||||
|  | @ -28,13 +28,13 @@ | |||
|         classesQueryResults = useStudentClassesQuery(username, true); | ||||
|     } | ||||
| 
 | ||||
|     //TODO: remove later | ||||
|     const classController = new ClassController(); | ||||
| 
 | ||||
|     //TODO: replace by query that fetches all user's assignment | ||||
|     const assignments = asyncComputed(async () => { | ||||
|     const assignments = asyncComputed( | ||||
|         async () => { | ||||
|             const classes = classesQueryResults?.data?.value?.classes; | ||||
|             if (!classes) return []; | ||||
| 
 | ||||
|             const result = await Promise.all( | ||||
|                 (classes as ClassDTO[]).map(async (cls) => { | ||||
|                     const { assignments } = await classController.getAssignments(cls.id); | ||||
|  | @ -45,13 +45,30 @@ | |||
|                         description: a.description, | ||||
|                         learningPath: a.learningPath, | ||||
|                         language: a.language, | ||||
|                         deadline: a.deadline, | ||||
|                         groups: a.groups, | ||||
|                     })); | ||||
|                 }), | ||||
|             ); | ||||
| 
 | ||||
|         return result.flat(); | ||||
|     }, []); | ||||
|             // Order the assignments by deadline | ||||
|             return result.flat().sort((a, b) => { | ||||
|                 const now = Date.now(); | ||||
|                 const aTime = new Date(a.deadline).getTime(); | ||||
|                 const bTime = new Date(b.deadline).getTime(); | ||||
| 
 | ||||
|                 const aIsPast = aTime < now; | ||||
|                 const bIsPast = bTime < now; | ||||
| 
 | ||||
|                 if (aIsPast && !bIsPast) return 1; | ||||
|                 if (!aIsPast && bIsPast) return -1; | ||||
| 
 | ||||
|                 return aTime - bTime; | ||||
|             }); | ||||
|         }, | ||||
|         [], | ||||
|         { evaluating: true }, | ||||
|     ); | ||||
| 
 | ||||
|     async function goToCreateAssignment(): Promise<void> { | ||||
|         await router.push("/assignment/create"); | ||||
|  | @ -73,6 +90,35 @@ | |||
|         mutate({ cid: clsId, an: num }); | ||||
|     } | ||||
| 
 | ||||
|     function formatDate(date?: string | Date): string { | ||||
|         if (!date) return "–"; | ||||
|         const d = new Date(date); | ||||
| 
 | ||||
|         // Choose locale based on selected language | ||||
|         const currentLocale = locale.value; | ||||
| 
 | ||||
|         return d.toLocaleDateString(currentLocale, { | ||||
|             weekday: "short", | ||||
|             day: "2-digit", | ||||
|             month: "long", | ||||
|             year: "numeric", | ||||
|             hour: "numeric", | ||||
|             minute: "2-digit", | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     function getDeadlineClass(deadline?: string | Date): string { | ||||
|         if (!deadline) return ""; | ||||
| 
 | ||||
|         const date = new Date(deadline); | ||||
|         const now = new Date(); | ||||
|         const in24Hours = new Date(now.getTime() + 24 * 60 * 60 * 1000); | ||||
| 
 | ||||
|         if (date.getTime() < now.getTime()) return "deadline-passed"; | ||||
|         if (date.getTime() <= in24Hours.getTime()) return "deadline-in24hours"; | ||||
|         return "deadline-upcoming"; | ||||
|     } | ||||
| 
 | ||||
|     onMounted(async () => { | ||||
|         const user = await auth.loadUser(); | ||||
|         username.value = user?.profile?.preferred_username ?? ""; | ||||
|  | @ -108,6 +154,13 @@ | |||
|                                     {{ assignment.class.displayName }} | ||||
|                                 </span> | ||||
|                             </div> | ||||
|                             <div | ||||
|                                 class="assignment-deadline" | ||||
|                                 :class="getDeadlineClass(assignment.deadline)" | ||||
|                             > | ||||
|                                 {{ t("deadline") }}: | ||||
|                                 <span>{{ formatDate(assignment.deadline) }}</span> | ||||
|                             </div> | ||||
|                         </div> | ||||
| 
 | ||||
|                         <div class="spacer"></div> | ||||
|  | @ -132,6 +185,13 @@ | |||
|                     </v-card> | ||||
|                 </v-col> | ||||
|             </v-row> | ||||
|             <v-row v-if="assignments.length === 0"> | ||||
|                 <v-col cols="12"> | ||||
|                     <div class="no-assignments"> | ||||
|                         {{ t("no-assignments") }} | ||||
|                     </div> | ||||
|                 </v-col> | ||||
|             </v-row> | ||||
|         </v-container> | ||||
|     </div> | ||||
| </template> | ||||
|  | @ -145,12 +205,27 @@ | |||
| 
 | ||||
|     .center-btn { | ||||
|         display: block; | ||||
|         margin-left: auto; | ||||
|         margin-right: auto; | ||||
|         margin: 0 auto 2rem auto; | ||||
|         font-weight: 600; | ||||
|         background-color: #10ad61; | ||||
|         color: white; | ||||
|         transition: background-color 0.2s; | ||||
|     } | ||||
|     .center-btn:hover { | ||||
|         background-color: #0e6942; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-card { | ||||
|         padding: 1rem; | ||||
|         padding: 1.25rem; | ||||
|         border-radius: 16px; | ||||
|         box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); | ||||
|         background-color: white; | ||||
|         transition: | ||||
|             transform 0.2s, | ||||
|             box-shadow 0.2s; | ||||
|     } | ||||
|     .assignment-card:hover { | ||||
|         box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12); | ||||
|     } | ||||
| 
 | ||||
|     .top-content { | ||||
|  | @ -158,6 +233,35 @@ | |||
|         word-break: break-word; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-title { | ||||
|         font-weight: 700; | ||||
|         font-size: 1.4rem; | ||||
|         color: #0e6942; | ||||
|         margin-bottom: 0.3rem; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-class, | ||||
|     .assignment-deadline { | ||||
|         font-size: 0.95rem; | ||||
|         color: #444; | ||||
|         margin-bottom: 0.2rem; | ||||
|     } | ||||
| 
 | ||||
|     .class-name { | ||||
|         font-weight: 600; | ||||
|         color: #097180; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-deadline.deadline-passed { | ||||
|         color: #d32f2f; | ||||
|         font-weight: bold; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-deadline.deadline-in24hours { | ||||
|         color: #f57c00; | ||||
|         font-weight: bold; | ||||
|     } | ||||
| 
 | ||||
|     .spacer { | ||||
|         flex: 1; | ||||
|     } | ||||
|  | @ -165,24 +269,14 @@ | |||
|     .button-row { | ||||
|         display: flex; | ||||
|         justify-content: flex-end; | ||||
|         gap: 0.5rem; | ||||
|         gap: 0.75rem; | ||||
|         flex-wrap: wrap; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-title { | ||||
|         font-weight: bold; | ||||
|         font-size: 1.5rem; | ||||
|         margin-bottom: 0.1rem; | ||||
|         word-break: break-word; | ||||
|     } | ||||
| 
 | ||||
|     .assignment-class { | ||||
|         color: #666; | ||||
|         font-size: 0.95rem; | ||||
|     } | ||||
| 
 | ||||
|     .class-name { | ||||
|         font-weight: 500; | ||||
|         color: #333; | ||||
|     .no-assignments { | ||||
|         text-align: center; | ||||
|         font-size: 1.2rem; | ||||
|         color: #777; | ||||
|         padding: 3rem 0; | ||||
|     } | ||||
| </style> | ||||
|  |  | |||
							
								
								
									
										5868
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										5868
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
		Reference in a new issue
	
	 Laure Jablonski
						Laure Jablonski