Skip to content

Commit

Permalink
built support.ticketComments
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Sep 2, 2019
1 parent 87349c2 commit 8a10c51
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/api/v2/routes/support/ticketComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const validate = require('../../validators/support/ticketComments');

module.exports = ({ instance, headers }) => {
const url = `https://${instance}.zendesk.com`;

return {
list: (options = {}) => {
const { error } = validate.list(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
};
},

emailCCs: (options = {}) => {
const { error } = validate.emailCCs(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: (options = {}) => {
const { error } = validate.redact(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
};
},

makePrivate: (options = {}) => {
const { error } = validate.makePrivate(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: {}
};
}
};
};
6 changes: 5 additions & 1 deletion src/api/v2/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = ({ instance, headers }) => ({
groups: require('./routes/support/groups')({ instance, headers }),
search: require('./routes/support/search')({ instance, headers }),
tags: require('./routes/support/tags')({ instance, headers }),
ticket_metrics: require('./routes/support/ticketMetrics')({
ticket_comments: require('./routes/support/ticketComments')({
instance,
headers
}),
Expand All @@ -11,5 +11,9 @@ module.exports = ({ instance, headers }) => ({
headers
}),
ticket_forms: require('./routes/support/ticketForms')({ instance, headers }),
ticket_metrics: require('./routes/support/ticketMetrics')({
instance,
headers
}),
tickets: require('./routes/support/tickets')({ instance, headers })
});
22 changes: 22 additions & 0 deletions src/api/v2/validators/support/ticketComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Joi = require('@hapi/joi');

const id = Joi.number().min(1);
const ticket_id = Joi.number().min(1);
const data = Joi.object();

module.exports = {
list: options => Joi.validate(options, { ticket_id: ticket_id.required() }),
emailCCs: options =>
Joi.validate(options, { ticket_id: ticket_id.required() }),
redact: options =>
Joi.validate(options, {
ticket_id: ticket_id.required(),
id: id.required(),
data: data.required()
}),
makePrivate: options =>
Joi.validate(options, {
ticket_id: ticket_id.required(),
id: id.required()
})
};
100 changes: 100 additions & 0 deletions tests/src/api/v2/routes/support/ticketComments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const endpoint = require('../../../../../../src/api/v2/routes/support/ticketComments');

const instance = 'instance';
const url = `https://${instance}.zendesk.com`;
const headers = {
'Content-Type': 'application/json',
Authorization: 'Basic <64bit_encoded_credentials>'
};

describe('ticket comments', () => {
let ticketComments;

beforeEach(() => (ticketComments = endpoint({ instance, headers })));
afterEach(() => (ticketComments = null));

describe('list comments', () => {
it('should process w/ valid input', () => {
expect(ticketComments.list({ ticket_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/tickets/123/comments.json`,
headers
});
});

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

describe('list email ccs for a comment', () => {
it('should process w/ valid input', () => {
expect(ticketComments.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(() => ticketComments.emailCCs()).toThrowError();
expect(() => ticketComments.emailCCs('invalid')).toThrowError();
expect(() =>
ticketComments.emailCCs({ ticket_id: 'invalid' })
).toThrowError();
});
});

describe('redact string in comment', () => {
it('should process w/ valid input', () => {
expect(
ticketComments.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(() => ticketComments.redact()).toThrowError();
expect(() => ticketComments.redact('invalid')).toThrowError();
expect(() =>
ticketComments.redact({ ticket_id: 'invalid' })
).toThrowError();
expect(() =>
ticketComments.redact({ ticket_id: 123, id: 'invalid' })
).toThrowError();
expect(() =>
ticketComments.redact({ ticket_id: 123, id: 456, data: 'invalid' })
).toThrowError();
});
});

describe('make comment private', () => {
it('should process w/ valid input', () => {
expect(ticketComments.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(() => ticketComments.makePrivate()).toThrowError();
expect(() => ticketComments.makePrivate('invalid')).toThrowError();
expect(() =>
ticketComments.makePrivate({ ticket_id: 'invalid' })
).toThrowError();
expect(() =>
ticketComments.makePrivate({ ticket_id: 123, id: 'invalid' })
).toThrowError();
});
});
});
1 change: 1 addition & 0 deletions tests/src/api/v2/support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('support api', () => {
describe('tickets', () => {
test('search', () => check(support.search, 'function'));
test('tags', () => check(support.tags));
test('ticket_comments', () => check(support.ticket_comments));
test('ticket_fields', () => check(support.ticket_fields));
test('ticket_forms', () => check(support.ticket_forms));
test('ticket_metrics', () => check(support.ticket_metrics));
Expand Down

0 comments on commit 8a10c51

Please sign in to comment.