Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create schedule middleware #35

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 미들웨어를 통해 등록된 행사와 관련된 일정만 생성할 수 있게 되는 것으로 확인했습니다.

제가 해당 토의가 정확하게 인지가 안되어서 그런데 해당 방향으로 결정된 것일까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확실하게 결정되지 않은 상황이여서 일단 기본적인 외래키 제약조건만 검사할 수 있도록 설정해두었습니다 :)
추후에 해당 안건이 결정된다면 @/middlewares/schedules.ts/#addScheduleMiddleware 에 체이닝하여 설정할 예정이에요!

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)
}
}
Loading