@veritree/services 1.5.0 → 2.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "1.5.0",
3
+ "version": "2.1.0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,5 +1,16 @@
1
1
  import Api from "../helpers/api";
2
2
 
3
- const resource = "field-updates";
3
+ class FieldUpdatesApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = "field-updates";
7
+ }
4
8
 
5
- export const FieldUpdates = new Api(resource);
9
+ async images(fieldUpdateId, args) {
10
+ const url = `${this.getUrl()}/${fieldUpdateId}/images${this.getUrlParams(args)}`;
11
+
12
+ return await this.get(url);
13
+ }
14
+ }
15
+
16
+ export const FieldUpdates = new FieldUpdatesApi();
@@ -6,6 +6,12 @@ class ImagesApi extends Api {
6
6
  this.resource = "images";
7
7
  }
8
8
 
9
+ rotate(id, data) {
10
+ const url = `${id}/rotate${this.getUrlParams()}`;
11
+
12
+ return this.update(url, data, 'patch');
13
+ }
14
+
9
15
  relation(prefix, id) {
10
16
  return Relations(this, prefix, id);
11
17
  }
@@ -1,4 +1,6 @@
1
1
  import Api from "../helpers/api";
2
+ import Team from "../helpers/team";
3
+ import { getSession } from "../helpers/session";
2
4
 
3
5
  class OrgsApi extends Api {
4
6
  constructor(resource) {
@@ -6,6 +8,16 @@ class OrgsApi extends Api {
6
8
  this.resource = "orgs";
7
9
  }
8
10
 
11
+ users() {
12
+ const { orgId } = getSession();
13
+ return Team(this, orgId);
14
+ }
15
+
16
+ async roles(orgId, params) {
17
+ const url = `${this.getUrl()}/${orgId}/roles${this.getUrlParams(params)}`;
18
+ return await this.get(url);
19
+ }
20
+
9
21
  async stats() {
10
22
  const url = `${this._geStatstUrl()}`;
11
23
  return await this.get(url);
@@ -8,13 +8,13 @@ import { getSession } from "./session";
8
8
  * @param {string} url
9
9
  * @returns {string} url
10
10
  */
11
- function addEnvelopeParam(url) {
11
+ function addVersionArg(url) {
12
12
  if (!url || url.includes("_result=1")) return url;
13
13
 
14
14
  const urlHasArgs = url.includes("?");
15
- const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
15
+ const urlVersionArg = urlHasArgs ? "&_v=5.0.0" : "?_v=5.0.0";
16
16
 
17
- return `${url}${urlEvenlopeArg}`;
17
+ return `${url}${urlVersionArg}`;
18
18
  }
19
19
 
20
20
  /**
@@ -26,7 +26,7 @@ function addEnvelopeParam(url) {
26
26
  */
27
27
  function getConfig(method, data, as) {
28
28
  const isGet = method === "get";
29
- const isSpoofing = as !== undefined;
29
+ const isSpoofing = as;
30
30
  const isFormData = data instanceof FormData;
31
31
  const accessToken = `Bearer ${getCookie("access_token")}`;
32
32
 
@@ -91,7 +91,7 @@ export default class Api {
91
91
  * @returns {promise}
92
92
  */
93
93
  async create(data, args) {
94
- return await this.post(null, data, args);
94
+ return await this.post(null, data, null, args);
95
95
  }
96
96
 
97
97
  /**
@@ -133,8 +133,10 @@ export default class Api {
133
133
  * @param {object} data
134
134
  * @returns {promise}
135
135
  */
136
- async post(url, data, as) {
137
- if (!url) url = `${this.getUrl()}${this.getUrlParams()}`;
136
+ async post(url, data, as, args) {
137
+ // console.log(args);
138
+ if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
139
+ console.log(url);
138
140
  return await this.unWrap(url, "post", data, as);
139
141
  }
140
142
 
@@ -158,15 +160,25 @@ export default class Api {
158
160
  getUrlParams(args) {
159
161
  this.setOrg();
160
162
  let isOrgLess = false;
161
- let orgIdParam = "";
162
- let orgTypeParam = "";
163
+ let isOrgAs = false;
164
+ let orgIdParam = null;
165
+ let orgTypeParam = null;
163
166
 
164
167
  // while most of endpoints require an org id and type, some endpoints do not
165
- if (args && args.length) {
166
- isOrgLess = Object.hasOwnProperty.call(...args, "orgless");
168
+ if (args) {
169
+ isOrgLess = Object.hasOwn(args, "orgless");
170
+ isOrgAs = Object.hasOwn(args, "org_id_as") && Object.hasOwn(args, "org_type_as");
171
+ }
172
+
173
+ // a super admin user can create/edit data from other orgs
174
+ // and for that, we can't use the org id and type in
175
+ // the session.
176
+ if(isOrgAs) {
177
+ orgIdParam = `org_id_as=${args.org_id_as}`;
178
+ orgTypeParam = `org_type_as=${args.org_type_as}`;
167
179
  }
168
180
 
169
- if (!isOrgLess) {
181
+ if (!isOrgLess && !isOrgAs) {
170
182
  if (this.orgId) orgIdParam = `org_id=${this.orgId}`;
171
183
  if (this.orgType) orgTypeParam = `&org_type=${this.orgType}`;
172
184
  }
@@ -183,7 +195,7 @@ export default class Api {
183
195
  * @returns {object} envelope
184
196
  */
185
197
  async unWrap(url, method = "get", data, as) {
186
- url = addEnvelopeParam(url);
198
+ url = addVersionArg(url);
187
199
  const config = getConfig(method, data, as);
188
200
  const response = await fetch(url, config);
189
201
 
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Creates an abstract team object that can be used
3
+ * for CRUD operations related to an org users.
4
+ *
5
+ * @param {object} api
6
+ * @param {number} id
7
+ * @returns {object}
8
+ */
9
+ const Team = (api, id) => {
10
+ let url = `${api.baseUrl}/${api.resource}/${id}/users`;
11
+
12
+ const all = async (args) => {
13
+ url = `${url}${api.getUrlParams(args)}`;
14
+ return await api.get(url);
15
+ };
16
+
17
+ const single = async (userId, args) => {
18
+ url = `${url}/${userId}${api.getUrlParams(args)}`;
19
+ return await api.get(url);
20
+ };
21
+
22
+ const create = async (data) => {
23
+ url = `${url}${api.getUrlParams()}`;
24
+ return await api.post(url, data);
25
+ }
26
+
27
+ const update = async (userId, data) => {
28
+ url = `${url}/${userId}${api.getUrlParams()}`;
29
+ return await api.update(url, data, 'patch');
30
+ }
31
+
32
+ const remove = async (userId) => {
33
+ url = `${url}/${userId}${api.getUrlParams()}`;
34
+ return await api.post(url, null, "delete");
35
+ }
36
+
37
+ return {
38
+ all,
39
+ single,
40
+ create,
41
+ update,
42
+ delete: remove
43
+ };
44
+ };
45
+
46
+ export default Team;