Skip to content

Commit

Permalink
updated support:tags
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Aug 30, 2019
1 parent 6eac823 commit c07df7a
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 150 deletions.
135 changes: 79 additions & 56 deletions src/api/v2/routes/support/tags.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,79 @@
module.exports = ({ instance, headers }) => ({
list: () => ({
method: 'GET',
url: `https://${instance}.zendesk.com/api/v2/tags.json`,
headers
}),

show: ({ type, id }) => ({
method: 'GET',
url: {
tickets: `https://${instance}.zendesk.com/api/v2/tickets/${id}/tags.json`,
organizations: `https://${instance}.zendesk.com/api/v2/organizations/${id}/tags.json`,
users: `https://${instance}.zendesk.com/api/v2/users/${id}/tags.json`
}[type],
headers
}),

set: ({ type, id, data }) => ({
method: 'POST',
url: {
tickets: `https://${instance}.zendesk.com/api/v2/tickets/${id}/tags.json`,
organizations: `https://${instance}.zendesk.com/api/v2/organizations/${id}/tags.json`,
users: `https://${instance}.zendesk.com/api/v2/users/${id}/tags.json`
}[type],
headers,
data
}),

add: ({ type, id, data }) => ({
method: 'PUT',
url: {
tickets: `https://${instance}.zendesk.com/api/v2/tickets/${id}/tags.json`,
organizations: `https://${instance}.zendesk.com/api/v2/organizations/${id}/tags.json`,
users: `https://${instance}.zendesk.com/api/v2/users/${id}/tags.json`
}[type],
headers,
data
}),

remove: ({ type, id, data }) => ({
method: 'DELETE',
url: {
tickets: `https://${instance}.zendesk.com/api/v2/tickets/${id}/tags.json`,
organizations: `https://${instance}.zendesk.com/api/v2/organizations/${id}/tags.json`,
users: `https://${instance}.zendesk.com/api/v2/users/${id}/tags.json`
}[type],
headers,
data
}),

autocomplete: ({ name }) => ({
method: 'GET',
url: `https://${instance}.zendesk.com/api/v2/autocomplete/tags.json?name=${name}`,
headers
})
});
const validate = require('../../validators/support/tags');

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/tags.json`,
headers
};
},

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

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

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

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

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

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

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

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

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

const { name } = options;
return {
method: 'GET',
url: `${url}/api/v2/autocomplete/tags.json?name=${name}`,
headers
};
}
};
};
27 changes: 27 additions & 0 deletions src/api/v2/validators/support/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Joi = require('@hapi/joi');

const type = Joi.string().valid('tickets', 'organizations', 'users');
const id = Joi.number().min(1);
const name = Joi.string().min(1);
const data = Joi.object();

module.exports = {
list: null, // no options
show: options =>
Joi.validate(options, { type: type.required(), id: id.required() }),
set: options =>
Joi.validate(options, {
type: type.required(),
id: id.required(),
data: data.required()
}),
add: options =>
Joi.validate(options, {
type: type.required(),
id: id.required(),
data: data.required()
}),
remove: options =>
Joi.validate(options, { type: type.required(), id: id.required() }),
autocomplete: options => Joi.validate(options, { name: name.required() })
};
Loading

0 comments on commit c07df7a

Please sign in to comment.