Skip to content

Commit

Permalink
updated support:ticketMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Aug 30, 2019
1 parent e3adef7 commit 6eac823
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
52 changes: 37 additions & 15 deletions src/api/v2/routes/support/ticketMetrics.js
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
};
}
};
};
9 changes: 9 additions & 0 deletions src/api/v2/validators/support/ticketMetrics.js
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 })
};
66 changes: 40 additions & 26 deletions tests/src/api/v2/routes/support/ticketMetrics.test.js
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();
});
});
});

0 comments on commit 6eac823

Please sign in to comment.