fix+feat: error bij het toevegoen van groups na het aanmaken van een assignment gefixt, group & assignment DTO Id toegevoegd
This commit is contained in:
		
							parent
							
								
									0ceb6761d7
								
							
						
					
					
						commit
						e3fc4b72a7
					
				
					 8 changed files with 53 additions and 50 deletions
				
			
		|  | @ -4,10 +4,10 @@ import { Class } from '../../entities/classes/class.entity.js'; | ||||||
| 
 | 
 | ||||||
| export class AssignmentRepository extends DwengoEntityRepository<Assignment> { | export class AssignmentRepository extends DwengoEntityRepository<Assignment> { | ||||||
|     public async findByClassAndId(within: Class, id: number): Promise<Assignment | null> { |     public async findByClassAndId(within: Class, id: number): Promise<Assignment | null> { | ||||||
|         return this.findOne({ within: within, id: id }, { populate: [ "groups" ]}); |         return this.findOne({ within: within, id: id }, { populate: [ "groups", "groups.members" ]}); | ||||||
|     } |     } | ||||||
|     public async findAllAssignmentsInClass(within: Class): Promise<Assignment[]> { |     public async findAllAssignmentsInClass(within: Class): Promise<Assignment[]> { | ||||||
|         return this.findAll({ where: { within: within }, populate: [ "groups" ] }); |         return this.findAll({ where: { within: within }, populate: [ "groups", "groups.members" ] }); | ||||||
|     } |     } | ||||||
|     public async deleteByClassAndId(within: Class, id: number): Promise<void> { |     public async deleteByClassAndId(within: Class, id: number): Promise<void> { | ||||||
|         return this.deleteWhere({ within: within, id: id }); |         return this.deleteWhere({ within: within, id: id }); | ||||||
|  |  | ||||||
|  | @ -3,22 +3,17 @@ import { FALLBACK_LANG } from '../config.js'; | ||||||
| import { Assignment } from '../entities/assignments/assignment.entity.js'; | import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||||
| import { Class } from '../entities/classes/class.entity.js'; | import { Class } from '../entities/classes/class.entity.js'; | ||||||
| import { getLogger } from '../logging/initalize.js'; | import { getLogger } from '../logging/initalize.js'; | ||||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | import { AssignmentDTO, AssignmentDTOId } from '@dwengo-1/common/interfaces/assignment'; | ||||||
| import { mapToGroupDTO, mapToGroupDTOId } from './group.js'; | import { mapToGroupDTO, mapToGroupDTOId } from './group.js'; | ||||||
| import { getAssignmentHandler } from '../controllers/assignments.js'; | import { getAssignmentHandler } from '../controllers/assignments.js'; | ||||||
| import { getAssignmentRepository, getClassRepository } from '../data/repositories.js'; | import { getAssignmentRepository, getClassRepository } from '../data/repositories.js'; | ||||||
| 
 | 
 | ||||||
| export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTOId { | ||||||
|     return { |         return { | ||||||
|         id: assignment.id!, |             id: assignment.id!, | ||||||
|         within: assignment.within.classId!, |             within: assignment.within.classId!, | ||||||
|         title: assignment.title, |         }; | ||||||
|         description: assignment.description, |     } | ||||||
|         learningPath: assignment.learningPathHruid, |  | ||||||
|         language: assignment.learningPathLanguage, |  | ||||||
|         groups: assignment.groups.map(group => mapToGroupDTOId(group, assignment.within)), |  | ||||||
|     }; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | ||||||
|     return { |     return { | ||||||
|  |  | ||||||
|  | @ -3,22 +3,25 @@ import { Class } from '../entities/classes/class.entity.js'; | ||||||
| import {mapToAssignmentDTOId} from './assignment.js'; | import {mapToAssignmentDTOId} from './assignment.js'; | ||||||
| import { mapToClassDTO } from './class.js'; | import { mapToClassDTO } from './class.js'; | ||||||
| import { mapToStudentDTO } from './student.js'; | import { mapToStudentDTO } from './student.js'; | ||||||
| import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | import { GroupDTO, GroupDTOId } from '@dwengo-1/common/interfaces/group'; | ||||||
| 
 | 
 | ||||||
| export function mapToGroupDTO(group: Group, cls: Class): GroupDTO { | export function mapToGroupDTO(group: Group, cls: Class, options?: { expandStudents: boolean }): GroupDTO { | ||||||
|     return { |     return { | ||||||
|         class: mapToClassDTO(cls), |         class: cls.classId!, | ||||||
|         assignment: mapToAssignmentDTOId(group.assignment), |         assignment: group.assignment.id!, | ||||||
|         groupNumber: group.groupNumber!, |         groupNumber: group.groupNumber!, | ||||||
|         members: group.members.map(mapToStudentDTO), |         members:  | ||||||
|     }; |             options?.expandStudents  | ||||||
| } |             ? group.members.map(mapToStudentDTO) | ||||||
| 
 |             : group.members.map((student) => student.username) | ||||||
| export function mapToGroupDTOId(group: Group, cls: Class): GroupDTO { | 
 | ||||||
|  |     }; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function mapToGroupDTOId(group: Group, cls: Class): GroupDTOId { | ||||||
|     return { |     return { | ||||||
|         class: cls.classId!, |         class: cls.classId!, | ||||||
|         assignment: group.assignment.id!, |         assignment: group.assignment.id!, | ||||||
|         groupNumber: group.groupNumber!, |         groupNumber: group.groupNumber!, | ||||||
|         members: group.members.map((member) => member.username), |  | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | import { AssignmentDTO, AssignmentDTOId } from '@dwengo-1/common/interfaces/assignment'; | ||||||
| import { | import { | ||||||
|     getAssignmentRepository, |     getAssignmentRepository, | ||||||
|     getClassRepository, |     getClassRepository, | ||||||
|  | @ -21,6 +21,9 @@ import { getLogger } from '../logging/initalize.js'; | ||||||
| import { languageMap } from '@dwengo-1/common/util/language'; | import { languageMap } from '@dwengo-1/common/util/language'; | ||||||
| import { createGroup } from './groups.js'; | import { createGroup } from './groups.js'; | ||||||
| import { GroupDTO } from 'dwengo-1-common/interfaces/group'; | import { GroupDTO } from 'dwengo-1-common/interfaces/group'; | ||||||
|  | import { fetchStudent } from './students.js'; | ||||||
|  | import { assert } from 'console'; | ||||||
|  | import { ServerErrorException } from '../exceptions/server-error-exception.js'; | ||||||
| 
 | 
 | ||||||
| export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> { | export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -40,7 +43,7 @@ export async function fetchAssignment(classid: string, assignmentNumber: number) | ||||||
|     return assignment; |     return assignment; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> { | export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[] | AssignmentDTOId[]> { | ||||||
|     const cls = await fetchClass(classid); |     const cls = await fetchClass(classid); | ||||||
| 
 | 
 | ||||||
|     const assignmentRepository = getAssignmentRepository(); |     const assignmentRepository = getAssignmentRepository(); | ||||||
|  | @ -60,26 +63,32 @@ export async function createAssignment(classid: string, assignmentData: Assignme | ||||||
|     const assignment = mapToAssignment(assignmentData, cls); |     const assignment = mapToAssignment(assignmentData, cls); | ||||||
|     await assignmentRepository.save(assignment); |     await assignmentRepository.save(assignment); | ||||||
| 
 | 
 | ||||||
|     /* |      | ||||||
|     if (assignmentData.groups) { |     if (assignmentData.groups) { | ||||||
|  |         /* | ||||||
|  |         For some reason when trying to add groups, it does not work when using the original assignment variable.  | ||||||
|  |         The assignment needs to be refetched in order for it to work. | ||||||
|  |         */ | ||||||
|  |         const assignmentCopy = await assignmentRepository.findByClassAndId(cls, assignment.id!); | ||||||
|  | 
 | ||||||
|  |         if (assignmentCopy === null) { | ||||||
|  |             throw new ServerErrorException("Something has gone horribly wrong. Could not find newly added assignment which is needed to add groups."); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         const groupRepository = getGroupRepository(); |         const groupRepository = getGroupRepository(); | ||||||
|         const studentRepository = getStudentRepository(); |  | ||||||
| 
 | 
 | ||||||
|         (assignmentData.groups as string[][]).forEach(async (memberUsernames) => { |         (assignmentData.groups as string[][]).forEach(async (memberUsernames) => { | ||||||
|             const members = (await Promise.all(memberUsernames.map(async (id) => studentRepository.findByUsername(id)))).filter( |             const members = (await Promise.all(memberUsernames.map(async (id) => fetchStudent(id)))).filter( | ||||||
|                 (student) => student !== null |                 (student) => student !== null | ||||||
|             ); |             ); | ||||||
| 
 | 
 | ||||||
|             const newGroup = groupRepository.create({ |             const newGroup = groupRepository.create({ | ||||||
|                 assignment: assignment, |                 assignment: assignmentCopy!, | ||||||
|                 members: members, |                 members: members, | ||||||
|             }); |             }); | ||||||
|             await groupRepository.save(newGroup); |             await groupRepository.save(newGroup); | ||||||
|             console.log('NEW GROUP'); |  | ||||||
|             console.log(newGroup); |  | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|     */ |  | ||||||
| 
 | 
 | ||||||
|     return mapToAssignmentDTO(assignment); |     return mapToAssignmentDTO(assignment); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ import { getGroupRepository, getStudentRepository, getSubmissionRepository } fro | ||||||
| import { Group } from '../entities/assignments/group.entity.js'; | import { Group } from '../entities/assignments/group.entity.js'; | ||||||
| import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | ||||||
| import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js'; | import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js'; | ||||||
| import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | import { GroupDTO, GroupDTOId } from '@dwengo-1/common/interfaces/group'; | ||||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||||
| import { fetchAssignment } from './assignments.js'; | import { fetchAssignment } from './assignments.js'; | ||||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||||
|  | @ -77,16 +77,12 @@ export async function createGroup(groupData: GroupDTO, classid: string, assignme | ||||||
|         members: members |         members: members | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     try { |     await groupRepository.save(newGroup); | ||||||
|         await groupRepository.save(newGroup); |  | ||||||
|     } catch(e) { |  | ||||||
|         console.log(e); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     return mapToGroupDTO(newGroup, newGroup.assignment.within); |     return mapToGroupDTO(newGroup, newGroup.assignment.within); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getAllGroups(classId: string, assignmentNumber: number, full: boolean): Promise<GroupDTO[]> { | export async function getAllGroups(classId: string, assignmentNumber: number, full: boolean): Promise<GroupDTO[] | GroupDTOId[]> { | ||||||
|     const assignment = await fetchAssignment(classId, assignmentNumber); |     const assignment = await fetchAssignment(classId, assignmentNumber); | ||||||
| 
 | 
 | ||||||
|     const groupRepository = getGroupRepository(); |     const groupRepository = getGroupRepository(); | ||||||
|  |  | ||||||
|  | @ -18,8 +18,8 @@ import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||||
| import { fetchClass } from './classes.js'; | import { fetchClass } from './classes.js'; | ||||||
| import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | ||||||
| import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | import { AssignmentDTO, AssignmentDTOId } from '@dwengo-1/common/interfaces/assignment'; | ||||||
| import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | import { GroupDTO, GroupDTOId } from '@dwengo-1/common/interfaces/group'; | ||||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||||
| import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request'; | import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request'; | ||||||
|  | @ -82,7 +82,7 @@ export async function getStudentClasses(username: string, full: boolean): Promis | ||||||
|     return classes.map((cls) => cls.classId!); |     return classes.map((cls) => cls.classId!); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getStudentAssignments(username: string, full: boolean): Promise<AssignmentDTO[]> { | export async function getStudentAssignments(username: string, full: boolean): Promise<AssignmentDTO[] | AssignmentDTOId[]> { | ||||||
|     const student = await fetchStudent(username); |     const student = await fetchStudent(username); | ||||||
| 
 | 
 | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -91,7 +91,7 @@ export async function getStudentAssignments(username: string, full: boolean): Pr | ||||||
|     return (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat(); |     return (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> { | export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[] | GroupDTOId[]> { | ||||||
|     const student = await fetchStudent(username); |     const student = await fetchStudent(username); | ||||||
| 
 | 
 | ||||||
|     const groupRepository = getGroupRepository(); |     const groupRepository = getGroupRepository(); | ||||||
|  |  | ||||||
|  | @ -1,11 +1,11 @@ | ||||||
| import { BaseController } from "./base-controller"; | import { BaseController } from "./base-controller"; | ||||||
| import type { AssignmentDTO } from "@dwengo-1/common/interfaces/assignment"; | import type { AssignmentDTO, AssignmentDTOId } from "@dwengo-1/common/interfaces/assignment"; | ||||||
| import type { SubmissionsResponse } from "./submissions"; | import type { SubmissionsResponse } from "./submissions"; | ||||||
| import type { QuestionsResponse } from "./questions"; | import type { QuestionsResponse } from "./questions"; | ||||||
| import type { GroupsResponse } from "./groups"; | import type { GroupsResponse } from "./groups"; | ||||||
| 
 | 
 | ||||||
| export interface AssignmentsResponse { | export interface AssignmentsResponse { | ||||||
|     assignments: AssignmentDTO[] | string[]; |     assignments: AssignmentDTO[] | AssignmentDTOId[]; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface AssignmentResponse { | export interface AssignmentResponse { | ||||||
|  |  | ||||||
|  | @ -1,10 +1,10 @@ | ||||||
| import { BaseController } from "./base-controller"; | import { BaseController } from "./base-controller"; | ||||||
| import type { GroupDTO } from "@dwengo-1/common/interfaces/group"; | import type { GroupDTO, GroupDTOId } from "@dwengo-1/common/interfaces/group"; | ||||||
| import type { SubmissionsResponse } from "./submissions"; | import type { SubmissionsResponse } from "./submissions"; | ||||||
| import type { QuestionsResponse } from "./questions"; | import type { QuestionsResponse } from "./questions"; | ||||||
| 
 | 
 | ||||||
| export interface GroupsResponse { | export interface GroupsResponse { | ||||||
|     groups: GroupDTO[]; |     groups: GroupDTO[] | GroupDTOId[]; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface GroupResponse { | export interface GroupResponse { | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet