import { BadRequestError, NotFoundError, UnauthorizedError, validateRequest, requireAuth } from '@dss-dev-mathis/dss-core-common' import express, { Request, Response } from 'express' import { body } from 'express-validator' import { isValidObjectId } from 'mongoose' import Sheet from '../models/sheet' const router = express.Router() const validateInput = [ body('title').not().isEmpty().withMessage('Title is required') ] router.put('/api/sheets/:id', requireAuth, validateInput, validateRequest, async (req: Request, res: Response) => { if (!isValidObjectId(req.params.id)) throw new BadRequestError('Invalid Sheet ID') const sheet = await Sheet.findById(req.params.id) if (!sheet) throw new NotFoundError() if (sheet.owner.toHexString() !== req.currentUser!.id) throw new UnauthorizedError() if (sheet.orderId) throw new BadRequestError('Ticket is reserved') const { title, price } = req.body sheet.set({ title, price }) await sheet.save() // TODO: Publish the event res.json(sheet) }) export default router