UNPKG

14.8 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _type = Joi.string().valid(
6 'tickets',
7 'organizations',
8 'users_requested',
9 'users_ccd',
10 'users_assigned',
11 'recent'
12);
13const _id = Joi.number().min(1);
14const _ids = Joi.string().min(3);
15const _external_id = Joi.string().min(1);
16const _name = Joi.string().min(1);
17const _data = Joi.object();
18
19// Initialize Endpoint
20module.exports = (options = {}) => {
21 const { error } = validate(options);
22 if (error) throw new Error(error.details[0].message);
23
24 const { url, headers } = prepare(options);
25
26 return {
27 /**
28 * List Tickets
29 *
30 * GET /api/v2/tickets.json
31 * https://developer.zendesk.com/rest_api/docs/support/tickets#list-tickets
32 */
33 list: (options = {}) => {
34 const { error } = Joi.object({
35 type: _type,
36 id: _id
37 }).validate(options);
38 if (error) throw new Error(error.details[0].message);
39
40 const { type = 'tickets', id = 0 } = options;
41 if (id && (type === 'tickets' || type === 'recent'))
42 throw new Error(
43 'if "id" is set, type cannont be "tickets" or "recent"'
44 );
45
46 return {
47 method: 'GET',
48 url: {
49 tickets: `${url}/api/v2/tickets.json`,
50 organizations: `${url}/api/v2/organizations/${id}/tickets.json`,
51 users_requested: `${url}/api/v2/users/${id}/tickets/requested.json`,
52 users_ccd: `${url}/api/v2/users/${id}/tickets/ccd.json`,
53 users_assigned: `${url}/api/v2/users/${id}/tickets/assigned.json`,
54 recent: `${url}/api/v2/tickets/recent.json`
55 }[type],
56 headers
57 };
58 },
59
60 /**
61 * List Tickets by External Id
62 *
63 * GET /api/v2/tickets.json?external_id={external_id}
64 * https://developer.zendesk.com/rest_api/docs/support/tickets#list-tickets-by-external-id
65 */
66 list_by_external_id: (options = {}) => {
67 const { error } = Joi.object({
68 external_id: _external_id.required()
69 }).validate(options);
70 if (error) throw new Error(error.details[0].message);
71
72 const { external_id } = options;
73 return {
74 method: 'GET',
75 url: `${url}/api/v2/tickets.json?external_id=${external_id}`,
76 headers
77 };
78 },
79
80 /**
81 * Show Ticket
82 *
83 * GET /api/v2/tickets/{id}.json
84 * https://developer.zendesk.com/rest_api/docs/support/tickets#show-ticket
85 */
86 show: (options = {}) => {
87 const { error } = Joi.object({
88 id: _id.required()
89 }).validate(options);
90 if (error) throw new Error(error.details[0].message);
91
92 const { id } = options;
93 return {
94 method: 'GET',
95 url: `${url}/api/v2/tickets/${id}.json`,
96 headers
97 };
98 },
99
100 /**
101 * Show Multiple Tickets
102 *
103 * GET /api/v2/tickets/show_many.json?ids={ids}
104 * https://developer.zendesk.com/rest_api/docs/support/tickets#show-multiple-tickets
105 */
106 show_many: (options = {}) => {
107 const { error } = Joi.object({
108 ids: _ids.required()
109 }).validate(options);
110 if (error) throw new Error(error.details[0].message);
111
112 const { ids } = options;
113 return {
114 method: 'GET',
115 url: `${url}/api/v2/tickets/show_many.json?ids=${ids}`,
116 headers
117 };
118 },
119
120 /**
121 * Create Ticket
122 *
123 * POST /api/v2/tickets.json
124 * https://developer.zendesk.com/rest_api/docs/support/tickets#create-ticket
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/tickets.json`,
136 headers,
137 data
138 };
139 },
140
141 /**
142 * Create Many Tickets
143 *
144 * POST /api/v2/tickets/create_many.json
145 * https://developer.zendesk.com/rest_api/docs/support/tickets#create-many-tickets
146 */
147 create_many: (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/tickets/create_many.json`,
157 headers,
158 data
159 };
160 },
161
162 /**
163 * Update Ticket
164 *
165 * PUT /api/v2/tickets/{id}.json
166 * https://developer.zendesk.com/rest_api/docs/support/tickets#update-ticket
167 */
168 update: (options = {}) => {
169 const { error } = Joi.object({
170 id: _id.required(),
171 data: _data.required()
172 }).validate(options);
173 if (error) throw new Error(error.details[0].message);
174
175 const { id, data } = options;
176 return {
177 method: 'PUT',
178 url: `${url}/api/v2/tickets/${id}.json`,
179 headers,
180 data
181 };
182 },
183
184 /**
185 * Update Many Tickets
186 *
187 * PUT /api/v2/tickets/update_many.json
188 * PUT /api/v2/tickets/update_many.json?ids={ids}
189 * https://developer.zendesk.com/rest_api/docs/support/tickets#update-many-tickets
190 */
191 update_many: (options = {}) => {
192 const { error } = Joi.object({
193 ids: _ids,
194 data: _data.required()
195 }).validate(options);
196 if (error) throw new Error(error.details[0].message);
197
198 const { ids = '', data } = options;
199 const params = ids ? `?ids=${ids}` : '';
200 return {
201 method: 'PUT',
202 url: `${url}/api/v2/tickets/update_many.json${params}`,
203 headers,
204 data
205 };
206 },
207
208 /**
209 * Mark Ticket as Spam and Suspend Requester
210 *
211 * PUT /api/v2/tickets/{id}/mark_as_spam.json
212 * https://developer.zendesk.com/rest_api/docs/support/tickets#mark-ticket-as-spam-and-suspend-requester
213 */
214 mark_as_spam: (options = {}) => {
215 const { error } = Joi.object({
216 id: _id.required()
217 }).validate(options);
218 if (error) throw new Error(error.details[0].message);
219
220 const { id } = options;
221 return {
222 method: 'PUT',
223 url: `${url}/api/v2/tickets/${id}/mark_as_spam.json`,
224 headers
225 };
226 },
227
228 /**
229 * Bulk Mark Tickets as Spam
230 *
231 * PUT /api/v2/tickets/mark_many_as_spam.json?ids={ids}
232 * https://developer.zendesk.com/rest_api/docs/support/tickets#bulk-mark-tickets-as-spam
233 */
234 mark_as_spam_bulk: (options = {}) => {
235 const { error } = Joi.object({
236 ids: _ids.required()
237 }).validate(options);
238 if (error) throw new Error(error.details[0].message);
239
240 const { ids } = options;
241 return {
242 method: 'PUT',
243 url: `${url}/api/v2/tickets/mark_many_as_spam.json?ids=${ids}`,
244 headers
245 };
246 },
247
248 /**
249 * Merge Tickets into Target Ticket
250 *
251 * POST /api/v2/tickets/{id}/merge.json
252 * https://developer.zendesk.com/rest_api/docs/support/tickets#merge-tickets-into-target-ticket
253 */
254 merge: (options = {}) => {
255 const { error } = Joi.object({
256 id: _id.required(),
257 data: _data.required()
258 }).validate(options);
259 if (error) throw new Error(error.details[0].message);
260
261 const { id, data } = options;
262 return {
263 method: 'POST',
264 url: `${url}/api/v2/tickets/${id}/merge.json`,
265 headers,
266 data
267 };
268 },
269
270 /**
271 * Ticket Related Information
272 *
273 * GET /api/v2/tickets/{id}/related.json
274 * https://developer.zendesk.com/rest_api/docs/support/tickets#ticket-related-information
275 */
276 related: (options = {}) => {
277 const { error } = Joi.object({
278 id: _id.required()
279 }).validate(options);
280 if (error) throw new Error(error.details[0].message);
281
282 const { id } = options;
283 return {
284 method: 'POST',
285 url: `${url}/api/v2/tickets/${id}/related.json`,
286 headers
287 };
288 },
289
290 /**
291 * Delete Ticket
292 *
293 * DELETE /api/v2/tickets/{id}.json
294 * https://developer.zendesk.com/rest_api/docs/support/tickets#delete-ticket
295 */
296 delete: (options = {}) => {
297 const { error } = Joi.object({
298 id: _id.required()
299 }).validate(options);
300 if (error) throw new Error(error.details[0].message);
301
302 const { id } = options;
303 return {
304 method: 'DELETE',
305 url: `${url}/api/v2/tickets/${id}.json`,
306 headers
307 };
308 },
309
310 /**
311 * Bulk Delete Tickets
312 *
313 * DELETE /api/v2/tickets/destroy_many.json?ids={ids}
314 * https://developer.zendesk.com/rest_api/docs/support/tickets#bulk-delete-tickets
315 */
316 delete_bulk: (options = {}) => {
317 const { error } = Joi.object({
318 ids: _ids.required()
319 }).validate(options);
320 if (error) throw new Error(error.details[0].message);
321
322 const { ids } = options;
323 return {
324 method: 'DELETE',
325 url: `${url}/api/v2/tickets/destroy_many.json?ids=${ids}`,
326 headers
327 };
328 },
329
330 /**
331 * List deleted tickets
332 *
333 * GET /api/v2/deleted_tickets.json
334 * https://developer.zendesk.com/rest_api/docs/support/tickets#list-deleted-tickets
335 */
336 deleted: () => {
337 // Ignore any options
338 return {
339 method: 'GET',
340 url: `${url}/api/v2/deleted_tickets.json`,
341 headers
342 };
343 },
344
345 /**
346 * Restore a previously deleted ticket
347 *
348 * PUT /api/v2/deleted_tickets/{id}/restore.json
349 * https://developer.zendesk.com/rest_api/docs/support/tickets#restore-a-previously-deleted-ticket
350 */
351 restore: (options = {}) => {
352 const { error } = Joi.object({
353 id: _id.required()
354 }).validate(options);
355 if (error) throw new Error(error.details[0].message);
356
357 const { id } = options;
358 return {
359 method: 'PUT',
360 url: `${url}/api/v2/deleted_tickets/${id}/restore.json`,
361 headers
362 };
363 },
364
365 /**
366 * Restore previously deleted tickets in bulk
367 *
368 * PUT /api/v2/deleted_tickets/restore_many?ids={ids}
369 * https://developer.zendesk.com/rest_api/docs/support/tickets#restore-previously-deleted-tickets-in-bulk
370 */
371 restore_bulk: (options = {}) => {
372 const { error } = Joi.object({
373 ids: _ids.required()
374 }).validate(options);
375 if (error) throw new Error(error.details[0].message);
376
377 const { ids } = options;
378 return {
379 method: 'PUT',
380 url: `${url}/api/v2/deleted_tickets/restore_many?ids=${ids}`,
381 headers
382 };
383 },
384
385 /**
386 * Delete ticket permanently
387 *
388 * DELETE /api/v2/deleted_tickets/{id}.json
389 * https://developer.zendesk.com/rest_api/docs/support/tickets#delete-ticket-permanently
390 */
391 delete_permanently: (options = {}) => {
392 const { error } = Joi.object({
393 id: _id.required()
394 }).validate(options);
395 if (error) throw new Error(error.details[0].message);
396
397 const { id } = options;
398 return {
399 method: 'DELETE',
400 url: `${url}/api/v2/deleted_tickets/${id}.json`,
401 headers
402 };
403 },
404
405 /**
406 * Delete multiple tickets permanently
407 *
408 * DELETE /api/v2/deleted_tickets/destroy_many?ids={ids}
409 * https://developer.zendesk.com/rest_api/docs/support/tickets#delete-multiple-tickets-permanently
410 */
411 delete_permanently_bulk: (options = {}) => {
412 const { error } = Joi.object({
413 ids: _ids.required()
414 }).validate(options);
415 if (error) throw new Error(error.details[0].message);
416
417 const { ids } = options;
418 return {
419 method: 'DELETE',
420 url: `${url}/api/v2/deleted_tickets/destroy_many?ids=${ids}`,
421 headers
422 };
423 },
424
425 /**
426 * List Collaborators for a Ticket
427 *
428 * GET /api/v2/tickets/{id}/collaborators.json
429 * https://developer.zendesk.com/rest_api/docs/support/tickets#list-collaborators-for-a-ticket
430 */
431 collaborators: (options = {}) => {
432 const { error } = Joi.object({
433 id: _id.required()
434 }).validate(options);
435 if (error) throw new Error(error.details[0].message);
436
437 const { id } = options;
438 return {
439 method: 'GET',
440 url: `${url}/api/v2/tickets/${id}/collaborators.json`,
441 headers
442 };
443 },
444
445 /**
446 * List Followers for a Ticket
447 *
448 * GET /api/v2/tickets/{id}/followers
449 * https://developer.zendesk.com/rest_api/docs/support/tickets#list-followers-for-a-ticket
450 */
451 followers: (options = {}) => {
452 const { error } = Joi.object({
453 id: _id.required()
454 }).validate(options);
455 if (error) throw new Error(error.details[0].message);
456
457 const { id } = options;
458 return {
459 method: 'GET',
460 url: `${url}/api/v2/tickets/${id}/followers.json`,
461 headers
462 };
463 },
464
465 /**
466 * List Email CCs for a Ticket
467 *
468 * GET /api/v2/tickets/{id}/email_ccs
469 * https://developer.zendesk.com/rest_api/docs/support/tickets#list-email-ccs-for-a-ticket
470 */
471 email_ccs: (options = {}) => {
472 const { error } = Joi.object({
473 id: _id.required()
474 }).validate(options);
475 if (error) throw new Error(error.details[0].message);
476
477 const { id } = options;
478 return {
479 method: 'GET',
480 url: `${url}/api/v2/tickets/${id}/email_ccs.json`,
481 headers
482 };
483 },
484
485 /**
486 * Listing Ticket Incidents
487 *
488 * GET /api/v2/tickets/{id}/incidents.json
489 * https://developer.zendesk.com/rest_api/docs/support/tickets#listing-ticket-incidents
490 */
491 incidents: (options = {}) => {
492 const { error } = Joi.object({
493 id: _id.required()
494 }).validate(options);
495 if (error) throw new Error(error.details[0].message);
496
497 const { id } = options;
498 return {
499 method: 'GET',
500 url: `${url}/api/v2/tickets/${id}/incidents.json`,
501 headers
502 };
503 },
504
505 /**
506 * Listing Ticket Problems
507 *
508 * GET /api/v2/problems.json
509 * https://developer.zendesk.com/rest_api/docs/support/tickets#listing-ticket-problems
510 */
511 problems: () => {
512 // Ignore any options
513 return {
514 method: 'GET',
515 url: `${url}/api/v2/problems.json`,
516 headers
517 };
518 },
519
520 /**
521 * Autocomplete Problems
522 *
523 * POST /api/v2/problems/autocomplete.json?text={name}
524 * https://developer.zendesk.com/rest_api/docs/support/tickets#autocomplete-problems
525 */
526 autocomplete_problems: (options = {}) => {
527 const { error } = Joi.object({
528 name: _name.required()
529 }).validate(options);
530 if (error) throw new Error(error.details[0].message);
531
532 const { name } = options;
533 return {
534 method: 'POST',
535 url: `${url}/api/v2/problems/autocomplete.json?text=${name}`,
536 headers
537 };
538 }
539 };
540};