feat: class join req controller + fixes tests
This commit is contained in:
		
							parent
							
								
									3093a6c131
								
							
						
					
					
						commit
						f679a324ab
					
				
					 11 changed files with 116 additions and 41 deletions
				
			
		|  | @ -122,7 +122,7 @@ export async function createStudentRequestHandler(req: Request, res: Response): | |||
|     requireFields({ username, classId }); | ||||
| 
 | ||||
|     await createClassJoinRequest(username, classId); | ||||
|     res.status(201).send(); | ||||
|     res.status(201); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentRequestHandler(req: Request, res: Response): Promise<void> { | ||||
|  | @ -144,12 +144,12 @@ export async function updateClassJoinRequestHandler(req: Request, res: Response) | |||
| } | ||||
| 
 | ||||
| export async function deleteClassJoinRequestHandler(req: Request, res: Response) { | ||||
|     const username = req.query.username as string; | ||||
|     const username = req.params.username as string; | ||||
|     const classId = req.params.classId; | ||||
|     requireFields({ username, classId }); | ||||
| 
 | ||||
|     await deleteClassJoinRequest(username, classId); | ||||
|     res.status(204).send(); | ||||
|     res.status(204); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -10,6 +10,9 @@ export class ClassJoinRequestRepository extends DwengoEntityRepository<ClassJoin | |||
|     public findAllOpenRequestsTo(clazz: Class): Promise<ClassJoinRequest[]> { | ||||
|         return this.findAll({ where: { class: clazz } }); | ||||
|     } | ||||
|     public findByStudentAndClass(requester: Student, clazz: Class): Promise<ClassJoinRequest | null> { | ||||
|         return this.findOne({ requester, class: clazz }); | ||||
|     } | ||||
|     public deleteBy(requester: Student, clazz: Class): Promise<void> { | ||||
|         return this.deleteWhere({ requester: requester, class: clazz }); | ||||
|     } | ||||
|  |  | |||
|  | @ -2,7 +2,6 @@ import express from "express"; | |||
| import { | ||||
|     createStudentRequestHandler, deleteClassJoinRequestHandler, | ||||
|     getStudentRequestHandler, | ||||
|     updateClassJoinRequestHandler | ||||
| } from "../controllers/students"; | ||||
| 
 | ||||
| const router = express.Router({ mergeParams: true }); | ||||
|  | @ -11,8 +10,6 @@ router.get('/', getStudentRequestHandler); | |||
| 
 | ||||
| router.post('/:classId', createStudentRequestHandler); | ||||
| 
 | ||||
| router.put('/:classId', updateClassJoinRequestHandler); | ||||
| 
 | ||||
| router.delete('/:classId', deleteClassJoinRequestHandler); | ||||
| 
 | ||||
| export default router; | ||||
|  |  | |||
|  | @ -39,6 +39,6 @@ router.get('/:username/groups', getStudentGroupsHandler); | |||
| // A list of questions a user has created
 | ||||
| router.get('/:username/questions', getStudentQuestionsHandler); | ||||
| 
 | ||||
| router.use('/:username/join-requests', joinRequestRouter) | ||||
| router.use('/:username/joinRequests', joinRequestRouter) | ||||
| 
 | ||||
| export default router; | ||||
|  |  | |||
|  | @ -140,6 +140,12 @@ export async function createClassJoinRequest(studentUsername: string, classId: s | |||
|         throw new NotFoundException("Class with id not found"); | ||||
|     } | ||||
| 
 | ||||
|     const req = await requestRepo.findByStudentAndClass(student, cls); | ||||
| 
 | ||||
|     if (req){ | ||||
|         throw new ConflictException("Request with student and class already exist"); | ||||
|     } | ||||
| 
 | ||||
|     const request = requestRepo.create({ | ||||
|         requester: student, | ||||
|         class: cls, | ||||
|  | @ -159,6 +165,7 @@ export async function getJoinRequestsByStudent(studentUsername: string) { | |||
|     return requests.map(mapToStudentRequestDTO); | ||||
| } | ||||
| 
 | ||||
| // TODO naar teacher
 | ||||
| export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true) { | ||||
|     const requestRepo = getClassJoinRequestRepository(); | ||||
|     const classRepo = getClassRepository(); | ||||
|  | @ -193,7 +200,7 @@ export async function deleteClassJoinRequest(studentUsername: string, classId: s | |||
|         throw new NotFoundException('Class not found'); | ||||
|     } | ||||
| 
 | ||||
|     const request = await requestRepo.findOne({ requester: student, class: cls }); | ||||
|     const request = await requestRepo.findByStudentAndClass(student, cls); | ||||
| 
 | ||||
|     if (!request) { | ||||
|         throw new NotFoundException('Join request not found'); | ||||
|  |  | |||
|  | @ -16,7 +16,7 @@ import { | |||
|     deleteClassJoinRequestHandler | ||||
| } from '../../src/controllers/students.js'; | ||||
| import {TEST_STUDENTS} from "../test_assets/users/students.testdata"; | ||||
| import {BadRequestException, NotFoundException} from "../../src/exceptions"; | ||||
| import {BadRequestException, ConflictException, NotFoundException} from "../../src/exceptions"; | ||||
| 
 | ||||
| describe('Student controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|  | @ -71,7 +71,21 @@ describe('Student controllers', () => { | |||
| 
 | ||||
|     // TODO create duplicate student id
 | ||||
| 
 | ||||
|     it('Create student no body 400', async () => { | ||||
|     it('Create duplicate student', async () => { | ||||
|         req = { | ||||
|             body: { | ||||
|                 username: 'DireStraits', | ||||
|                 firstName: 'dupe', | ||||
|                 lastName: 'dupe' | ||||
|             } | ||||
|         }; | ||||
| 
 | ||||
|         await expect(() => createStudentHandler(req as Request, res as Response)) | ||||
|             .rejects | ||||
|             .toThrowError(ConflictException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create student no body', async () => { | ||||
|         req = { body: {} }; | ||||
| 
 | ||||
|         await expect(() => createStudentHandler(req as Request, res as Response)) | ||||
|  | @ -179,44 +193,37 @@ describe('Student controllers', () => { | |||
| 
 | ||||
|     it('Create join request', async () => { | ||||
|         req = { | ||||
|             params: { username: 'DireStraits', classId: '' }, | ||||
|             params: { username: 'Noordkaap', classId: 'id02' }, | ||||
|         }; | ||||
| 
 | ||||
|         await createStudentRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(201); | ||||
|         expect(jsonMock).toHaveBeenCalled(); | ||||
|     }); | ||||
| 
 | ||||
|     /* | ||||
| 
 | ||||
|     it('Update join request status (accept)', async () => { | ||||
|     it('Create join request duplicate', async () => { | ||||
|         req = { | ||||
|             params: { classId }, | ||||
|             query: { username }, | ||||
|             params: { username: 'Tool', classId: 'id02' }, | ||||
|         }; | ||||
| 
 | ||||
|         await updateClassJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(200); | ||||
|         expect(jsonMock).toHaveBeenCalled(); | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         console.log('[UPDATED REQUEST]', result); | ||||
|         await expect(() => createStudentRequestHandler(req as Request, res as Response)) | ||||
|             .rejects | ||||
|             .toThrow(ConflictException); | ||||
|     }); | ||||
| 
 | ||||
| 
 | ||||
|     it('Delete join request', async () => { | ||||
|         req = { | ||||
|             params: { classId }, | ||||
|             query: { username }, | ||||
|             params: { username: 'Noordkaap', classId: 'id02' }, | ||||
|         }; | ||||
| 
 | ||||
|         await deleteClassJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(204); | ||||
|         expect(sendMock).toHaveBeenCalled(); | ||||
| 
 | ||||
|         await expect(() => deleteClassJoinRequestHandler(req as Request, res as Response)) | ||||
|             .rejects | ||||
|             .toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|      */ | ||||
| 
 | ||||
| 
 | ||||
| }); | ||||
|  |  | |||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl