fix: bug in put assignment waarbij groepen niet werden aangepast gefixt
This commit is contained in:
		
							parent
							
								
									b7486b4d1b
								
							
						
					
					
						commit
						3d8c8302d6
					
				
					 3 changed files with 40 additions and 2 deletions
				
			
		|  | @ -28,4 +28,9 @@ export class GroupRepository extends DwengoEntityRepository<Group> { | |||
|             groupNumber: groupNumber, | ||||
|         }); | ||||
|     } | ||||
|     public async deleteAllByAssignment(assignment: Assignment): Promise<void> { | ||||
|         return this.deleteAllWhere({ | ||||
|             assignment: assignment, | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| import { EntityRepository, FilterQuery } from '@mikro-orm/core'; | ||||
| import { EntityAlreadyExistsException } from '../exceptions/entity-already-exists-exception.js'; | ||||
| import { getLogger } from '../logging/initalize.js'; | ||||
| 
 | ||||
| export abstract class DwengoEntityRepository<T extends object> extends EntityRepository<T> { | ||||
|     public async save(entity: T, options?: { preventOverwrite?: boolean }): Promise<void> { | ||||
|  | @ -16,4 +17,13 @@ export abstract class DwengoEntityRepository<T extends object> extends EntityRep | |||
|             await em.flush(); | ||||
|         } | ||||
|     } | ||||
|     public async deleteAllWhere(query: FilterQuery<T>): Promise<void> { | ||||
|         const toDelete = await this.find(query); | ||||
|         const em = this.getEntityManager(); | ||||
| 
 | ||||
|         if (toDelete) { | ||||
|             em.remove(toDelete); | ||||
|             await em.flush(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -18,6 +18,8 @@ import { EntityDTO } from '@mikro-orm/core'; | |||
| import { putObject } from './service-helper.js'; | ||||
| import { fetchStudents } from './students.js'; | ||||
| import { ServerErrorException } from '../exceptions/server-error-exception.js'; | ||||
| import { createGroup, deleteGroup, fetchAllGroups } from './groups.js'; | ||||
| import { BadRequestException } from '../exceptions/bad-request-exception.js'; | ||||
| 
 | ||||
| export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> { | ||||
|     const classRepository = getClassRepository(); | ||||
|  | @ -95,10 +97,31 @@ export async function getAssignment(classid: string, id: number): Promise<Assign | |||
|     return mapToAssignmentDTO(assignment); | ||||
| } | ||||
| 
 | ||||
| export async function putAssignment(classid: string, id: number, assignmentData: Partial<EntityDTO<Assignment>>): Promise<AssignmentDTO> { | ||||
| export async function putAssignment(classid: string, id: number, assignmentData: Partial<AssignmentDTO>): Promise<AssignmentDTO> { | ||||
|     const assignment = await fetchAssignment(classid, id); | ||||
| 
 | ||||
|     await putObject<Assignment>(assignment, assignmentData, getAssignmentRepository()); | ||||
|     if (assignmentData.groups) { | ||||
|         const hasDuplicates = (arr: string[]) => new Set(arr).size !== arr.length; | ||||
|         if (hasDuplicates(assignmentData.groups.flat() as string[])) { | ||||
|             throw new BadRequestException("Student can only be in one group"); | ||||
|         } | ||||
| 
 | ||||
|         const studentLists = await Promise.all((assignmentData.groups! as string[][]).map(async group => await fetchStudents(group))); | ||||
| 
 | ||||
|         const groupRepository = getGroupRepository(); | ||||
|         await groupRepository.deleteAllByAssignment(assignment); | ||||
|         await Promise.all(studentLists.map(async students => { | ||||
|             const newGroup = groupRepository.create({ | ||||
|                 assignment: assignment, | ||||
|                 members: students, | ||||
|             }); | ||||
|             await groupRepository.save(newGroup); | ||||
|         })); | ||||
| 
 | ||||
|         delete assignmentData.groups; | ||||
|     } | ||||
| 
 | ||||
|     await putObject<Assignment>(assignment, assignmentData as Partial<EntityDTO<Assignment>>, getAssignmentRepository()); | ||||
| 
 | ||||
|     return mapToAssignmentDTO(assignment); | ||||
| } | ||||
|  |  | |||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet