Skip to content

Commit

Permalink
built support.organization_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Sep 2, 2019
1 parent ca6da50 commit 7667122
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/v2/support/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = ({ instance, headers }) => ({
groups: require('./groups')({ instance, headers }),
organizations: require('./organizations')({ instance, headers }),
organization_fields: require('./organization_fields')({ instance, headers }),
search: require('./search')({ instance, headers }),
tags: require('./tags')({ instance, headers }),
ticket_comments: require('./ticket_comments')({ instance, headers }),
Expand Down
80 changes: 80 additions & 0 deletions src/v2/support/organization_fields/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const validate = require('./validate');

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

return {
list: (options = null) => {
if (options) throw new Error('no options are allowed');

return {
method: 'GET',
url: `${url}/api/v2/organization_fields.json`,
headers
};
},

show: (options = {}) => {
const { error } = validate.show(options);
if (error) throw new Error(error.details[0].message);

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

create: (options = {}) => {
const { error } = validate.create(options);
if (error) throw new Error(error.details[0].message);

const { data } = options;
return {
method: 'POST',
url: `${url}/api/v2/organization_fields.json`,
headers,
data
};
},

update: (options = {}) => {
const { error } = validate.update(options);
if (error) throw new Error(error.details[0].message);

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

delete: (options = {}) => {
const { error } = validate.delete(options);
if (error) throw new Error(error.details[0].message);

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

reorder: (options = {}) => {
const { error } = validate.reorder(options);
if (error) throw new Error(error.details[0].message);

const { data } = options;
return {
method: 'PUT',
url: `${url}/api/v2/organization_fields/reorder.json`,
headers,
data
};
}
};
};
14 changes: 14 additions & 0 deletions src/v2/support/organization_fields/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Joi = require('@hapi/joi');

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

module.exports = {
list: null, // no options
show: options => Joi.validate(options, { id: id.required() }),
create: options => Joi.validate(options, { data: data.required() }),
update: options =>
Joi.validate(options, { id: id.required(), data: data.required() }),
delete: options => Joi.validate(options, { id: id.required() }),
reorder: options => Joi.validate(options, { data: data.required() })
};
23 changes: 11 additions & 12 deletions tests/src/v2/support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ describe('support api', () => {
beforeEach(() => (support = endpoint({ instance, headers })));
afterEach(() => (support = null));

describe('tickets', () => {
test('groups', () => check(support.groups));
test('organizations', () => check(support.organizations));
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));
test('tickets', () => check(support.tickets));
test('users', () => check(support.users));
});
test('groups', () => check(support.groups));
test('organizations', () => check(support.organizations));
test('organization_fields', () => check(support.organization_fields));
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));
test('tickets', () => check(support.tickets));
test('users', () => check(support.users));
});
127 changes: 127 additions & 0 deletions tests/src/v2/support/organization_fields.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const endpoint = require('../../../../src/v2/support/organization_fields');

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

describe('organizations', () => {
let organization_fields;

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

describe('list organization fields', () => {
it('should process w/ valid input', () => {
expect(organization_fields.list()).toEqual({
method: 'GET',
url: `${url}/api/v2/organization_fields.json`,
headers
});
});

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

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

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

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

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

describe('update organization fields', () => {
it('should process w/ valid input', () => {
expect(organization_fields.update({ id: 123, data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/organization_fields/123.json`,
headers,
data: {}
});
});

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

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

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

describe('reorder organization field', () => {
it('should process w/ valid input', () => {
expect(organization_fields.reorder({ data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/organization_fields/reorder.json`,
headers,
data: {}
});
});

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

1 comment on commit 7667122

@androozka
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolves #45

Please sign in to comment.