UNPKG

3.4 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _user_id = Joi.number().min(1);
7const _organization_id = Joi.number().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 Organization Subscriptions
20 *
21 * GET /api/v2/organizations/{organization_id}/subscriptions.json
22 * GET /api/v2/organization_subscriptions.json
23 * GET /api/v2/users/{user_id}/organization_subscriptions.json
24 * https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#list-organization-subscriptions
25 */
26 list: (options = {}) => {
27 const { error } = Joi.object({
28 user_id: _user_id,
29 organization_id: _organization_id
30 }).validate(options);
31 if (error) throw new Error(error.details[0].message);
32
33 const { user_id, organization_id } = options;
34 if (user_id && organization_id)
35 throw new Error(
36 'either "user_id" or "organization_id" may be set, not both'
37 );
38
39 return {
40 method: 'GET',
41 url: `${url}/api/v2/${
42 user_id
43 ? `users/${user_id}/organization_subscriptions.json`
44 : organization_id
45 ? `organizations/${organization_id}/subscriptions.json`
46 : `organization_subscriptions.json`
47 }`,
48 headers
49 };
50 },
51
52 /**
53 * Show Organization Subscription
54 *
55 * GET /api/v2/organization_subscriptions/{id}.json
56 * https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#show-organization-subscription
57 */
58 show: (options = {}) => {
59 const { error } = Joi.object({
60 id: _id.required()
61 }).validate(options);
62 if (error) throw new Error(error.details[0].message);
63
64 const { id } = options;
65 return {
66 method: 'GET',
67 url: `${url}/api/v2/organization_subscriptions/${id}.json`,
68 headers
69 };
70 },
71
72 /**
73 * Create Organization Subscription
74 *
75 * POST /api/v2/organization_subscriptions.json
76 * https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#create-organization-subscription
77 */
78 create: (options = {}) => {
79 const { error } = Joi.object({
80 data: _data.required()
81 }).validate(options);
82 if (error) throw new Error(error.details[0].message);
83
84 const { data } = options;
85 return {
86 method: 'POST',
87 url: `${url}/api/v2/organization_subscriptions.json`,
88 headers,
89 data
90 };
91 },
92
93 /**
94 * Delete Organization Subscription
95 *
96 * DELETE /api/v2/organization_subscriptions/{id}.json
97 * https://developer.zendesk.com/rest_api/docs/support/organization_subscriptions#delete-organization-subscription
98 */
99 delete: (options = {}) => {
100 const { error } = Joi.object({
101 id: _id.required()
102 }).validate(options);
103 if (error) throw new Error(error.details[0].message);
104
105 const { id } = options;
106 return {
107 method: 'DELETE',
108 url: `${url}/api/v2/organization_subscriptions/${id}.json`,
109 headers
110 };
111 }
112 };
113};