1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _type = Joi.string().valid('', 'groups', 'organizations');
|
6 | const _id = Joi.number().min(1);
|
7 | const _ids = Joi.string().min(3);
|
8 | const _external_id = Joi.string().min(1);
|
9 | const _external_ids = Joi.string().min(1);
|
10 | const _query = Joi.string().min(3);
|
11 | const _name = Joi.string().min(1);
|
12 | const _data = Joi.object();
|
13 |
|
14 |
|
15 | module.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 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
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 |
|
55 |
|
56 |
|
57 |
|
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 |
|
75 |
|
76 |
|
77 |
|
78 |
|
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 |
|
102 |
|
103 |
|
104 |
|
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 |
|
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/users.json`,
|
136 | headers,
|
137 | data
|
138 | };
|
139 | },
|
140 |
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
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 |
|
164 |
|
165 |
|
166 |
|
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 |
|
185 |
|
186 |
|
187 |
|
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 |
|
206 |
|
207 |
|
208 |
|
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 |
|
228 |
|
229 |
|
230 |
|
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 |
|
249 |
|
250 |
|
251 |
|
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 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
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 |
|
305 |
|
306 |
|
307 |
|
308 |
|
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 |
|
333 |
|
334 |
|
335 |
|
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 |
|
353 |
|
354 |
|
355 |
|
356 |
|
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 |
|
381 |
|
382 |
|
383 |
|
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 |
|
401 |
|
402 |
|
403 |
|
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 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | list_deleted: () => {
|
427 |
|
428 | return {
|
429 | method: 'GET',
|
430 | url: `${url}/api/v2/deleted_users.json`,
|
431 | headers
|
432 | };
|
433 | },
|
434 |
|
435 | |
436 |
|
437 |
|
438 |
|
439 |
|
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 |
|
457 |
|
458 |
|
459 |
|
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 |
|
477 |
|
478 |
|
479 |
|
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 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | current: () => {
|
502 |
|
503 | return {
|
504 | method: 'GET',
|
505 | url: `${url}/api/v2/users/me.json`,
|
506 | headers
|
507 | };
|
508 | }
|
509 | };
|
510 | };
|