Skip to content

Commit

Permalink
feat: create schedule validation middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
joonamin committed Dec 1, 2024
1 parent bb4c18b commit 4da9b5e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 1 addition & 2 deletions apps/server/src/controllers/v1/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 23 additions & 1 deletion apps/server/src/middlewares/schedules.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
8 changes: 8 additions & 0 deletions apps/server/src/types/errors/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 4da9b5e

Please sign in to comment.