From 107559d340a028a4ee24f09831c3cb5aa75626ea Mon Sep 17 00:00:00 2001 From: joonamin Date: Sun, 1 Dec 2024 22:01:27 +0900 Subject: [PATCH 1/3] feat: add `validate schedule` middleware --- apps/server/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/package.json b/apps/server/package.json index da4829f..4f6e8eb 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -13,6 +13,7 @@ "private": true, "dependencies": { "@typegoose/typegoose": "^12.9.0", + "@types/node": "^20", "compression": "^1.7.5", "cors": "^2.8.5", "cross-env": "^7.0.3", From 406ac5e9641c30d8e0f322f952f0cb74a4843638 Mon Sep 17 00:00:00 2001 From: joonamin Date: Wed, 4 Dec 2024 01:00:11 +0900 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/package.json b/apps/server/package.json index 4f6e8eb..da4829f 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -13,7 +13,6 @@ "private": true, "dependencies": { "@typegoose/typegoose": "^12.9.0", - "@types/node": "^20", "compression": "^1.7.5", "cors": "^2.8.5", "cross-env": "^7.0.3", From d9f16dd3d57f4c8a9ecfad7a20613d11970666b4 Mon Sep 17 00:00:00 2001 From: joonamin Date: Wed, 4 Dec 2024 01:03:12 +0900 Subject: [PATCH 3/3] feat: create schedule validation middleware --- apps/server/src/controllers/v1/schedules.ts | 3 +-- apps/server/src/middlewares/schedules.ts | 24 ++++++++++++++++++++- apps/server/src/types/errors/schedule.ts | 8 +++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/apps/server/src/controllers/v1/schedules.ts b/apps/server/src/controllers/v1/schedules.ts index 00dc879..bef6172 100644 --- a/apps/server/src/controllers/v1/schedules.ts +++ b/apps/server/src/controllers/v1/schedules.ts @@ -5,8 +5,7 @@ import middlewares from '@/middlewares' const router: Router = asyncify(express.Router()) -// TODO: add validator(eventId, authorId) -router.post('/', async (req: Request, res: Response) => { +router.post('/', middlewares.schedules.addScheduleMiddleware, async (req: Request, res: Response) => { const { name, eventId, authorId, startDate, endDate, address, location, description, images } = req.body const schedule = await ScheduleModel.create({ name: name, diff --git a/apps/server/src/middlewares/schedules.ts b/apps/server/src/middlewares/schedules.ts index 3779cfd..6870902 100644 --- a/apps/server/src/middlewares/schedules.ts +++ b/apps/server/src/middlewares/schedules.ts @@ -1,6 +1,8 @@ import { NextFunction, Request, Response } from 'express' import { ScheduleModel } from '@/models/schedule' -import { UnauthorizedSchedule } from '@/types/errors/schedule' +import { InvalidRequestFormat, UnauthorizedSchedule } from '@/types/errors/schedule' +import { EventsModel } from '@/models/event' +import { UserModel } from '@/models/user' export const verifyAuthorMiddleware = async (req: Request, res: Response, next: NextFunction) => { const authorId = (await ScheduleModel.findOne({ _id: req.params.id }).exec())?.authorId @@ -12,3 +14,23 @@ export const verifyAuthorMiddleware = async (req: Request, res: Response, next: } next() } + +const verifyEventId = async (req: Request, res: Response, next: NextFunction) => { + const { eventId } = req.body + const target = await EventsModel.findOne({ _id: eventId }).exec() + if (!target) { + throw new InvalidRequestFormat(new Error(`not found event => id: ${eventId}`)) + } + next() +} + +const verifyAuthorId = async (req: Request, res: Response, next: NextFunction) => { + const { authorId } = req.body + const target = await UserModel.findOne({ _id: authorId }).exec() + if (!target) { + throw new InvalidRequestFormat(new Error(`not found author => id: ${authorId}`)) + } + next() +} + +export const addScheduleMiddleware = [verifyAuthorId, verifyEventId] diff --git a/apps/server/src/types/errors/schedule.ts b/apps/server/src/types/errors/schedule.ts index 2b901c0..79a74de 100644 --- a/apps/server/src/types/errors/schedule.ts +++ b/apps/server/src/types/errors/schedule.ts @@ -7,3 +7,11 @@ export class UnauthorizedSchedule extends APIError { Error.captureStackTrace(this, UnauthorizedSchedule) } } + +export class InvalidRequestFormat extends APIError { + constructor(cause: Error | string = null) { + super(400, 40000, 'invalid request format', cause) + Object.setPrototypeOf(this, InvalidRequestFormat.prototype) + Error.captureStackTrace(this, InvalidRequestFormat) + } +}