diff --git a/src/api/support/automations.js b/src/api/support/automations.js new file mode 100644 index 0000000..241fa1c --- /dev/null +++ b/src/api/support/automations.js @@ -0,0 +1,192 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _id = Joi.number().min(1); +const _ids = Joi.string().min(1); +const _query = Joi.string().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 Automations + * + * GET /api/v2/automations.json + * https://developer.zendesk.com/rest_api/docs/support/automations#list-automations + */ + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/automations.json`, + headers + }; + }, + + /** + * Show Automation + * + * GET /api/v2/automations/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/automations#show-automation + */ + 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/automations/${id}.json`, + headers + }; + }, + + /** + * List Active Automations + * + * GET /api/v2/automations/active.json + * https://developer.zendesk.com/rest_api/docs/support/automations#list-active-automations + */ + active: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/automations/active.json`, + headers + }; + }, + + /** + * Create Automation + * + * POST /api/v2/automations.json + * https://developer.zendesk.com/rest_api/docs/support/automations#create-automation + */ + 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/automations.json`, + headers, + data + }; + }, + + /** + * Update Automation + * + * PUT /api/v2/automations/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/automations#update-automation + */ + 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/automations/${id}.json`, + headers, + data + }; + }, + + /** + * Update Many Automations + * + * PUT /api/v2/automations/update_many.json + * https://developer.zendesk.com/rest_api/docs/support/automations#update-many-automations + */ + update_many: (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/automations/update_many.json`, + headers, + data + }; + }, + + /** + * Delete Automation + * + * DELETE /api/v2/automations/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/automations#delete-automation + */ + 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/automations/${id}.json`, + headers + }; + }, + + /** + * Bulk Delete Automations + * + * DELETE /api/v2/automations/destroy_many.json + * https://developer.zendesk.com/rest_api/docs/support/automations#bulk-delete-automations + */ + delete_bulk: (options = {}) => { + const { error } = Joi.object({ + ids: _ids.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { ids } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/automations/destroy_many.json?ids=${ids}`, + headers + }; + }, + + /** + * Search Automations + * + * GET /api/v2/automations/search.json + * https://developer.zendesk.com/rest_api/docs/support/automations#search-automations + */ + search: (options = {}) => { + const { error } = Joi.object({ + query: _query.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { query } = options; + return { + method: 'GET', + url: `${url}/api/v2/automations/search.json?query=${query}`, + headers + }; + } + }; +}; diff --git a/tests/src/api/support/automations.test.js b/tests/src/api/support/automations.test.js new file mode 100644 index 0000000..8a89fe2 --- /dev/null +++ b/tests/src/api/support/automations.test.js @@ -0,0 +1,178 @@ +const endpoint = require('../../../../src/api/support/automations'); +const { prepare } = require('../../../../src/utils/options'); + +describe('Automations', () => { + 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('List Automations', () => { + it('should process w/ valid input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/automations.json`, + headers + }); + }); + }); + + describe('Show Automation', () => { + it('should process w/ valid input', () => { + expect(endPoint.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/automations/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.show()).toThrowError(); + expect(() => endPoint.show({})).toThrowError(); + expect(() => endPoint.show({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('List Active Automations', () => { + it('should process w/ valid input', () => { + expect(endPoint.active()).toEqual({ + method: 'GET', + url: `${url}/api/v2/automations/active.json`, + headers + }); + }); + }); + + describe('Create Automation', () => { + it('should process w/ valid input', () => { + expect(endPoint.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/automations.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 Automation', () => { + it('should process w/ valid input', () => { + expect(endPoint.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/automations/123.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.update()).toThrowError(); + expect(() => endPoint.update({})).toThrowError(); + expect(() => endPoint.update({ data: {} })).toThrowError(); + expect(() => endPoint.update({ id: 'invalid', data: {} })).toThrowError(); + expect(() => + endPoint.update({ id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('Update Many Automations', () => { + it('should process w/ valid input', () => { + expect(endPoint.update_many({ data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/automations/update_many.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.update_many()).toThrowError(); + expect(() => endPoint.update_many('invalid')).toThrowError(); + expect(() => endPoint.update_many({})).toThrowError(); + expect(() => endPoint.update_many({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('Delete Automation', () => { + it('should process w/ valid input', () => { + expect(endPoint.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/automations/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('Bulk Delete Automations', () => { + it('should process w/ valid input', () => { + expect(endPoint.delete_bulk({ ids: '1,2,3' })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/automations/destroy_many.json?ids=1,2,3`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.delete_bulk()).toThrowError(); + expect(() => endPoint.delete_bulk('invalid')).toThrowError(); + expect(() => endPoint.delete_bulk({})).toThrowError(); + expect(() => endPoint.delete_bulk({ ids: 0 })).toThrowError(); + }); + }); + + describe('Search Automations', () => { + it('should process w/ valid input', () => { + expect(endPoint.search({ query: 'close' })).toEqual({ + method: 'GET', + url: `${url}/api/v2/automations/search.json?query=close`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.search()).toThrowError(); + expect(() => endPoint.search('invalid')).toThrowError(); + expect(() => endPoint.search({})).toThrowError(); + expect(() => endPoint.search({ query: 0 })).toThrowError(); + }); + }); +});