Skip to content

Commit

Permalink
Merge pull request #66 from androozka/support-groups
Browse files Browse the repository at this point in the history
built support.groups
  • Loading branch information
androozka committed Sep 1, 2019
2 parents 80209b5 + 8744022 commit 61a7bcb
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 2 deletions.
79 changes: 79 additions & 0 deletions src/api/v2/routes/support/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const validate = require('../../validators/support/groups');

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 { user_id } = options;
return {
method: 'GET',
url: `${url}/api/v2${user_id ? `/users/${user_id}` : ''}/groups.json`,
headers
};
},

show_assignable: (options = null) => {
if (options) throw new Error('no options are allowed');

return {
method: 'GET',
url: `${url}/api/v2/groups/assignable.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/groups/${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/groups.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/groups/${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/groups/${id}.json`,
headers
};
}
};
};
5 changes: 3 additions & 2 deletions src/api/v2/support.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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 }),
tickets: require('./routes/support/tickets')({ instance, headers }),
ticket_metrics: require('./routes/support/ticketMetrics')({
instance,
headers
}),
tags: require('./routes/support/tags')({ instance, headers })
})
});
15 changes: 15 additions & 0 deletions src/api/v2/validators/support/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Joi = require('@hapi/joi');

const id = Joi.number().min(1);
const user_id = Joi.number().min(1);
const data = Joi.object();

module.exports = {
list: options => Joi.validate(options, { user_id }),
show_assignable: 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() })
};
128 changes: 128 additions & 0 deletions tests/src/api/v2/routes/support/groups.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const endpoint = require('../../../../../../src/api/v2/routes/support/groups');

const instance = 'instance';
const url = `https://${instance}.zendesk.com`;
const headers = {
'Content-Type': 'application/json',
Authorization: 'Basic <64bit_encoded_credentials>'
};

describe('groups', () => {
let groups;

beforeEach(() => (groups = endpoint({ instance, headers })));
afterEach(() => (groups = null));

describe('list groups', () => {
it('should process w/ valid input', () => {
expect(groups.list()).toEqual({
method: 'GET',
url: `${url}/api/v2/groups.json`,
headers
});

expect(groups.list({})).toEqual({
method: 'GET',
url: `${url}/api/v2/groups.json`,
headers
});

expect(groups.list({ user_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/users/123/groups.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => groups.list('invalid')).toThrowError();
expect(() => groups.list({ invalid: 123 })).toThrowError();
expect(() => groups.list({ user_id: 'invalid' })).toThrowError();
});
});

describe('show assignable groups', () => {
it('should process w/ valid input', () => {
expect(groups.show_assignable()).toEqual({
method: 'GET',
url: `${url}/api/v2/groups/assignable.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => groups.show_assignable('invalid')).toThrowError();
});
});

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

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

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

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

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

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

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

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

0 comments on commit 61a7bcb

Please sign in to comment.