1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _type = Joi.string().valid(
|
6 | 'tickets',
|
7 | 'organizations',
|
8 | 'users_requested',
|
9 | 'users_ccd',
|
10 | 'users_assigned',
|
11 | 'recent'
|
12 | );
|
13 | const _id = Joi.number().min(1);
|
14 | const _ids = Joi.string().min(3);
|
15 | const _external_id = Joi.string().min(1);
|
16 | const _name = Joi.string().min(1);
|
17 | const _data = Joi.object();
|
18 |
|
19 |
|
20 | module.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 |
|
29 |
|
30 |
|
31 |
|
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 |
|
62 |
|
63 |
|
64 |
|
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 |
|
82 |
|
83 |
|
84 |
|
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 |
|
102 |
|
103 |
|
104 |
|
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 |
|
122 |
|
123 |
|
124 |
|
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 |
|
143 |
|
144 |
|
145 |
|
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 |
|
164 |
|
165 |
|
166 |
|
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 |
|
186 |
|
187 |
|
188 |
|
189 |
|
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 |
|
210 |
|
211 |
|
212 |
|
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 |
|
230 |
|
231 |
|
232 |
|
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 |
|
250 |
|
251 |
|
252 |
|
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 |
|
272 |
|
273 |
|
274 |
|
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 |
|
292 |
|
293 |
|
294 |
|
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 |
|
312 |
|
313 |
|
314 |
|
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 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | deleted: () => {
|
337 |
|
338 | return {
|
339 | method: 'GET',
|
340 | url: `${url}/api/v2/deleted_tickets.json`,
|
341 | headers
|
342 | };
|
343 | },
|
344 |
|
345 | |
346 |
|
347 |
|
348 |
|
349 |
|
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 |
|
367 |
|
368 |
|
369 |
|
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 |
|
387 |
|
388 |
|
389 |
|
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 |
|
407 |
|
408 |
|
409 |
|
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 |
|
427 |
|
428 |
|
429 |
|
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 |
|
447 |
|
448 |
|
449 |
|
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 |
|
467 |
|
468 |
|
469 |
|
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 |
|
487 |
|
488 |
|
489 |
|
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 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 | problems: () => {
|
512 |
|
513 | return {
|
514 | method: 'GET',
|
515 | url: `${url}/api/v2/problems.json`,
|
516 | headers
|
517 | };
|
518 | },
|
519 |
|
520 | |
521 |
|
522 |
|
523 |
|
524 |
|
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 | };
|