fix: types in teacher

This commit is contained in:
Gabriellvl 2025-03-30 15:17:01 +02:00
parent c8cff2e820
commit cf5b446a47

View file

@ -2,7 +2,7 @@ import {
getClassJoinRequestRepository, getClassJoinRequestRepository,
getClassRepository, getClassRepository,
getLearningObjectRepository, getLearningObjectRepository,
getQuestionRepository, getStudentRepository, getQuestionRepository,
getTeacherRepository, getTeacherRepository,
} from '../data/repositories.js'; } from '../data/repositories.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
@ -12,12 +12,22 @@ import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher
import {ConflictException, NotFoundException} from "../exceptions"; import {ConflictException, NotFoundException} from "../exceptions";
import {Teacher} from "../entities/users/teacher.entity"; import {Teacher} from "../entities/users/teacher.entity";
import {fetchStudent} from "./students"; import {fetchStudent} from "./students";
import {ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity";
import {mapToStudentRequestDTO} from "../interfaces/student-request"; import {mapToStudentRequestDTO, StudentRequestDTO} from "../interfaces/student-request";
import {TeacherRepository} from "../data/users/teacher-repository";
import {ClassRepository} from "../data/classes/class-repository";
import {Class} from "../entities/classes/class.entity";
import {StudentDTO} from "../interfaces/student";
import {LearningObjectRepository} from "../data/content/learning-object-repository";
import {LearningObject} from "../entities/content/learning-object.entity";
import {QuestionRepository} from "../data/questions/question-repository";
import {Question} from "../entities/questions/question.entity";
import {ClassJoinRequestRepository} from "../data/classes/class-join-request-repository";
import {Student} from "../entities/users/student.entity";
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> { export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
const teacherRepository = getTeacherRepository(); const teacherRepository: TeacherRepository = getTeacherRepository();
const users = await teacherRepository.findAll(); const users: Teacher[] = await teacherRepository.findAll();
if (full) { if (full) {
return users.map(mapToTeacherDTO); return users.map(mapToTeacherDTO);
@ -26,8 +36,8 @@ export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | stri
} }
export async function fetchTeacher(username: string): Promise<Teacher> { export async function fetchTeacher(username: string): Promise<Teacher> {
const studentRepository = getStudentRepository(); const teacherRepository: TeacherRepository = getTeacherRepository();
const user = await studentRepository.findByUsername(username); const user: Teacher | null = await teacherRepository.findByUsername(username);
if (!user) { if (!user) {
throw new NotFoundException("Teacher with username not found"); throw new NotFoundException("Teacher with username not found");
@ -36,26 +46,26 @@ export async function fetchTeacher(username: string): Promise<Teacher> {
return user; return user;
} }
export async function getTeacher(username: string): Promise<TeacherDTO | null> { export async function getTeacher(username: string): Promise<TeacherDTO> {
const user = await fetchTeacher(username); const user: Teacher = await fetchTeacher(username);
return mapToTeacherDTO(user); return mapToTeacherDTO(user);
} }
export async function createTeacher(userData: TeacherDTO): Promise<void> { export async function createTeacher(userData: TeacherDTO): Promise<void> {
const teacherRepository = getTeacherRepository(); const teacherRepository: TeacherRepository = getTeacherRepository();
const user = await teacherRepository.findByUsername(userData.username); const user: Teacher | null = await teacherRepository.findByUsername(userData.username);
if (user){ if (user){
throw new ConflictException("Teacher with that username already exists"); throw new ConflictException("Teacher with that username already exists");
} }
const newTeacher = teacherRepository.create(mapToTeacher(userData)); const newTeacher: Teacher = teacherRepository.create(mapToTeacher(userData));
await teacherRepository.save(newTeacher); await teacherRepository.save(newTeacher);
} }
export async function deleteTeacher(username: string): Promise<void> { export async function deleteTeacher(username: string): Promise<void> {
const teacherRepository = getTeacherRepository(); const teacherRepository: TeacherRepository = getTeacherRepository();
await fetchTeacher(username); // throws error if it does not exist await fetchTeacher(username); // throws error if it does not exist
@ -63,15 +73,15 @@ export async function deleteTeacher(username: string): Promise<void> {
} }
async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> { async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
const teacher = await fetchTeacher(username); const teacher: Teacher = await fetchTeacher(username);
const classRepository = getClassRepository(); const classRepository: ClassRepository = getClassRepository();
const classes = await classRepository.findByTeacher(teacher); const classes: Class[] = await classRepository.findByTeacher(teacher);
return classes.map(mapToClassDTO); return classes.map(mapToClassDTO);
} }
export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[]> { export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[]> {
const classes = await fetchClassesByTeacher(username); const classes: ClassDTO[] = await fetchClassesByTeacher(username);
if (full) { if (full) {
return classes; return classes;
@ -80,15 +90,15 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom
} }
export async function getStudentsByTeacher(username: string, full: boolean) { export async function getStudentsByTeacher(username: string, full: boolean) {
const classes = await fetchClassesByTeacher(username); const classes: ClassDTO[] = await fetchClassesByTeacher(username);
if (!classes || classes.length === 0){ if (!classes || classes.length === 0){
return []; return [];
} }
const classIds = classes.map((cls) => cls.id); const classIds: string[] = classes.map((cls) => cls.id);
const students = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat(); const students: StudentDTO[] = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat();
if (full) { if (full) {
return students; return students;
} }
@ -96,20 +106,20 @@ export async function getStudentsByTeacher(username: string, full: boolean) {
} }
export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
const teacher = await fetchTeacher(username); const teacher: Teacher = await fetchTeacher(username);
// Find all learning objects that this teacher manages // Find all learning objects that this teacher manages
const learningObjectRepository = getLearningObjectRepository(); const learningObjectRepository: LearningObjectRepository = getLearningObjectRepository();
const learningObjects = await learningObjectRepository.findAllByTeacher(teacher); const learningObjects: LearningObject[] = await learningObjectRepository.findAllByTeacher(teacher);
if (!learningObjects || learningObjects.length === 0){ if (!learningObjects || learningObjects.length === 0){
return []; return [];
} }
// Fetch all questions related to these learning objects // Fetch all questions related to these learning objects
const questionRepository = getQuestionRepository(); const questionRepository: QuestionRepository = getQuestionRepository();
const questions = await questionRepository.findAllByLearningObjects(learningObjects); const questions: Question[] = await questionRepository.findAllByLearningObjects(learningObjects);
const questionsDTO = questions.map(mapToQuestionDTO); const questionsDTO: QuestionDTO[] = questions.map(mapToQuestionDTO);
if (full) { if (full) {
return questionsDTO; return questionsDTO;
@ -118,31 +128,31 @@ export async function getTeacherQuestions(username: string, full: boolean): Prom
return questionsDTO.map(mapToQuestionId); return questionsDTO.map(mapToQuestionId);
} }
export async function getJoinRequestsByClass( classId: string ){ export async function getJoinRequestsByClass( classId: string ): Promise<StudentRequestDTO[]> {
const classRepository = getClassRepository(); const classRepository: ClassRepository = getClassRepository();
const cls = await classRepository.findById(classId); const cls: Class | null = await classRepository.findById(classId);
if (!cls) { if (!cls) {
throw new NotFoundException("Class with id not found"); throw new NotFoundException("Class with id not found");
} }
const requestRepo = getClassJoinRequestRepository(); const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository();
const requests = await requestRepo.findAllOpenRequestsTo(cls); const requests: ClassJoinRequest[] = await requestRepo.findAllOpenRequestsTo(cls);
return requests.map(mapToStudentRequestDTO); return requests.map(mapToStudentRequestDTO);
} }
export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true) { export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true): Promise<void> {
const requestRepo = getClassJoinRequestRepository(); const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository();
const classRepo = getClassRepository(); const classRepo: ClassRepository = getClassRepository();
const student = await fetchStudent(studentUsername); const student: Student = await fetchStudent(studentUsername);
const cls = await classRepo.findById(classId); const cls: Class | null = await classRepo.findById(classId);
if (!cls) { if (!cls) {
throw new NotFoundException('Class not found'); throw new NotFoundException('Class not found');
} }
const request = await requestRepo.findByStudentAndClass(student, cls); const request: ClassJoinRequest | null = await requestRepo.findByStudentAndClass(student, cls);
if (!request) { if (!request) {
throw new NotFoundException('Join request not found'); throw new NotFoundException('Join request not found');