diff --git a/src/api/support/ticket_metrics.js b/src/api/support/ticket_metrics.js new file mode 100644 index 0000000..e953276 --- /dev/null +++ b/src/api/support/ticket_metrics.js @@ -0,0 +1,62 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _ticket_id = Joi.number().min(1); +const _ticket_metric_id = Joi.number().min(1); + +// Initialize Endpoint +module.exports = (options = {}) => { + const { error } = validate(options); + if (error) throw new Error(error.details[0].message); + + const { url, headers } = prepare(options); + + return { + /** + * List Ticket Metrics + * + * GET /api/v2/ticket_metrics.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_metrics#list-ticket-metrics + */ + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/ticket_metrics.json`, + headers + }; + }, + + /** + * Show Ticket Metrics + * + * GET /api/v2/ticket_metrics/{ticket_metric_id}.json + * GET /api/v2/tickets/{ticket_id}/metrics.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_metrics#show-ticket-metrics + */ + show: (options = {}) => { + const { error } = Joi.object({ + ticket_id: _ticket_id, + ticket_metric_id: _ticket_metric_id + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { ticket_id = 0, ticket_metric_id = 0 } = options; + if ((!ticket_id && !ticket_metric_id) || (ticket_id && ticket_metric_id)) + throw new Error( + 'either "ticket_id" or "ticket_metric_id" must be set, but not both' + ); + + const part = ticket_id + ? `tickets/${ticket_id}/metrics.json` + : `ticket_metrics/${ticket_metric_id}.json`; + + return { + method: 'GET', + url: `${url}/api/v2/${part}`, + headers + }; + } + }; +}; diff --git a/src/api/support/ticket_metrics/index.js b/src/api/support/ticket_metrics/index.js deleted file mode 100644 index 2a86805..0000000 --- a/src/api/support/ticket_metrics/index.js +++ /dev/null @@ -1,38 +0,0 @@ -const validate = require('./validate'); - -module.exports = ({ instance, headers }) => { - const url = `https://${instance}.zendesk.com`; - - return { - list: (options = null) => { - if (options) throw new Error('options not allowed'); - - return { - method: 'GET', - url: `${url}/api/v2/ticket_metrics.json`, - headers - }; - }, - - show: (options = {}) => { - const { error } = validate.show(options); - if (error) throw new Error(error.details[0].message); - - const { ticket_id = 0, ticket_metric_id = 0 } = options; - if ((!ticket_id && !ticket_metric_id) || (ticket_id && ticket_metric_id)) - throw new Error( - 'either "ticket_id" or "ticket_metric_id" must be set, but not both' - ); - - const part = ticket_id - ? `tickets/${ticket_id}/metrics.json` - : `ticket_metrics/${ticket_metric_id}.json`; - - return { - method: 'GET', - url: `${url}/api/v2/${part}`, - headers - }; - } - }; -}; diff --git a/src/api/support/ticket_metrics/validate.js b/src/api/support/ticket_metrics/validate.js deleted file mode 100644 index b5db851..0000000 --- a/src/api/support/ticket_metrics/validate.js +++ /dev/null @@ -1,14 +0,0 @@ -const Joi = require('@hapi/joi'); - -const ticket_id = Joi.number().min(1); -const ticket_metric_id = Joi.number().min(1); - -module.exports = { - list: null, // no options - - show: options => - Joi.object({ - ticket_id, - ticket_metric_id - }).validate(options) -}; diff --git a/tests/api/support/ticket_metrics.test.js b/tests/api/support/ticket_metrics.test.js new file mode 100644 index 0000000..f8ac0d2 --- /dev/null +++ b/tests/api/support/ticket_metrics.test.js @@ -0,0 +1,72 @@ +const endpoint = require('../../../src/api/support/ticket_metrics'); +const { prepare } = require('../../../src/utils/options'); + +describe('object types', () => { + let endPoint, options, url, headers; + + beforeEach(() => { + options = { + instance: 'instance', + email: 'user@email.com', + token: 'token' + }; + endPoint = endpoint(options); + ({ url, headers } = prepare(options)); + }); + + afterEach(() => { + options = null; + endPoint = null; + url = null; + headers = null; + }); + + describe('init', () => { + it('should setup endpoint object', () => { + const ep = endpoint(options); + expect(ep).toBeTruthy(); + }); + + it('should fail with invalid input', () => { + expect(() => endpoint()).toThrowError(); + expect(() => endpoint({})).toThrowError(); + }); + }); + + describe('list ticket metrics', () => { + it('should process without input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_metrics.json`, + headers + }); + }); + }); + + describe('show ticket metrics', () => { + it('should process w/ valid input', () => { + expect(endPoint.show({ ticket_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/metrics.json`, + headers + }); + + expect(endPoint.show({ ticket_metric_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_metrics/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.show()).toThrowError(); + expect(() => endPoint.show({})).toThrowError(); + expect(() => endPoint.show('invalid')).toThrowError(); + expect(() => endPoint.show({ ticket_id: 0 })).toThrowError(); + expect(() => endPoint.show({ ticket_metric_id: 0 })).toThrowError(); + expect(() => + endPoint.show({ ticket_id: 0, ticket_metric_id: 0 }) + ).toThrowError(); + }); + }); +});