UNPKG

6.88 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().positive();
6const _holiday_id = Joi.number().positive();
7const _data = Joi.object();
8
9// Initialize Endpoint
10module.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 * List all schedules
19 *
20 * GET /api/v2/business_hours/schedules.json
21 * https://developer.zendesk.com/rest_api/docs/support/schedules#list-all-schedules
22 */
23 list: () => {
24 // Ignore any options
25 return {
26 method: 'GET',
27 url: `${url}/api/v2/business_hours/schedules.json`,
28 headers
29 };
30 },
31
32 /**
33 * Show a schedule
34 *
35 * GET /api/v2/business_hours/schedules/{id}.json
36 * https://developer.zendesk.com/rest_api/docs/support/schedules#show-a-schedule
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/business_hours/schedules/${id}.json`,
48 headers
49 };
50 },
51
52 /**
53 * Create a schedule
54 *
55 * POST /api/v2/business_hours/schedules.json
56 * https://developer.zendesk.com/rest_api/docs/support/schedules#create-a-schedule
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/business_hours/schedules.json`,
68 headers,
69 data
70 };
71 },
72
73 /**
74 * Update a schedule
75 *
76 * PUT /api/v2/business_hours/schedules/{id}.json
77 * https://developer.zendesk.com/rest_api/docs/support/schedules#update-a-schedule
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/business_hours/schedules/${id}.json`,
90 headers,
91 data
92 };
93 },
94
95 /**
96 * Delete a schedule
97 *
98 * DELETE /api/v2/business_hours/schedules/{id}.json
99 * https://developer.zendesk.com/rest_api/docs/support/schedules#delete-a-schedule
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/business_hours/schedules/${id}.json`,
111 headers
112 };
113 },
114
115 /**
116 * Update intervals for a schedule
117 *
118 * PUT /api/v2/business_hours/schedules/{id}/workweek.json
119 * https://developer.zendesk.com/rest_api/docs/support/schedules#update-intervals-for-a-schedule
120 */
121 update_intervals: (options = {}) => {
122 const { error } = Joi.object({
123 id: _id.required(),
124 data: _data.required()
125 }).validate(options);
126 if (error) throw new Error(error.details[0].message);
127
128 const { id, data } = options;
129 return {
130 method: 'PUT',
131 url: `${url}/api/v2/business_hours/schedules/${id}/workweek.json`,
132 headers,
133 data
134 };
135 },
136
137 /**
138 * List holidays for a schedule
139 *
140 * GET /api/v2/business_hours/schedules/{id}/holidays.json
141 * https://developer.zendesk.com/rest_api/docs/support/schedules#list-holidays-for-a-schedule
142 */
143 list_holidays: (options = {}) => {
144 const { error } = Joi.object({
145 id: _id.required()
146 }).validate(options);
147 if (error) throw new Error(error.details[0].message);
148
149 const { id } = options;
150 return {
151 method: 'GET',
152 url: `${url}/api/v2/business_hours/schedules/${id}/holidays.json`,
153 headers
154 };
155 },
156
157 /**
158 * Show a holiday
159 *
160 * GET /api/v2/business_hours/schedules/{id}/holidays/{holiday_id}.json
161 * https://developer.zendesk.com/rest_api/docs/support/schedules#show-a-holiday
162 */
163 show_holiday: (options = {}) => {
164 const { error } = Joi.object({
165 id: _id.required(),
166 holiday_id: _holiday_id.required()
167 }).validate(options);
168 if (error) throw new Error(error.details[0].message);
169
170 const { id, holiday_id } = options;
171 return {
172 method: 'GET',
173 url: `${url}/api/v2/business_hours/schedules/${id}/holidays/${holiday_id}.json`,
174 headers
175 };
176 },
177
178 /**
179 * Create a holiday
180 *
181 * POST /api/v2/business_hours/schedules/{id}/holidays.json
182 * https://developer.zendesk.com/rest_api/docs/support/schedules#create-a-holiday
183 */
184 create_holiday: (options = {}) => {
185 const { error } = Joi.object({
186 id: _id.required(),
187 data: _data.required()
188 }).validate(options);
189 if (error) throw new Error(error.details[0].message);
190
191 const { id, data } = options;
192 return {
193 method: 'POST',
194 url: `${url}/api/v2/business_hours/schedules/${id}/holidays.json`,
195 headers,
196 data
197 };
198 },
199
200 /**
201 * Update a holiday
202 *
203 * PUT /api/v2/business_hours/schedules/{id}/holidays/{holiday_id}.json
204 * https://developer.zendesk.com/rest_api/docs/support/schedules#update-a-holiday
205 */
206 update_holiday: (options = {}) => {
207 const { error } = Joi.object({
208 id: _id.required(),
209 holiday_id: _holiday_id.required(),
210 data: _data.required()
211 }).validate(options);
212 if (error) throw new Error(error.details[0].message);
213
214 const { id, holiday_id, data } = options;
215 return {
216 method: 'PUT',
217 url: `${url}/api/v2/business_hours/schedules/${id}/holidays/${holiday_id}.json`,
218 headers,
219 data
220 };
221 },
222
223 /**
224 * Delete a holiday
225 *
226 * DELETE /api/v2/business_hours/schedules/{id}/holidays/{id}.json
227 * https://developer.zendesk.com/rest_api/docs/support/schedules#delete-a-holiday
228 */
229 delete_holiday: (options = {}) => {
230 const { error } = Joi.object({
231 id: _id.required(),
232 holiday_id: _holiday_id.required()
233 }).validate(options);
234 if (error) throw new Error(error.details[0].message);
235
236 const { id, holiday_id } = options;
237 return {
238 method: 'DELETE',
239 url: `${url}/api/v2/business_hours/schedules/${id}/holidays/${holiday_id}.json`,
240 headers
241 };
242 }
243 };
244};