@@ -12,35 +12,27 @@ export interface EmploymentHistoryControllerType {
1212export class EmploymentHistoryController implements EmploymentHistoryControllerType {
1313 constructor ( private readonly traineesRepository : TraineesRepository ) { }
1414
15- async getEmploymentHistory ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
16- try {
17- const traineeID = req . params . id ;
18- const employmentHistory = await this . traineesRepository . getEmploymentHistory ( traineeID ) ;
19- res . json ( employmentHistory ) ;
20- } catch ( error : any ) {
21- next ( error ) ;
22- }
15+ async getEmploymentHistory ( req : Request , res : Response ) : Promise < void > {
16+ const traineeID = req . params . id ;
17+ const employmentHistory = await this . traineesRepository . getEmploymentHistory ( traineeID ) ;
18+ res . json ( employmentHistory ) ;
2319 }
2420
25- async addEmploymentHistory ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
21+ async addEmploymentHistory ( req : Request , res : Response ) : Promise < void > {
2622 const traineeID = req . params . id ;
2723 const employmentHistoryData : EmploymentHistory = req . body ;
2824 try {
2925 validateEmploymentHistory ( employmentHistoryData ) ;
30- } catch ( error : any ) {
31- res . status ( 400 ) . send ( new ResponseError ( error . message ) ) ;
26+ } catch ( error ) {
27+ if ( error instanceof Error ) res . status ( 400 ) . send ( new ResponseError ( error . message ) ) ;
3228 return ;
3329 }
3430
35- try {
36- const newEmploymentHistory = await this . traineesRepository . addEmploymentHistory ( traineeID , employmentHistoryData ) ;
37- res . status ( 201 ) . json ( newEmploymentHistory ) ;
38- } catch ( error : any ) {
39- next ( error ) ;
40- }
31+ const newEmploymentHistory = await this . traineesRepository . addEmploymentHistory ( traineeID , employmentHistoryData ) ;
32+ res . status ( 201 ) . json ( newEmploymentHistory ) ;
4133 }
4234
43- async updateEmploymentHistory ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
35+ async updateEmploymentHistory ( req : Request , res : Response ) : Promise < void > {
4436 const trainee = await this . traineesRepository . getTrainee ( req . params . id ) ;
4537 if ( ! trainee ) {
4638 res . status ( 404 ) . send ( new ResponseError ( 'Trainee not found' ) ) ;
@@ -69,41 +61,30 @@ export class EmploymentHistoryController implements EmploymentHistoryControllerT
6961
7062 try {
7163 validateEmploymentHistory ( historyToUpdate ) ;
72- } catch ( error : any ) {
73- res . status ( 400 ) . send ( new ResponseError ( error . message ) ) ;
64+ } catch ( error ) {
65+ if ( error instanceof Error ) res . status ( 400 ) . send ( new ResponseError ( error . message ) ) ;
7466 return ;
7567 }
7668
77- try {
78- const updatedEmploymentHistory = await this . traineesRepository . updateEmploymentHistory (
79- trainee . id ,
80- historyToUpdate
81- ) ;
69+ const updatedEmploymentHistory = await this . traineesRepository . updateEmploymentHistory ( trainee . id , historyToUpdate ) ;
8270
83- res . json ( updatedEmploymentHistory ) ;
84- } catch ( error : any ) {
85- next ( error ) ;
86- }
71+ res . json ( updatedEmploymentHistory ) ;
8772 }
8873
89- async deleteEmploymentHistory ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
90- try {
91- const trainee = await this . traineesRepository . getTrainee ( req . params . id ) ;
92- if ( ! trainee ) {
93- res . status ( 404 ) . send ( new ResponseError ( 'Trainee not found' ) ) ;
94- return ;
95- }
96-
97- const employmentHistoryID = req . params . employmentHistoryID ;
98- if ( ! trainee . employmentInfo . employmentHistory . find ( ( history ) => history . id === employmentHistoryID ) ) {
99- res . status ( 404 ) . send ( new ResponseError ( 'Employment history not found' ) ) ;
100- return ;
101- }
74+ async deleteEmploymentHistory ( req : Request , res : Response ) : Promise < void > {
75+ const trainee = await this . traineesRepository . getTrainee ( req . params . id ) ;
76+ if ( ! trainee ) {
77+ res . status ( 404 ) . send ( new ResponseError ( 'Trainee not found' ) ) ;
78+ return ;
79+ }
10280
103- await this . traineesRepository . deleteEmploymentHistory ( trainee . id , employmentHistoryID ) ;
104- res . status ( 204 ) . send ( ) ;
105- } catch ( error : any ) {
106- next ( error ) ;
81+ const employmentHistoryID = req . params . employmentHistoryID ;
82+ if ( ! trainee . employmentInfo . employmentHistory . find ( ( history ) => history . id === employmentHistoryID ) ) {
83+ res . status ( 404 ) . send ( new ResponseError ( 'Employment history not found' ) ) ;
84+ return ;
10785 }
86+
87+ await this . traineesRepository . deleteEmploymentHistory ( trainee . id , employmentHistoryID ) ;
88+ res . status ( 204 ) . send ( ) ;
10889 }
10990}
0 commit comments