All files / src/routes update.ts

39.13% Statements 9/23
0% Branches 0/4
0% Functions 0/2
50% Lines 9/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x 1x 1x 1x   1x   1x   1x       1x                         1x  
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) => {
  Iif (!isValidObjectId(req.params.id)) throw new BadRequestError('Invalid Sheet ID')
  const sheet = await Sheet.findById(req.params.id)
  Iif (!sheet) throw new NotFoundError()
  Iif (sheet.owner.toHexString() !== req.currentUser!.id) throw new UnauthorizedError()
  Iif (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