-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceRequestRoutes.ts
185 lines (166 loc) · 6.58 KB
/
serviceRequestRoutes.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Router } from "express";
import IServiceRequest from "../services/interfaces/serviceRequest";
import ServiceRequest from "../services/implementations/serviceRequest";
import { getErrorMessage } from "../utilities/errorUtils";
import { getAccessToken, isAuthorizedByRole} from "../middlewares/auth";
import AuthService from "../services/implementations/authService";
import IAuthService from "../services/interfaces/authService";
import IUserService from "../services/interfaces/userService";
import UserService from "../services/implementations/userService";
import IEmailService from "../services/interfaces/emailService";
import EmailService from "../services/implementations/emailService";
import nodemailerConfig from "../nodemailer.config";
const serviceRequestRouter: Router = Router();
const serviceRequestService: IServiceRequest = new ServiceRequest();
const userService: IUserService = new UserService();
const emailService: IEmailService = new EmailService(nodemailerConfig);
const authService: IAuthService = new AuthService(userService, emailService);
/* Get service request by ID if requestId is specified; otherwise, return all service requests. */
serviceRequestRouter.get("/", isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])),async (req, res) => {
const { requestId, fromDate, toDate } = req.body;
if (requestId) {
if (typeof requestId !== "string") {
res
.status(400)
.json({ error: "requestId query parameter must be a string." });
} else {
try {
const serviceRequest =
await serviceRequestService.getServiceRequestByID(requestId);
res.status(200).json(serviceRequest);
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
}
} else {
try {
let serviceRequests;
if (fromDate && toDate) {
const fromDateFormatted = new Date(fromDate as string).toISOString();
const toDateFormatted = new Date(toDate as string).toISOString();
if (
isNaN(new Date(fromDateFormatted).getTime()) ||
isNaN(new Date(toDateFormatted).getTime())
) {
res
.status(400)
.json({
error:
"fromDate and toDate query parameters must be valid dates in ISO format.",
});
return;
}
serviceRequests = await serviceRequestService.getServiceRequests();
serviceRequests = serviceRequests.filter((request) => {
const requestDate = request.shiftTime
? new Date(request.shiftTime).toISOString()
: null;
if (requestDate) {
return (
requestDate >= fromDateFormatted && requestDate <= toDateFormatted
);
}
return false;
});
} else {
serviceRequests = await serviceRequestService.getServiceRequests();
}
res.status(200).json(serviceRequests);
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
}
});
/* Get service requests by requester ID */
serviceRequestRouter.get("/requester/:requesterId",isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])), async (req, res) => {
const { requesterId } = req.params;
try {
const serviceRequests =
await serviceRequestService.getServiceRequestsByRequesterId(requesterId);
res.status(200).json(serviceRequests);
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
});
/* Post service request by requester ID */
serviceRequestRouter.post("/requester/:requesterId",isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])), async (req, res) => {
const { requesterId, } = req.params;
const { serviceRequestId } = req.body;
try {
await serviceRequestService.postServiceRequestByRequesterId(
requesterId,
serviceRequestId,
);
res
.status(200)
.json({ message: `Service Request added to user successfully.` });
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
});
/* Get service requests by user ID */
serviceRequestRouter.get("/user/:id",isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])), async (req, res) => {
const userId = req.params.id;
try {
const serviceRequests =
await serviceRequestService.getServiceRequestsByUserId(userId);
res.status(200).json(serviceRequests);
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
});
/* Post service request by user ID */
serviceRequestRouter.post("/user/:userId",isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])), async (req, res) => {
const { userId } = req.params;
const { serviceRequestId } = req.body;
try {
await serviceRequestService.postServiceRequestByUserId(
userId,
serviceRequestId,
);
res
.status(200)
.json({ message: `Service Request added to user successfully.` });
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
});
/* Post ServiceRequest route. Requires ADMIN role to perform this action. */
serviceRequestRouter.post("/post",isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])), async (req, res) => {
try {
const newServiceRequest = await serviceRequestService.postServiceRequest(
req.body,
);
res.status(200).json(newServiceRequest);
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
});
/* Delete a ServiceRequest given an id */
serviceRequestRouter.delete("/delete/:id", isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])),async (req, res) => {
try {
const requestId = req.params.id;
await serviceRequestService.deleteServiceRequestByID(requestId);
res.status(200).json({ message: `Service Request deleted successfully.` });
} catch (error: unknown) {
res.status(500).json({ error: getErrorMessage(error) });
}
});
/* Get user by email - this is for displaying first/last name for email invitation*/
serviceRequestRouter.get("/user-by-email", isAuthorizedByRole(new Set(["ADMIN", "VOLUNTEER"])), async (req, res) => {
const { email } = req.query;
if (!email || typeof email !== "string") {
return res.status(400).json({ error: "A valid email query parameter is required." });
}
try {
const user = await userService.getUserByEmail(email.trim());
if (user) {
res.status(200).json({ firstName: user.firstName, lastName: user.lastName });
} else {
res.status(404).json({ message: "User not found" });
}
} catch (error: unknown) {
res.status(404).json({ error: getErrorMessage(error) });
}
});
export default serviceRequestRouter;