Skip to content

Commit

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

// Validation
const _id = Joi.number().min(1);
const _user_id = Joi.number().min(1);
const _organization_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 Organization Subscriptions
*
* GET /api/v2/organizations/{organization_id}/subscriptions.json
* GET /api/v2/organization_subscriptions.json
* GET /api/v2/users/{user_id}/organization_subscriptions.json
* https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#list-organization-subscriptions
*/
list: (options = {}) => {
const { error } = Joi.object({
user_id: _user_id,
organization_id: _organization_id
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { user_id, organization_id } = options;
if (user_id && organization_id)
throw new Error(
'either "user_id" or "organization_id" may be set, not both'
);

return {
method: 'GET',
url: `${url}/api/v2/${
user_id
? `users/${user_id}/organization_subscriptions.json`
: organization_id
? `organizations/${organization_id}/subscriptions.json`
: `organization_subscriptions.json`
}`,
headers
};
},

/**
* Show Organization Subscription
*
* GET /api/v2/organization_subscriptions/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#show-organization-subscription
*/
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/organization_subscriptions/${id}.json`,
headers
};
},

/**
* Create Organization Subscription
*
* POST /api/v2/organization_subscriptions.json
* https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#create-organization-subscription
*/
create: (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/organization_subscriptions.json`,
headers,
data
};
},

/**
* Delete Organization Subscription
*
* DELETE /api/v2/organization_subscriptions/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#delete-organization-subscription
*/
delete: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id } = options;
return {
method: 'DELETE',
url: `${url}/api/v2/organization_subscriptions/${id}.json`,
headers
};
}
};
};
67 changes: 0 additions & 67 deletions src/api/support/organization_subscriptions/index.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/api/support/organization_subscriptions/validate.js

This file was deleted.

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

expect(endPoint.list({ user_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/users/123/organization_subscriptions.json`,
headers
});

expect(endPoint.list({ organization_id: 456 })).toEqual({
method: 'GET',
url: `${url}/api/v2/organizations/456/subscriptions.json`,
headers
});
});

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

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

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

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

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

describe('delete subscription', () => {
it('should process w/ valid input', () => {
expect(endPoint.delete({ id: 123 })).toEqual({
method: 'DELETE',
url: `${url}/api/v2/organization_subscriptions/123.json`,
headers
});
});

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

0 comments on commit 625c318

Please sign in to comment.