-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from androozka/support-groups
built support.groups
- Loading branch information
Showing
4 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |