fix: integratie user + errors gefixt zodat het runt + format

This commit is contained in:
Gabriellvl 2025-03-09 23:59:31 +01:00
parent 6c4ea0eefb
commit 1b096b411b
55 changed files with 858 additions and 594 deletions

View file

@ -2,130 +2,108 @@ import {
getClassRepository,
getLearningObjectRepository,
getQuestionRepository,
getTeacherRepository
} from "../data/repositories.js";
import {mapToTeacher, mapToTeacherDTO, TeacherDTO} from "../interfaces/teacher.js";
import { Teacher } from "../entities/users/teacher.entity";
import {ClassDTO, mapToClassDTO} from "../interfaces/class";
import {getClassStudents, getClassStudentsIds} from "./class";
import {StudentDTO} from "../interfaces/student";
import {mapToQuestionDTO, QuestionDTO, QuestionId} from "../interfaces/question";
getStudentRepository,
getTeacherRepository,
} from '../data/repositories.js';
import { Teacher } from '../entities/users/teacher.entity.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { getClassStudents } from './class.js';
import { StudentDTO } from '../interfaces/student.js';
import {
mapToQuestionDTO,
mapToQuestionId,
QuestionDTO,
QuestionId,
} from '../interfaces/question.js';
import { UserService } from './users.js';
import { mapToUser } from '../interfaces/user.js';
async function fetchAllTeachers(): Promise<TeacherDTO[]> {
const teacherRepository = getTeacherRepository();
const teachers = await teacherRepository.find({});
return teachers.map(mapToTeacherDTO);
export class TeacherUserService extends UserService<Teacher> {
constructor() {
super(getTeacherRepository());
}
}
export async function getAllTeachers(): Promise<TeacherDTO[]> {
return await fetchAllTeachers();
}
export class TeacherService {
protected teacherService = new TeacherUserService();
protected teacherRepository = getTeacherRepository();
protected classRepository = getClassRepository();
protected learningObjectRepository = getLearningObjectRepository();
protected questionRepository = getQuestionRepository();
export async function getAllTeachersIds(): Promise<string[]> {
return await fetchAllTeachers().map((teacher) => teacher.username)
}
async fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
const teacher = await this.teacherRepository.findByUsername(username);
if (!teacher) {
return [];
}
export async function createTeacher(teacherData: TeacherDTO): Promise<Teacher> {
const teacherRepository = getTeacherRepository();
const newTeacher = mapToTeacher(teacherData);
await teacherRepository.addTeacher(newTeacher);
return newTeacher;
}
export async function getTeacherByUsername(username: string): Promise<TeacherDTO | null> {
const teacherRepository = getTeacherRepository();
const teacher = await teacherRepository.findByUsername(username);
return teacher ? mapToTeacherDTO(teacher) : null;
}
export async function deleteTeacher(username: string): Promise<TeacherDTO | null> {
const teacherRepository = getTeacherRepository();
const teacher = await teacherRepository.findByUsername(username);
if (!teacher)
return null;
await teacherRepository.deleteByUsername(username);
return teacher;
}
async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
const teacherRepository = getTeacherRepository();
const classRepository = getClassRepository();
const teacher = await teacherRepository.findByUsername(username);
if (!teacher) {
return [];
const classes = await this.classRepository.findByTeacher(teacher);
return classes.map(mapToClassDTO);
}
const classes = await classRepository.findByTeacher(teacher);
return classes.map(mapToClassDTO);
}
export async function getClassesByTeacher(username: string): Promise<ClassDTO[]> {
return await fetchClassesByTeacher(username)
}
export async function getClassIdsByTeacher(): Promise<string[]> {
return await fetchClassesByTeacher(username).map((cls) => cls.id);
}
async function fetchStudentsByTeacher(username: string) {
const classes = await getClassIdsByTeacher();
return Promise.all(
classes.map( async (id) => getClassStudents(id))
);
}
export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
return await fetchStudentsByTeacher(username);
}
export async function getStudentIdsByTeacher(): Promise<string[]> {
return await fetchStudentsByTeacher(username).map((student) => student.username);
}
async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> {
const learningObjectRepository = getLearningObjectRepository();
const questionRepository = getQuestionRepository();
const teacher = getTeacherByUsername(username);
if (!teacher) {
throw new Error(`Teacher with username '${username}' not found.`);
async getClassesByTeacher(username: string): Promise<ClassDTO[]> {
return await this.fetchClassesByTeacher(username);
}
// Find all learning objects that this teacher manages
const learningObjects = await learningObjectRepository.findAllByTeacher(teacher);
async getClassIdsByTeacher(username: string): Promise<string[]> {
const classes = await this.fetchClassesByTeacher(username);
return classes.map((cls) => {
return cls.id;
});
}
// Fetch all questions related to these learning objects
const questions = await questionRepository.findAllByLearningObjects(learningObjects);
async fetchStudentsByTeacher(username: string) {
const classes = await this.getClassIdsByTeacher(username);
return questions.map(mapToQuestionDTO);
return (
await Promise.all(
classes.map(async (id) => {
return getClassStudents(id);
})
)
).flat();
}
async getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
return await this.fetchStudentsByTeacher(username);
}
async getStudentIdsByTeacher(username: string): Promise<string[]> {
const students = await this.fetchStudentsByTeacher(username);
return students.map((student) => {
return student.username;
});
}
async fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> {
const teacherDTO =
await this.teacherService.getUserByUsername(username);
if (!teacherDTO) {
throw new Error(`Teacher with username '${username}' not found.`);
}
const teacher = mapToUser<Teacher>(teacherDTO, new Teacher());
// Find all learning objects that this teacher manages
const learningObjects =
await this.learningObjectRepository.findAllByTeacher(teacher);
// Fetch all questions related to these learning objects
const questions =
await this.questionRepository.findAllByLearningObjects(
learningObjects
);
return questions.map(mapToQuestionDTO);
}
async getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
return await this.fetchTeacherQuestions(username);
}
async getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
const questions = await this.fetchTeacherQuestions(username);
return questions.map(mapToQuestionId);
}
}
export async function getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
return await fetchTeacherQuestions(username);
}
export async function getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
const questions = await fetchTeacherQuestions(username);
return questions.map((question) => ({
learningObjectHruid: question.learningObjectHruid,
learningObjectLanguage: question.learningObjectLanguage,
learningObjectVersion: question.learningObjectVersion,
sequenceNumber: question.sequenceNumber
}));
}