1 | const validate = require("./validate");
|
2 |
|
3 | module.exports = ({ instance, headers }) => {
|
4 | const url = `https://${instance}.zendesk.com`;
|
5 |
|
6 | return {
|
7 | list: (options = {}) => {
|
8 | const { error } = validate.list(options);
|
9 | if (error) throw new Error(error.details[0].message);
|
10 |
|
11 | const { user_id, organization_id } = options;
|
12 | if (user_id && organization_id)
|
13 | throw new Error(
|
14 | 'either "user_id" or "organization_id" may be set, not both'
|
15 | );
|
16 |
|
17 | return {
|
18 | method: "GET",
|
19 | url: `${url}/api/v2/${
|
20 | user_id
|
21 | ? `users/${user_id}/organization_subscriptions.json`
|
22 | : organization_id
|
23 | ? `organizations/${organization_id}/subscriptions.json`
|
24 | : `organization_subscriptions.json`
|
25 | }`,
|
26 | headers
|
27 | };
|
28 | },
|
29 |
|
30 | show: (options = {}) => {
|
31 | const { error } = validate.show(options);
|
32 | if (error) throw new Error(error.details[0].message);
|
33 |
|
34 | const { id } = options;
|
35 | return {
|
36 | method: "GET",
|
37 | url: `${url}/api/v2/organization_subscriptions/${id}.json`,
|
38 | headers
|
39 | };
|
40 | },
|
41 |
|
42 | create: (options = {}) => {
|
43 | const { error } = validate.create(options);
|
44 | if (error) throw new Error(error.details[0].message);
|
45 |
|
46 | const { data } = options;
|
47 | return {
|
48 | method: "POST",
|
49 | url: `${url}/api/v2/organization_subscriptions.json`,
|
50 | headers,
|
51 | data
|
52 | };
|
53 | },
|
54 |
|
55 | delete: (options = {}) => {
|
56 | const { error } = validate.delete(options);
|
57 | if (error) throw new Error(error.details[0].message);
|
58 |
|
59 | const { id } = options;
|
60 | return {
|
61 | method: "DELETE",
|
62 | url: `${url}/api/v2/organization_subscriptions/${id}.json`,
|
63 | headers
|
64 | };
|
65 | }
|
66 | };
|
67 | };
|