Skip to content

Commit

Permalink
converted ticket_comments
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 24, 2019
1 parent 24c0e33 commit 84a74c9
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 87 deletions.
102 changes: 102 additions & 0 deletions src/api/support/ticket_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _id = Joi.number().min(1);
const _ticket_id = Joi.number().min(1);
const _data = Joi.object();

// 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 Comments
*
* GET /api/v2/tickets/{ticket_id}/comments.json
* https://developer.zendesk.com/rest_api/docs/support/ticket_comments#list-comments
*/
list: (options = {}) => {
const { error } = Joi.object({
ticket_id: _ticket_id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { ticket_id } = options;
return {
method: 'GET',
url: `${url}/api/v2/tickets/${ticket_id}/comments.json`,
headers
};
},

/**
* List Email CCs for a Comment
*
* GET /api/v2/tickets/{ticket_id}/comments.json?include=users
* https://developer.zendesk.com/rest_api/docs/support/ticket_comments#list-email-ccs-for-a-comment
*/
emailCCs: (options = {}) => {
const { error } = Joi.object({
ticket_id: _ticket_id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { ticket_id } = options;
return {
method: 'GET',
url: `${url}/api/v2/tickets/${ticket_id}/comments.json?include=users`,
headers
};
},

/**
* Redact String in Comment
*
* PUT /api/v2/tickets/{ticket_id}/comments/{id}/redact.json
* https://developer.zendesk.com/rest_api/docs/support/ticket_comments#redact-string-in-comment
*/
redact: (options = {}) => {
const { error } = Joi.object({
ticket_id: _ticket_id.required(),
id: _id.required(),
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { ticket_id, id, data } = options;
return {
method: 'PUT',
url: `${url}/api/v2/tickets/${ticket_id}/comments/${id}/redact.json`,
headers,
data
};
},

/**
* Make Comment Private
*
* PUT /api/v2/tickets/{ticket_id}/comments/{id}/make_private.json
* https://developer.zendesk.com/rest_api/docs/support/ticket_comments#make-comment-private
*/
makePrivate: (options = {}) => {
const { error } = Joi.object({
ticket_id: _ticket_id.required(),
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { ticket_id, id } = options;
return {
method: 'PUT',
url: `${url}/api/v2/tickets/${ticket_id}/comments/${id}/make_private.json`,
headers,
data: {}
};
}
};
};
57 changes: 0 additions & 57 deletions src/api/support/ticket_comments/index.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/api/support/ticket_comments/validate.js

This file was deleted.

112 changes: 112 additions & 0 deletions tests/api/support/ticket_comments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const endpoint = require('../../../src/api/support/ticket_comments');
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 comments', () => {
it('should process w/ valid input', () => {
expect(endPoint.list({ ticket_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/tickets/123/comments.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.list()).toThrowError();
expect(() => endPoint.list('invalid')).toThrowError();
expect(() => endPoint.list({ ticket_id: 'invalid' })).toThrowError();
});
});

describe('list email ccs for a comment', () => {
it('should process w/ valid input', () => {
expect(endPoint.emailCCs({ ticket_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/tickets/123/comments.json?include=users`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.emailCCs()).toThrowError();
expect(() => endPoint.emailCCs('invalid')).toThrowError();
expect(() => endPoint.emailCCs({ ticket_id: 'invalid' })).toThrowError();
});
});

describe('redact string in comment', () => {
it('should process w/ valid input', () => {
expect(endPoint.redact({ ticket_id: 123, id: 456, data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/tickets/123/comments/456/redact.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.redact()).toThrowError();
expect(() => endPoint.redact('invalid')).toThrowError();
expect(() => endPoint.redact({ ticket_id: 'invalid' })).toThrowError();
expect(() =>
endPoint.redact({ ticket_id: 123, id: 'invalid' })
).toThrowError();
expect(() =>
endPoint.redact({ ticket_id: 123, id: 456, data: 'invalid' })
).toThrowError();
});
});

describe('make comment private', () => {
it('should process w/ valid input', () => {
expect(endPoint.makePrivate({ ticket_id: 123, id: 456 })).toEqual({
method: 'PUT',
url: `${url}/api/v2/tickets/123/comments/456/make_private.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.makePrivate()).toThrowError();
expect(() => endPoint.makePrivate('invalid')).toThrowError();
expect(() =>
endPoint.makePrivate({ ticket_id: 'invalid' })
).toThrowError();
expect(() =>
endPoint.makePrivate({ ticket_id: 123, id: 'invalid' })
).toThrowError();
});
});
});

0 comments on commit 84a74c9

Please sign in to comment.