diff --git a/src/api/support/ticket_skips.js b/src/api/support/ticket_skips.js new file mode 100644 index 0000000..b975d75 --- /dev/null +++ b/src/api/support/ticket_skips.js @@ -0,0 +1,67 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _data = Joi.object(); +const _ticket_id = Joi.number().positive(); +const _user_id = Joi.number().positive(); + +// Initialize Endpoint +module.exports = (options = {}) => { + const { error } = validate(options); + if (error) throw new Error(error.details[0].message); + + const { url, headers } = prepare(options); + + return { + /** + * Record a new skip for the current user + * + * POST /api/v2/skips.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_skips#record-a-new-skip-for-the-current-user + */ + record: (options = {}) => { + const { error } = Joi.object({ + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { data } = options; + return { + method: 'POST', + url: `${url}/api/v2/skips.json`, + headers, + data + }; + }, + + /** + * List skips for the current account + * + * GET /api/v2/skips.json + * GET /api/v2/tickets/{ticket_id}/skips.json + * GET /api/v2/users/{user_id}/skips.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_skips#list-skips-for-the-current-account + */ + list: (options = {}) => { + const { error } = Joi.object({ + ticket_id: _ticket_id, + user_id: _user_id + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { ticket_id = 0, user_id = 0 } = options; + const section = ticket_id + ? `tickets/${ticket_id}/` + : user_id + ? `users/${user_id}/` + : ''; + + return { + method: 'GET', + url: `${url}/api/v2/${section}skips.json`, + headers + }; + } + }; +}; diff --git a/tests/src/api/support/ticket_skips.test.js b/tests/src/api/support/ticket_skips.test.js new file mode 100644 index 0000000..7f8b0f8 --- /dev/null +++ b/tests/src/api/support/ticket_skips.test.js @@ -0,0 +1,80 @@ +const endpoint = require('../../../../src/api/support/ticket_skips'); +const { prepare } = require('../../../../src/utils/options'); + +describe('ticket skips', () => { + 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', () => { + expect(endpoint(options)).toBeTruthy(); + }); + + it('should fail with invalid input', () => { + expect(() => endpoint()).toThrowError(); + expect(() => endpoint({})).toThrowError(); + }); + }); + + describe('record a new skip for the current account', () => { + it('should process w/ valid input', () => { + const data = { skip: { ticket_id: 123, reason: 'I have no idea.' } }; + + expect(endPoint.record({ data })).toEqual({ + method: 'POST', + url: `${url}/api/v2/skips.json`, + headers, + data + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.record()).toThrowError(); + expect(() => endPoint.record('invalid')).toThrowError(); + expect(() => endPoint.record({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('list skips for the current account', () => { + it('should process w/ valid input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/skips.json`, + headers + }); + + expect(endPoint.list({ ticket_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/skips.json`, + headers + }); + + expect(endPoint.list({ user_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/skips.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.list({ ticket_id: 'invalid' })).toThrowError(); + expect(() => endPoint.list({ user_id: 'invalid' })).toThrowError(); + }); + }); +});