Skip to content

Commit

Permalink
converted ticket_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 24, 2019
1 parent d8c2cef commit 4225079
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 52 deletions.
62 changes: 62 additions & 0 deletions src/api/support/ticket_metrics.js
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
};
}
};
};
38 changes: 0 additions & 38 deletions src/api/support/ticket_metrics/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/api/support/ticket_metrics/validate.js

This file was deleted.

72 changes: 72 additions & 0 deletions tests/api/support/ticket_metrics.test.js
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();
});
});
});

0 comments on commit 4225079

Please sign in to comment.