Skip to content

Commit

Permalink
built support:satisfaction_ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Jan 22, 2020
1 parent bff07f0 commit b2b2024
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/api/support/satisfaction_ratings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _params = Joi.string().min(1);
const _id = Joi.number().positive();
const _ticket_id = Joi.number().positive();
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 Satisfaction Ratings
*
* GET /api/v2/satisfaction_ratings.json
* https://developer.zendesk.com/rest_api/docs/support/satisfaction_ratings#list-satisfaction-ratings
*/
list: (options = {}) => {
const { error } = Joi.object({
params: _params
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { params } = options;
const paramStr = params ? `?${params}` : '';
return {
method: 'GET',
url: `${url}/api/v2/satisfaction_ratings.json${paramStr}`,
headers
};
},

/**
* Show Satisfaction Rating
*
* GET /api/v2/satisfaction_ratings/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/satisfaction_ratings#show-satisfaction-rating
*/
show: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

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

/**
* Create a Satisfaction Rating
*
* POST /api/v2/tickets/{ticket_id}/satisfaction_rating.json
* https://developer.zendesk.com/rest_api/docs/support/satisfaction_ratings#create-a-satisfaction-rating
*/
create: (options = {}) => {
const { error } = Joi.object({
ticket_id: _ticket_id.required(),
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

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

expect(endPoint.list({ params: 'score=good' })).toEqual({
method: 'GET',
url: `${url}/api/v2/satisfaction_ratings.json?score=good`,
headers
});
});

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

describe('show satisfaction rating', () => {
it('should process w/ valid input', () => {
expect(endPoint.show({ id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/satisfaction_ratings/123.json`,
headers
});
});

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

describe('create satisfaction rating', () => {
it('should process w/ valid input', () => {
expect(endPoint.create({ ticket_id: 123, data: {} })).toEqual({
method: 'POST',
url: `${url}/api/v2/tickets/123/satisfaction_rating.json`,
headers,
data: {}
});
});

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

0 comments on commit b2b2024

Please sign in to comment.