import { getClassJoinRequestRepository, getClassRepository, getGroupRepository, getQuestionRepository, getStudentRepository, getSubmissionRepository, } from '../data/repositories.js'; import { AssignmentDTO } from '../interfaces/assignment.js'; import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js'; import { getAllAssignments } from './assignments.js'; import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question'; import {ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; import {ConflictException, NotFoundException} from "../exceptions"; import {Student} from "../entities/users/student.entity"; import {mapToStudentRequestDTO} from "../interfaces/student-request"; export async function getAllStudents(full: boolean): Promise { const studentRepository = getStudentRepository(); const users = await studentRepository.findAll(); if (full) return users.map(mapToStudentDTO); return users.map((user) => user.username); } export async function fetchStudent(username: string): Promise { const studentRepository = getStudentRepository(); const user = await studentRepository.findByUsername(username); if (!user) { throw new NotFoundException("Student with username not found"); } return user; } export async function getStudent(username: string): Promise { const user = await fetchStudent(username); return mapToStudentDTO(user); } export async function createStudent(userData: StudentDTO): Promise { const studentRepository = getStudentRepository(); const user = await studentRepository.findByUsername(userData.username); if (user) { throw new ConflictException("Student with that sername already exists"); } const newStudent = studentRepository.create(mapToStudent(userData)); await studentRepository.save(newStudent); } export async function deleteStudent(username: string): Promise { const studentRepository = getStudentRepository(); await fetchStudent(username); // throws error if it does not exist await studentRepository.deleteByUsername(username); } export async function getStudentClasses(username: string, full: boolean): Promise { const student = await fetchStudent(username); const classRepository = getClassRepository(); const classes = await classRepository.findByStudent(student); if (full) { return classes.map(mapToClassDTO); } return classes.map((cls) => cls.classId!); } export async function getStudentAssignments(username: string, full: boolean): Promise { const student = await fetchStudent(username); const classRepository = getClassRepository(); const classes = await classRepository.findByStudent(student); return (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat(); } export async function getStudentGroups(username: string, full: boolean): Promise { const student = await fetchStudent(username); const groupRepository = getGroupRepository(); const groups = await groupRepository.findAllGroupsWithStudent(student); if (full) { return groups.map(mapToGroupDTO); } return groups.map(mapToGroupDTOId); } export async function getStudentSubmissions(username: string): Promise { const student = await fetchStudent(username); const submissionRepository = getSubmissionRepository(); const submissions = await submissionRepository.findAllSubmissionsForStudent(student); return submissions.map(mapToSubmissionDTO); } export async function getStudentQuestions(username: string, full: boolean): Promise { const student = await fetchStudent(username); const questionRepository = getQuestionRepository(); const questions = await questionRepository.findAllByAuthor(student); const questionsDTO = questions.map(mapToQuestionDTO); if (full) return questionsDTO; return questionsDTO.map(mapToQuestionId); } export async function createClassJoinRequest(studentUsername: string, classId: string) { const classRepo = getClassRepository(); const requestRepo = getClassJoinRequestRepository(); const student = await fetchStudent(studentUsername); const cls = await classRepo.findById(classId); if (!cls){ throw new NotFoundException("Class with id not found"); } const req = await requestRepo.findByStudentAndClass(student, cls); if (req){ throw new ConflictException("Request with student and class already exist"); } const request = requestRepo.create({ requester: student, class: cls, status: ClassJoinRequestStatus.Open, }); await requestRepo.save(request); } export async function getJoinRequestsByStudent(studentUsername: string) { const requestRepo = getClassJoinRequestRepository(); const student = await fetchStudent(studentUsername); const requests = await requestRepo.findAllRequestsBy(student); return requests.map(mapToStudentRequestDTO); } export async function deleteClassJoinRequest(studentUsername: string, classId: string) { const requestRepo = getClassJoinRequestRepository(); const classRepo = getClassRepository(); const student = await fetchStudent(studentUsername); const cls = await classRepo.findById(classId); if (!cls) { throw new NotFoundException('Class not found'); } const request = await requestRepo.findByStudentAndClass(student, cls); if (!request) { throw new NotFoundException('Join request not found'); } await requestRepo.deleteBy(student, cls); }