1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _id = Joi.number().min(1);
|
6 | const _ids = Joi.string().min(3);
|
7 | const _user_id = Joi.number().min(1);
|
8 | const _external_id = Joi.number().min(1);
|
9 | const _external_ids = Joi.string().min(1);
|
10 | const _name = Joi.string().min(1);
|
11 | const _data = Joi.object();
|
12 |
|
13 |
|
14 | module.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 |
|
23 |
|
24 |
|
25 |
|
26 |
|
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 |
|
45 |
|
46 |
|
47 |
|
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 |
|
65 |
|
66 |
|
67 |
|
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 |
|
85 |
|
86 |
|
87 |
|
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 |
|
105 |
|
106 |
|
107 |
|
108 |
|
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 |
|
133 |
|
134 |
|
135 |
|
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 |
|
154 |
|
155 |
|
156 |
|
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 |
|
175 |
|
176 |
|
177 |
|
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 |
|
196 |
|
197 |
|
198 |
|
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 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
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 |
|
252 |
|
253 |
|
254 |
|
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 |
|
272 |
|
273 |
|
274 |
|
275 |
|
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 |
|
300 |
|
301 |
|
302 |
|
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 | };
|