Skip to content

Commit

Permalink
built support:ticket_skips
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Jan 22, 2020
1 parent 0283eb8 commit 82266f0
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/api/support/ticket_skips.js
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
};
}
};
};
80 changes: 80 additions & 0 deletions tests/src/api/support/ticket_skips.test.js
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();
});
});
});

0 comments on commit 82266f0

Please sign in to comment.