text: import TicketSerializer from '@apps/events/serializers/ticket_serializer'
  import Registration from '@apps/events/models/registration'
  import Ticket from '@apps/events/models/ticket'
  import Event from '@apps/events/models/event'
  
  const ticketsRoute = async (req, res) => {
  
    const event = await Event.query(qb => {
      qb.where('team_id', req.team.get('id'))
      qb.where('id', req.params.event_id)
      qb.whereNull('deleted_at')
    }).fetch({
      transacting: req.trx
    })
  
    if(!event) return res.status(404).respond({
      code: 404,
      message: 'Unable to load event'
    })
  
    const registration = await Registration.query(qb => {
      qb.select('events_registrations.*','events_registration_totals.*')
      qb.innerJoin('events_registration_totals','events_registration_totals.registration_id','events_registrations.id')
      qb.where('events_registrations.team_id', req.team.get('id'))
      qb.where('events_registrations.event_id', event.get('id'))
      qb.where('events_registrations.id', req.params.id)
    }).fetch({
      withRelated: ['invoice.line_items','tickets','payment'],
      transacting: req.trx
    })
  
    if(!registration) return res.status(404).respond({
      code: 404,
      message: 'Unable to load registration'
    })
  
    const tickets = await Ticket.filterFetch({
      scope: (qb) => {
        qb.innerJoin('events_ticket_types','events_ticket_types.id','events_tickets.ticket_type_id')
        qb.where('events_tickets.team_id', req.team.get('id'))
        qb.where('events_tickets.registration_id', registration.get('id'))
      },
      aliases: {
        name: 'events_tickets.name',
        ticket_type: 'events_ticket_types.title'
      },
      virtuals: [
        { column: 'events_tickets.values', fields: event.get('ticket_config').fields }
      ],
      filter: {
        params: req.query.$filter,
        allowed: ['title'],
        search: ['name']
      },
      sort: {
        params: req.query.$sort,
        defaults: 'created_at',
        allowed: ['created_at']
      },
      withRelated: ['ticket_type'],
      page: req.query.$page,
      transacting: req.trx
    })
  
    await res.status(200).respond(tickets, TicketSerializer)
  
  }
  
  export default ticketsRoute
  