UNPKG

14 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _type = Joi.string().valid('', 'groups', 'organizations');
6const _id = Joi.number().min(1);
7const _ids = Joi.string().min(3);
8const _external_id = Joi.string().min(1);
9const _external_ids = Joi.string().min(1);
10const _query = Joi.string().min(3);
11const _name = Joi.string().min(1);
12const _data = Joi.object();
13
14// Initialize Endpoint
15module.exports = (options = {}) => {
16 const { error } = validate(options);
17 if (error) throw new Error(error.details[0].message);
18
19 const { url, headers } = prepare(options);
20
21 return {
22 /**
23 * List Users
24 *
25 * GET /api/v2/users.json
26 * GET /api/v2/groups/{id}/users.json
27 * GET /api/v2/organizations/{id}/users.json
28 * https://developer.zendesk.com/rest_api/docs/support/users#list-users
29 */
30 list: (options = {}) => {
31 const { error } = Joi.object({
32 type: _type,
33 id: _id
34 }).validate(options);
35 if (error) throw new Error(error.details[0].message);
36
37 const { type = '', id = 0 } = options;
38 if (!type && id > 0)
39 throw new Error('if "id" is set, "type" must be set');
40 if (type && !id) throw new Error('if "type" is set, "id" must be set');
41
42 return {
43 method: 'GET',
44 url: {
45 '': `${url}/api/v2/users.json`,
46 groups: `${url}/api/v2/groups/${id}/users.json`,
47 organizations: `${url}/api/v2/organizations/${id}/users.json`
48 }[type],
49 headers
50 };
51 },
52
53 /**
54 * Show User
55 *
56 * GET /api/v2/users/{id}.json
57 * https://developer.zendesk.com/rest_api/docs/support/users#show-user
58 */
59 show: (options = {}) => {
60 const { error } = Joi.object({
61 id: _id.required()
62 }).validate(options);
63 if (error) throw new Error(error.details[0].message);
64
65 const { id } = options;
66 return {
67 method: 'GET',
68 url: `${url}/api/v2/users/${id}.json`,
69 headers
70 };
71 },
72
73 /**
74 * Show Many Users
75 *
76 * GET /api/v2/users/show_many.json?ids={ids}
77 * GET /api/v2/users/show_many.json?external_ids={external_ids}
78 * https://developer.zendesk.com/rest_api/docs/support/users#show-many-users
79 */
80 show_many: (options = {}) => {
81 const { error } = Joi.object({
82 ids: _ids,
83 external_ids: _external_ids
84 }).validate(options);
85 if (error) throw new Error(error.details[0].message);
86
87 const { ids = '', external_ids = '' } = options;
88 if ((!ids && !external_ids) || (ids && external_ids))
89 throw new Error('either "ids" or "external_ids" can be set, not both');
90
91 return {
92 method: 'GET',
93 url: `${url}/api/v2/users/show_many.json${
94 ids ? `?ids=${ids}` : `?external_ids=${external_ids}`
95 }`,
96 headers
97 };
98 },
99
100 /**
101 * User related information
102 *
103 * GET /api/v2/users/{id}/related.json
104 * https://developer.zendesk.com/rest_api/docs/support/users#user-related-information
105 */
106 related: (options = {}) => {
107 const { error } = Joi.object({
108 id: _id.required()
109 }).validate(options);
110 if (error) throw new Error(error.details[0].message);
111
112 const { id } = options;
113 return {
114 method: 'GET',
115 url: `${url}/api/v2/users/${id}/related.json`,
116 headers
117 };
118 },
119
120 /**
121 * Create User
122 *
123 * POST /api/v2/users.json
124 * https://developer.zendesk.com/rest_api/docs/support/users#create-user
125 */
126 create: (options = {}) => {
127 const { error } = Joi.object({
128 data: _data.required()
129 }).validate(options);
130 if (error) throw new Error(error.details[0].message);
131
132 const { data } = options;
133 return {
134 method: 'POST',
135 url: `${url}/api/v2/users.json`,
136 headers,
137 data
138 };
139 },
140
141 /**
142 * Create Or Update User
143 *
144 * POST /api/v2/users/create_or_update.json
145 * https://developer.zendesk.com/rest_api/docs/support/users#create-or-update-user
146 */
147 create_or_update: (options = {}) => {
148 const { error } = Joi.object({
149 data: _data.required()
150 }).validate(options);
151 if (error) throw new Error(error.details[0].message);
152
153 const { data } = options;
154 return {
155 method: 'POST',
156 url: `${url}/api/v2/users/create_or_update.json`,
157 headers,
158 data
159 };
160 },
161
162 /**
163 * Create Or Update Many Users
164 *
165 * POST /api/v2/users/create_or_update_many.json
166 * https://developer.zendesk.com/rest_api/docs/support/users#create-or-update-many-users
167 */
168 create_or_update_many: (options = {}) => {
169 const { error } = Joi.object({
170 data: _data.required()
171 }).validate(options);
172 if (error) throw new Error(error.details[0].message);
173
174 const { data } = options;
175 return {
176 method: 'POST',
177 url: `${url}/api/v2/users/create_or_update_many.json`,
178 headers,
179 data
180 };
181 },
182
183 /**
184 * Merge Self With Another User
185 *
186 * PUT /api/v2/users/me/merge.json
187 * https://developer.zendesk.com/rest_api/docs/support/users#merge-self-with-another-user
188 */
189 merge_self: (options = {}) => {
190 const { error } = Joi.object({
191 data: _data.required()
192 }).validate(options);
193 if (error) throw new Error(error.details[0].message);
194
195 const { data } = options;
196 return {
197 method: 'PUT',
198 url: `${url}/api/v2/users/me/merge.json`,
199 headers,
200 data
201 };
202 },
203
204 /**
205 * Merge End Users
206 *
207 * PUT /api/v2/users/{id}/merge.json
208 * https://developer.zendesk.com/rest_api/docs/support/users#merge-end-users
209 */
210 merge: (options = {}) => {
211 const { error } = Joi.object({
212 id: _id.required(),
213 data: _data.required()
214 }).validate(options);
215 if (error) throw new Error(error.details[0].message);
216
217 const { id, data } = options;
218 return {
219 method: 'POST',
220 url: `${url}/api/v2/users/${id}/merge.json`,
221 headers,
222 data
223 };
224 },
225
226 /**
227 * Create Many Users
228 *
229 * POST /api/v2/users/create_many.json
230 * https://developer.zendesk.com/rest_api/docs/support/users#create-many-users
231 */
232 create_many: (options = {}) => {
233 const { error } = Joi.object({
234 data: _data.required()
235 }).validate(options);
236 if (error) throw new Error(error.details[0].message);
237
238 const { data } = options;
239 return {
240 method: 'POST',
241 url: `${url}/api/v2/users/create_many.json`,
242 headers,
243 data
244 };
245 },
246
247 /**
248 * Update User
249 *
250 * PUT /api/v2/users/{id}.json
251 * https://developer.zendesk.com/rest_api/docs/support/users#update-user
252 */
253 update: (options = {}) => {
254 const { error } = Joi.object({
255 id: _id.required(),
256 data: _data.required()
257 }).validate(options);
258 if (error) throw new Error(error.details[0].message);
259
260 const { id, data } = options;
261 return {
262 method: 'PUT',
263 url: `${url}/api/v2/users/${id}.json`,
264 headers,
265 data
266 };
267 },
268
269 /**
270 * Update Many Users
271 *
272 * PUT /api/v2/users/update_many.json
273 * PUT /api/v2/users/update_many.json?ids={ids}
274 * PUT /api/v2/users/update_many.json?external_ids={external_ids}
275 * https://developer.zendesk.com/rest_api/docs/support/users#update-many-users
276 */
277 update_many: (options = {}) => {
278 const { error } = Joi.object({
279 ids: _ids,
280 external_ids: _external_ids,
281 data: _data.required()
282 }).validate(options);
283 if (error) throw new Error(error.details[0].message);
284
285 const { ids = '', external_ids = '', data } = options;
286 if (ids && external_ids)
287 throw new Error('both "ids" and "external_ids" cannot be set');
288
289 let params = ids
290 ? `?ids=${ids}`
291 : external_ids
292 ? `?external_ids=${external_ids}`
293 : '';
294
295 return {
296 method: 'PUT',
297 url: `${url}/api/v2/users.json${params}`,
298 headers,
299 data
300 };
301 },
302
303 /**
304 * Bulk Deleting Users
305 *
306 * DELETE /api/v2/users/destroy_many.json?ids={ids}
307 * DELETE /api/v2/users/destroy_many.json?external_ids={external_ids}
308 * https://developer.zendesk.com/rest_api/docs/support/users#bulk-deleting-users
309 */
310 delete_many: (options = {}) => {
311 const { error } = Joi.object({
312 ids: _ids,
313 external_ids: _external_ids
314 }).validate(options);
315 if (error) throw new Error(error.details[0].message);
316
317 const { ids = '', external_ids = '' } = options;
318 if ((!ids && !external_ids) || (ids && external_ids))
319 throw new Error(
320 'either "ids" or "external_ids" must be set, but not both'
321 );
322
323 let params = ids ? `?ids=${ids}` : `?external_ids=${external_ids}`;
324 return {
325 method: 'DELETE',
326 url: `${url}/api/v2/users/destroy_many.json${params}`,
327 headers
328 };
329 },
330
331 /**
332 * Delete User
333 *
334 * DELETE /api/v2/users/{id}.json
335 * https://developer.zendesk.com/rest_api/docs/support/users#delete-user
336 */
337 delete: (options = {}) => {
338 const { error } = Joi.object({
339 id: _id.required()
340 }).validate(options);
341 if (error) throw new Error(error.details[0].message);
342
343 const { id } = options;
344 return {
345 method: 'DELETE',
346 url: `${url}/api/v2/users/${id}.json`,
347 headers
348 };
349 },
350
351 /**
352 * Search Users
353 *
354 * GET /api/v2/users/search.json?query={query}
355 * GET /api/v2/users/search.json?external_id={external_id}
356 * https://developer.zendesk.com/rest_api/docs/support/users#search-users
357 */
358 search: (options = {}) => {
359 const { error } = Joi.object({
360 query: _query,
361 external_id: _external_id
362 }).validate(options);
363 if (error) throw new Error(error.details[0].message);
364
365 const { query = '', external_id = '' } = options;
366 if ((!query && !external_id) || (query && external_id))
367 throw new Error(
368 'either "query" or "external_id" must be set, but not both'
369 );
370
371 const params = query ? `?query=${query}` : `?external_id=${external_id}`;
372 return {
373 method: 'GET',
374 url: `${url}/api/v2/users/search.json${params}`,
375 headers
376 };
377 },
378
379 /**
380 * Autocomplete Users
381 *
382 * GET /api/v2/users/autocomplete.json?name={name}
383 * https://developer.zendesk.com/rest_api/docs/support/users#autocomplete-users
384 */
385 autocomplete: (options = {}) => {
386 const { error } = Joi.object({
387 name: _name.required()
388 }).validate(options);
389 if (error) throw new Error(error.details[0].message);
390
391 const { name } = options;
392 return {
393 method: 'GET',
394 url: `${url}/api/v2/users/autocomplete.json?name=${name}`,
395 headers
396 };
397 },
398
399 /**
400 * Request User Create
401 *
402 * POST /api/v2/users/request_create.json
403 * https://developer.zendesk.com/rest_api/docs/support/users#request-user-create
404 */
405 request_create: (options = {}) => {
406 const { error } = Joi.object({
407 data: _data.required()
408 }).validate(options);
409 if (error) throw new Error(error.details[0].message);
410
411 const { data } = options;
412 return {
413 method: 'POST',
414 url: `${url}/api/v2/users/request_create.json`,
415 headers,
416 data
417 };
418 },
419
420 /**
421 * List Deleted Users
422 *
423 * GET /api/v2/deleted_users.json
424 * https://developer.zendesk.com/rest_api/docs/support/users#list-deleted-users
425 */
426 list_deleted: () => {
427 // Ignore any options
428 return {
429 method: 'GET',
430 url: `${url}/api/v2/deleted_users.json`,
431 headers
432 };
433 },
434
435 /**
436 * Show Deleted User
437 *
438 * GET /api/v2/deleted_users/{id}.json
439 * https://developer.zendesk.com/rest_api/docs/support/users#show-deleted-user
440 */
441 show_deleted: (options = {}) => {
442 const { error } = Joi.object({
443 id: _id.required()
444 }).validate(options);
445 if (error) throw new Error(error.details[0].message);
446
447 const { id } = options;
448 return {
449 method: 'GET',
450 url: `${url}/api/v2/deleted_users/${id}.json`,
451 headers
452 };
453 },
454
455 /**
456 * Permanently Delete User
457 *
458 * DELETE /api/v2/deleted_users/{id}.json
459 * https://developer.zendesk.com/rest_api/docs/support/users#permanently-delete-user
460 */
461 permanently_delete: (options = {}) => {
462 const { error } = Joi.object({
463 id: _id.required()
464 }).validate(options);
465 if (error) throw new Error(error.details[0].message);
466
467 const { id } = options;
468 return {
469 method: 'DELETE',
470 url: `${url}/api/v2/deleted_users/${id}.json`,
471 headers
472 };
473 },
474
475 /**
476 * Show Compliance Deletion Statuses
477 *
478 * GET /api/v2/users/{id}/compliance_deletion_statuses.json
479 * https://developer.zendesk.com/rest_api/docs/support/users#show-compliance-deletion-statuses
480 */
481 compliance_deletion_statuses: (options = {}) => {
482 const { error } = Joi.object({
483 id: _id.required()
484 }).validate(options);
485 if (error) throw new Error(error.details[0].message);
486
487 const { id } = options;
488 return {
489 method: 'GET',
490 url: `${url}/api/v2/users/${id}/compliance_deletion_statuses.json`,
491 headers
492 };
493 },
494
495 /**
496 * Show the Current User
497 *
498 * GET /api/v2/users/me.json
499 * https://developer.zendesk.com/rest_api/docs/support/users#show-the-current-user
500 */
501 current: () => {
502 // Ignore any options
503 return {
504 method: 'GET',
505 url: `${url}/api/v2/users/me.json`,
506 headers
507 };
508 }
509 };
510};