@veritree/services 2.18.1-7 → 2.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/endpoints/orgs.js +23 -0
- package/src/helpers/api.js +26 -40
package/package.json
CHANGED
package/src/endpoints/orgs.js
CHANGED
|
@@ -33,6 +33,29 @@ class OrgsApi extends Api {
|
|
|
33
33
|
return await this.get(url);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
siteManagers(orgId) {
|
|
37
|
+
const locations = () => {
|
|
38
|
+
const all = async(userId) => {
|
|
39
|
+
const url = `${this.getUrl()}/${orgId}/site-managers/${userId}/locations${this.getUrlParams()}`;
|
|
40
|
+
return await this.get(url);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const update = async (data) => {
|
|
44
|
+
const url = `${this.getUrl()}/${orgId}/site-managers/locations${this.getUrlParams()}`;
|
|
45
|
+
return await this.post(url, data, 'put');
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
all,
|
|
50
|
+
update
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
locations
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
36
59
|
_geStatstUrl(isPublic) {
|
|
37
60
|
const endpoint = isPublic ? "pstats" : "stats";
|
|
38
61
|
return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
|
package/src/helpers/api.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { getCookie } from
|
|
2
|
-
import { createParamsStringFromArgs } from
|
|
3
|
-
import { getSession } from
|
|
1
|
+
import { getCookie } from './cookies';
|
|
2
|
+
import { createParamsStringFromArgs } from '../utils/args';
|
|
3
|
+
import { getSession } from './session';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Adds the envelope argument to the url
|
|
@@ -9,10 +9,10 @@ import { getSession } from "./session";
|
|
|
9
9
|
* @returns {string} url
|
|
10
10
|
*/
|
|
11
11
|
function addVersionArg(url) {
|
|
12
|
-
if (!url || url.includes(
|
|
12
|
+
if (!url || url.includes('_result=1')) return url;
|
|
13
13
|
|
|
14
|
-
const version
|
|
15
|
-
const urlHasArgs = url.includes(
|
|
14
|
+
const version = process.env ? process.env.API_VERITREE_VERSION ? process.env.API_VERITREE_VERSION : '5.0.0' : '5.0.0' || '5.0.0';
|
|
15
|
+
const urlHasArgs = url.includes('?');
|
|
16
16
|
const urlVersionArg = urlHasArgs ? `&_v=${version}` : `?_v=${version}`;
|
|
17
17
|
|
|
18
18
|
return `${url}${urlVersionArg}`;
|
|
@@ -26,10 +26,10 @@ function addVersionArg(url) {
|
|
|
26
26
|
* @returns {object} data
|
|
27
27
|
*/
|
|
28
28
|
function getConfig(method, data, as) {
|
|
29
|
-
const isGet = method ===
|
|
29
|
+
const isGet = method === 'get';
|
|
30
30
|
const isSpoofing = as;
|
|
31
31
|
const isFormData = data instanceof FormData;
|
|
32
|
-
const accessToken = `Bearer ${getCookie(
|
|
32
|
+
const accessToken = `Bearer ${getCookie('access_token')}`;
|
|
33
33
|
|
|
34
34
|
const config = {
|
|
35
35
|
method,
|
|
@@ -39,7 +39,7 @@ function getConfig(method, data, as) {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
if (!isFormData) {
|
|
42
|
-
config.headers[
|
|
42
|
+
config.headers['Content-Type'] = 'application/json';
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
// TODO: improve this ifs and elses
|
|
@@ -47,7 +47,7 @@ function getConfig(method, data, as) {
|
|
|
47
47
|
if (!data) data = {};
|
|
48
48
|
|
|
49
49
|
if (isFormData) {
|
|
50
|
-
if (isSpoofing) data.set(
|
|
50
|
+
if (isSpoofing) data.set('_method', as.toUpperCase());
|
|
51
51
|
config.body = data;
|
|
52
52
|
} else {
|
|
53
53
|
if (isSpoofing) data._method = as.toUpperCase();
|
|
@@ -60,8 +60,8 @@ function getConfig(method, data, as) {
|
|
|
60
60
|
|
|
61
61
|
export default class Api {
|
|
62
62
|
constructor(resource) {
|
|
63
|
-
this.baseUrl = null;
|
|
64
|
-
this.resource = resource ? resource :
|
|
63
|
+
this.baseUrl = process.env ? `${process.env.API_VERITREE_URL}/api` : null;
|
|
64
|
+
this.resource = resource ? resource : '';
|
|
65
65
|
this.orgId = null;
|
|
66
66
|
this.orgType = null;
|
|
67
67
|
}
|
|
@@ -106,7 +106,7 @@ export default class Api {
|
|
|
106
106
|
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
107
107
|
* @returns {promise}
|
|
108
108
|
*/
|
|
109
|
-
async update(id, data, as =
|
|
109
|
+
async update(id, data, as = 'put', args) {
|
|
110
110
|
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
111
111
|
return await this.post(url, data, as);
|
|
112
112
|
}
|
|
@@ -120,7 +120,7 @@ export default class Api {
|
|
|
120
120
|
*/
|
|
121
121
|
async delete(id, args) {
|
|
122
122
|
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
123
|
-
return await this.post(url, null,
|
|
123
|
+
return await this.post(url, null, 'delete');
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
/**
|
|
@@ -140,7 +140,7 @@ export default class Api {
|
|
|
140
140
|
*/
|
|
141
141
|
async post(url, data, as, args) {
|
|
142
142
|
if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
143
|
-
return await this.unWrap(url,
|
|
143
|
+
return await this.unWrap(url, 'post', data, as);
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
// ----------
|
|
@@ -151,7 +151,7 @@ export default class Api {
|
|
|
151
151
|
* @returns
|
|
152
152
|
*/
|
|
153
153
|
getUrl(id) {
|
|
154
|
-
id = id ? `/${id}` :
|
|
154
|
+
id = id ? `/${id}` : '';
|
|
155
155
|
return `${this.baseUrl}/${this.resource}${id}`;
|
|
156
156
|
}
|
|
157
157
|
|
|
@@ -164,14 +164,14 @@ export default class Api {
|
|
|
164
164
|
this.setOrg();
|
|
165
165
|
let isOrgLess = false;
|
|
166
166
|
let isOrgAs = false;
|
|
167
|
-
let orgIdParam =
|
|
168
|
-
let orgTypeParam =
|
|
167
|
+
let orgIdParam = '';
|
|
168
|
+
let orgTypeParam = '';
|
|
169
169
|
|
|
170
170
|
// while most of endpoints require an org id and type, some endpoints do not
|
|
171
171
|
if (args) {
|
|
172
|
-
isOrgLess = Object.hasOwn(args,
|
|
172
|
+
isOrgLess = Object.hasOwn(args, 'orgless');
|
|
173
173
|
isOrgAs =
|
|
174
|
-
Object.hasOwn(args,
|
|
174
|
+
Object.hasOwn(args, 'org_id_as') && Object.hasOwn(args, 'org_type_as');
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
// a super admin user can create/edit data from other orgs
|
|
@@ -201,34 +201,20 @@ export default class Api {
|
|
|
201
201
|
* @param {object} data
|
|
202
202
|
* @returns {object} envelope
|
|
203
203
|
*/
|
|
204
|
-
async unWrap(url, method =
|
|
204
|
+
async unWrap(url, method = 'get', data, as) {
|
|
205
205
|
url = addVersionArg(url);
|
|
206
206
|
const config = getConfig(method, data, as);
|
|
207
207
|
const response = await fetch(url, config);
|
|
208
208
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return new Promise(async (resolve, reject) => {
|
|
209
|
+
return new Promise((resolve, reject) => {
|
|
212
210
|
if (response.ok && response.status === 204) {
|
|
213
211
|
resolve();
|
|
214
212
|
return;
|
|
215
213
|
}
|
|
216
214
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
response.json().then((json) => {
|
|
221
|
-
response.ok ? resolve(json) : reject(json);
|
|
222
|
-
});
|
|
223
|
-
} else {
|
|
224
|
-
const buff = await response.arrayBuffer().then(Buffer.from)
|
|
225
|
-
|
|
226
|
-
try {
|
|
227
|
-
resolve(buff.toString());
|
|
228
|
-
} catch (err) {
|
|
229
|
-
reject();
|
|
230
|
-
}
|
|
231
|
-
}
|
|
215
|
+
response.json().then((json) => {
|
|
216
|
+
response.ok ? resolve(json) : reject(json);
|
|
217
|
+
});
|
|
232
218
|
});
|
|
233
219
|
}
|
|
234
220
|
|
|
@@ -239,4 +225,4 @@ export default class Api {
|
|
|
239
225
|
this.orgId = orgId;
|
|
240
226
|
this.orgType = orgType;
|
|
241
227
|
}
|
|
242
|
-
}
|
|
228
|
+
}
|