1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _id = Joi.number().min(1);
|
6 | const _user_id = Joi.number().min(1);
|
7 | const _organization_id = Joi.number().min(1);
|
8 | const _data = Joi.object();
|
9 |
|
10 |
|
11 | module.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 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
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 |
|
54 |
|
55 |
|
56 |
|
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 |
|
74 |
|
75 |
|
76 |
|
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 |
|
95 |
|
96 |
|
97 |
|
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 | };
|