1- import { ApplicationDTO , ReviewedApplicantsDTO } from "../../types" ;
1+ import {
2+ ApplicationDTO ,
3+ ReviewedApplicantRecordDTO ,
4+ ReviewedApplicantsDTO ,
5+ } from "../../types" ;
26import IReviewPageService from "../interfaces/IReviewPageService" ;
37import { getErrorMessage } from "../../utilities/errorUtils" ;
48import logger from "../../utilities/logger" ;
59import ApplicantRecord from "../../models/applicantRecord.model" ;
6- import type ReviewedApplicantRecord from "../../models/reviewedApplicantRecord.model" ;
10+ import ReviewedApplicantRecord from "../../models/reviewedApplicantRecord.model" ;
711import Applicant from "../../models/applicant.model" ;
812import User from "../../models/user.model" ;
913
@@ -39,7 +43,7 @@ function toApplicationDTO(model: Applicant): ApplicationDTO {
3943 } ;
4044}
4145
42- function toReviewedApplicantRecordDTO (
46+ function toReviewedApplicantsDTO (
4347 model : ReviewedApplicantRecord ,
4448) : ReviewedApplicantsDTO {
4549 /* eslint-disable @typescript-eslint/no-non-null-assertion */
@@ -51,6 +55,18 @@ function toReviewedApplicantRecordDTO(
5155 } ;
5256}
5357
58+ function toReviewedApplicantRecordDTO (
59+ model : ReviewedApplicantRecord ,
60+ ) : ReviewedApplicantRecordDTO {
61+ return {
62+ applicantRecordId : model . applicantRecordId ,
63+ reviewerId : model . reviewerId ,
64+ review : model . review ,
65+ status : model . status ,
66+ reviewerHasConflict : model . reviewerHasConflict ,
67+ } ;
68+ }
69+
5470class ReviewPageService implements IReviewPageService {
5571 /* eslint-disable class-methods-use-this */
5672 async getReviewPage ( applicantRecordId : string ) : Promise < ApplicationDTO > {
@@ -60,8 +76,9 @@ class ReviewPageService implements IReviewPageService {
6076 where : { id : applicantRecordId } ,
6177 attributes : { exclude : [ "createdAt" , "updatedAt" ] } ,
6278 } ) ;
63- if ( ! applicantRecord )
79+ if ( ! applicantRecord ) {
6480 throw new Error ( `Database integrity has been violated` ) ;
81+ }
6582
6683 const applicant : Applicant | null = await Applicant . findOne ( {
6784 where : { id : applicantRecord . applicantId } ,
@@ -73,7 +90,9 @@ class ReviewPageService implements IReviewPageService {
7390 } ,
7491 ] ,
7592 } ) ;
76- if ( ! applicant ) throw new Error ( `Database integrity has been violated` ) ;
93+ if ( ! applicant ) {
94+ throw new Error ( `Database integrity has been violated` ) ;
95+ }
7796
7897 return toApplicationDTO ( applicant ) ;
7998 } catch ( error : unknown ) {
@@ -108,14 +127,43 @@ class ReviewPageService implements IReviewPageService {
108127 } ,
109128 ] ,
110129 } ) ;
111- if ( ! user ) throw new Error ( `No user with ${ userId } found.` ) ;
112- if ( ! user . reviewedApplicantRecords ) return [ ] ;
113- return user . reviewedApplicantRecords . map ( toReviewedApplicantRecordDTO ) ;
130+ if ( ! user ) {
131+ throw new Error ( `No user with ${ userId } found.` ) ;
132+ }
133+ if ( ! user . reviewedApplicantRecords ) {
134+ return [ ] ;
135+ }
136+ return user . reviewedApplicantRecords . map ( toReviewedApplicantsDTO ) ;
114137 } catch ( error : unknown ) {
115138 Logger . error ( `Failed to fetch. Reason = ${ getErrorMessage ( error ) } ` ) ;
116139 throw error ;
117140 }
118141 }
142+
143+ async reportReviewConflict (
144+ applicantRecordId : string ,
145+ reviewerId : number ,
146+ ) : Promise < ReviewedApplicantRecordDTO > {
147+ try {
148+ const reviewedApplicantRecord : ReviewedApplicantRecord | null =
149+ await ReviewedApplicantRecord . findOne ( {
150+ where : { applicantRecordId, reviewerId } ,
151+ } ) ;
152+ if ( ! reviewedApplicantRecord ) {
153+ throw new Error (
154+ `No reviewed applicant record with ${ applicantRecordId } and ${ reviewerId } found.` ,
155+ ) ;
156+ }
157+ reviewedApplicantRecord . reviewerHasConflict = true ;
158+ await reviewedApplicantRecord . save ( ) ;
159+ return toReviewedApplicantRecordDTO ( reviewedApplicantRecord ) ;
160+ } catch ( error : unknown ) {
161+ Logger . error (
162+ `Failed to report conflict. Reason = ${ getErrorMessage ( error ) } ` ,
163+ ) ;
164+ throw error ;
165+ }
166+ }
119167}
120168
121169export default ReviewPageService ;
0 commit comments