1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _id = Joi.number().min(1);
|
6 | const _field_id = Joi.number().min(1);
|
7 | const _data = Joi.object();
|
8 |
|
9 |
|
10 | module.exports = (options = {}) => {
|
11 | const { error } = validate(options);
|
12 | if (error) throw new Error(error.details[0].message);
|
13 |
|
14 | const { url, headers } = prepare(options);
|
15 |
|
16 | return {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | list: () => {
|
24 |
|
25 | return {
|
26 | method: 'GET',
|
27 | url: `${url}/api/v2/user_fields.json`,
|
28 | headers
|
29 | };
|
30 | },
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | show: (options = {}) => {
|
39 | const { error } = Joi.object({
|
40 | id: _id.required()
|
41 | }).validate(options);
|
42 | if (error) throw new Error(error.details[0].message);
|
43 |
|
44 | const { id } = options;
|
45 | return {
|
46 | method: 'GET',
|
47 | url: `${url}/api/v2/user_fields/${id}.json`,
|
48 | headers
|
49 | };
|
50 | },
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | create: (options = {}) => {
|
59 | const { error } = Joi.object({
|
60 | data: _data.required()
|
61 | }).validate(options);
|
62 | if (error) throw new Error(error.details[0].message);
|
63 |
|
64 | const { data } = options;
|
65 | return {
|
66 | method: 'POST',
|
67 | url: `${url}/api/v2/user_fields.json`,
|
68 | headers,
|
69 | data
|
70 | };
|
71 | },
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | update: (options = {}) => {
|
80 | const { error } = Joi.object({
|
81 | id: _id.required(),
|
82 | data: _data.required()
|
83 | }).validate(options);
|
84 | if (error) throw new Error(error.details[0].message);
|
85 |
|
86 | const { id, data } = options;
|
87 | return {
|
88 | method: 'PUT',
|
89 | url: `${url}/api/v2/user_fields/${id}.json`,
|
90 | headers,
|
91 | data
|
92 | };
|
93 | },
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | delete: (options = {}) => {
|
102 | const { error } = Joi.object({
|
103 | id: _id.required()
|
104 | }).validate(options);
|
105 | if (error) throw new Error(error.details[0].message);
|
106 |
|
107 | const { id } = options;
|
108 | return {
|
109 | method: 'DELETE',
|
110 | url: `${url}/api/v2/user_fields/${id}.json`,
|
111 | headers
|
112 | };
|
113 | },
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | reorder: (options = {}) => {
|
122 | const { error } = Joi.object({
|
123 | data: _data.required()
|
124 | }).validate(options);
|
125 | if (error) throw new Error(error.details[0].message);
|
126 |
|
127 | const { data } = options;
|
128 | return {
|
129 | method: 'PUT',
|
130 | url: `${url}/api/v2/user_fields/reorder.json`,
|
131 | headers,
|
132 | data
|
133 | };
|
134 | },
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | listOptions: (options = {}) => {
|
143 | const { error } = Joi.object({
|
144 | field_id: _field_id.required()
|
145 | }).validate(options);
|
146 | if (error) throw new Error(error.details[0].message);
|
147 |
|
148 | const { field_id } = options;
|
149 | return {
|
150 | method: 'GET',
|
151 | url: `${url}/api/v2/user_fields/${field_id}/options.json`,
|
152 | headers
|
153 | };
|
154 | },
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | showOption: (options = {}) => {
|
163 | const { error } = Joi.object({
|
164 | field_id: _field_id.required(),
|
165 | id: _id.required()
|
166 | }).validate(options);
|
167 | if (error) throw new Error(error.details[0].message);
|
168 |
|
169 | const { field_id, id } = options;
|
170 | return {
|
171 | method: 'GET',
|
172 | url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
|
173 | headers
|
174 | };
|
175 | },
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | createOrUpdateOption: (options = {}) => {
|
184 | const { error } = Joi.object({
|
185 | field_id: _field_id.required(),
|
186 | data: _data.required()
|
187 | }).validate(options);
|
188 | if (error) throw new Error(error.details[0].message);
|
189 |
|
190 | const { field_id, data } = options;
|
191 | return {
|
192 | method: 'POST',
|
193 | url: `${url}/api/v2/user_fields/${field_id}/options.json`,
|
194 | headers,
|
195 | data
|
196 | };
|
197 | },
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | deleteOption: (options = {}) => {
|
206 | const { error } = Joi.object({
|
207 | field_id: _field_id.required(),
|
208 | id: _id.required()
|
209 | }).validate(options);
|
210 | if (error) throw new Error(error.details[0].message);
|
211 |
|
212 | const { field_id, id } = options;
|
213 | return {
|
214 | method: 'DELETE',
|
215 | url: `${url}/api/v2/user_fields/${field_id}/options/${id}.json`,
|
216 | headers
|
217 | };
|
218 | }
|
219 | };
|
220 | };
|