Skip to content

Commit

Permalink
converted custom_agent_roles
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 23, 2019
1 parent 74cb3e7 commit c62106e
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 96 deletions.
113 changes: 113 additions & 0 deletions src/api/support/custom_agent_roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _id = Joi.number().min(1);
const _data = Joi.object();

module.exports = (options = {}) => {
const { error } = validate(options);
if (error) throw new Error(error.details[0].message);

const { url, headers } = prepare(options);

return {
/**
* List Custom Roles
*
* GET /api/v2/custom_roles.json
* https://developer.zendesk.com/rest_api/docs/support/custom_roles#list-custom-roles
*/
list: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/custom_roles.json`,
headers
};
},

/**
* Show Custom Role
*
* GET /api/v2/custom_roles/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/custom_roles#show-custom-role
*/
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/custom_roles/${id}.json`,
headers
};
},

/**
* Create Custom Role
*
* POST /api/v2/custom_roles.json
* https://developer.zendesk.com/rest_api/docs/support/custom_roles#create-custom-role
*/
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/custom_roles.json`,
headers,
data
};
},

/**
* Update Custom Role
*
* PUT /api/v2/custom_roles/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/custom_roles#update-custom-role
*/
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/custom_roles/${id}.json`,
headers,
data
};
},

/**
* Delete Custom Role
*
* DELETE /api/v2/custom_roles/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/custom_roles#delete-custom-role
*/
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/custom_roles/${id}.json`,
headers
};
}
};
};
67 changes: 0 additions & 67 deletions src/api/support/custom_agent_roles/index.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/api/support/custom_agent_roles/validate.js

This file was deleted.

121 changes: 121 additions & 0 deletions tests/api/support/custom_agent_roles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const endpoint = require('../../../src/api/support/custom_agent_roles');
const { prepare } = require('../../../src/utils/options');

describe('support: custom agent roles', () => {
let custom_agent_roles, options, url, headers;

beforeEach(() => {
options = {
instance: 'instance',
email: 'user@email.com',
token: 'token'
};
custom_agent_roles = endpoint(options);
({ url, headers } = prepare(options));
});

afterEach(() => {
options = null;
custom_agent_roles = null;
url = null;
headers = null;
});

describe('init', () => {
it('should setup endpoint object', () => {
const ep = endpoint(options);
expect(ep).toBeTruthy();
});

it('should fail with invalid input', () => {
expect(() => endpoint()).toThrowError();
expect(() => endpoint({})).toThrowError();
});
});

describe('list custom roles', () => {
it('should process without input', () => {
expect(custom_agent_roles.list()).toEqual({
method: 'GET',
url: `${url}/api/v2/custom_roles.json`,
headers
});
});
});

describe('show custom role', () => {
it('should process w/ valid input', () => {
expect(custom_agent_roles.show({ id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/custom_roles/123.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => custom_agent_roles.show()).toThrowError();
expect(() => custom_agent_roles.show({})).toThrowError();
expect(() => custom_agent_roles.show('invalid')).toThrowError();
expect(() => custom_agent_roles.show({ id: 'invalid' })).toThrowError();
});
});

describe('create custom role', () => {
it('should process w/ valid input', () => {
expect(custom_agent_roles.create({ data: {} })).toEqual({
method: 'POST',
url: `${url}/api/v2/custom_roles.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => custom_agent_roles.create()).toThrowError();
expect(() => custom_agent_roles.create({})).toThrowError();
expect(() => custom_agent_roles.create('invalid')).toThrowError();
expect(() =>
custom_agent_roles.create({ data: 'invalid' })
).toThrowError();
});
});

describe('update custom role', () => {
it('should process w/ valid input', () => {
expect(custom_agent_roles.update({ id: 123, data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/custom_roles/123.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => custom_agent_roles.update()).toThrowError();
expect(() => custom_agent_roles.update({})).toThrowError();
expect(() => custom_agent_roles.update('invalid')).toThrowError();
expect(() => custom_agent_roles.update({ id: 0 })).toThrowError();
expect(() => custom_agent_roles.update({ id: 'invalid' })).toThrowError();
expect(() =>
custom_agent_roles.update({ id: 123, data: 'invalid' })
).toThrowError();
});
});

describe('delete custom role', () => {
it('should process w/ valid input', () => {
expect(custom_agent_roles.delete({ id: 123 })).toEqual({
method: 'DELETE',
url: `${url}/api/v2/custom_roles/123.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => custom_agent_roles.delete()).toThrowError();
expect(() => custom_agent_roles.delete({})).toThrowError();
expect(() => custom_agent_roles.delete('invalid')).toThrowError();
expect(() => custom_agent_roles.delete({ id: 'invalid' })).toThrowError();
});
});
});

0 comments on commit c62106e

Please sign in to comment.