fix: fixed groups in return van createAssignment

This commit is contained in:
Adriaan Jacquet 2025-04-17 12:20:36 +02:00
parent e3fc4b72a7
commit ba9906eb93
4 changed files with 17 additions and 23 deletions

View file

@ -1,7 +1,5 @@
import { Group } from '../entities/assignments/group.entity.js';
import { Class } from '../entities/classes/class.entity.js';
import {mapToAssignmentDTOId} from './assignment.js';
import { mapToClassDTO } from './class.js';
import { mapToStudentDTO } from './student.js';
import { GroupDTO, GroupDTOId } from '@dwengo-1/common/interfaces/group';

View file

@ -15,14 +15,9 @@ import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submissi
import { fetchClass } from './classes.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
import { assign, EntityDTO } from '@mikro-orm/core';
import { EntityDTO } from '@mikro-orm/core';
import { putObject } from './service-helper.js';
import { getLogger } from '../logging/initalize.js';
import { languageMap } from '@dwengo-1/common/util/language';
import { createGroup } from './groups.js';
import { GroupDTO } from 'dwengo-1-common/interfaces/group';
import { fetchStudent } from './students.js';
import { assert } from 'console';
import { fetchStudents } from './students.js';
import { ServerErrorException } from '../exceptions/server-error-exception.js';
export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> {
@ -69,8 +64,9 @@ export async function createAssignment(classid: string, assignmentData: Assignme
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.");
}
@ -78,9 +74,7 @@ export async function createAssignment(classid: string, assignmentData: Assignme
const groupRepository = getGroupRepository();
(assignmentData.groups as string[][]).forEach(async (memberUsernames) => {
const members = (await Promise.all(memberUsernames.map(async (id) => fetchStudent(id)))).filter(
(student) => student !== null
);
const members = await fetchStudents(memberUsernames);
const newGroup = groupRepository.create({
assignment: assignmentCopy!,
@ -90,7 +84,10 @@ export async function createAssignment(classid: string, assignmentData: Assignme
});
}
return mapToAssignmentDTO(assignment);
/* Need to refetch the assignment here again such that the groups are added. */
const assignmentWithGroups = await fetchAssignment(classid, assignment.id!);
return mapToAssignmentDTO(assignmentWithGroups);
}
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO> {

View file

@ -1,5 +1,5 @@
import { EntityDTO } from '@mikro-orm/core';
import { getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js';
import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
import { Group } from '../entities/assignments/group.entity.js';
import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
@ -8,7 +8,7 @@ import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/subm
import { fetchAssignment } from './assignments.js';
import { NotFoundException } from '../exceptions/not-found-exception.js';
import { putObject } from './service-helper.js';
import { Student } from '../entities/users/student.entity.js';
import { fetchStudents } from './students.js';
export async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group> {
const assignment = await fetchAssignment(classId, assignmentNumber);
@ -60,14 +60,8 @@ export async function getExistingGroupFromGroupDTO(groupData: GroupDTO): Promise
}
export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<GroupDTO> {
const studentRepository = getStudentRepository();
const memberUsernames = (groupData.members as string[]) || [];
const members = (
await Promise.all(
memberUsernames.map(async (id) => studentRepository.findByUsername(id))
)
).filter((student): student is Student => student !== null);
const members = await fetchStudents(memberUsernames);
const assignment = await fetchAssignment(classid, assignmentNumber);

View file

@ -47,6 +47,11 @@ export async function fetchStudent(username: string): Promise<Student> {
return user;
}
export async function fetchStudents(usernames: string[]): Promise<Student[]> {
const members = await Promise.all(usernames.map(async (username) => await fetchStudent(username)));
return members;
}
export async function getStudent(username: string): Promise<StudentDTO> {
const user = await fetchStudent(username);
return mapToStudentDTO(user);