From 948767153ffafe9ca5f87bf039cfa0f6b02f3683 Mon Sep 17 00:00:00 2001 From: joonamin Date: Wed, 4 Dec 2024 01:03:12 +0900 Subject: [PATCH] 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 dbdfac1..0f10b76 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 41e3991..40fa287 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) + } +}