Skip to content

Commit ebbd2b9

Browse files
committed
added activity service implentation, validators, and routing
1 parent ca31ec7 commit ebbd2b9

File tree

4 files changed

+437
-1
lines changed

4 files changed

+437
-1
lines changed

backend/typescript/middlewares/validators/activityValidators.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,74 @@ export const activityUpdateDtoValidator = async (
134134

135135
return next();
136136
};
137+
138+
export const activityUserPatchValidator = async (
139+
req: Request,
140+
res: Response,
141+
next: NextFunction,
142+
) => {
143+
const { body } = req;
144+
145+
if (!validatePrimitive(body.userId, "integer")) {
146+
return res.status(400).send(getApiValidationError("userId", "integer"));
147+
}
148+
149+
return next();
150+
};
151+
152+
export const activityScheduledTimePatchValidator = async (
153+
req: Request,
154+
res: Response,
155+
next: NextFunction,
156+
) => {
157+
const { body } = req;
158+
159+
if (!validateDate(body.scheduledStartTime)) {
160+
return res.status(400).send(getApiValidationError("scheduledStartTime", "Date"));
161+
}
162+
163+
return next();
164+
};
165+
166+
export const activityStartTimePatchValidator = async (
167+
req: Request,
168+
res: Response,
169+
next: NextFunction,
170+
) => {
171+
const { body } = req;
172+
173+
if (!validateDate(body.startTime)) {
174+
return res.status(400).send(getApiValidationError("startTime", "Date"));
175+
}
176+
177+
return next();
178+
};
179+
180+
export const activityEndTimePatchValidator = async (
181+
req: Request,
182+
res: Response,
183+
next: NextFunction,
184+
) => {
185+
const { body } = req;
186+
187+
if (!validateDate(body.endTime)) {
188+
return res.status(400).send(getApiValidationError("endTime", "Date"));
189+
}
190+
191+
return next();
192+
};
193+
194+
export const activityNotesPatchValidator = async (
195+
req: Request,
196+
res: Response,
197+
next: NextFunction,
198+
) => {
199+
const { body } = req;
200+
if (
201+
!validatePrimitive(body.notes, "string")
202+
) {
203+
return res.status(400).send(getApiValidationError("notes", "string"));
204+
}
205+
206+
return next();
207+
};

backend/typescript/rest/activityRoutes.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { Router } from "express";
22
import { isAuthorizedByRole } from "../middlewares/auth";
33
import {
4+
activityEndTimePatchValidator,
5+
activityNotesPatchValidator,
46
activityRequestDtoValidator,
7+
activityScheduledTimePatchValidator,
8+
activityStartTimePatchValidator,
59
activityUpdateDtoValidator,
10+
activityUserPatchValidator,
611
} from "../middlewares/validators/activityValidators";
712
import ActivityService from "../services/implementations/activityService";
813
import {
@@ -50,6 +55,36 @@ activityRouter.get("/:id", async (req, res) => {
5055
}
5156
});
5257

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+
5388
/* Create Activity */
5489
activityRouter.post(
5590
"/",
@@ -103,6 +138,98 @@ activityRouter.patch(
103138
},
104139
);
105140

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+
106233
/* Delete Activity by id */
107234
activityRouter.delete(
108235
"/:id",

0 commit comments

Comments
 (0)