11import { sequelize } from "../../models" ;
22import ReviewedApplicantRecord from "../../models/reviewedApplicantRecord.model" ;
3- import {
4- ReviewedApplicantRecordDTO ,
5- CreateReviewedApplicantRecordDTO ,
6- DeleteReviewedApplicantRecordDTO
3+ import {
4+ ReviewedApplicantRecordDTO ,
5+ CreateReviewedApplicantRecordDTO ,
6+ DeleteReviewedApplicantRecordDTO ,
77} from "../../types" ;
8-
8+ import { getErrorMessage } from "../../utilities/errorUtils" ;
9+ import logger from "../../utilities/logger" ;
910import IReviewApplicantRecordService from "../interfaces/IReviewedApplicantRecordService" ;
1011
11- class ReviewedApplicantRecordService implements IReviewApplicantRecordService {
12+ const Logger = logger ( __filename ) ;
1213
13- async createReviewedApplicantRecord (
14- dto : CreateReviewedApplicantRecordDTO
15- ) : Promise < ReviewedApplicantRecordDTO > {
16- const record = await ReviewedApplicantRecord . create ( dto ) ;
17- return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
14+ class ReviewedApplicantRecordService implements IReviewApplicantRecordService {
15+ /* eslint-disable class-methods-use-this */
16+ async createReviewedApplicantRecord (
17+ dto : CreateReviewedApplicantRecordDTO ,
18+ ) : Promise < ReviewedApplicantRecordDTO > {
19+ try {
20+ const record = await ReviewedApplicantRecord . create ( dto ) ;
21+ return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
22+ } catch ( error : unknown ) {
23+ Logger . error (
24+ `Failed to create reviewed applicant record. Reason = ${ getErrorMessage (
25+ error ,
26+ ) } `,
27+ ) ;
28+ throw error ;
1829 }
30+ }
1931
20- async bulkCreateReviewedApplicantRecord (
21- createReviewedApplicantRecordDTOs : CreateReviewedApplicantRecordDTO [ ]
22- ) : Promise < ReviewedApplicantRecordDTO [ ] > {
23- const reviewedApplicantRecords = await sequelize . transaction ( async ( t ) => {
24- const records = await ReviewedApplicantRecord . bulkCreate (
25- createReviewedApplicantRecordDTOs ,
26- { transaction : t }
27- ) ;
28- return records ;
29- } ) ;
32+ async bulkCreateReviewedApplicantRecord (
33+ createReviewedApplicantRecordDTOs : CreateReviewedApplicantRecordDTO [ ] ,
34+ ) : Promise < ReviewedApplicantRecordDTO [ ] > {
35+ try {
36+ const reviewedApplicantRecords = await sequelize . transaction (
37+ async ( t ) => {
38+ const records = await ReviewedApplicantRecord . bulkCreate (
39+ createReviewedApplicantRecordDTOs ,
40+ { transaction : t } ,
41+ ) ;
42+ return records ;
43+ } ,
44+ ) ;
3045
31- return reviewedApplicantRecords . map ( ( record ) =>
32- record . toJSON ( ) as ReviewedApplicantRecordDTO
33- ) ;
46+ return reviewedApplicantRecords . map (
47+ ( record ) => record . toJSON ( ) as ReviewedApplicantRecordDTO ,
48+ ) ;
49+ } catch ( error : unknown ) {
50+ Logger . error (
51+ `Failed to bulk create reviewed applicant records. Reason = ${ getErrorMessage (
52+ error ,
53+ ) } `,
54+ ) ;
55+ throw error ;
3456 }
57+ }
3558
36- async deleteReviewedApplicantRecord ( deleteReviewedApplicantRecord : DeleteReviewedApplicantRecordDTO ) : Promise < ReviewedApplicantRecordDTO > {
37- const applicantRecordId = deleteReviewedApplicantRecord . applicantRecordId ;
38- const reviewerId = deleteReviewedApplicantRecord . reviewerId ;
39- const record = await ReviewedApplicantRecord . findOne ( { where : { applicantRecordId, reviewerId } } ) ;
59+ async deleteReviewedApplicantRecord (
60+ deleteReviewedApplicantRecord : DeleteReviewedApplicantRecordDTO ,
61+ ) : Promise < ReviewedApplicantRecordDTO > {
62+ try {
63+ const { applicantRecordId } = deleteReviewedApplicantRecord ;
64+ const { reviewerId } = deleteReviewedApplicantRecord ;
65+ const record = await ReviewedApplicantRecord . findOne ( {
66+ where : { applicantRecordId, reviewerId } ,
67+ } ) ;
4068
41- if ( ! record ) {
42- throw new Error ( "ReviewedApplicantRecord not found, delete failed" ) ;
43- }
69+ if ( ! record ) {
70+ throw new Error ( "ReviewedApplicantRecord not found, delete failed" ) ;
71+ }
4472
45- await record . destroy ( ) ;
46- return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
73+ await record . destroy ( ) ;
74+ return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
75+ } catch ( error : unknown ) {
76+ Logger . error (
77+ `Failed to bulk create reviewed applicant records. Reason = ${ getErrorMessage (
78+ error ,
79+ ) } `,
80+ ) ;
81+ throw error ;
4782 }
83+ }
4884
49- async bulkDeleteReviewedApplicantRecord ( deleteReviewedApplicantRecords : DeleteReviewedApplicantRecordDTO [ ] ) : Promise < ReviewedApplicantRecordDTO [ ] > {
50- const deletedRecords = await sequelize . transaction ( async ( t ) => {
51- const records = await Promise . all (
52- deleteReviewedApplicantRecords . map ( ( { applicantRecordId, reviewerId } ) =>
53- ReviewedApplicantRecord . findOne ( {
54- where : { applicantRecordId, reviewerId } ,
55- transaction : t ,
56- } )
57- )
58- ) ;
85+ async bulkDeleteReviewedApplicantRecord (
86+ deleteReviewedApplicantRecords : DeleteReviewedApplicantRecordDTO [ ] ,
87+ ) : Promise < ReviewedApplicantRecordDTO [ ] > {
88+ try {
89+ const deletedRecords = await sequelize . transaction ( async ( t ) => {
90+ const records = await Promise . all (
91+ deleteReviewedApplicantRecords . map (
92+ ( { applicantRecordId, reviewerId } ) =>
93+ ReviewedApplicantRecord . findOne ( {
94+ where : { applicantRecordId, reviewerId } ,
95+ transaction : t ,
96+ } ) ,
97+ ) ,
98+ ) ;
5999
60- if ( records . some ( ( r ) => ! r ) ) {
61- throw new Error ( "Not all records were found, bulk delete failed" ) ;
62- }
100+ if ( records . some ( ( r ) => ! r ) ) {
101+ throw new Error ( "Not all records were found, bulk delete failed" ) ;
102+ }
63103
64- const existingRecords = records as ReviewedApplicantRecord [ ] ;
65- await Promise . all ( existingRecords . map ( ( r ) => r . destroy ( { transaction : t } ) ) ) ;
104+ const existingRecords = records as ReviewedApplicantRecord [ ] ;
105+ await Promise . all (
106+ existingRecords . map ( ( r ) => r . destroy ( { transaction : t } ) ) ,
107+ ) ;
66108
67- return existingRecords ;
68- } ) ;
109+ return existingRecords ;
110+ } ) ;
69111
70- return deletedRecords . map ( ( r ) => r . toJSON ( ) as ReviewedApplicantRecordDTO ) ;
112+ return deletedRecords . map (
113+ ( r ) => r . toJSON ( ) as ReviewedApplicantRecordDTO ,
114+ ) ;
115+ } catch ( error : unknown ) {
116+ Logger . error (
117+ `Failed to bulk create reviewed applicant records. Reason = ${ getErrorMessage (
118+ error ,
119+ ) } `,
120+ ) ;
121+ throw error ;
71122 }
123+ }
72124}
73125
74- export default ReviewedApplicantRecordService ;
126+ export default ReviewedApplicantRecordService ;
0 commit comments