-
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
3 changed files
with
86 additions
and
41 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 |
---|---|---|
@@ -1,16 +1,38 @@ | ||
module.exports = ({ instance, headers }) => ({ | ||
list: () => ({ | ||
method: 'GET', | ||
url: `https://${instance}.zendesk.com/api/v2/ticket_metrics.json`, | ||
headers | ||
}), | ||
const validate = require('../../validators/support/ticketMetrics'); | ||
|
||
show: ({ type = 'default', ticket_metric_id, ticket_id }) => ({ | ||
method: 'GET', | ||
url: { | ||
default: `https://${instance}.zendesk.com/api/v2/ticket_metrics/${ticket_metric_id}.json`, | ||
tickets: `https://${instance}.zendesk.com/api/v2/tickets/${ticket_id}/metrics.json` | ||
}[type], | ||
headers | ||
}) | ||
}); | ||
module.exports = ({ instance, headers }) => { | ||
const url = `https://${instance}.zendesk.com`; | ||
|
||
return { | ||
list: (options = null) => { | ||
if (options) throw new Error('options not allowed'); | ||
|
||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_metrics.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
show: (options = {}) => { | ||
const { error } = validate.show(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 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,9 @@ | ||
const Joi = require('@hapi/joi'); | ||
|
||
const ticket_id = Joi.number().min(1); | ||
const ticket_metric_id = Joi.number().min(1); | ||
|
||
module.exports = { | ||
list: null, // no options | ||
show: options => Joi.validate(options, { ticket_id, ticket_metric_id }) | ||
}; |
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,42 +1,56 @@ | ||
const endpoint = require('../../../../../../src/api/v2/routes/support/ticketMetrics'); | ||
|
||
const instance = 'instance'; | ||
const headers = {}; | ||
const url = `https://${instance}.zendesk.com`; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
Authorization: 'Basic <64bit_encoded_credentials>' | ||
}; | ||
|
||
describe('ticket metrics', () => { | ||
let ticketMetrics, ticket_metric_id, ticket_id; | ||
let ticketMetrics; | ||
|
||
beforeAll(() => { | ||
ticketMetrics = endpoint({ instance, headers }); | ||
ticket_metric_id = 123; | ||
ticket_id = 123; | ||
}); | ||
beforeAll(() => (ticketMetrics = endpoint({ instance, headers }))); | ||
afterAll(() => (ticketMetrics = null)); | ||
|
||
afterAll(() => { | ||
ticketMetrics = null; | ||
ticket_metric_id = 0; | ||
ticket_id = 0; | ||
}); | ||
describe('list ticket metrics', () => { | ||
it('should process w/ valid input', () => { | ||
expect(ticketMetrics.list()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_metrics.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
test('list ticket metrics', () => { | ||
expect(ticketMetrics.list()).toEqual({ | ||
method: 'GET', | ||
url: `https://${instance}.zendesk.com/api/v2/ticket_metrics.json`, | ||
headers | ||
it('should throw error w/ invalid input', () => { | ||
expect(() => ticketMetrics.list('invalid')).toThrowError(); | ||
}); | ||
}); | ||
|
||
test('show ticket metrics', () => { | ||
expect(ticketMetrics.show({ ticket_metric_id })).toEqual({ | ||
method: 'GET', | ||
url: `https://instance.zendesk.com/api/v2/ticket_metrics/${ticket_metric_id}.json`, | ||
headers | ||
describe('show ticket metrics', () => { | ||
it('should process w/ valid input', () => { | ||
expect(ticketMetrics.show({ ticket_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/tickets/123/metrics.json`, | ||
headers | ||
}); | ||
|
||
expect(ticketMetrics.show({ ticket_metric_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_metrics/123.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
expect(ticketMetrics.show({ type: 'tickets', ticket_id })).toEqual({ | ||
method: 'GET', | ||
url: `https://instance.zendesk.com/api/v2/tickets/${ticket_id}/metrics.json`, | ||
headers | ||
it('should throw error w/ invalid input', () => { | ||
expect(() => ticketMetrics.show()).toThrowError(); | ||
expect(() => ticketMetrics.show({})).toThrowError(); | ||
expect(() => ticketMetrics.show('invalid')).toThrowError(); | ||
expect(() => ticketMetrics.show({ ticket_id: 0 })).toThrowError(); | ||
expect(() => ticketMetrics.show({ ticket_metric_id: 0 })).toThrowError(); | ||
expect(() => | ||
ticketMetrics.show({ ticket_id: 0, ticket_metric_id: 0 }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
}); |