diff --git a/src/v2/support/index.js b/src/v2/support/index.js index df823fa..4939374 100644 --- a/src/v2/support/index.js +++ b/src/v2/support/index.js @@ -11,5 +11,6 @@ module.exports = ({ instance, headers }) => ({ ticket_forms: require('./ticket_forms')({ instance, headers }), ticket_metrics: require('./ticket_metrics')({ instance, headers }), tickets: require('./tickets')({ instance, headers }), + user_fields: require('./user_fields')({ instance, headers }), users: require('./users')({ instance, headers }) }); diff --git a/src/v2/support/user_fields/index.js b/src/v2/support/user_fields/index.js new file mode 100644 index 0000000..e5b3e7b --- /dev/null +++ b/src/v2/support/user_fields/index.js @@ -0,0 +1,129 @@ +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/user_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/user_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/user_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/user_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/user_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/user_fields/reorder.json`, + headers, + data + }; + }, + + listOptions: (options = {}) => { + const { error } = validate.listOptions(options); + if (error) throw new Error(error.details[0].message); + + const { field_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/user_fields/${field_id}/options.json`, + headers + }; + }, + + showOption: (options = {}) => { + const { error } = validate.showOption(options); + if (error) throw new Error(error.details[0].message); + + const { field_id, id } = options; + return { + method: 'GET', + url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, + headers + }; + }, + + createOrUpdateOption: (options = {}) => { + const { error } = validate.createOrUpdateOption(options); + if (error) throw new Error(error.details[0].message); + + const { field_id, data } = options; + return { + method: 'POST', + url: `${url}/api/v2/user_fields/${field_id}/options.json`, + headers, + data + }; + }, + + deleteOption: (options = {}) => { + const { error } = validate.deleteOption(options); + if (error) throw new Error(error.details[0].message); + + const { field_id, id } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/user_fields/${field_id}/options/${id}.json`, + headers + }; + } + }; +}; diff --git a/src/v2/support/user_fields/validate.js b/src/v2/support/user_fields/validate.js new file mode 100644 index 0000000..81a367d --- /dev/null +++ b/src/v2/support/user_fields/validate.js @@ -0,0 +1,26 @@ +const Joi = require('@hapi/joi'); + +const id = Joi.number().min(1); +const field_id = Joi.number().min(1); +const data = Joi.object(); + +module.exports = { + list: null, // no options + show: options => Joi.validate(options, { id: id.required() }), + create: options => Joi.validate(options, { data: data.required() }), + update: options => + Joi.validate(options, { id: id.required(), data: data.required() }), + delete: options => Joi.validate(options, { id: id.required() }), + reorder: options => Joi.validate(options, { data: data.required() }), + listOptions: options => + Joi.validate(options, { field_id: field_id.required() }), + showOption: options => + Joi.validate(options, { field_id: field_id.required(), id: id.required() }), + createOrUpdateOption: options => + Joi.validate(options, { + field_id: field_id.required(), + data: data.required() + }), + deleteOption: options => + Joi.validate(options, { field_id: field_id.required(), id: id.required() }) +}; diff --git a/tests/src/v2/support.test.js b/tests/src/v2/support.test.js index a5d8b7e..4b5c474 100644 --- a/tests/src/v2/support.test.js +++ b/tests/src/v2/support.test.js @@ -23,5 +23,6 @@ describe('support api', () => { test('ticket_forms', () => check(support.ticket_forms)); test('ticket_metrics', () => check(support.ticket_metrics)); test('tickets', () => check(support.tickets)); + test('user_fields', () => check(support.user_fields)); test('users', () => check(support.users)); }); diff --git a/tests/src/v2/support/user_fields.test.js b/tests/src/v2/support/user_fields.test.js new file mode 100644 index 0000000..7761d18 --- /dev/null +++ b/tests/src/v2/support/user_fields.test.js @@ -0,0 +1,208 @@ +const endpoint = require('../../../../src/v2/support/user_fields'); + +const instance = 'instance'; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; + +describe('ticket fields', () => { + let user_fields; + + beforeEach(() => (user_fields = endpoint({ instance, headers }))); + afterEach(() => (user_fields = null)); + + describe('list user fields', () => { + it('should process w/ valid input', () => { + expect(user_fields.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/user_fields.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.list('invalid')).toThrowError(); + }); + }); + + describe('show user field', () => { + it('should process w/ valid input', () => { + expect(user_fields.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/user_fields/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.show()).toThrowError(); + expect(() => user_fields.show('invalid')).toThrowError(); + expect(() => user_fields.show({})).toThrowError(); + expect(() => user_fields.show({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('create user fields', () => { + it('should process w/ valid input', () => { + expect(user_fields.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/user_fields.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.create()).toThrowError(); + expect(() => user_fields.create('invalid')).toThrowError(); + expect(() => user_fields.create({})).toThrowError(); + expect(() => user_fields.create({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('update user fields', () => { + it('should process w/ valid input', () => { + expect(user_fields.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/user_fields/123.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.update()).toThrowError(); + expect(() => user_fields.update('invalid')).toThrowError(); + expect(() => user_fields.update({})).toThrowError(); + expect(() => user_fields.update({ id: 'invalid' })).toThrowError(); + expect(() => + user_fields.update({ id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('delete user field', () => { + it('should process w/ valid input', () => { + expect(user_fields.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/user_fields/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.delete()).toThrowError(); + expect(() => user_fields.delete('invalid')).toThrowError(); + expect(() => user_fields.delete({})).toThrowError(); + expect(() => user_fields.delete({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('reorder user field', () => { + it('should process w/ valid input', () => { + expect(user_fields.reorder({ data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/user_fields/reorder.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.reorder()).toThrowError(); + expect(() => user_fields.reorder('invalid')).toThrowError(); + expect(() => user_fields.reorder({})).toThrowError(); + expect(() => user_fields.reorder({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('list user field options', () => { + it('should process w/ valid input', () => { + expect(user_fields.listOptions({ field_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/user_fields/123/options.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.listOptions()).toThrowError(); + expect(() => user_fields.listOptions('invalid')).toThrowError(); + expect(() => user_fields.listOptions({})).toThrowError(); + expect(() => + user_fields.listOptions({ field_id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('show a user field option', () => { + it('should process w/ valid input', () => { + expect(user_fields.showOption({ field_id: 123, id: 456 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/ticket_fields/123/options/456.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.showOption()).toThrowError(); + expect(() => user_fields.showOption('invalid')).toThrowError(); + expect(() => user_fields.showOption({})).toThrowError(); + expect(() => + user_fields.showOption({ field_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_fields.showOption({ field_id: 123, id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('create or update a user field option', () => { + it('should process w/ valid input', () => { + expect( + user_fields.createOrUpdateOption({ field_id: 123, data: {} }) + ).toEqual({ + method: 'POST', + url: `${url}/api/v2/user_fields/123/options.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.createOrUpdateOption()).toThrowError(); + expect(() => user_fields.createOrUpdateOption('invalid')).toThrowError(); + expect(() => user_fields.createOrUpdateOption({})).toThrowError(); + expect(() => + user_fields.createOrUpdateOption({ field_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_fields.createOrUpdateOption({ field_id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('delete user field option', () => { + it('should process w/ valid input', () => { + expect(user_fields.deleteOption({ field_id: 123, id: 456 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/user_fields/123/options/456.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_fields.deleteOption()).toThrowError(); + expect(() => user_fields.deleteOption('invalid')).toThrowError(); + expect(() => user_fields.deleteOption({})).toThrowError(); + expect(() => + user_fields.deleteOption({ field_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_fields.deleteOption({ field_id: 123, id: 'invalid' }) + ).toThrowError(); + }); + }); +});