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(1);
|
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 | list: () => {
|
25 |
|
26 | return {
|
27 | method: 'GET',
|
28 | url: `${url}/api/v2/automations.json`,
|
29 | headers
|
30 | };
|
31 | },
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | show: (options = {}) => {
|
40 | const { error } = Joi.object({
|
41 | id: _id.required()
|
42 | }).validate(options);
|
43 | if (error) throw new Error(error.details[0].message);
|
44 |
|
45 | const { id } = options;
|
46 | return {
|
47 | method: 'GET',
|
48 | url: `${url}/api/v2/automations/${id}.json`,
|
49 | headers
|
50 | };
|
51 | },
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | active: () => {
|
60 |
|
61 | return {
|
62 | method: 'GET',
|
63 | url: `${url}/api/v2/automations/active.json`,
|
64 | headers
|
65 | };
|
66 | },
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | create: (options = {}) => {
|
75 | const { error } = Joi.object({
|
76 | data: _data.required()
|
77 | }).validate(options);
|
78 | if (error) throw new Error(error.details[0].message);
|
79 |
|
80 | const { data } = options;
|
81 | return {
|
82 | method: 'POST',
|
83 | url: `${url}/api/v2/automations.json`,
|
84 | headers,
|
85 | data
|
86 | };
|
87 | },
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | update: (options = {}) => {
|
96 | const { error } = Joi.object({
|
97 | id: _id.required(),
|
98 | data: _data.required()
|
99 | }).validate(options);
|
100 | if (error) throw new Error(error.details[0].message);
|
101 |
|
102 | const { id, data } = options;
|
103 | return {
|
104 | method: 'PUT',
|
105 | url: `${url}/api/v2/automations/${id}.json`,
|
106 | headers,
|
107 | data
|
108 | };
|
109 | },
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | update_many: (options = {}) => {
|
118 | const { error } = Joi.object({
|
119 | data: _data.required()
|
120 | }).validate(options);
|
121 | if (error) throw new Error(error.details[0].message);
|
122 |
|
123 | const { data } = options;
|
124 | return {
|
125 | method: 'PUT',
|
126 | url: `${url}/api/v2/automations/update_many.json`,
|
127 | headers,
|
128 | data
|
129 | };
|
130 | },
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | delete: (options = {}) => {
|
139 | const { error } = Joi.object({
|
140 | id: _id.required()
|
141 | }).validate(options);
|
142 | if (error) throw new Error(error.details[0].message);
|
143 |
|
144 | const { id } = options;
|
145 | return {
|
146 | method: 'DELETE',
|
147 | url: `${url}/api/v2/automations/${id}.json`,
|
148 | headers
|
149 | };
|
150 | },
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | delete_bulk: (options = {}) => {
|
159 | const { error } = Joi.object({
|
160 | ids: _ids.required()
|
161 | }).validate(options);
|
162 | if (error) throw new Error(error.details[0].message);
|
163 |
|
164 | const { ids } = options;
|
165 | return {
|
166 | method: 'DELETE',
|
167 | url: `${url}/api/v2/automations/destroy_many.json?ids=${ids}`,
|
168 | headers
|
169 | };
|
170 | },
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | search: (options = {}) => {
|
179 | const { error } = Joi.object({
|
180 | query: _query.required()
|
181 | }).validate(options);
|
182 | if (error) throw new Error(error.details[0].message);
|
183 |
|
184 | const { query } = options;
|
185 | return {
|
186 | method: 'GET',
|
187 | url: `${url}/api/v2/automations/search.json?query=${query}`,
|
188 | headers
|
189 | };
|
190 | }
|
191 | };
|
192 | };
|