UNPKG

4.97 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _ids = Joi.string().min(1);
7const _query = Joi.string().min(1);
8const _data = Joi.object();
9
10// Initialize Endpoint
11module.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 * List Automations
20 *
21 * GET /api/v2/automations.json
22 * https://developer.zendesk.com/rest_api/docs/support/automations#list-automations
23 */
24 list: () => {
25 // Ignore any options
26 return {
27 method: 'GET',
28 url: `${url}/api/v2/automations.json`,
29 headers
30 };
31 },
32
33 /**
34 * Show Automation
35 *
36 * GET /api/v2/automations/{id}.json
37 * https://developer.zendesk.com/rest_api/docs/support/automations#show-automation
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 * List Active Automations
55 *
56 * GET /api/v2/automations/active.json
57 * https://developer.zendesk.com/rest_api/docs/support/automations#list-active-automations
58 */
59 active: () => {
60 // Ignore any options
61 return {
62 method: 'GET',
63 url: `${url}/api/v2/automations/active.json`,
64 headers
65 };
66 },
67
68 /**
69 * Create Automation
70 *
71 * POST /api/v2/automations.json
72 * https://developer.zendesk.com/rest_api/docs/support/automations#create-automation
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 * Update Automation
91 *
92 * PUT /api/v2/automations/{id}.json
93 * https://developer.zendesk.com/rest_api/docs/support/automations#update-automation
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 * Update Many Automations
113 *
114 * PUT /api/v2/automations/update_many.json
115 * https://developer.zendesk.com/rest_api/docs/support/automations#update-many-automations
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 * Delete Automation
134 *
135 * DELETE /api/v2/automations/{id}.json
136 * https://developer.zendesk.com/rest_api/docs/support/automations#delete-automation
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 * Bulk Delete Automations
154 *
155 * DELETE /api/v2/automations/destroy_many.json
156 * https://developer.zendesk.com/rest_api/docs/support/automations#bulk-delete-automations
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 * Search Automations
174 *
175 * GET /api/v2/automations/search.json
176 * https://developer.zendesk.com/rest_api/docs/support/automations#search-automations
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};