1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _object_type = Joi.string().min(1);
|
6 | const _type = Joi.string().min(1);
|
7 | const _id = Joi.number().min(1);
|
8 | const _ids = Joi.string().min(1);
|
9 | const _relationship_type = Joi.string().min(1);
|
10 | const _external_id = Joi.string().min(1);
|
11 | const _external_ids = Joi.string().min(1);
|
12 | const _data = Joi.object();
|
13 |
|
14 |
|
15 | module.exports = (options = {}) => {
|
16 | const { error } = validate(options);
|
17 | if (error) throw new Error(error.details[0].message);
|
18 |
|
19 | const { url, headers } = prepare(options);
|
20 |
|
21 | return {
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | list: (options = {}) => {
|
35 | const { error } = Joi.object({
|
36 | object_type: _object_type,
|
37 | ids: _ids,
|
38 | id: _id,
|
39 | relationship_type: _relationship_type
|
40 | }).validate(options);
|
41 | if (error) throw new Error(error.details[0].message);
|
42 |
|
43 | const { object_type, ids, id, relationship_type } = options;
|
44 | if (!object_type && !ids && !(id && relationship_type))
|
45 | throw new Error(
|
46 | 'object_type, ids, or id with relationship_type required'
|
47 | );
|
48 |
|
49 | return {
|
50 | method: 'GET',
|
51 | url: `${url}/api/sunshine/objects/records${
|
52 | object_type
|
53 | ? `?type=${object_type}`
|
54 | : ids
|
55 | ? `?ids=${ids}`
|
56 | : `/${id}/related/${relationship_type}`
|
57 | }`,
|
58 | headers
|
59 | };
|
60 | },
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | show: (options = {}) => {
|
74 | const { error } = Joi.object({
|
75 | id: _id,
|
76 | object_type: _object_type,
|
77 | external_id: _external_id,
|
78 | external_ids: _external_ids
|
79 | }).validate(options);
|
80 | if (error) throw new Error(error.details[0].message);
|
81 |
|
82 | const { id, object_type, external_id, external_ids } = options;
|
83 | if (!id && !object_type) throw new Error('id or object_type required');
|
84 | if (object_type && !external_id && !external_ids)
|
85 | throw new Error(
|
86 | 'object_type requires either external_id or external_ids set'
|
87 | );
|
88 |
|
89 | return {
|
90 | method: 'GET',
|
91 | url: `${url}/api/sunshine/objects/records${
|
92 | id
|
93 | ? `/${id}`
|
94 | : `?type=${object_type}&external_id${
|
95 | external_id ? `=${external_id}` : `s=${external_ids}`
|
96 | }`
|
97 | }`,
|
98 | headers
|
99 | };
|
100 | },
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | create: (options = {}) => {
|
109 | const { error } = Joi.object({
|
110 | data: _data.required()
|
111 | }).validate(options);
|
112 | if (error) throw new Error(error.details[0].message);
|
113 |
|
114 | const { data } = options;
|
115 | return {
|
116 | method: 'POST',
|
117 | url: `${url}/api/sunshine/objects/records`,
|
118 | headers,
|
119 | data
|
120 | };
|
121 | },
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | update: (options = {}) => {
|
133 | const { error } = Joi.object({
|
134 | id: _id,
|
135 | data: _data.required()
|
136 | }).validate(options);
|
137 | if (error) throw new Error(error.details[0].message);
|
138 |
|
139 | const { id, data } = options;
|
140 | return {
|
141 | method: 'PATCH',
|
142 | url: `${url}/api/sunshine/objects/records${id ? `/${id}` : ''}`,
|
143 | headers,
|
144 | data
|
145 | };
|
146 | },
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | delete: (options = {}) => {
|
158 | const { error } = Joi.object({
|
159 | id: _id,
|
160 | external_id: _external_id,
|
161 | type: _type
|
162 | }).validate(options);
|
163 | if (error) throw new Error(error.details[0].message);
|
164 |
|
165 | const { id, external_id, type } = options;
|
166 | if (!id && !(external_id && type))
|
167 | throw new Error('id or external_id with type required');
|
168 |
|
169 | return {
|
170 | method: 'DELETE',
|
171 | url: `${url}/api/sunshine/objects/records${
|
172 | id ? `/${id}` : `?external_id=${external_id}&type=${type}`
|
173 | }`,
|
174 | headers
|
175 | };
|
176 | }
|
177 | };
|
178 | };
|