|
| 1 | +import { ApplicationDTO } from "../../types"; |
| 2 | +import IReviewPageService from "../interfaces/IReviewPageService"; |
| 3 | +import { getErrorMessage } from "../../utilities/errorUtils"; |
| 4 | +import logger from "../../utilities/logger"; |
| 5 | +import ApplicantRecord from "../../models/applicantRecord.model"; |
| 6 | +import ReviewedApplicantRecord from "../../models/reviewedApplicantRecord.model"; |
| 7 | +import Applicant from "../../models/applicant.model"; |
| 8 | + |
| 9 | +const Logger = logger(__filename); |
| 10 | + |
| 11 | +function toDTO(model: Applicant): ApplicationDTO { |
| 12 | + const firstChoice = model.applicantRecords!.find((ar) => ar.choice === 1); |
| 13 | + const secondChoice = model.applicantRecords!.find((ar) => ar.choice === 2); |
| 14 | + |
| 15 | + return { |
| 16 | + id: model.id, |
| 17 | + academicOrCoop: model.academicOrCoop, |
| 18 | + academicYear: model.academicYear, |
| 19 | + email: model.email, |
| 20 | + firstChoiceRole: firstChoice!.position, |
| 21 | + firstName: model.firstName, |
| 22 | + lastName: model.lastName, |
| 23 | + heardFrom: model.heardFrom, |
| 24 | + locationPreference: model.locationPreference, |
| 25 | + program: model.program, |
| 26 | + timesApplied: model.timesApplied.toString(), |
| 27 | + pronouns: model.pronouns, |
| 28 | + pronounsSpecified: "", |
| 29 | + resumeUrl: model.resumeUrl, |
| 30 | + roleSpecificQuestions: firstChoice!.roleSpecificQuestions, |
| 31 | + secondChoiceRole: secondChoice ? secondChoice.position : "", |
| 32 | + shortAnswerQuestions: model.shortAnswerQuestions, |
| 33 | + status: firstChoice!.status, |
| 34 | + term: model.term, |
| 35 | + secondChoiceStatus: secondChoice ? secondChoice.status : "", |
| 36 | + timestamp: BigInt(1728673405), |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +class ReviewPageService implements IReviewPageService { |
| 41 | + /* eslint-disable class-methods-use-this */ |
| 42 | + async getReviewPage( |
| 43 | + reviewedApplicantRecordId: number, |
| 44 | + ): Promise<ApplicationDTO[]> { |
| 45 | + try { |
| 46 | + // get applicant ids first |
| 47 | + const rARs: Array<ReviewedApplicantRecord> = |
| 48 | + await ReviewedApplicantRecord.findAll({ |
| 49 | + where: { applicantRecordId: reviewedApplicantRecordId }, |
| 50 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 51 | + }); |
| 52 | + if (rARs.length === 0) |
| 53 | + throw new Error( |
| 54 | + `ReviewedApplicantRecords with ID ${reviewedApplicantRecordId} not found.`, |
| 55 | + ); |
| 56 | + |
| 57 | + const aRIds: Array<number> = [ |
| 58 | + ...new Set(rARs.map((r) => r.applicantRecordId)), |
| 59 | + ]; |
| 60 | + const aRs: Array<ApplicantRecord> = await ApplicantRecord.findAll({ |
| 61 | + where: { id: aRIds }, |
| 62 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 63 | + }); |
| 64 | + if (aRs.length === 0) |
| 65 | + throw new Error(`Database integrity has been violated`); |
| 66 | + |
| 67 | + const aIds: Array<number> = [...new Set(aRs.map((a) => a.applicantId))]; |
| 68 | + const as: Array<Applicant> = await Applicant.findAll({ |
| 69 | + where: { id: aIds }, |
| 70 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 71 | + include: [ |
| 72 | + { |
| 73 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 74 | + association: "applicantRecords", |
| 75 | + }, |
| 76 | + ], |
| 77 | + }); |
| 78 | + if (as.length === 0) |
| 79 | + throw new Error(`Database integrity has been violated`); |
| 80 | + |
| 81 | + return as.map(toDTO); |
| 82 | + } catch (error: unknown) { |
| 83 | + Logger.error(`Failed to fetch. Reason = ${getErrorMessage(error)}`); |
| 84 | + throw error; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export default ReviewPageService; |
0 commit comments