@veritree/services 0.8.1 → 0.11.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": "0.8.1",
3
+ "version": "0.11.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,4 +1,4 @@
1
- import { getCookie } from './cookies';
1
+ import { getCookie } from "./cookies";
2
2
 
3
3
  const Api = {
4
4
  baseUrl: `${process.env.API_VERITREE_URL}/api`,
@@ -8,12 +8,12 @@ const Api = {
8
8
  return await this.unWrap(url);
9
9
  },
10
10
 
11
- async post(url, data, isMultipart = false) {
12
- return await this.unWrap(url, 'post', data, isMultipart);
11
+ async post(url, data) {
12
+ return await this.unWrap(url, "post", data);
13
13
  },
14
14
 
15
- async update(url, data, isMultipart = false) {
16
- return await this.unWrap(url, 'put', data, isMultipart);
15
+ async update(url, data) {
16
+ return await this.unWrap(url, "put", data);
17
17
  },
18
18
 
19
19
  /**
@@ -24,9 +24,9 @@ const Api = {
24
24
  * @param {object} data
25
25
  * @returns {object} envelope
26
26
  */
27
- async unWrap(url, method = 'get', data, isMultipart) {
28
- if (this.forceEnvelopeResponse) url = this.addEvenlopeArgToUrl(url); // TODO: remove when API is fully migrated to envelopes
29
- const config = this.getConfig(method, data, isMultipart);
27
+ async unWrap(url, method = "get", data) {
28
+ if (this.forceEnvelopeResponse) url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
29
+ const config = this.getConfig(method, data);
30
30
 
31
31
  try {
32
32
  const response = await fetch(url, config);
@@ -45,22 +45,24 @@ const Api = {
45
45
  * @param {object} body
46
46
  * @returns {object} data
47
47
  */
48
- getConfig(method, data, isMultipart) {
49
- const contentType = isMultipart ? 'multipart/form-data' : 'application/json';
50
- const token = `Bearer ${getCookie('access_token')}`;
48
+ getConfig(method, data) {
49
+ const isGet = method === "get";
50
+ const isFormData = this._isFormData(data);
51
+ const accessToken = `Bearer ${getCookie('access_token')}`;
51
52
 
52
53
  const config = {
53
54
  method,
54
55
  headers: {
55
- 'Content-Type': contentType,
56
- Authorization: token
56
+ Authorization: accessToken,
57
57
  },
58
58
  };
59
59
 
60
- if (method !== 'get' && !isMultipart) {
61
- config.body = JSON.stringify(data);
62
- } else {
63
- config.body = data;
60
+ if(!isFormData) {
61
+ config.headers['Content-Type'] = 'application/json';
62
+ }
63
+
64
+ if (!isGet) {
65
+ isFormData ? (config.body = data) : (config.body = JSON.stringify(data));
64
66
  }
65
67
 
66
68
  return config;
@@ -72,13 +74,18 @@ const Api = {
72
74
  * @param {string} url
73
75
  * @returns {string} url
74
76
  */
75
- addEvenlopeArgToUrl(url) {
76
- if (!url || url.includes('_result=1')) return;
77
- const urlHasArgs = url.includes('?');
78
- const urlEvenlopeArg = urlHasArgs ? '&_result=1' : '?_result=1';
77
+ handleEnvelopParam(url) {
78
+ if (!url || url.includes("_result=1")) return url;
79
+
80
+ const urlHasArgs = url.includes("?");
81
+ const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
79
82
 
80
83
  return `${url}${urlEvenlopeArg}`;
81
84
  },
85
+
86
+ _isFormData(data) {
87
+ return data instanceof FormData;
88
+ },
82
89
  };
83
90
 
84
91
  export default Api;
@@ -0,0 +1,7 @@
1
+ export const getSessionOrgId = () => {
2
+ return getSession().orgId;
3
+ };
4
+
5
+ export const getSession = () => {
6
+ return JSON.parse(localStorage.getItem('session'));
7
+ };
@@ -1,52 +1,90 @@
1
- import Api from '../helpers/api';
1
+ import Api from "../helpers/api";
2
+ import { getSession } from "../helpers/session";
2
3
 
3
- export const createFieldUpdatesApiService = (orgId, orgType) => {
4
+ export const createFieldUpdatesApiService = () => {
4
5
  const resource = `field-updates`;
6
+ const { orgId, orgType } = getSession();
7
+
8
+ if (!orgId && !orgType) {
9
+ throw new Error('Organization id and type are required');
10
+ }
5
11
 
6
12
  const get = {
7
- async all(countryId, regionId, subsiteId, page = 1, pageSize = 10) {
13
+ async all(countryId, regionId, subsiteId, page = 1, orderBy = 'date_planted-desc', pageSize = 10) {
8
14
  const params = `${_getParams(
9
15
  countryId,
10
16
  regionId,
11
17
  subsiteId,
12
18
  page,
19
+ orderBy,
13
20
  pageSize
14
21
  )}`;
15
22
  const url = `${_getUrl()}?${params}`;
16
23
 
17
24
  return await Api.get(url);
18
25
  },
26
+
27
+ async specific(id) {
28
+ const url = `${_getUrl()}/${id}?${_getParams()}`;
29
+
30
+ return await Api.get(url);
31
+ }
19
32
  };
20
33
 
21
34
  /**
22
- *
23
- * @param {string} fieldUpdateId
24
- * @param {object} data
25
- * @returns
35
+ *
36
+ * @param {object} data
37
+ * @returns
26
38
  */
27
- const update = (fieldUpdateId, data, isMultipart = false) => {
39
+ const post = (data) => {
40
+ const url = `${_getUrl()}?${_getParams()}`;
41
+
42
+ return Api.post(url, data);
43
+ };
44
+
45
+ /**
46
+ *
47
+ * @param {string} fieldUpdateId
48
+ * @param {object} data
49
+ * @returns
50
+ */
51
+ const update = (fieldUpdateId, data) => {
28
52
  const url = `${_getUrl()}/${fieldUpdateId}?${_getParams()}`;
29
53
 
30
- return Api.update(url, data, isMultipart);
54
+ return Api.post(url, data);
55
+ };
56
+
57
+ /**
58
+ *
59
+ */
60
+ const remove = {
61
+ async img(fieldUpdateId, data) {
62
+ const url = `${_getUrl()}/${fieldUpdateId}/images/detach?${_getParams()}`;
63
+
64
+ return await Api.post(url, data);
65
+ }
31
66
  }
32
67
 
33
68
  const _getUrl = () => {
34
69
  return `${Api.baseUrl}/${resource}`;
35
70
  };
36
71
 
37
- const _getParams = (countryId, regionId, subsiteId, page, pageSize) => {
38
- const countryIdParam = countryId ? `&country_id=${countryId}` : '';
39
- const regionIdParam = regionId ? `&region_id=${regionId}` : '';
40
- const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : '';
41
- const pageParam = page ? `&page=${page}` : '';
42
- const pageSizeParam = pageSize ? `&page_size=${pageSize}` : '';
43
- const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}`;
72
+ const _getParams = (countryId, regionId, subsiteId, page, orderBy, pageSize) => {
73
+ const countryIdParam = countryId ? `&country_id=${countryId}` : "";
74
+ const regionIdParam = regionId ? `&region_id=${regionId}` : "";
75
+ const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : "";
76
+ const pageParam = page ? `&page=${page}` : "";
77
+ const orderByParam = orderBy ? `&orderBy=${orderBy}` : "";
78
+ const pageSizeParam = pageSize ? `&page_size=${pageSize}` : "";
79
+ const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}${orderByParam}`;
44
80
 
45
81
  return `org_id=${orgId}&org_type=${orgType}${params}`;
46
82
  };
47
83
 
48
84
  return {
49
85
  get,
50
- update
86
+ post,
87
+ update,
88
+ remove
51
89
  };
52
90
  };
@@ -1,8 +1,13 @@
1
1
  import Api from '../helpers/api';
2
+ import { getSession } from "../helpers/session";
2
3
 
3
- export const createForestTypesApiService = (orgId, orgType) => {
4
- if (!orgId || !orgType) throw new Error('orgId and orgType are required');
4
+ export const createForestTypesApiService = () => {
5
5
  const resource = 'forest-types';
6
+ const { orgId, orgType } = getSession();
7
+
8
+ if (!orgId && !orgType) {
9
+ throw new Error('Organization id and type are required');
10
+ }
6
11
 
7
12
  const get = {
8
13
  /**
@@ -1,8 +1,13 @@
1
1
  import Api from "../helpers/api";
2
+ import { getSession } from "../helpers/session";
2
3
 
3
- export const createImagesApiService = (orgId, orgType) => {
4
- if (!orgId && !orgType) throw new Error("orgId and orgType are required");
4
+ export const createImagesApiService = () => {
5
5
  const resource = "images";
6
+ const { orgId, orgType } = getSession();
7
+
8
+ if (!orgId && !orgType) {
9
+ throw new Error('Organization id and type are required');
10
+ }
6
11
 
7
12
  const get = {
8
13
  async all(
@@ -23,6 +28,12 @@ export const createImagesApiService = (orgId, orgType) => {
23
28
  )}`;
24
29
  return await Api.get(url);
25
30
  },
31
+
32
+ async specific(id) {
33
+ const url = `${_getUrl()}/${id}?${_getParams()}`;
34
+
35
+ return await Api.get(url);
36
+ }
26
37
  };
27
38
 
28
39
  const _getUrl = () => {
@@ -1,8 +1,13 @@
1
1
  import Api from '../helpers/api';
2
+ import { getSession } from "../helpers/session";
2
3
 
3
- export const createRegionsApiService = (orgId, orgType) => {
4
- if (!orgId && !orgType) throw new Error('orgId and orgType are required');
4
+ export const createRegionsApiService = () => {
5
5
  const resource = 'regions';
6
+ const { orgId, orgType } = getSession();
7
+
8
+ if (!orgId && !orgType) {
9
+ throw new Error('Organization id and type are required');
10
+ }
6
11
 
7
12
  const get = {
8
13
  async all(page = 1, pageSize = 10) {
@@ -1,8 +1,13 @@
1
1
  import Api from '../helpers/api';
2
+ import { getSession } from "../helpers/session";
2
3
 
3
- export const createSubsitesApiService = (orgId, orgType) => {
4
- if (!orgId && !orgType) throw new Error('orgId is required');
4
+ export const createSubsitesApiService = () => {
5
5
  const resource = 'subsites';
6
+ const { orgId, orgType } = getSession();
7
+
8
+ if (!orgId && !orgType) {
9
+ throw new Error('Organization id and type are required');
10
+ }
6
11
 
7
12
  const get = {
8
13
  /**