From 2286538b0d78016e6c9be3e62bd4d94f21b2b53e Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Thu, 11 Apr 2019 00:55:38 -0400 Subject: [PATCH] chore(apiv1): added get ticket count with custom query --- src/controllers/api/v1/routes.js | 7 ++++++ src/controllers/api/v1/tickets.js | 42 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/controllers/api/v1/routes.js b/src/controllers/api/v1/routes.js index 5eafea128..849347a19 100644 --- a/src/controllers/api/v1/routes.js +++ b/src/controllers/api/v1/routes.js @@ -66,6 +66,13 @@ module.exports = function (middleware, router, controllers) { router.get('/api/v1/tickets/count/topgroups', apiv1, apiCtrl.tickets.getTopTicketGroups) router.get('/api/v1/tickets/count/topgroups/:top', apiv1, apiCtrl.tickets.getTopTicketGroups) router.get('/api/v1/tickets/count/topgroups/:timespan/:top', apiv1, apiCtrl.tickets.getTopTicketGroups) + router.get( + '/api/v1/tickets/count/group/:id', + apiv1, + isAgentOrAdmin, + canUser('tickets:view'), + apiCtrl.tickets.getCountByGroup + ) router.get('/api/v1/tickets/stats', apiv1, apiCtrl.tickets.getTicketStats) router.get('/api/v1/tickets/stats/group/:group', apiv1, apiCtrl.tickets.getTicketStatsForGroup) router.get('/api/v1/tickets/stats/user/:user', apiv1, apiCtrl.tickets.getTicketStatsForUser) diff --git a/src/controllers/api/v1/tickets.js b/src/controllers/api/v1/tickets.js index c83794a64..410c462f8 100644 --- a/src/controllers/api/v1/tickets.js +++ b/src/controllers/api/v1/tickets.js @@ -242,6 +242,48 @@ apiTickets.getByGroup = function (req, res) { }) } +apiTickets.getCountByGroup = function (req, res) { + var groupId = req.params.id + if (!groupId) return res.status(400).json({ success: false, error: 'Invalid Group Id' }) + if (_.isUndefined(req.query.type) || _.isUndefined(req.query.value)) + return res.status(400).json({ success: false, error: 'Invalid QueryString' }) + + var type = req.query.type + var value = req.query.value + // var limit = req.query.limit ? Number(req.query.limit) : -1 + // var page = req.query.page ? Number(req.query.page) : 0 + + var ticketSchema = require('../../../models/ticket') + + var obj = { + // limit: limit, + // page: page + } + + switch (type.toLowerCase()) { + case 'status': + obj.status = [Number(value)] + ticketSchema.getCountWithObject([groupId], obj, function (err, count) { + if (err) return res.status(500).json({ success: false, error: err.message }) + + return res.json({ success: true, count: count }) + }) + break + case 'tickettype': + obj.filter = { + types: [value] + } + ticketSchema.getCountWithObject([groupId], obj, function (err, count) { + if (err) return res.status(500).json({ success: false, error: err.message }) + + return res.json({ success: true, count: count }) + }) + break + default: + return res.status(400).json({ success: false, error: 'Unsupported type query' }) + } +} + /** * @api {get} /api/v1/tickets/search/?search={searchString} Get Tickets by Search String * @apiName search