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" ;
88
99import IReviewApplicantRecordService from "../interfaces/IReviewedApplicantRecordService" ;
1010
1111class ReviewedApplicantRecordService implements IReviewApplicantRecordService {
12+ async createReviewedApplicantRecord (
13+ dto : CreateReviewedApplicantRecordDTO ,
14+ ) : Promise < ReviewedApplicantRecordDTO > {
15+ const record = await ReviewedApplicantRecord . create ( dto ) ;
16+ return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
17+ }
1218
13- async createReviewedApplicantRecord (
14- dto : CreateReviewedApplicantRecordDTO
15- ) : Promise < ReviewedApplicantRecordDTO > {
16- const record = await ReviewedApplicantRecord . create ( dto ) ;
17- return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
18- }
19-
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- } ) ;
30-
31- return reviewedApplicantRecords . map ( ( record ) =>
32- record . toJSON ( ) as ReviewedApplicantRecordDTO
33- ) ;
34- }
19+ async bulkCreateReviewedApplicantRecord (
20+ createReviewedApplicantRecordDTOs : CreateReviewedApplicantRecordDTO [ ] ,
21+ ) : Promise < ReviewedApplicantRecordDTO [ ] > {
22+ const reviewedApplicantRecords = await sequelize . transaction ( async ( t ) => {
23+ const records = await ReviewedApplicantRecord . bulkCreate (
24+ createReviewedApplicantRecordDTOs ,
25+ { transaction : t } ,
26+ ) ;
27+ return records ;
28+ } ) ;
3529
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 } } ) ;
30+ return reviewedApplicantRecords . map (
31+ ( record ) => record . toJSON ( ) as ReviewedApplicantRecordDTO ,
32+ ) ;
33+ }
4034
41- if ( ! record ) {
42- throw new Error ( "ReviewedApplicantRecord not found, delete failed" ) ;
43- }
35+ async deleteReviewedApplicantRecord (
36+ deleteReviewedApplicantRecord : DeleteReviewedApplicantRecordDTO ,
37+ ) : Promise < ReviewedApplicantRecordDTO > {
38+ const applicantRecordId = deleteReviewedApplicantRecord . applicantRecordId ;
39+ const reviewerId = deleteReviewedApplicantRecord . reviewerId ;
40+ const record = await ReviewedApplicantRecord . findOne ( {
41+ where : { applicantRecordId, reviewerId } ,
42+ } ) ;
4443
45- await record . destroy ( ) ;
46- return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
44+ if ( ! record ) {
45+ throw new Error ( "ReviewedApplicantRecord not found, delete failed" ) ;
4746 }
4847
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- ) ;
48+ await record . destroy ( ) ;
49+ return record . toJSON ( ) as ReviewedApplicantRecordDTO ;
50+ }
5951
60- if ( records . some ( ( r ) => ! r ) ) {
61- throw new Error ( "Not all records were found, bulk delete failed" ) ;
62- }
52+ async bulkDeleteReviewedApplicantRecord (
53+ deleteReviewedApplicantRecords : DeleteReviewedApplicantRecordDTO [ ] ,
54+ ) : Promise < ReviewedApplicantRecordDTO [ ] > {
55+ const deletedRecords = await sequelize . transaction ( async ( t ) => {
56+ const records = await Promise . all (
57+ deleteReviewedApplicantRecords . map (
58+ ( { applicantRecordId, reviewerId } ) =>
59+ ReviewedApplicantRecord . findOne ( {
60+ where : { applicantRecordId, reviewerId } ,
61+ transaction : t ,
62+ } ) ,
63+ ) ,
64+ ) ;
6365
64- const existingRecords = records as ReviewedApplicantRecord [ ] ;
65- await Promise . all ( existingRecords . map ( ( r ) => r . destroy ( { transaction : t } ) ) ) ;
66+ if ( records . some ( ( r ) => ! r ) ) {
67+ throw new Error ( "Not all records were found, bulk delete failed" ) ;
68+ }
6669
67- return existingRecords ;
68- } ) ;
70+ const existingRecords = records as ReviewedApplicantRecord [ ] ;
71+ await Promise . all (
72+ existingRecords . map ( ( r ) => r . destroy ( { transaction : t } ) ) ,
73+ ) ;
6974
70- return deletedRecords . map ( ( r ) => r . toJSON ( ) as ReviewedApplicantRecordDTO ) ;
71- }
75+ return existingRecords ;
76+ } ) ;
77+
78+ return deletedRecords . map ( ( r ) => r . toJSON ( ) as ReviewedApplicantRecordDTO ) ;
79+ }
7280}
7381
74- export default ReviewedApplicantRecordService ;
82+ export default ReviewedApplicantRecordService ;
0 commit comments