Skip to content

Commit

Permalink
built support.user_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Sep 2, 2019
1 parent 40473e5 commit 9e476c3
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/v2/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = ({ instance, headers }) => ({
ticket_forms: require('./ticket_forms')({ instance, headers }),
ticket_metrics: require('./ticket_metrics')({ instance, headers }),
tickets: require('./tickets')({ instance, headers }),
user_fields: require('./user_fields')({ instance, headers }),
users: require('./users')({ instance, headers })
});
129 changes: 129 additions & 0 deletions src/v2/support/user_fields/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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/user_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/user_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/user_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/user_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/user_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/user_fields/reorder.json`,
headers,
data
};
},

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

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

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

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

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

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

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

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

const id = Joi.number().min(1);
const field_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() }),
listOptions: options =>
Joi.validate(options, { field_id: field_id.required() }),
showOption: options =>
Joi.validate(options, { field_id: field_id.required(), id: id.required() }),
createOrUpdateOption: options =>
Joi.validate(options, {
field_id: field_id.required(),
data: data.required()
}),
deleteOption: options =>
Joi.validate(options, { field_id: field_id.required(), id: id.required() })
};
1 change: 1 addition & 0 deletions tests/src/v2/support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ describe('support api', () => {
test('ticket_forms', () => check(support.ticket_forms));
test('ticket_metrics', () => check(support.ticket_metrics));
test('tickets', () => check(support.tickets));
test('user_fields', () => check(support.user_fields));
test('users', () => check(support.users));
});
208 changes: 208 additions & 0 deletions tests/src/v2/support/user_fields.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
const endpoint = require('../../../../src/v2/support/user_fields');

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

describe('ticket fields', () => {
let user_fields;

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

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

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

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

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

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

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

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

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

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

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

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

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

describe('list user field options', () => {
it('should process w/ valid input', () => {
expect(user_fields.listOptions({ field_id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/user_fields/123/options.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => user_fields.listOptions()).toThrowError();
expect(() => user_fields.listOptions('invalid')).toThrowError();
expect(() => user_fields.listOptions({})).toThrowError();
expect(() =>
user_fields.listOptions({ field_id: 'invalid' })
).toThrowError();
});
});

describe('show a user field option', () => {
it('should process w/ valid input', () => {
expect(user_fields.showOption({ field_id: 123, id: 456 })).toEqual({
method: 'GET',
url: `${url}/api/v2/ticket_fields/123/options/456.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => user_fields.showOption()).toThrowError();
expect(() => user_fields.showOption('invalid')).toThrowError();
expect(() => user_fields.showOption({})).toThrowError();
expect(() =>
user_fields.showOption({ field_id: 'invalid' })
).toThrowError();
expect(() =>
user_fields.showOption({ field_id: 123, id: 'invalid' })
).toThrowError();
});
});

describe('create or update a user field option', () => {
it('should process w/ valid input', () => {
expect(
user_fields.createOrUpdateOption({ field_id: 123, data: {} })
).toEqual({
method: 'POST',
url: `${url}/api/v2/user_fields/123/options.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => user_fields.createOrUpdateOption()).toThrowError();
expect(() => user_fields.createOrUpdateOption('invalid')).toThrowError();
expect(() => user_fields.createOrUpdateOption({})).toThrowError();
expect(() =>
user_fields.createOrUpdateOption({ field_id: 'invalid' })
).toThrowError();
expect(() =>
user_fields.createOrUpdateOption({ field_id: 123, data: 'invalid' })
).toThrowError();
});
});

describe('delete user field option', () => {
it('should process w/ valid input', () => {
expect(user_fields.deleteOption({ field_id: 123, id: 456 })).toEqual({
method: 'DELETE',
url: `${url}/api/v2/user_fields/123/options/456.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => user_fields.deleteOption()).toThrowError();
expect(() => user_fields.deleteOption('invalid')).toThrowError();
expect(() => user_fields.deleteOption({})).toThrowError();
expect(() =>
user_fields.deleteOption({ field_id: 'invalid' })
).toThrowError();
expect(() =>
user_fields.deleteOption({ field_id: 123, id: 'invalid' })
).toThrowError();
});
});
});

1 comment on commit 9e476c3

@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 #13

Please sign in to comment.