|
| 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: model.pronouns, |
| 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: model.submittedAt.getSeconds(), */ |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +class ReviewPageService implements IReviewPageService { |
| 41 | + /* eslint-disable class-methods-use-this */ |
| 42 | + async getReviewPage(applicantRecordId: string): Promise<ApplicationDTO> { |
| 43 | + try { |
| 44 | + const applicantRecord: ApplicantRecord | null = |
| 45 | + await ApplicantRecord.findOne({ |
| 46 | + where: { id: applicantRecordId }, |
| 47 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 48 | + }); |
| 49 | + if (!applicantRecord) |
| 50 | + throw new Error(`Database integrity has been violated`); |
| 51 | + |
| 52 | + const applicant: Applicant | null = await Applicant.findOne({ |
| 53 | + where: { id: applicantRecord.applicantId }, |
| 54 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 55 | + include: [ |
| 56 | + { |
| 57 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 58 | + association: "applicantRecords", |
| 59 | + }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + if (!applicant) throw new Error(`Database integrity has been violated`); |
| 63 | + |
| 64 | + return toDTO(applicant); |
| 65 | + } catch (error: unknown) { |
| 66 | + Logger.error(`Failed to fetch. Reason = ${getErrorMessage(error)}`); |
| 67 | + throw error; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export default ReviewPageService; |
0 commit comments