-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEmploymentHistoryController.ts
More file actions
109 lines (95 loc) · 3.87 KB
/
EmploymentHistoryController.ts
File metadata and controls
109 lines (95 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Request, Response, NextFunction } from 'express';
import { ResponseError, EmploymentHistory, validateEmploymentHistory } from '../../models';
import { TraineesRepository } from '../../repositories';
export interface EmploymentHistoryControllerType {
getEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void>;
addEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void>;
updateEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void>;
deleteEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void>;
}
export class EmploymentHistoryController implements EmploymentHistoryControllerType {
constructor(private readonly traineesRepository: TraineesRepository) {}
async getEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const traineeID = String(req.params.id);
const employmentHistory = await this.traineesRepository.getEmploymentHistory(traineeID);
res.json(employmentHistory);
} catch (error: any) {
next(error);
}
}
async addEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void> {
const traineeID = String(req.params.id);
const employmentHistoryData: EmploymentHistory = req.body;
try {
validateEmploymentHistory(employmentHistoryData);
} catch (error: any) {
res.status(400).send(new ResponseError(error.message));
return;
}
try {
const newEmploymentHistory = await this.traineesRepository.addEmploymentHistory(traineeID, employmentHistoryData);
res.status(201).json(newEmploymentHistory);
} catch (error: any) {
next(error);
}
}
async updateEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void> {
const trainee = await this.traineesRepository.getTrainee(String(req.params.id));
if (!trainee) {
res.status(404).send(new ResponseError('Trainee not found'));
return;
}
const employmentHistoryData = trainee.employmentInfo.employmentHistory.find(
(history) => history.id === String(req.params.employmentHistoryID)
);
if (!employmentHistoryData) {
res.status(404).send(new ResponseError('Employment history was not found'));
return;
}
const historyToUpdate: EmploymentHistory = {
id: employmentHistoryData.id,
type: req.body.type,
companyName: req.body.companyName,
role: req.body.role,
startDate: new Date(req.body.startDate),
endDate: req.body.endDate ? new Date(req.body.endDate) : undefined,
feeCollected: req.body.feeCollected,
feeAmount: req.body.feeAmount,
comments: req.body.comments,
};
try {
validateEmploymentHistory(historyToUpdate);
} catch (error: any) {
res.status(400).send(new ResponseError(error.message));
return;
}
try {
const updatedEmploymentHistory = await this.traineesRepository.updateEmploymentHistory(
trainee.id,
historyToUpdate
);
res.json(updatedEmploymentHistory);
} catch (error: any) {
next(error);
}
}
async deleteEmploymentHistory(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const trainee = await this.traineesRepository.getTrainee(String(req.params.id));
if (!trainee) {
res.status(404).send(new ResponseError('Trainee not found'));
return;
}
const employmentHistoryID = String(req.params.employmentHistoryID);
if (!trainee.employmentInfo.employmentHistory.find((history) => history.id === employmentHistoryID)) {
res.status(404).send(new ResponseError('Employment history not found'));
return;
}
await this.traineesRepository.deleteEmploymentHistory(trainee.id, employmentHistoryID);
res.status(204).send();
} catch (error: any) {
next(error);
}
}
}