fix: merge errors
This commit is contained in:
		
							parent
							
								
									066f9e64c6
								
							
						
					
					
						commit
						66a65c1e67
					
				
					 7 changed files with 97 additions and 15 deletions
				
			
		|  | @ -1,4 +1,4 @@ | ||||||
| import { BadRequestException } from '../exceptions.js'; | import {BadRequestException} from "../exceptions/bad-request-exception"; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Checks for the presence of required fields and throws a BadRequestException |  * Checks for the presence of required fields and throws a BadRequestException | ||||||
|  |  | ||||||
|  | @ -7,8 +7,7 @@ import { | ||||||
|     getStudent, |     getStudent, | ||||||
|     getStudentAssignments, |     getStudentAssignments, | ||||||
|     getStudentClasses, |     getStudentClasses, | ||||||
|     getStudentGroups, |     getStudentGroups, getStudentQuestions, | ||||||
|     getStudentQuestions, |  | ||||||
|     getStudentSubmissions, |     getStudentSubmissions, | ||||||
| } from '../services/students.js'; | } from '../services/students.js'; | ||||||
| import { StudentDTO } from '../interfaces/student.js'; | import { StudentDTO } from '../interfaces/student.js'; | ||||||
|  |  | ||||||
|  | @ -1,5 +1,8 @@ | ||||||
| import {mapToStudentDTO, StudentDTO} from "./student"; | import {mapToStudentDTO, StudentDTO} from "./student"; | ||||||
| import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; | import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; | ||||||
|  | import {getClassJoinRequestRepository} from "../data/repositories"; | ||||||
|  | import {Student} from "../entities/users/student.entity"; | ||||||
|  | import {Class} from "../entities/classes/class.entity"; | ||||||
| 
 | 
 | ||||||
