From 8a10c51a1df26c7523cef8baee50033f9d8f7cf6 Mon Sep 17 00:00:00 2001 From: Andrew Milenic Date: Sun, 1 Sep 2019 17:32:58 -0700 Subject: [PATCH] built support.ticketComments --- src/api/v2/routes/support/ticketComments.js | 57 ++++++++++ src/api/v2/support.js | 6 +- .../v2/validators/support/ticketComments.js | 22 ++++ .../v2/routes/support/ticketComments.test.js | 100 ++++++++++++++++++ tests/src/api/v2/support.test.js | 1 + 5 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/api/v2/routes/support/ticketComments.js create mode 100644 src/api/v2/validators/support/ticketComments.js create mode 100644 tests/src/api/v2/routes/support/ticketComments.test.js diff --git a/src/api/v2/routes/support/ticketComments.js b/src/api/v2/routes/support/ticketComments.js new file mode 100644 index 0000000..eb7c3b7 --- /dev/null +++ b/src/api/v2/routes/support/ticketComments.js @@ -0,0 +1,57 @@ +const validate = require('../../validators/support/ticketComments'); + +module.exports = ({ instance, headers }) => { + const url = `https://${instance}.zendesk.com`; + + return { + list: (options = {}) => { + const { error } = validate.list(options); + if (error) throw new Error(error.details[0].message); + + const { ticket_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${ticket_id}/comments.json`, + headers + }; + }, + + emailCCs: (options = {}) => { + const { error } = validate.emailCCs(options); + if (error) throw new Error(error.details[0].message); + + const { ticket_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/tickets/${ticket_id}/comments.json?include=users`, + headers + }; + }, + + redact: (options = {}) => { + const { error } = validate.redact(options); + if (error) throw new Error(error.details[0].message); + + const { ticket_id, id, data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/tickets/${ticket_id}/comments/${id}/redact.json`, + headers, + data + }; + }, + + makePrivate: (options = {}) => { + const { error } = validate.makePrivate(options); + if (error) throw new Error(error.details[0].message); + + const { ticket_id, id } = options; + return { + method: 'PUT', + url: `${url}/api/v2/tickets/${ticket_id}/comments/${id}/make_private.json`, + headers, + data: {} + }; + } + }; +}; diff --git a/src/api/v2/support.js b/src/api/v2/support.js index 9f0ab56..a543da8 100644 --- a/src/api/v2/support.js +++ b/src/api/v2/support.js @@ -2,7 +2,7 @@ module.exports = ({ instance, headers }) => ({ groups: require('./routes/support/groups')({ instance, headers }), search: require('./routes/support/search')({ instance, headers }), tags: require('./routes/support/tags')({ instance, headers }), - ticket_metrics: require('./routes/support/ticketMetrics')({ + ticket_comments: require('./routes/support/ticketComments')({ instance, headers }), @@ -11,5 +11,9 @@ module.exports = ({ instance, headers }) => ({ headers }), ticket_forms: require('./routes/support/ticketForms')({ instance, headers }), + ticket_metrics: require('./routes/support/ticketMetrics')({ + instance, + headers + }), tickets: require('./routes/support/tickets')({ instance, headers }) }); diff --git a/src/api/v2/validators/support/ticketComments.js b/src/api/v2/validators/support/ticketComments.js new file mode 100644 index 0000000..cad6483 --- /dev/null +++ b/src/api/v2/validators/support/ticketComments.js @@ -0,0 +1,22 @@ +const Joi = require('@hapi/joi'); + +const id = Joi.number().min(1); +const ticket_id = Joi.number().min(1); +const data = Joi.object(); + +module.exports = { + list: options => Joi.validate(options, { ticket_id: ticket_id.required() }), + emailCCs: options => + Joi.validate(options, { ticket_id: ticket_id.required() }), + redact: options => + Joi.validate(options, { + ticket_id: ticket_id.required(), + id: id.required(), + data: data.required() + }), + makePrivate: options => + Joi.validate(options, { + ticket_id: ticket_id.required(), + id: id.required() + }) +}; diff --git a/tests/src/api/v2/routes/support/ticketComments.test.js b/tests/src/api/v2/routes/support/ticketComments.test.js new file mode 100644 index 0000000..432a114 --- /dev/null +++ b/tests/src/api/v2/routes/support/ticketComments.test.js @@ -0,0 +1,100 @@ +const endpoint = require('../../../../../../src/api/v2/routes/support/ticketComments'); + +const instance = 'instance'; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; + +describe('ticket comments', () => { + let ticketComments; + + beforeEach(() => (ticketComments = endpoint({ instance, headers }))); + afterEach(() => (ticketComments = null)); + + describe('list comments', () => { + it('should process w/ valid input', () => { + expect(ticketComments.list({ ticket_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/comments.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => ticketComments.list()).toThrowError(); + expect(() => ticketComments.list('invalid')).toThrowError(); + expect(() => + ticketComments.list({ ticket_id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('list email ccs for a comment', () => { + it('should process w/ valid input', () => { + expect(ticketComments.emailCCs({ ticket_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/tickets/123/comments.json?include=users`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => ticketComments.emailCCs()).toThrowError(); + expect(() => ticketComments.emailCCs('invalid')).toThrowError(); + expect(() => + ticketComments.emailCCs({ ticket_id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('redact string in comment', () => { + it('should process w/ valid input', () => { + expect( + ticketComments.redact({ ticket_id: 123, id: 456, data: {} }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/123/comments/456/redact.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => ticketComments.redact()).toThrowError(); + expect(() => ticketComments.redact('invalid')).toThrowError(); + expect(() => + ticketComments.redact({ ticket_id: 'invalid' }) + ).toThrowError(); + expect(() => + ticketComments.redact({ ticket_id: 123, id: 'invalid' }) + ).toThrowError(); + expect(() => + ticketComments.redact({ ticket_id: 123, id: 456, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('make comment private', () => { + it('should process w/ valid input', () => { + expect(ticketComments.makePrivate({ ticket_id: 123, id: 456 })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/tickets/123/comments/456/make_private.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => ticketComments.makePrivate()).toThrowError(); + expect(() => ticketComments.makePrivate('invalid')).toThrowError(); + expect(() => + ticketComments.makePrivate({ ticket_id: 'invalid' }) + ).toThrowError(); + expect(() => + ticketComments.makePrivate({ ticket_id: 123, id: 'invalid' }) + ).toThrowError(); + }); + }); +}); diff --git a/tests/src/api/v2/support.test.js b/tests/src/api/v2/support.test.js index 4fbe4f7..8431a48 100644 --- a/tests/src/api/v2/support.test.js +++ b/tests/src/api/v2/support.test.js @@ -14,6 +14,7 @@ describe('support api', () => { describe('tickets', () => { test('search', () => check(support.search, 'function')); test('tags', () => check(support.tags)); + test('ticket_comments', () => check(support.ticket_comments)); test('ticket_fields', () => check(support.ticket_fields)); test('ticket_forms', () => check(support.ticket_forms)); test('ticket_metrics', () => check(support.ticket_metrics));