From 332ddc14f62591e6cf49d5713d868001c04cc38d Mon Sep 17 00:00:00 2001 From: androozka Date: Mon, 23 Dec 2019 22:18:26 -0800 Subject: [PATCH] converted ticket_fields --- src/api/support/ticket_fields.js | 200 ++++++++++++++++++++ src/api/support/ticket_fields/index.js | 116 ------------ src/api/support/ticket_fields/validate.js | 54 ------ tests/api/support/ticket_fields.test.js | 211 ++++++++++++++++++++++ 4 files changed, 411 insertions(+), 170 deletions(-) create mode 100644 src/api/support/ticket_fields.js delete mode 100644 src/api/support/ticket_fields/index.js delete mode 100644 src/api/support/ticket_fields/validate.js create mode 100644 tests/api/support/ticket_fields.test.js diff --git a/src/api/support/ticket_fields.js b/src/api/support/ticket_fields.js new file mode 100644 index 0000000..99f3f41 --- /dev/null +++ b/src/api/support/ticket_fields.js @@ -0,0 +1,200 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _id = Joi.number().min(1); +const _field_id = Joi.number().min(1); +const _data = Joi.object(); + +// 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 Fields + * + * GET /api/v2/ticket_fields.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#list-ticket-fields + */ + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/ticket_fields.json`, + headers + }; + }, + + /** + * Show Ticket Field + * + * GET /api/v2/ticket_fields/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#show-ticket-field + */ + show: (options = {}) => { + const { error } = Joi.object({ + id: _id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'GET', + url: `${url}/api/v2/ticket_fields/${id}.json`, + headers + }; + }, + + /** + * Create Ticket Field + * + * POST /api/v2/ticket_fields.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#create-ticket-field + */ + create: (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/ticket_fields.json`, + headers, + data + }; + }, + + /** + * Update Ticket Field + * + * PUT /api/v2/ticket_fields/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#update-ticket-field + */ + update: (options = {}) => { + const { error } = Joi.object({ + id: _id.required(), + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { id, data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/ticket_fields/${id}.json`, + headers, + data + }; + }, + + /** + * Delete Ticket Field + * + * DELETE /api/v2/ticket_fields/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#delete-ticket-field + */ + delete: (options = {}) => { + const { error } = Joi.object({ + id: _id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/ticket_fields/${id}.json`, + headers + }; + }, + + /** + * List Ticket Field Options + * + * GET /api/v2/ticket_fields/{field_id}/options.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#list-ticket-field-options + */ + listOptions: (options = {}) => { + const { error } = Joi.object({ + field_id: _field_id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { field_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/ticket_fields/${field_id}/options.json`, + headers + }; + }, + + /** + * Show Ticket Field Option + * + * GET /api/v2/ticket_fields/{field_id}/options/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#show-ticket-field-option + */ + showOption: (options = {}) => { + const { error } = Joi.object({ + field_id: _field_id.required(), + id: _id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { field_id, id } = options; + return { + method: 'GET', + url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, + headers + }; + }, + + /** + * Create or Update Ticket Field Option + * + * POST /api/v2/ticket_fields/{field_id}/options.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#create-or-update-ticket-field-option + */ + createOrUpdateOption: (options = {}) => { + const { error } = Joi.object({ + field_id: _field_id.required(), + id: _id.required(), + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { field_id, id, data } = options; + return { + method: 'POST', + url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, + headers, + data + }; + }, + + /** + * Delete Ticket Field Option + * + * DELETE /api/v2/ticket_fields/{field_id}/options/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#delete-ticket-field-option + */ + deleteOption: (options = {}) => { + const { error } = Joi.object({ + field_id: _field_id.required(), + id: _id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { field_id, id } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, + headers + }; + } + }; +}; diff --git a/src/api/support/ticket_fields/index.js b/src/api/support/ticket_fields/index.js deleted file mode 100644 index 4f29bb6..0000000 --- a/src/api/support/ticket_fields/index.js +++ /dev/null @@ -1,116 +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('no options are allowed'); - - return { - method: 'GET', - url: `${url}/api/v2/ticket_fields.json`, - headers - }; - }, - - show: (options = {}) => { - const { error } = validate.show(options); - if (error) throw new Error(error.details[0].message); - - const { id } = options; - return { - method: 'GET', - url: `${url}/api/v2/ticket_fields/${id}.json`, - headers - }; - }, - - create: (options = {}) => { - const { error } = validate.create(options); - if (error) throw new Error(error.details[0].message); - - const { data } = options; - return { - method: 'POST', - url: `${url}/api/v2/ticket_fields.json`, - headers, - data - }; - }, - - update: (options = {}) => { - const { error } = validate.update(options); - if (error) throw new Error(error.details[0].message); - - const { id, data } = options; - return { - method: 'PUT', - url: `${url}/api/v2/ticket_fields/${id}.json`, - headers, - data - }; - }, - - delete: (options = {}) => { - const { error } = validate.delete(options); - if (error) throw new Error(error.details[0].message); - - const { id } = options; - return { - method: 'DELETE', - url: `${url}/api/v2/ticket_fields/${id}.json`, - headers - }; - }, - - listOptions: (options = {}) => { - const { error } = validate.listOptions(options); - if (error) throw new Error(error.details[0].message); - - const { field_id } = options; - return { - method: 'GET', - url: `${url}/api/v2/ticket_fields/${field_id}/options.json`, - headers - }; - }, - - showOption: (options = {}) => { - const { error } = validate.showOption(options); - if (error) throw new Error(error.details[0].message); - - const { field_id, id } = options; - return { - method: 'GET', - url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, - headers - }; - }, - - createOrUpdateOption: (options = {}) => { - const { error } = validate.createOrUpdateOption(options); - if (error) throw new Error(error.details[0].message); - - const { field_id, id, data } = options; - return { - method: 'POST', - url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, - headers, - data - }; - }, - - deleteOption: (options = {}) => { - const { error } = validate.deleteOption(options); - if (error) throw new Error(error.details[0].message); - - const { field_id, id } = options; - return { - method: 'DELETE', - url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, - headers - }; - } - }; -}; diff --git a/src/api/support/ticket_fields/validate.js b/src/api/support/ticket_fields/validate.js deleted file mode 100644 index 823c05f..0000000 --- a/src/api/support/ticket_fields/validate.js +++ /dev/null @@ -1,54 +0,0 @@ -const Joi = require('@hapi/joi'); - -const id = Joi.number().min(1); -const field_id = Joi.number().min(1); -const data = Joi.object(); - -module.exports = { - list: null, // no options - - show: options => - Joi.object({ - id: id.required() - }).validate(options), - - create: options => - Joi.object({ - data: data.required() - }).validate(options), - - update: options => - Joi.object({ - id: id.required(), - data: data.required() - }).validate(options), - - delete: options => - Joi.object({ - id: id.required() - }).validate(options), - - listOptions: options => - Joi.object({ - field_id: field_id.required() - }).validate(options), - - showOption: options => - Joi.object({ - field_id: field_id.required(), - id: id.required() - }).validate(options), - - createOrUpdateOption: options => - Joi.object({ - field_id: field_id.required(), - id: id.required(), - data: data.required() - }).validate(options), - - deleteOption: options => - Joi.object({ - field_id: field_id.required(), - id: id.required() - }).validate(options) -}; diff --git a/tests/api/support/ticket_fields.test.js b/tests/api/support/ticket_fields.test.js new file mode 100644 index 0000000..e39e78e --- /dev/null +++ b/tests/api/support/ticket_fields.test.js @@ -0,0 +1,211 @@ +const endpoint = require('../../../src/api/support/ticket_fields'); +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 fields', () => { + it('should process without input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_fields.json`, + headers + }); + }); + }); + + describe('show ticket field', () => { + it('should process w/ valid input', () => { + expect(endPoint.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_fields/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.show()).toThrowError(); + expect(() => endPoint.show('invalid')).toThrowError(); + expect(() => endPoint.show({})).toThrowError(); + expect(() => endPoint.show({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('create ticket field', () => { + it('should process w/ valid input', () => { + expect(endPoint.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/ticket_fields.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.create()).toThrowError(); + expect(() => endPoint.create('invalid')).toThrowError(); + expect(() => endPoint.create({})).toThrowError(); + expect(() => endPoint.create({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('update ticket field', () => { + it('should process w/ valid input', () => { + expect(endPoint.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/ticket_fields/123.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.update()).toThrowError(); + expect(() => endPoint.update('invalid')).toThrowError(); + expect(() => endPoint.update({})).toThrowError(); + expect(() => endPoint.update({ id: 'invalid' })).toThrowError(); + expect(() => + endPoint.update({ id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('delete ticket field', () => { + it('should process w/ valid input', () => { + expect(endPoint.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/ticket_fields/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.delete()).toThrowError(); + expect(() => endPoint.delete('invalid')).toThrowError(); + expect(() => endPoint.delete({})).toThrowError(); + expect(() => endPoint.delete({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('list ticket field options', () => { + it('should process w/ valid input', () => { + expect(endPoint.listOptions({ field_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_fields/123/options.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.listOptions()).toThrowError(); + expect(() => endPoint.listOptions('invalid')).toThrowError(); + expect(() => endPoint.listOptions({})).toThrowError(); + expect(() => + endPoint.listOptions({ field_id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('show a ticket field option', () => { + it('should process w/ valid input', () => { + expect(endPoint.showOption({ field_id: 123, id: 456 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_fields/123/options/456.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.showOption()).toThrowError(); + expect(() => endPoint.showOption('invalid')).toThrowError(); + expect(() => endPoint.showOption({})).toThrowError(); + expect(() => endPoint.showOption({ field_id: 'invalid' })).toThrowError(); + expect(() => + endPoint.showOption({ field_id: 123, id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('create or update a ticket field option', () => { + it('should process w/ valid input', () => { + expect( + endPoint.createOrUpdateOption({ field_id: 123, id: 456, data: {} }) + ).toEqual({ + method: 'POST', + url: `${url}/api/v2/ticket_fields/123/options/456.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.createOrUpdateOption()).toThrowError(); + expect(() => endPoint.createOrUpdateOption('invalid')).toThrowError(); + expect(() => endPoint.createOrUpdateOption({})).toThrowError(); + expect(() => + endPoint.createOrUpdateOption({ field_id: 'invalid' }) + ).toThrowError(); + expect(() => + endPoint.createOrUpdateOption({ field_id: 123, id: 'invalid' }) + ).toThrowError(); + expect(() => + endPoint.createOrUpdateOption({ + field_id: 123, + id: 456, + data: 'invalid' + }) + ).toThrowError(); + }); + }); + + describe('delete ticket field option', () => { + it('should process w/ valid input', () => { + expect(endPoint.deleteOption({ field_id: 123, id: 456 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/ticket_fields/123/options/456.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.deleteOption()).toThrowError(); + expect(() => endPoint.deleteOption('invalid')).toThrowError(); + expect(() => endPoint.deleteOption({})).toThrowError(); + expect(() => + endPoint.deleteOption({ field_id: 'invalid' }) + ).toThrowError(); + expect(() => + endPoint.deleteOption({ field_id: 123, id: 'invalid' }) + ).toThrowError(); + }); + }); +});