All files / src/routes create.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 2/2
100% Lines 15/15

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 253x 3x 3x   3x 3x 3x   3x   3x         11x 11x 11x 11x 11x 11x     3x  
import express, { Request, Response } from 'express'
import { body } from 'express-validator'
import { requireAuth, validateRequest } from '@next-k8s/common'
 
import Ticket from '../models/ticket'
import TicketCreatedPublisher from '../events/publishers/created'
import natsClient from '../nats-client'
 
const router = express.Router()
 
const validateInput = [
  body('title').not().isEmpty().withMessage('Title is required'),
  body('price').isInt({ gt: -1, lt: Number.MAX_SAFE_INTEGER }).withMessage('Price must be an integer of cents or units')
]
 
router.post('/api/tickets', requireAuth, validateInput, validateRequest, async (req: Request, res: Response) => {
  const { title, price } = req.body
  const ticket = new Ticket({ title, price, owner: req.currentUser!.id })
  await ticket.save()
  new TicketCreatedPublisher(natsClient.client).publish({ id: ticket.id, version: ticket.version, title: ticket.title, price: ticket.price, owner: ticket.owner })
  res.status(201).send({ ticket })
})
 
export default router