|
| 1 | +import { PositionTitle, ReviewDashboardRowDTO } from "../../types"; |
| 2 | +import IReviewDashboardService from "../interfaces/IReviewDashboardService"; |
| 3 | +import { getErrorMessage } from "../../utilities/errorUtils"; |
| 4 | +import logger from "../../utilities/logger"; |
| 5 | +import ApplicantRecord from "../../models/applicantRecord.model"; |
| 6 | + |
| 7 | +const Logger = logger(__filename); |
| 8 | + |
| 9 | +function toDTO(model: ApplicantRecord): ReviewDashboardRowDTO { |
| 10 | + return { |
| 11 | + firstName: model.applicant!.firstName, |
| 12 | + lastName: model.applicant!.lastName, |
| 13 | + position: model.position as PositionTitle, |
| 14 | + timesApplied: model.applicant!.timesApplied.toString(), |
| 15 | + applicationStatus: model.status, |
| 16 | + choice: model.choice, |
| 17 | + reviewers: model.reviewedApplicantRecords!.map((r) => ({ |
| 18 | + firstName: r.user!.first_name, |
| 19 | + lastName: r.user!.last_name, |
| 20 | + })), |
| 21 | + totalScore: model.combined_score, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +class ReviewDashboardService implements IReviewDashboardService { |
| 26 | + /* eslint-disable class-methods-use-this */ |
| 27 | + async getReviewDashboard( |
| 28 | + pageNumber: number, |
| 29 | + resultsPerPage: number, |
| 30 | + ): Promise<ReviewDashboardRowDTO[]> { |
| 31 | + try { |
| 32 | + const perPage = Number.isFinite(Number(resultsPerPage)) |
| 33 | + ? Number(resultsPerPage) |
| 34 | + : 1; |
| 35 | + const currentPage = Number.isFinite(Number(pageNumber)) |
| 36 | + ? Number(pageNumber) |
| 37 | + : 1; |
| 38 | + const offsetRow = (currentPage - 1) * perPage; |
| 39 | + |
| 40 | + // get applicant_record |
| 41 | + // JOIN applicant ON applicant_id |
| 42 | + // JOIN reviewed_applicant_record ON applicant_record_id |
| 43 | + // JOIN user ON reviewer_id |
| 44 | + const applicants: Array<ApplicantRecord> | null = |
| 45 | + await ApplicantRecord.findAll({ |
| 46 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 47 | + include: [ |
| 48 | + { |
| 49 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 50 | + association: "reviewedApplicantRecords", |
| 51 | + include: [ |
| 52 | + { |
| 53 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 54 | + association: "user", |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + attributes: { exclude: ["createdAt", "updatedAt"] }, |
| 60 | + association: "applicant", |
| 61 | + }, |
| 62 | + ], |
| 63 | + order: [["id", "ASC"]], |
| 64 | + limit: perPage, |
| 65 | + offset: offsetRow, |
| 66 | + }); |
| 67 | + return applicants.map(toDTO); |
| 68 | + } catch (error: unknown) { |
| 69 | + Logger.error( |
| 70 | + `Failed to get dashboard. Reason = ${getErrorMessage(error)}`, |
| 71 | + ); |
| 72 | + throw error; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export default ReviewDashboardService; |
0 commit comments