-
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.
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 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,67 @@ | ||
const Joi = require('@hapi/joi'); | ||
const { validate, prepare } = require('../../utils/options'); | ||
|
||
// Validation | ||
const _data = Joi.object(); | ||
const _ticket_id = Joi.number().positive(); | ||
const _user_id = Joi.number().positive(); | ||
|
||
// Initialize Endpoint | ||
module.exports = (options = {}) => { | ||
const { error } = validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { url, headers } = prepare(options); | ||
|
||
return { | ||
/** | ||
* Record a new skip for the current user | ||
* | ||
* POST /api/v2/skips.json | ||
* https://developer.zendesk.com/rest_api/docs/support/ticket_skips#record-a-new-skip-for-the-current-user | ||
*/ | ||
record: (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/skips.json`, | ||
headers, | ||
data | ||
}; | ||
}, | ||
|
||
/** | ||
* List skips for the current account | ||
* | ||
* GET /api/v2/skips.json | ||
* GET /api/v2/tickets/{ticket_id}/skips.json | ||
* GET /api/v2/users/{user_id}/skips.json | ||
* https://developer.zendesk.com/rest_api/docs/support/ticket_skips#list-skips-for-the-current-account | ||
*/ | ||
list: (options = {}) => { | ||
const { error } = Joi.object({ | ||
ticket_id: _ticket_id, | ||
user_id: _user_id | ||
}).validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { ticket_id = 0, user_id = 0 } = options; | ||
const section = ticket_id | ||
? `tickets/${ticket_id}/` | ||
: user_id | ||
? `users/${user_id}/` | ||
: ''; | ||
|
||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/${section}skips.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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const endpoint = require('../../../../src/api/support/ticket_skips'); | ||
const { prepare } = require('../../../../src/utils/options'); | ||
|
||
describe('ticket skips', () => { | ||
let endPoint, options, url, headers; | ||
|
||
beforeEach(() => { | ||
options = { | ||
instance: 'instance', | ||
email: 'user@email.com', | ||
token: 'token' | ||
}; | ||
endPoint = endpoint(options); | ||
({ url, headers } = prepare(options)); | ||
}); | ||
|
||
afterEach(() => { | ||
options = null; | ||
endPoint = null; | ||
url = null; | ||
headers = null; | ||
}); | ||
|
||
describe('init', () => { | ||
it('should setup endpoint object', () => { | ||
expect(endpoint(options)).toBeTruthy(); | ||
}); | ||
|
||
it('should fail with invalid input', () => { | ||
expect(() => endpoint()).toThrowError(); | ||
expect(() => endpoint({})).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('record a new skip for the current account', () => { | ||
it('should process w/ valid input', () => { | ||
const data = { skip: { ticket_id: 123, reason: 'I have no idea.' } }; | ||
|
||
expect(endPoint.record({ data })).toEqual({ | ||
method: 'POST', | ||
url: `${url}/api/v2/skips.json`, | ||
headers, | ||
data | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.record()).toThrowError(); | ||
expect(() => endPoint.record('invalid')).toThrowError(); | ||
expect(() => endPoint.record({ data: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('list skips for the current account', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.list()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/skips.json`, | ||
headers | ||
}); | ||
|
||
expect(endPoint.list({ ticket_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/tickets/123/skips.json`, | ||
headers | ||
}); | ||
|
||
expect(endPoint.list({ user_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/users/123/skips.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.list({ ticket_id: 'invalid' })).toThrowError(); | ||
expect(() => endPoint.list({ user_id: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
}); |