UNPKG

9.31 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _ids = Joi.string().min(3);
7const _user_id = Joi.number().min(1);
8const _external_id = Joi.number().min(1);
9const _external_ids = Joi.string().min(1);
10const _name = Joi.string().min(1);
11const _data = Joi.object();
12
13// Initialize Endpoint
14module.exports = (options = {}) => {
15 const { error } = validate(options);
16 if (error) throw new Error(error.details[0].message);
17
18 const { url, headers } = prepare(options);
19
20 return {
21 /**
22 * List Organizations
23 *
24 * GET /api/v2/organizations.json
25 * GET /api/v2/users/{user_id}/organizations.json
26 * https://developer.zendesk.com/rest_api/docs/support/organizations#list-organizations
27 */
28 list: (options = {}) => {
29 const { error } = Joi.object({
30 user_id: _user_id
31 }).validate(options);
32 if (error) throw new Error(error.details[0].message);
33
34 const { user_id = '' } = options;
35 const part = user_id ? `/users/${user_id}` : '';
36 return {
37 method: 'GET',
38 url: `${url}/api/v2${part}/organizations.json`,
39 headers
40 };
41 },
42
43 /**
44 * Autocomplete Organizations
45 *
46 * GET /api/v2/organizations/autocomplete.json?name={name}
47 * https://developer.zendesk.com/rest_api/docs/support/organizations#autocomplete-organizations
48 */
49 autocomplete: (options = {}) => {
50 const { error } = Joi.object({
51 name: _name.required()
52 }).validate(options);
53 if (error) throw new Error(error.details[0].message);
54
55 const { name } = options;
56 return {
57 method: 'GET',
58 url: `${url}/api/v2/organizations/autocomplete.json?name=${name}`,
59 headers
60 };
61 },
62
63 /**
64 * Show Organization's Related Information
65 *
66 * GET /api/v2/organizations/{id}/related.json
67 * https://developer.zendesk.com/rest_api/docs/support/organizations#show-organizations-related-information
68 */
69 related: (options = {}) => {
70 const { error } = Joi.object({
71 id: _id.required()
72 }).validate(options);
73 if (error) throw new Error(error.details[0].message);
74
75 const { id } = options;
76 return {
77 method: 'GET',
78 url: `${url}/api/v2/organizations/${id}/related.json`,
79 headers
80 };
81 },
82
83 /**
84 * Show Organization
85 *
86 * GET /api/v2/organizations/{id}.json
87 * https://developer.zendesk.com/rest_api/docs/support/organizations#show-organization
88 */
89 show: (options = {}) => {
90 const { error } = Joi.object({
91 id: _id.required()
92 }).validate(options);
93 if (error) throw new Error(error.details[0].message);
94
95 const { id } = options;
96 return {
97 method: 'GET',
98 url: `${url}/api/v2/organizations/${id}.json`,
99 headers
100 };
101 },
102
103 /**
104 * Show Many Organizations
105 *
106 * GET /api/v2/organizations/show_many.json?ids={ids}
107 * GET /api/v2/organizations/show_many.json?external_ids={external_ids}
108 * https://developer.zendesk.com/rest_api/docs/support/organizations#show-many-organizations
109 */
110 show_many: (options = {}) => {
111 const { error } = Joi.object({
112 ids: _ids,
113 external_ids: _external_ids
114 }).validate(options);
115 if (error) throw new Error(error.details[0].message);
116
117 const { ids = '', external_ids = '' } = options;
118 if ((!ids && !external_ids) || (ids && external_ids))
119 throw new Error(
120 'either "user_ids" or "external_ids" must be set, not both'
121 );
122
123 const part = ids ? `?ids=${ids}` : `?external_ids=${external_ids}`;
124 return {
125 method: 'GET',
126 url: `${url}/api/v2/organizations/show_many.json${part}`,
127 headers
128 };
129 },
130
131 /**
132 * Create Organization
133 *
134 * POST /api/v2/organizations.json
135 * https://developer.zendesk.com/rest_api/docs/support/organizations#create-organization
136 */
137 create: (options = {}) => {
138 const { error } = Joi.object({
139 data: _data.required()
140 }).validate(options);
141 if (error) throw new Error(error.details[0].message);
142
143 const { data } = options;
144 return {
145 method: 'POST',
146 url: `${url}/api/v2/organizations.json`,
147 headers,
148 data
149 };
150 },
151
152 /**
153 * Create Many Organizations
154 *
155 * POST /api/v2/organizations/create_many.json
156 * https://developer.zendesk.com/rest_api/docs/support/organizations#create-many-organizations
157 */
158 create_many: (options = {}) => {
159 const { error } = Joi.object({
160 data: _data.required()
161 }).validate(options);
162 if (error) throw new Error(error.details[0].message);
163
164 const { data } = options;
165 return {
166 method: 'POST',
167 url: `${url}/api/v2/organizations/create_many.json`,
168 headers,
169 data
170 };
171 },
172
173 /**
174 * Create Or Update Organization
175 *
176 * POST /api/v2/organizations/create_or_update.json
177 * https://developer.zendesk.com/rest_api/docs/support/organizations#create-or-update-organization
178 */
179 create_or_update: (options = {}) => {
180 const { error } = Joi.object({
181 data: _data.required()
182 }).validate(options);
183 if (error) throw new Error(error.details[0].message);
184
185 const { data } = options;
186 return {
187 method: 'POST',
188 url: `${url}/api/v2/organizations/create_or_update.json`,
189 headers,
190 data
191 };
192 },
193
194 /**
195 * Update Organization
196 *
197 * PUT /api/v2/organizations/{id}.json
198 * https://developer.zendesk.com/rest_api/docs/support/organizations#update-organization
199 */
200 update: (options = {}) => {
201 const { error } = Joi.object({
202 id: _id.required(),
203 data: _data.required()
204 }).validate(options);
205 if (error) throw new Error(error.details[0].message);
206
207 const { id, data } = options;
208 return {
209 method: 'PUT',
210 url: `${url}/api/v2/organizations/${id}.json`,
211 headers,
212 data
213 };
214 },
215
216 /**
217 * Update Many Organizations
218 *
219 * PUT /api/v2/organizations/update_many.json
220 * PUT /api/v2/organizations/update_many.json?ids={ids}
221 * PUT /api/v2/organizations/update_many.json?external_ids={external_ids}
222 * https://developer.zendesk.com/rest_api/docs/support/organizations#update-many-organizations
223 */
224 update_many: (options = {}) => {
225 const { error } = Joi.object({
226 ids: _ids,
227 external_ids: _external_ids,
228 data: _data.required()
229 }).validate(options);
230 if (error) throw new Error(error.details[0].message);
231
232 const { ids, external_ids, data } = options;
233 if (ids && external_ids)
234 throw new Error('either "ids" or "external_ids" can be set, not both');
235
236 const part = ids
237 ? `?ids=${ids}`
238 : external_ids
239 ? `?external_ids=${external_ids}`
240 : '';
241
242 return {
243 method: 'PUT',
244 url: `${url}/api/v2/organizations/update_many.json${part}`,
245 headers,
246 data
247 };
248 },
249
250 /**
251 * Delete Organization
252 *
253 * DELETE /api/v2/organizations/{id}.json
254 * https://developer.zendesk.com/rest_api/docs/support/organizations#delete-organization
255 */
256 delete: (options = {}) => {
257 const { error } = Joi.object({
258 id: _id.required()
259 }).validate(options);
260 if (error) throw new Error(error.details[0].message);
261
262 const { id } = options;
263 return {
264 method: 'DELETE',
265 url: `${url}/api/v2/organizations/${id}.json`,
266 headers
267 };
268 },
269
270 /**
271 * Bulk Delete Organizations
272 *
273 * DELETE /api/v2/organizations/destroy_many.json?ids={ids}
274 * DELETE /api/v2/organizations/destroy_many.json?external_ids={external_ids}
275 * https://developer.zendesk.com/rest_api/docs/support/organizations#bulk-delete-organizations
276 */
277 delete_many: (options = {}) => {
278 const { error } = Joi.object({
279 ids: _ids,
280 external_ids: _external_ids
281 }).validate(options);
282 if (error) throw new Error(error.details[0].message);
283
284 const { ids, external_ids } = options;
285 if ((!ids && !external_ids) || (ids && external_ids))
286 throw new Error(
287 'either "user_ids" or "external_ids" must be set, not both'
288 );
289
290 const part = ids ? `?ids=${ids}` : `?external_ids=${external_ids}`;
291 return {
292 method: 'DELETE',
293 url: `${url}/api/v2/organizations/destroy_many.json${part}`,
294 headers
295 };
296 },
297
298 /**
299 * Search Organizations by External ID
300 *
301 * GET /api/v2/organizations/search.json?external_id={external_id}
302 * https://developer.zendesk.com/rest_api/docs/support/organizations#search-organizations-by-external-id
303 */
304 search: (options = {}) => {
305 const { error } = Joi.object({
306 external_id: _external_id.required()
307 }).validate(options);
308 if (error) throw new Error(error.details[0].message);
309
310 const { external_id } = options;
311 return {
312 method: 'GET',
313 url: `${url}/api/v2/organizations/search.json?external_id=${external_id}`,
314 headers
315 };
316 }
317 };
318};