UNPKG

1.75 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _ticket_id = Joi.number().min(1);
6const _ticket_metric_id = Joi.number().min(1);
7
8// Initialize Endpoint
9module.exports = (options = {}) => {
10 const { error } = validate(options);
11 if (error) throw new Error(error.details[0].message);
12
13 const { url, headers } = prepare(options);
14
15 return {
16 /**
17 * List Ticket Metrics
18 *
19 * GET /api/v2/ticket_metrics.json
20 * https://developer.zendesk.com/rest_api/docs/support/ticket_metrics#list-ticket-metrics
21 */
22 list: () => {
23 // Ignore any options
24 return {
25 method: 'GET',
26 url: `${url}/api/v2/ticket_metrics.json`,
27 headers
28 };
29 },
30
31 /**
32 * Show Ticket Metrics
33 *
34 * GET /api/v2/ticket_metrics/{ticket_metric_id}.json
35 * GET /api/v2/tickets/{ticket_id}/metrics.json
36 * https://developer.zendesk.com/rest_api/docs/support/ticket_metrics#show-ticket-metrics
37 */
38 show: (options = {}) => {
39 const { error } = Joi.object({
40 ticket_id: _ticket_id,
41 ticket_metric_id: _ticket_metric_id
42 }).validate(options);
43 if (error) throw new Error(error.details[0].message);
44
45 const { ticket_id = 0, ticket_metric_id = 0 } = options;
46 if ((!ticket_id && !ticket_metric_id) || (ticket_id && ticket_metric_id))
47 throw new Error(
48 'either "ticket_id" or "ticket_metric_id" must be set, but not both'
49 );
50
51 const part = ticket_id
52 ? `tickets/${ticket_id}/metrics.json`
53 : `ticket_metrics/${ticket_metric_id}.json`;
54
55 return {
56 method: 'GET',
57 url: `${url}/api/v2/${part}`,
58 headers
59 };
60 }
61 };
62};