merge: merged into feat/error-flow-backend

This commit is contained in:
Adriaan Jacquet 2025-04-06 17:49:55 +02:00
commit effaeb0277
249 changed files with 6832 additions and 3679 deletions

View file

@ -1,12 +1,17 @@
import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.js';
import { Class } from '../entities/classes/class.entity.js';
import { NotFoundException } from '../exceptions/not-found-exception.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js';
import { mapToClassDTO } from '../interfaces/class.js';
import { mapToStudentDTO } from '../interfaces/student.js';
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds } from '../interfaces/teacher-invitation.js';
import { getLogger } from '../logging/initalize.js';
const logger = getLogger();
import { NotFoundException } from '../exceptions/not-found-exception.js';
import { Class } from '../entities/classes/class.entity.js';
import { ClassDTO } from '@dwengo-1/common/interfaces/class';
import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation';
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
import {fetchTeacher} from "./teachers";
import {fetchStudent} from "./students";
import {TeacherDTO} from "@dwengo-1/common/interfaces/teacher";
import {mapToTeacherDTO} from "../interfaces/teacher";
export async function fetchClass(classid: string): Promise<Class> {
const classRepository = getClassRepository();
@ -36,38 +41,27 @@ export async function getClass(classId: string): Promise<ClassDTO | null> {
}
export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> {
const teacherRepository = getTeacherRepository();
const teacherUsernames = classData.teachers || [];
const teachers = (await Promise.all(teacherUsernames.map((id) => teacherRepository.findByUsername(id)))).filter((teacher) => teacher !== null);
const teachers = (await Promise.all(teacherUsernames.map(async (id) => fetchTeacher(id) )));
const studentRepository = getStudentRepository();
const studentUsernames = classData.students || [];
const students = (await Promise.all(studentUsernames.map((id) => studentRepository.findByUsername(id)))).filter((student) => student !== null);
const students = (await Promise.all(studentUsernames.map(async (id) => fetchStudent(id) )));
const classRepository = getClassRepository();
try {
const newClass = classRepository.create({
displayName: classData.displayName,
teachers: teachers,
students: students,
});
await classRepository.save(newClass);
const newClass = classRepository.create({
displayName: classData.displayName,
teachers: teachers,
students: students,
});
await classRepository.save(newClass, {preventOverwrite: true});
return mapToClassDTO(newClass);
} catch (e) {
logger.error(e);
return null;
}
return mapToClassDTO(newClass);
}
export async function deleteClass(classId: string): Promise<ClassDTO> {
const cls = await fetchClass(classId);
if (!cls) {
throw new NotFoundException('Could not delete class because it does not exist');
}
const classRepository = getClassRepository();
await classRepository.deleteById(classId);
@ -80,10 +74,23 @@ export async function getClassStudents(classId: string, full: boolean): Promise<
if (full) {
return cls.students.map(mapToStudentDTO);
}
return cls.students.map((student) => student.username);
}
export async function getClassStudentsDTO(classId: string): Promise<StudentDTO[]> {
const cls = await fetchClass(classId);
return cls.students.map(mapToStudentDTO);
}
export async function getClassTeachers(classId: string, full: boolean): Promise<TeacherDTO[] | string[]> {
const cls = await fetchClass(classId);
if (full){
return cls.teachers.map(mapToTeacherDTO);
}
return cls.teachers.map((student) => student.username);
}
export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> {
const cls = await fetchClass(classId);