Skip to content

Commit

Permalink
deleted activityid
Browse files Browse the repository at this point in the history
  • Loading branch information
liya-zhu committed Nov 22, 2024
1 parent e093f59 commit 69147b9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 29 deletions.
33 changes: 20 additions & 13 deletions backend/typescript/middlewares/validators/activityValidators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from "express";
import { getApiValidationError, validateDate, validatePrimitive,} from "./util";
import { getApiValidationError, validateDate, validatePrimitive, } from "./util";

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable-next-line import/prefer-default-export */
Expand All @@ -9,12 +9,11 @@ export const activityRequestDtoValidator = async (
next: NextFunction,
) => {
const { body } = req;
console.log(body)

if (!validatePrimitive(body.activityId, "integer")) {
return res.status(400).send(getApiValidationError("activityId", "integer"));
}

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

Expand All @@ -26,19 +25,27 @@ export const activityRequestDtoValidator = async (
return res.status(400).send(getApiValidationError("activityTypeId", "integer"));
}

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion backend/typescript/middlewares/validators/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Type = "string" | "integer" | "boolean" | "decimal" | "PetStatus" | "Sex";
type Type = "string" | "integer" | "boolean" | "decimal" | "PetStatus" | "Sex" | "Date";

const allowableContentTypes = new Set([
"text/plain",
Expand Down
3 changes: 0 additions & 3 deletions backend/typescript/models/activity.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import ActivityType from "./activityType.model";

@Table({ timestamps: false, tableName: "activities" })
export default class Activity extends Model {
@Column({})
activity_id!: number;

@ForeignKey(() => User) // in case of null, task has not been assigned
@Column({})
user_id?: number;
Expand Down
6 changes: 2 additions & 4 deletions backend/typescript/rest/activityRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { sendResponseByMimeType } from "../utilities/responseUtil";
import { Role } from "../types";

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

/* Get all Activities */
Expand Down Expand Up @@ -48,7 +48,7 @@ activityRouter.get("/:id", async (req, res) => {
activityRouter.post(
"/",
activityRequestDtoValidator,
isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])),
// isAuthorizedByRole(new Set([Role.ANIMAL_BEHAVIOURIST, Role.ADMINISTRATOR])),
async (req, res) => {
const accessToken = getAccessToken(req);
if (!accessToken) {
Expand All @@ -60,7 +60,6 @@ activityRouter.post(

const { body } = req;
const newActivity = await activityService.createActivity({
activityId: body.activityId,
userId: body.userId,
petId: body.petId,
activityTypeId: body.activityTypeId,
Expand Down Expand Up @@ -88,7 +87,6 @@ activityRouter.put(
try {
const { body } = req;
const Activity = await activityService.updateActivity(id, {
activityId: body.activityId,
userId: body.userId,
petId: body.petId,
activityTypeId: body.activityTypeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class ActivityService implements IActivityService {

return {
id: activity.id,
activityId: activity.activity_id,
userId: activity.user_id,
petId: activity.pet_id,
activityTypeId: activity.activity_type_id,
Expand All @@ -41,7 +40,6 @@ class ActivityService implements IActivityService {
const activities: Array<PgActivity> = await PgActivity.findAll({ raw: true });
return activities.map((activity) => ({
id: activity.id,
activityId: activity.activity_id,
userId: activity.user_id,
petId: activity.pet_id,
activityTypeId: activity.activity_type_id,
Expand All @@ -64,7 +62,6 @@ class ActivityService implements IActivityService {
let newActivity: PgActivity | null;
try {
newActivity = await PgActivity.create({
activity_id: activity.activityId,
user_id: activity.userId,
pet_id: activity.petId,
activity_type_id: activity.activityTypeId,
Expand All @@ -81,7 +78,6 @@ class ActivityService implements IActivityService {
}
return {
id: newActivity.id,
activityId: newActivity.activity_id,
userId: newActivity.user_id,
petId: newActivity.pet_id,
activityTypeId: newActivity.activity_type_id,
Expand All @@ -101,7 +97,6 @@ class ActivityService implements IActivityService {
try {
updateResult = await PgActivity.update(
{
activity_id: activity.activityId,
user_id: activity.userId,
pet_id: activity.petId,
activity_type_id: activity.activityTypeId,
Expand All @@ -125,7 +120,6 @@ class ActivityService implements IActivityService {
}
return {
id: resultingActivity.id,
activityId: resultingActivity.activity_id,
userId: resultingActivity.user_id,
petId: resultingActivity.pet_id,
activityTypeId: resultingActivity.activity_type_id,
Expand Down
2 changes: 0 additions & 2 deletions backend/typescript/services/interfaces/activityService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface ActivityRequestDTO {
activityId: number;
userId?: number;
petId: number;
activityTypeId: number;
Expand All @@ -12,7 +11,6 @@ export interface ActivityRequestDTO {

export interface ActivityResponseDTO {
id: number;
activityId: number;
userId?: number;
petId: number;
activityTypeId: number;
Expand Down

0 comments on commit 69147b9

Please sign in to comment.