|
1 | 1 | import { Router } from "express"; |
2 | 2 | import { isAuthorizedByRole } from "../middlewares/auth"; |
3 | 3 | import { |
| 4 | + activityEndTimePatchValidator, |
| 5 | + activityNotesPatchValidator, |
4 | 6 | activityRequestDtoValidator, |
| 7 | + activityScheduledTimePatchValidator, |
| 8 | + activityStartTimePatchValidator, |
5 | 9 | activityUpdateDtoValidator, |
| 10 | + activityUserPatchValidator, |
6 | 11 | } from "../middlewares/validators/activityValidators"; |
7 | 12 | import ActivityService from "../services/implementations/activityService"; |
8 | 13 | import { |
@@ -50,6 +55,36 @@ activityRouter.get("/:id", async (req, res) => { |
50 | 55 | } |
51 | 56 | }); |
52 | 57 |
|
| 58 | +/* Get Activities for specific Pet by Pet id */ |
| 59 | +activityRouter.get("/pet/:petId", async (req, res) => { |
| 60 | + const { petId } = req.params; |
| 61 | + try { |
| 62 | + const activitiesByPet = await activityService.getPetActivities(petId); |
| 63 | + res.status(200).json(activitiesByPet); |
| 64 | + } catch (e: unknown) { |
| 65 | + if (e instanceof NotFoundError) { |
| 66 | + res.status(404).send(getErrorMessage(e)); |
| 67 | + } else { |
| 68 | + res.status(500).send(getErrorMessage(e)); |
| 69 | + } |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +/* Get Activities for specific User by User id */ |
| 74 | +activityRouter.get("/user/:userId", async (req, res) => { |
| 75 | + const { userId } = req.params; |
| 76 | + try { |
| 77 | + const activitiesByUser = await activityService.getUserActivities(userId); |
| 78 | + res.status(200).json(activitiesByUser); |
| 79 | + } catch (e: unknown) { |
| 80 | + if (e instanceof NotFoundError) { |
| 81 | + res.status(404).send(getErrorMessage(e)); |
| 82 | + } else { |
| 83 | + res.status(500).send(getErrorMessage(e)); |
| 84 | + } |
| 85 | + } |
| 86 | +}); |
| 87 | + |
53 | 88 | /* Create Activity */ |
54 | 89 | activityRouter.post( |
55 | 90 | "/", |
@@ -103,6 +138,98 @@ activityRouter.patch( |
103 | 138 | }, |
104 | 139 | ); |
105 | 140 |
|
| 141 | +/* Updates/Sets User assigned to an Activity */ |
| 142 | +activityRouter.patch( |
| 143 | + "/:id/assign-user", |
| 144 | + isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])), |
| 145 | + activityUserPatchValidator, |
| 146 | + async (req, res) => { |
| 147 | + const { id } = req.params; |
| 148 | + try { |
| 149 | + const { body } = req; |
| 150 | + const Activity = await activityService.assignUser(id, { |
| 151 | + userId: body.userId, |
| 152 | + }); |
| 153 | + res.status(200).json(Activity); |
| 154 | + } catch (e: unknown) { |
| 155 | + res.status(500).send(getErrorMessage(e)); |
| 156 | + } |
| 157 | + }, |
| 158 | +); |
| 159 | + |
| 160 | +/* Updates/Sets a scheduled start time to an Activity */ |
| 161 | +activityRouter.patch( |
| 162 | + "/:id/schedule", |
| 163 | + isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])), |
| 164 | + activityScheduledTimePatchValidator, |
| 165 | + async (req, res) => { |
| 166 | + const { id } = req.params; |
| 167 | + try { |
| 168 | + const { body } = req; |
| 169 | + const Activity = await activityService.scheduleActivity(id, { |
| 170 | + time: body.scheduledStartTime |
| 171 | + }); |
| 172 | + res.status(200).json(Activity); |
| 173 | + } catch (e: unknown) { |
| 174 | + res.status(500).send(getErrorMessage(e)); |
| 175 | + } |
| 176 | + }, |
| 177 | +); |
| 178 | + |
| 179 | +/* Adds a start time to an Activity */ |
| 180 | +activityRouter.patch( |
| 181 | + "/:id/start", |
| 182 | + activityStartTimePatchValidator, |
| 183 | + async (req, res) => { |
| 184 | + const { id } = req.params; |
| 185 | + try { |
| 186 | + const { body } = req; |
| 187 | + const Activity = await activityService.startActivity(id, { |
| 188 | + time: body.startTime |
| 189 | + }); |
| 190 | + res.status(200).json(Activity); |
| 191 | + } catch (e: unknown) { |
| 192 | + res.status(500).send(getErrorMessage(e)); |
| 193 | + } |
| 194 | + }, |
| 195 | +); |
| 196 | + |
| 197 | +/* Adds an end time to an Activity */ |
| 198 | +activityRouter.patch( |
| 199 | + "/:id/end", |
| 200 | + activityEndTimePatchValidator, |
| 201 | + async (req, res) => { |
| 202 | + const { id } = req.params; |
| 203 | + try { |
| 204 | + const { body } = req; |
| 205 | + const Activity = await activityService.endActivity(id, { |
| 206 | + time: body.endTime |
| 207 | + }); |
| 208 | + res.status(200).json(Activity); |
| 209 | + } catch (e: unknown) { |
| 210 | + res.status(500).send(getErrorMessage(e)); |
| 211 | + } |
| 212 | + }, |
| 213 | +); |
| 214 | + |
| 215 | +/* Updates/Adds notes to an Activity */ |
| 216 | +activityRouter.patch( |
| 217 | + "/:id/notes", |
| 218 | + activityNotesPatchValidator, |
| 219 | + async (req, res) => { |
| 220 | + const { id } = req.params; |
| 221 | + try { |
| 222 | + const { body } = req; |
| 223 | + const Activity = await activityService.updateActivityNotes(id, { |
| 224 | + notes: body.notes |
| 225 | + }); |
| 226 | + res.status(200).json(Activity); |
| 227 | + } catch (e: unknown) { |
| 228 | + res.status(500).send(getErrorMessage(e)); |
| 229 | + } |
| 230 | + }, |
| 231 | +); |
| 232 | + |
106 | 233 | /* Delete Activity by id */ |
107 | 234 | activityRouter.delete( |
108 | 235 | "/:id", |
|
0 commit comments