| export interface StudentRequestDTO { | export interface StudentRequestDTO { | ||||||
|     requester: StudentDTO; |     requester: StudentDTO; | ||||||
|  | @ -14,3 +17,11 @@ export function mapToStudentRequestDTO(request: ClassJoinRequest): StudentReques | ||||||
|         status: request.status, |         status: request.status, | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export function mapToStudentRequest(student: Student, cls: Class) { | ||||||
|  |     return getClassJoinRequestRepository().create({ | ||||||
|  |         requester: student, | ||||||
|  |         class: cls, | ||||||
|  |         status: ClassJoinRequestStatus.Open, | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -3,9 +3,22 @@ import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | ||||||
| import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; | import { mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; | ||||||
| import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js'; | import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from '../interfaces/teacher-invitation.js'; | ||||||
| import { getLogger } from '../logging/initalize.js'; | import { getLogger } from '../logging/initalize.js'; | ||||||
|  | import {NotFoundException} from "../exceptions/not-found-exception"; | ||||||
|  | import {Class} from "../entities/classes/class.entity"; | ||||||
| 
 | 
 | ||||||
| const logger = getLogger(); | const logger = getLogger(); | ||||||
| 
 | 
 | ||||||
|  | export async function fetchClass(classId: string): Promise<Class> { | ||||||
|  |     const classRepository = getClassRepository(); | ||||||
|  |     const cls = await classRepository.findById(classId); | ||||||
|  | 
 | ||||||
|  |     if (!cls) { | ||||||
|  |         throw new NotFoundException("Class with id not found"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return cls; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> { | export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|     const classes = await classRepository.find({}, { populate: ['students', 'teachers'] }); |     const classes = await classRepository.find({}, { populate: ['students', 'teachers'] }); | ||||||
|  |  | ||||||
|  | @ -1,4 +1,11 @@ | ||||||
| import { getClassRepository, getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; | import { | ||||||
|  |     getClassJoinRequestRepository, | ||||||
|  |     getClassRepository, | ||||||
|  |     getGroupRepository, | ||||||
|  |     getQuestionRepository, | ||||||
|  |     getStudentRepository, | ||||||
|  |     getSubmissionRepository | ||||||
|  | } from '../data/repositories.js'; | ||||||
| import { AssignmentDTO } from '../interfaces/assignment.js'; | import { AssignmentDTO } from '../interfaces/assignment.js'; | ||||||
| import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | ||||||
| import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | ||||||
|  | @ -6,9 +13,10 @@ import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student | ||||||
| import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js'; | import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js'; | ||||||
| import { getAllAssignments } from './assignments.js'; | import { getAllAssignments } from './assignments.js'; | ||||||
| import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question'; | import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question'; | ||||||
| import {ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; | import {mapToStudentRequest, mapToStudentRequestDTO} from "../interfaces/student-request"; | ||||||
| import {ConflictException, NotFoundException} from "../exceptions"; | import {Student} from "../entities/users/student.entity"; | ||||||
| import {mapToStudentRequestDTO} from "../interfaces/student-request"; | import {NotFoundException} from "../exceptions/not-found-exception"; | ||||||
|  | import {fetchClass} from "./classes"; | ||||||
| 
 | 
 | ||||||
| export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> { | export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> { | ||||||
|     const studentRepository = getStudentRepository(); |     const studentRepository = getStudentRepository(); | ||||||
|  | @ -98,3 +106,53 @@ export async function getStudentSubmissions(username: string, full: boolean): Pr | ||||||
| 
 | 
 | ||||||
|     return submissions.map(mapToSubmissionDTOId); |     return submissions.map(mapToSubmissionDTOId); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export async function getStudentQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { | ||||||
|  |     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); // throws error if student not found
 | ||||||
|  |     const cls = await fetchClass(classId); | ||||||
|  | 
 | ||||||
|  |     const request = mapToStudentRequest(student, cls); | ||||||
|  |     await requestRepo.save(request, { preventOverwrite: true }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 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 student = await fetchStudent(studentUsername); | ||||||
|  |     const cls = await fetchClass(classId); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     const request = await requestRepo.findByStudentAndClass(student, cls); | ||||||
|  | 
 | ||||||
|  |     if (!request) { | ||||||
|  |         throw new NotFoundException('Join request not found'); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     await requestRepo.deleteBy(student, cls); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -6,10 +6,8 @@ import { | ||||||
|     getTeacherRepository, |     getTeacherRepository, | ||||||
| } from '../data/repositories.js'; | } from '../data/repositories.js'; | ||||||
| import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; | ||||||
| import { getClassStudents } from './class.js'; |  | ||||||
| import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from '../interfaces/question.js'; | import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from '../interfaces/question.js'; | ||||||
| import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js'; | import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js'; | ||||||
| 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 {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; | import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity"; | ||||||
|  | @ -24,6 +22,8 @@ import {QuestionRepository} from "../data/questions/question-repository"; | ||||||
| import {Question} from "../entities/questions/question.entity"; | import {Question} from "../entities/questions/question.entity"; | ||||||
| import {ClassJoinRequestRepository} from "../data/classes/class-join-request-repository"; | import {ClassJoinRequestRepository} from "../data/classes/class-join-request-repository"; | ||||||
| import {Student} from "../entities/users/student.entity"; | import {Student} from "../entities/users/student.entity"; | ||||||
|  | import {NotFoundException} from "../exceptions/not-found-exception"; | ||||||
|  | import {getClassStudents} from "./classes"; | ||||||
| 
 | 
 | ||||||
| export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> { | export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> { | ||||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); |     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||||
|  |  | ||||||
|  | @ -12,11 +12,13 @@ import { | ||||||
|     getStudentQuestionsHandler, |     getStudentQuestionsHandler, | ||||||
|     createStudentRequestHandler, |     createStudentRequestHandler, | ||||||
|     getStudentRequestHandler, |     getStudentRequestHandler, | ||||||
|     updateClassJoinRequestHandler, |  | ||||||
|     deleteClassJoinRequestHandler |     deleteClassJoinRequestHandler | ||||||
| } from '../../src/controllers/students.js'; | } from '../../src/controllers/students.js'; | ||||||
| import {TEST_STUDENTS} from "../test_assets/users/students.testdata"; | import {TEST_STUDENTS} from "../test_assets/users/students.testdata"; | ||||||
| import {BadRequestException, ConflictException, NotFoundException} from "../../src/exceptions"; | import {NotFoundException} from "../../src/exceptions/not-found-exception"; | ||||||
|  | import {BadRequestException} from "../../src/exceptions/bad-request-exception"; | ||||||
|  | import {ConflictException} from "../../src/exceptions/conflict-exception"; | ||||||
|  | import {EntityAlreadyExistsException} from "../../src/exceptions/entity-already-exists-exception"; | ||||||
| 
 | 
 | ||||||
| describe('Student controllers', () => { | describe('Student controllers', () => { | ||||||
|     let req: Partial<Request>; |     let req: Partial<Request>; | ||||||
|  | @ -57,8 +59,8 @@ describe('Student controllers', () => { | ||||||
|     it('Create student', async () => { |     it('Create student', async () => { | ||||||
|         req = { |         req = { | ||||||
|             body: { |             body: { | ||||||
|                 username: 'coolstudent', |                 username: 'NewstudentId21', | ||||||
|                 firstName: 'Cool', |                 firstName: 'New', | ||||||
|                 lastName: 'Student' |                 lastName: 'Student' | ||||||
|             } |             } | ||||||
|         }; |         }; | ||||||
|  | @ -69,7 +71,6 @@ describe('Student controllers', () => { | ||||||
|         expect(jsonMock).toHaveBeenCalled(); |         expect(jsonMock).toHaveBeenCalled(); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     // TODO create duplicate student id
 |  | ||||||
| 
 | 
 | ||||||
|     it('Create duplicate student', async () => { |     it('Create duplicate student', async () => { | ||||||
|         req = { |         req = { | ||||||
|  | @ -82,7 +83,7 @@ describe('Student controllers', () => { | ||||||
| 
 | 
 | ||||||
|         await expect(() => createStudentHandler(req as Request, res as Response)) |         await expect(() => createStudentHandler(req as Request, res as Response)) | ||||||
|             .rejects |             .rejects | ||||||
|             .toThrowError(ConflictException); |             .toThrowError(EntityAlreadyExistsException); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it('Create student no body', async () => { |     it('Create student no body', async () => { | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl