diff --git a/src/v2/support/index.js b/src/v2/support/index.js index e111b5b..df823fa 100644 --- a/src/v2/support/index.js +++ b/src/v2/support/index.js @@ -5,6 +5,7 @@ module.exports = ({ instance, headers }) => ({ organization_fields: require('./organization_fields')({ instance, headers }), search: require('./search')({ instance, headers }), tags: require('./tags')({ instance, headers }), + ticket_activities: require('./ticket_activities')({ instance, headers }), ticket_comments: require('./ticket_comments')({ instance, headers }), ticket_fields: require('./ticket_fields')({ instance, headers }), ticket_forms: require('./ticket_forms')({ instance, headers }), diff --git a/src/v2/support/ticket_activities/index.js b/src/v2/support/ticket_activities/index.js new file mode 100644 index 0000000..646e4d0 --- /dev/null +++ b/src/v2/support/ticket_activities/index.js @@ -0,0 +1,29 @@ +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/activities.json`, + headers + }; + }, + + show: (options = {}) => { + const { error } = validate.show(options); + if (error) throw new Error(error.details[0].message); + + const { activity_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/activities/${activity_id}.json`, + headers + }; + } + }; +}; diff --git a/src/v2/support/ticket_activities/validate.js b/src/v2/support/ticket_activities/validate.js new file mode 100644 index 0000000..bd5f186 --- /dev/null +++ b/src/v2/support/ticket_activities/validate.js @@ -0,0 +1,9 @@ +const Joi = require('@hapi/joi'); + +const activity_id = Joi.number().min(1); + +module.exports = { + list: null, // no options + show: options => + Joi.validate(options, { activity_id: activity_id.required() }) +}; diff --git a/tests/src/v2/support.test.js b/tests/src/v2/support.test.js index 00dfa67..a5d8b7e 100644 --- a/tests/src/v2/support.test.js +++ b/tests/src/v2/support.test.js @@ -17,6 +17,7 @@ describe('support api', () => { test('organization_fields', () => check(support.organization_fields)); test('search', () => check(support.search, 'function')); test('tags', () => check(support.tags)); + test('ticket_activities', () => check(support.ticket_activities)); test('ticket_comments', () => check(support.ticket_comments)); test('ticket_fields', () => check(support.ticket_fields)); test('ticket_forms', () => check(support.ticket_forms)); diff --git a/tests/src/v2/support/ticket_activities.test.js b/tests/src/v2/support/ticket_activities.test.js new file mode 100644 index 0000000..567eec2 --- /dev/null +++ b/tests/src/v2/support/ticket_activities.test.js @@ -0,0 +1,48 @@ +const endpoint = require('../../../../src/v2/support/ticket_activities'); + +const instance = 'instance'; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; + +describe('end users', () => { + let ticket_activities; + + beforeEach(() => (ticket_activities = endpoint({ instance, headers }))); + afterEach(() => (ticket_activities = null)); + + describe('list activities', () => { + it('should process w/ valid input', () => { + expect(ticket_activities.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/activities.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => ticket_activities.list('invalid')).toThrowError(); + }); + }); + + describe('show activity', () => { + it('should process w/ valid input', () => { + expect(ticket_activities.show({ activity_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/activities/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => ticket_activities.show()).toThrowError(); + expect(() => ticket_activities.show('invalid')).toThrowError(); + expect(() => ticket_activities.show({})).toThrowError(); + expect(() => + ticket_activities.show({ activity_id: 'invalid' }) + ).toThrowError(); + }); + }); +});