-
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
4 changed files
with
134 additions
and
52 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,62 @@ | ||
const Joi = require('@hapi/joi'); | ||
const { validate, prepare } = require('../../utils/options'); | ||
|
||
// Validation | ||
const _ticket_id = Joi.number().min(1); | ||
const _ticket_metric_id = Joi.number().min(1); | ||
|
||
// Initialize Endpoint | ||
module.exports = (options = {}) => { | ||
const { error } = validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { url, headers } = prepare(options); | ||
|
||
return { | ||
/** | ||
* List Ticket Metrics | ||
* | ||
* GET /api/v2/ticket_metrics.json | ||
* https://developer.zendesk.com/rest_api/docs/support/ticket_metrics#list-ticket-metrics | ||
*/ | ||
list: () => { | ||
// Ignore any options | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_metrics.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* Show Ticket Metrics | ||
* | ||
* GET /api/v2/ticket_metrics/{ticket_metric_id}.json | ||
* GET /api/v2/tickets/{ticket_id}/metrics.json | ||
* https://developer.zendesk.com/rest_api/docs/support/ticket_metrics#show-ticket-metrics | ||
*/ | ||
show: (options = {}) => { | ||
const { error } = Joi.object({ | ||
ticket_id: _ticket_id, | ||
ticket_metric_id: _ticket_metric_id | ||
}).validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { ticket_id = 0, ticket_metric_id = 0 } = options; | ||
if ((!ticket_id && !ticket_metric_id) || (ticket_id && ticket_metric_id)) | ||
throw new Error( | ||
'either "ticket_id" or "ticket_metric_id" must be set, but not both' | ||
); | ||
|
||
const part = ticket_id | ||
? `tickets/${ticket_id}/metrics.json` | ||
: `ticket_metrics/${ticket_metric_id}.json`; | ||
|
||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/${part}`, | ||
headers | ||
}; | ||
} | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,72 @@ | ||
const endpoint = require('../../../src/api/support/ticket_metrics'); | ||
const { prepare } = require('../../../src/utils/options'); | ||
|
||
describe('object types', () => { | ||
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', () => { | ||
const ep = endpoint(options); | ||
expect(ep).toBeTruthy(); | ||
}); | ||
|
||
it('should fail with invalid input', () => { | ||
expect(() => endpoint()).toThrowError(); | ||
expect(() => endpoint({})).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('list ticket metrics', () => { | ||
it('should process without input', () => { | ||
expect(endPoint.list()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_metrics.json`, | ||
headers | ||
}); | ||
}); | ||
}); | ||
|
||
describe('show ticket metrics', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.show({ ticket_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/tickets/123/metrics.json`, | ||
headers | ||
}); | ||
|
||
expect(endPoint.show({ ticket_metric_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_metrics/123.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.show()).toThrowError(); | ||
expect(() => endPoint.show({})).toThrowError(); | ||
expect(() => endPoint.show('invalid')).toThrowError(); | ||
expect(() => endPoint.show({ ticket_id: 0 })).toThrowError(); | ||
expect(() => endPoint.show({ ticket_metric_id: 0 })).toThrowError(); | ||
expect(() => | ||
endPoint.show({ ticket_id: 0, ticket_metric_id: 0 }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
}); |