UNPKG

3.81 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _data = Joi.object();
7
8// Initialize Endpoint
9module.exports = (options = {}) => {
10 const { error } = validate(options);
11 if (error) throw new Error(error.details[0].message);
12
13 const { url, headers } = prepare(options);
14
15 return {
16 /**
17 * List SLA Policies
18 *
19 * GET /api/v2/slas/policies
20 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#list-sla-policies
21 */
22 list: () => {
23 // Ignore any options
24 return {
25 method: 'GET',
26 url: `${url}/api/v2/slas/policies`,
27 headers
28 };
29 },
30
31 /**
32 * Get SLA Policy
33 *
34 * GET /api/v2/slas/policies/{id}
35 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#get-sla-policy
36 */
37 get: (options = {}) => {
38 const { error } = Joi.object({
39 id: _id.required()
40 }).validate(options);
41 if (error) throw new Error(error.details[0].message);
42
43 const { id } = options;
44 return {
45 method: 'GET',
46 url: `${url}/api/v2/slas/policies/${id}`,
47 headers
48 };
49 },
50
51 /**
52 * Create SLA Policy
53 *
54 * POST /api/v2/slas/policies
55 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#create-sla-policy
56 */
57 create: (options = {}) => {
58 const { error } = Joi.object({
59 data: _data.required()
60 }).validate(options);
61 if (error) throw new Error(error.details[0].message);
62
63 const { data } = options;
64 return {
65 method: 'POST',
66 url: `${url}/api/v2/slas/policies`,
67 headers,
68 data
69 };
70 },
71
72 /**
73 * Update SLA Policy
74 *
75 * PUT /api/v2/slas/policies/{id}
76 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#update-sla-policy
77 */
78 update: (options = {}) => {
79 const { error } = Joi.object({
80 id: _id.required(),
81 data: _data.required()
82 }).validate(options);
83 if (error) throw new Error(error.details[0].message);
84
85 const { id, data } = options;
86 return {
87 method: 'PUT',
88 url: `${url}/api/v2/slas/policies/${id}`,
89 headers,
90 data
91 };
92 },
93
94 /**
95 * Delete SLA Policy
96 *
97 * DELETE /api/v2/slas/policies/{id}
98 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#delete-sla-policy
99 */
100 delete: (options = {}) => {
101 const { error } = Joi.object({
102 id: _id.required()
103 }).validate(options);
104 if (error) throw new Error(error.details[0].message);
105
106 const { id } = options;
107 return {
108 method: 'DELETE',
109 url: `${url}/api/v2/slas/policies/${id}`,
110 headers
111 };
112 },
113
114 /**
115 * Reorder SLA Policies
116 *
117 * PUT /api/v2/slas/policies/reorder.json
118 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#reorder-sla-policies
119 */
120 reorder: (options = {}) => {
121 const { error } = Joi.object({
122 data: _data.required()
123 }).validate(options);
124 if (error) throw new Error(error.details[0].message);
125
126 const { data } = options;
127 return {
128 method: 'PUT',
129 url: `${url}/api/v2/slas/policies/reorder.json`,
130 headers,
131 data
132 };
133 },
134
135 /**
136 * Retrieve supported filter definition items
137 *
138 * GET /api/v2/slas/policies/definitions.json
139 * https://developer.zendesk.com/rest_api/docs/support/sla_policies#retrieve-supported-filter-definition-items
140 */
141 definitions: () => {
142 // Ignore any options
143 return {
144 method: 'GET',
145 url: `${url}/api/v2/slas/policies/definitions.json`,
146 headers
147 };
148 }
149 };
150};