Skip to content

Commit

Permalink
Added update validator and enabled auto-generated created_at field in…
Browse files Browse the repository at this point in the history
… activity model
  • Loading branch information
liya-zhu committed Nov 28, 2024
1 parent 3923fc2 commit df42fef
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
53 changes: 53 additions & 0 deletions backend/typescript/middlewares/validators/activityValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,56 @@ export const activityRequestDtoValidator = async (

return next();
};

export const activityUpdateDtoValidator = async (
req: Request,
res: Response,
next: NextFunction,
) => {
const { body } = req;
console.log(body)

if (body.userId !== undefined &&
body.userId !== null &&
!validatePrimitive(body.userId, "integer")) {
return res.status(400).send(getApiValidationError("userId", "integer"));
}

if (body.petId !== undefined &&
body.petId !== null &&
!validatePrimitive(body.petId, "integer")) {
return res.status(400).send(getApiValidationError("petId", "integer"));
}

if (body.activityTypeId !== undefined &&
body.activityTypeId !== null &&
!validatePrimitive(body.activityTypeId, "integer")) {
return res.status(400).send(getApiValidationError("activityTypeId", "integer"));
}

if (body.scheduledStartTime !== undefined &&
body.scheduledStartTime !== null &&
!validateDate(body.scheduledStartTime)) {
return res.status(400).send(getApiValidationError("scheduledStartTime", "Date"));
}

if (body.startTime !== undefined &&
body.startTime !== null &&
!validateDate(body.startTime)) {
return res.status(400).send(getApiValidationError("startTime", "Date"));
}

if (body.endTime !== undefined &&
body.endTime !== null &&
!validateDate(body.endTime)) {
return res.status(400).send(getApiValidationError("endTime", "Date"));
}

if (body.notes !== undefined &&
body.notes !== null &&
!validatePrimitive(body.notes, "string")) {
return res.status(400).send(getApiValidationError("notes", "string"));
}

return next();
};
7 changes: 6 additions & 1 deletion backend/typescript/models/activity.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import User from "./user.model";
import Pet from "./pet.model";
import ActivityType from "./activityType.model";

@Table({ timestamps: false, tableName: "activities" })
@Table({
tableName: "activities",
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
})
export default class Activity extends Model {
@ForeignKey(() => User) // in case of null, task has not been assigned
@Column({})
Expand Down
13 changes: 8 additions & 5 deletions backend/typescript/rest/activityRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from "express";
import { getAccessToken, isAuthorizedByRole } from "../middlewares/auth";
import { activityRequestDtoValidator } from "../middlewares/validators/activityValidators";
import { activityRequestDtoValidator, activityUpdateDtoValidator } from "../middlewares/validators/activityValidators";
import ActivityService from "../services/implementations/activityService";
import {
ActivityResponseDTO,
Expand All @@ -11,7 +11,9 @@ import { sendResponseByMimeType } from "../utilities/responseUtil";
import { Role } from "../types";

const activityRouter: Router = Router();
// activityRouter.uzse(isAuthorizedByRole((new Set([Role.ADMINISTRATOR, Role.ANIMAL_BEHAVIOURIST, Role.STAFF, Role.VOLUNTEER]))));
activityRouter.use(
isAuthorizedByRole(new Set([Role.ADMINISTRATOR, Role.ANIMAL_BEHAVIOURIST, Role.STAFF, Role.VOLUNTEER])),
);
const activityService: IActivityService = new ActivityService();

/* Get all Activities */
Expand Down Expand Up @@ -47,8 +49,8 @@ activityRouter.get("/:id", async (req, res) => {
/* Create Activity */
activityRouter.post(
"/",
isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])),
activityRequestDtoValidator,
// isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])),
async (req, res) => {
const accessToken = getAccessToken(req);
if (!accessToken) {
Expand Down Expand Up @@ -78,10 +80,10 @@ activityRouter.post(


/* Update Activity by id */
activityRouter.put(
activityRouter.patch(
"/:id",
isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])),
activityRequestDtoValidator,
activityUpdateDtoValidator,
async (req, res) => {
const { id } = req.params;
try {
Expand All @@ -107,6 +109,7 @@ activityRouter.delete(
"/:id",
isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])),
async (req, res) => {
console.log("Request body:", req.body); // Log incoming request body
const { id } = req.params;

try {
Expand Down

0 comments on commit df42fef

Please sign in to comment.