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/ticket_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/ticket_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/ticket_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/ticket_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/ticket_fields/${id}.json`,
|
111 | headers
|
112 | };
|
113 | },
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | listOptions: (options = {}) => {
|
122 | const { error } = Joi.object({
|
123 | field_id: _field_id.required()
|
124 | }).validate(options);
|
125 | if (error) throw new Error(error.details[0].message);
|
126 |
|
127 | const { field_id } = options;
|
128 | return {
|
129 | method: 'GET',
|
130 | url: `${url}/api/v2/ticket_fields/${field_id}/options.json`,
|
131 | headers
|
132 | };
|
133 | },
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | showOption: (options = {}) => {
|
142 | const { error } = Joi.object({
|
143 | field_id: _field_id.required(),
|
144 | id: _id.required()
|
145 | }).validate(options);
|
146 | if (error) throw new Error(error.details[0].message);
|
147 |
|
148 | const { field_id, id } = options;
|
149 | return {
|
150 | method: 'GET',
|
151 | url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
|
152 | headers
|
153 | };
|
154 | },
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | createOrUpdateOption: (options = {}) => {
|
163 | const { error } = Joi.object({
|
164 | field_id: _field_id.required(),
|
165 | id: _id.required(),
|
166 | data: _data.required()
|
167 | }).validate(options);
|
168 | if (error) throw new Error(error.details[0].message);
|
169 |
|
170 | const { field_id, id, data } = options;
|
171 | return {
|
172 | method: 'POST',
|
173 | url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
|
174 | headers,
|
175 | data
|
176 | };
|
177 | },
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | deleteOption: (options = {}) => {
|
186 | const { error } = Joi.object({
|
187 | field_id: _field_id.required(),
|
188 | id: _id.required()
|
189 | }).validate(options);
|
190 | if (error) throw new Error(error.details[0].message);
|
191 |
|
192 | const { field_id, id } = options;
|
193 | return {
|
194 | method: 'DELETE',
|
195 | url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
|
196 | headers
|
197 | };
|
198 | }
|
199 | };
|
200 | };
|