From 16c54807a5b2606fb13e0938674cfc43e28bd7ae Mon Sep 17 00:00:00 2001 From: androozka Date: Sun, 22 Dec 2019 21:30:57 -0800 Subject: [PATCH] converted organization_fields --- src/api/support/organization_fields.js | 135 ++++++++++++++++++ src/api/support/organization_fields/index.js | 80 ----------- .../support/organization_fields/validate.js | 34 ----- tests/api/support/organization_fields.test.js | 135 ++++++++++++++++++ 4 files changed, 270 insertions(+), 114 deletions(-) create mode 100644 src/api/support/organization_fields.js delete mode 100644 src/api/support/organization_fields/index.js delete mode 100644 src/api/support/organization_fields/validate.js create mode 100644 tests/api/support/organization_fields.test.js diff --git a/src/api/support/organization_fields.js b/src/api/support/organization_fields.js new file mode 100644 index 0000000..69afdcb --- /dev/null +++ b/src/api/support/organization_fields.js @@ -0,0 +1,135 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _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 Organization Fields + * + * GET /api/v2/organization_fields.json + * https://developer.zendesk.com/rest_api/docs/support/organization_fields#list-organization-fields + */ + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/organization_fields.json`, + headers + }; + }, + + /** + * Show Organization Field + * + * GET /api/v2/organization_fields/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/organization_fields#show-organization-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/organization_fields/${id}.json`, + headers + }; + }, + + /** + * Create Organization Fields + * + * POST /api/v2/organization_fields.json + * https://developer.zendesk.com/rest_api/docs/support/organization_fields#create-organization-fields + */ + 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/organization_fields.json`, + headers, + data + }; + }, + + /** + * Update Organization Fields + * + * PUT /api/v2/organization_fields/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/organization_fields#update-organization-fields + */ + 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/organization_fields/${id}.json`, + headers, + data + }; + }, + + /** + * Delete Organization Field + * + * DELETE /api/v2/organization_fields/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/organization_fields#delete-organization-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/organization_fields/${id}.json`, + headers + }; + }, + + /** + * Reorder Organization Field + * + * PUT /api/v2/organization_fields/reorder.json + * https://developer.zendesk.com/rest_api/docs/support/organization_fields#reorder-organization-field + */ + reorder: (options = {}) => { + const { error } = Joi.object({ + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/organization_fields/reorder.json`, + headers, + data + }; + } + }; +}; diff --git a/src/api/support/organization_fields/index.js b/src/api/support/organization_fields/index.js deleted file mode 100644 index 86105c1..0000000 --- a/src/api/support/organization_fields/index.js +++ /dev/null @@ -1,80 +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/organization_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/organization_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/organization_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/organization_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/organization_fields/${id}.json`, - headers - }; - }, - - reorder: (options = {}) => { - const { error } = validate.reorder(options); - if (error) throw new Error(error.details[0].message); - - const { data } = options; - return { - method: 'PUT', - url: `${url}/api/v2/organization_fields/reorder.json`, - headers, - data - }; - } - }; -}; diff --git a/src/api/support/organization_fields/validate.js b/src/api/support/organization_fields/validate.js deleted file mode 100644 index f6f1266..0000000 --- a/src/api/support/organization_fields/validate.js +++ /dev/null @@ -1,34 +0,0 @@ -const Joi = require('@hapi/joi'); - -const 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), - - reorder: options => - Joi.object({ - data: data.required() - }).validate(options) -}; diff --git a/tests/api/support/organization_fields.test.js b/tests/api/support/organization_fields.test.js new file mode 100644 index 0000000..dc43d30 --- /dev/null +++ b/tests/api/support/organization_fields.test.js @@ -0,0 +1,135 @@ +const endpoint = require('../../../src/api/support/organization_fields'); +const { prepare } = require('../../../src/utils/options'); + +describe('organizations', () => { + 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 organization fields', () => { + it('should process without input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/organization_fields.json`, + headers + }); + }); + }); + + describe('show organization field', () => { + it('should process w/ valid input', () => { + expect(endPoint.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/organization_fields/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({ id: 0 })).toThrowError(); + expect(() => endPoint.show({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('create organization fields', () => { + it('should process w/ valid input', () => { + expect(endPoint.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/organization_fields.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.create()).toThrowError(); + expect(() => endPoint.create({})).toThrowError(); + expect(() => endPoint.create('invalid')).toThrowError(); + expect(() => endPoint.create({ ids: 'invalid' })).toThrowError(); + }); + }); + + describe('update organization fields', () => { + it('should process w/ valid input', () => { + expect(endPoint.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/organization_fields/123.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.update()).toThrowError(); + expect(() => endPoint.update({})).toThrowError(); + expect(() => endPoint.update('invalid')).toThrowError(); + expect(() => endPoint.update({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('delete organization fields', () => { + it('should process w/ valid input', () => { + expect(endPoint.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/organization_fields/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.delete()).toThrowError(); + expect(() => endPoint.delete({})).toThrowError(); + expect(() => endPoint.delete('invalid')).toThrowError(); + expect(() => endPoint.delete({ id: 0 })).toThrowError(); + expect(() => endPoint.delete({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('reorder organization field', () => { + it('should process w/ valid input', () => { + expect(endPoint.reorder({ data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/organization_fields/reorder.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.reorder()).toThrowError(); + expect(() => endPoint.reorder({})).toThrowError(); + expect(() => endPoint.reorder('invalid')).toThrowError(); + expect(() => endPoint.reorder({ data: 'invalid' })).toThrowError(); + }); + }); +});