From cf221b1ac9bbab377890aa4ece59f40fbf834bd2 Mon Sep 17 00:00:00 2001 From: androozka Date: Fri, 24 Jan 2020 18:19:49 -0800 Subject: [PATCH] built support:twitter_channel --- src/api/support/twitter_channel.js | 92 ++++++++++++++++++ tests/src/api/support/twitter_channel.test.js | 94 +++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 src/api/support/twitter_channel.js create mode 100644 tests/src/api/support/twitter_channel.test.js diff --git a/src/api/support/twitter_channel.js b/src/api/support/twitter_channel.js new file mode 100644 index 0000000..6c734b8 --- /dev/null +++ b/src/api/support/twitter_channel.js @@ -0,0 +1,92 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _id = Joi.number().positive(); +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 { + /** + * Listing Monitored Twitter Handles + * + * GET /api/v2/channels/twitter/monitored_twitter_handles.json + * https://developer.zendesk.com/rest_api/docs/support/twitter_channel#listing-monitored-twitter-handles + */ + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/channels/twitter/monitored_twitter_handles.json`, + headers + }; + }, + + /** + * Getting Monitored Twitter Handle + * + * GET /api/v2/channels/twitter/monitored_twitter_handles/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/twitter_channel#getting-monitored-twitter-handle + */ + 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/channels/twitter/monitored_twitter_handles/${id}.json`, + headers + }; + }, + + /** + * Create Ticket from Tweet + * + * POST /api/v2/channels/twitter/tickets.json + * https://developer.zendesk.com/rest_api/docs/support/twitter_channel#create-ticket-from-tweet + */ + 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/channels/twitter/tickets.json`, + headers, + data + }; + }, + + /** + * Getting Twicket status + * + * GET /api/v2/channels/twitter/tickets/{id}/statuses.json + * https://developer.zendesk.com/rest_api/docs/support/twitter_channel#getting-twicket-status + */ + status: (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/channels/twitter/tickets/${id}/statuses.json`, + headers + }; + } + }; +}; diff --git a/tests/src/api/support/twitter_channel.test.js b/tests/src/api/support/twitter_channel.test.js new file mode 100644 index 0000000..97b9ad3 --- /dev/null +++ b/tests/src/api/support/twitter_channel.test.js @@ -0,0 +1,94 @@ +const endpoint = require('../../../../src/api/support/twitter_channel'); +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', () => { + expect(endpoint(options)).toBeTruthy(); + }); + + it('should fail with invalid input', () => { + expect(() => endpoint()).toThrowError(); + expect(() => endpoint({})).toThrowError(); + }); + }); + + describe('listing monitored twitter handles', () => { + it('should process w/ valid input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/channels/twitter/monitored_twitter_handles.json`, + headers + }); + }); + }); + + describe('getting monitored twitter handle', () => { + it('should process w/ valid input', () => { + expect(endPoint.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/channels/twitter/monitored_twitter_handles/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('create ticket from tweet', () => { + it('should process w/ valid input', () => { + expect(endPoint.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/channels/twitter/tickets.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('getting twicket status', () => { + it('should process w/ valid input', () => { + expect(endPoint.status({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/channels/twitter/tickets/123/statuses.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.status()).toThrowError(); + expect(() => endPoint.status({})).toThrowError(); + expect(() => endPoint.status({ id: 'invalid' })).toThrowError(); + }); + }); +});