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 _query = Joi.string().min(1);
|
8 | const _data = Joi.object();
|
9 |
|
10 |
|
11 | module.exports = (options = {}) => {
|
12 | const { error } = validate(options);
|
13 | if (error) throw new Error(error.details[0].message);
|
14 |
|
15 | const { url, headers } = prepare(options);
|
16 |
|
17 | return {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | list: (options = {}) => {
|
30 | const { error } = Joi.object({
|
31 | ids: _ids
|
32 | }).validate(options);
|
33 | if (error) throw new Error(error.details[0].message);
|
34 |
|
35 | const { ids = '' } = options;
|
36 | const params = ids ? `?ids=${ids}` : '';
|
37 |
|
38 | return {
|
39 | method: 'GET',
|
40 | url: `${url}/api/v2/views.json${params}`,
|
41 | headers
|
42 | };
|
43 | },
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | update_many: (options = {}) => {
|
52 | const { error } = Joi.object({
|
53 | data: _data.required()
|
54 | }).validate(options);
|
55 | if (error) throw new Error(error.details[0].message);
|
56 |
|
57 | const { data } = options;
|
58 | return {
|
59 | method: 'PUT',
|
60 | url: `${url}/api/v2/views/update_many.json`,
|
61 | headers,
|
62 | data
|
63 | };
|
64 | },
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | active: () => {
|
73 |
|
74 | return {
|
75 | method: 'GET',
|
76 | url: `${url}/api/v2/views/active.json`,
|
77 | headers
|
78 | };
|
79 | },
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | compact: () => {
|
88 |
|
89 | return {
|
90 | method: 'GET',
|
91 | url: `${url}/api/v2/views/compact.json`,
|
92 | headers
|
93 | };
|
94 | },
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | show: (options = {}) => {
|
103 | const { error } = Joi.object({
|
104 | id: _id.required()
|
105 | }).validate(options);
|
106 | if (error) throw new Error(error.details[0].message);
|
107 |
|
108 | const { id } = options;
|
109 | return {
|
110 | method: 'GET',
|
111 | url: `${url}/api/v2/views/${id}.json`,
|
112 | headers
|
113 | };
|
114 | },
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | create: (options = {}) => {
|
123 | const { error } = Joi.object({
|
124 | data: _data.required()
|
125 | }).validate(options);
|
126 | if (error) throw new Error(error.details[0].message);
|
127 |
|
128 | const { data } = options;
|
129 | return {
|
130 | method: 'POST',
|
131 | url: `${url}/api/v2/views.json`,
|
132 | headers,
|
133 | data
|
134 | };
|
135 | },
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | update: (options = {}) => {
|
144 | const { error } = Joi.object({
|
145 | id: _id.required(),
|
146 | data: _data.required()
|
147 | }).validate(options);
|
148 | if (error) throw new Error(error.details[0].message);
|
149 |
|
150 | const { id, data } = options;
|
151 | return {
|
152 | method: 'PUT',
|
153 | url: `${url}/api/v2/views/${id}.json`,
|
154 | headers,
|
155 | data
|
156 | };
|
157 | },
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | delete: (options = {}) => {
|
166 | const { error } = Joi.object({
|
167 | id: _id.required()
|
168 | }).validate(options);
|
169 | if (error) throw new Error(error.details[0].message);
|
170 |
|
171 | const { id } = options;
|
172 | return {
|
173 | method: 'DELETE',
|
174 | url: `${url}/api/v2/views/${id}.json`,
|
175 | headers
|
176 | };
|
177 | },
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | delete_many: (options = {}) => {
|
186 | const { error } = Joi.object({
|
187 | data: _data.required()
|
188 | }).validate(options);
|
189 | if (error) throw new Error(error.details[0].message);
|
190 |
|
191 | const { data } = options;
|
192 | return {
|
193 | method: 'DELETE',
|
194 | url: `${url}/api/v2/views/destroy_many.json`,
|
195 | headers,
|
196 | data
|
197 | };
|
198 | },
|
199 |
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | execute: (options = {}) => {
|
207 | const { error } = Joi.object({
|
208 | id: _id.required()
|
209 | }).validate(options);
|
210 | if (error) throw new Error(error.details[0].message);
|
211 |
|
212 | const { id } = options;
|
213 | return {
|
214 | method: 'GET',
|
215 | url: `${url}/api/v2/views/${id}/execute.json`,
|
216 | headers
|
217 | };
|
218 | },
|
219 |
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | tickets: (options = {}) => {
|
227 | const { error } = Joi.object({
|
228 | id: _id.required()
|
229 | }).validate(options);
|
230 | if (error) throw new Error(error.details[0].message);
|
231 |
|
232 | const { id } = options;
|
233 | return {
|
234 | method: 'GET',
|
235 | url: `${url}/api/v2/views/${id}/tickets.json`,
|
236 | headers
|
237 | };
|
238 | },
|
239 |
|
240 | |
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | count_many: (options = {}) => {
|
247 | const { error } = Joi.object({
|
248 | ids: _ids.required()
|
249 | }).validate(options);
|
250 | if (error) throw new Error(error.details[0].message);
|
251 |
|
252 | const { ids } = options;
|
253 | return {
|
254 | method: 'GET',
|
255 | url: `${url}/api/v2/views/count_many.json?ids=${ids}`,
|
256 | headers
|
257 | };
|
258 | },
|
259 |
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | count: (options = {}) => {
|
267 | const { error } = Joi.object({
|
268 | id: _id.required()
|
269 | }).validate(options);
|
270 | if (error) throw new Error(error.details[0].message);
|
271 |
|
272 | const { id } = options;
|
273 | return {
|
274 | method: 'GET',
|
275 | url: `${url}/api/v2/views/${id}/count.json`,
|
276 | headers
|
277 | };
|
278 | },
|
279 |
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | export: (options = {}) => {
|
287 | const { error } = Joi.object({
|
288 | id: _id.required()
|
289 | }).validate(options);
|
290 | if (error) throw new Error(error.details[0].message);
|
291 |
|
292 | const { id } = options;
|
293 | return {
|
294 | method: 'GET',
|
295 | url: `${url}/api/v2/views/${id}/export.json`,
|
296 | headers
|
297 | };
|
298 | },
|
299 |
|
300 | |
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | search: (options = {}) => {
|
307 | const { error } = Joi.object({
|
308 | query: _query.required()
|
309 | }).validate(options);
|
310 | if (error) throw new Error(error.details[0].message);
|
311 |
|
312 | const { query } = options;
|
313 | return {
|
314 | method: 'GET',
|
315 | url: `${url}/api/v2/views/search.json?query=${query}`,
|
316 | headers
|
317 | };
|
318 | },
|
319 |
|
320 | |
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | preview: (options = {}) => {
|
327 | const { error } = Joi.object({
|
328 | data: _data.required()
|
329 | }).validate(options);
|
330 | if (error) throw new Error(error.details[0].message);
|
331 |
|
332 | const { data } = options;
|
333 | return {
|
334 | method: 'POST',
|
335 | url: `${url}/api/v2/views/preview.json`,
|
336 | headers,
|
337 | data
|
338 | };
|
339 | },
|
340 |
|
341 | |
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | preview_count: (options = {}) => {
|
348 | const { error } = Joi.object({
|
349 | data: _data.required()
|
350 | }).validate(options);
|
351 | if (error) throw new Error(error.details[0].message);
|
352 |
|
353 | const { data } = options;
|
354 | return {
|
355 | method: 'POST',
|
356 | url: `${url}/api/v2/views/preview/count.json`,
|
357 | headers,
|
358 | data
|
359 | };
|
360 | }
|
361 | };
|
362 | };
|