@veritree/services 0.7.0 → 0.10.2

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.7.0",
3
+ "version": "0.10.2",
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`,
@@ -9,11 +9,11 @@ const Api = {
9
9
  },
10
10
 
11
11
  async post(url, data) {
12
- return await this.unWrap(url, 'post', data);
12
+ return await this.unWrap(url, "post", data);
13
13
  },
14
14
 
15
15
  async update(url, data) {
16
- return await this.unWrap(url, 'put', data);
16
+ return await this.unWrap(url, "put", data);
17
17
  },
18
18
 
19
19
  /**
@@ -24,8 +24,8 @@ const Api = {
24
24
  * @param {object} data
25
25
  * @returns {object} envelope
26
26
  */
27
- async unWrap(url, method = 'get', data) {
28
- if (this.forceEnvelopeResponse) url = this.addEvenlopeArgToUrl(url); // TODO: remove when API is fully migrated to envelopes
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
29
  const config = this.getConfig(method, data);
30
30
 
31
31
  try {
@@ -46,15 +46,24 @@ const Api = {
46
46
  * @returns {object} data
47
47
  */
48
48
  getConfig(method, data) {
49
+ const isGet = method === "get";
50
+ const isFormData = this._isFormData(data);
51
+ const accessToken = `Bearer ${getCookie('access_token')}`;
52
+
49
53
  const config = {
50
54
  method,
51
55
  headers: {
52
- 'Content-Type': 'application/json',
53
- Authorization: `Bearer ${getCookie('access_token')}`,
56
+ Authorization: accessToken,
54
57
  },
55
58
  };
56
59
 
57
- if (method !== 'get') config.body = JSON.stringify(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));
66
+ }
58
67
 
59
68
  return config;
60
69
  },
@@ -65,13 +74,18 @@ const Api = {
65
74
  * @param {string} url
66
75
  * @returns {string} url
67
76
  */
68
- addEvenlopeArgToUrl(url) {
69
- if (!url || url.includes('_result=1')) return;
70
- const urlHasArgs = url.includes('?');
71
- 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";
72
82
 
73
83
  return `${url}${urlEvenlopeArg}`;
74
84
  },
85
+
86
+ _isFormData(data) {
87
+ return data instanceof FormData;
88
+ },
75
89
  };
76
90
 
77
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,39 +1,78 @@
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
 
34
+ /**
35
+ *
36
+ * @param {string} fieldUpdateId
37
+ * @param {object} data
38
+ * @returns
39
+ */
40
+ const update = (fieldUpdateId, data) => {
41
+ const url = `${_getUrl()}/${fieldUpdateId}?${_getParams()}`;
42
+
43
+ return Api.post(url, data);
44
+ };
45
+
46
+ /**
47
+ *
48
+ */
49
+ const remove = {
50
+ async img(fieldUpdateId, data) {
51
+ const url = `${_getUrl()}/${fieldUpdateId}/images/detach?${_getParams()}`;
52
+
53
+ return await Api.post(url, data);
54
+ }
55
+ }
56
+
21
57
  const _getUrl = () => {
22
58
  return `${Api.baseUrl}/${resource}`;
23
59
  };
24
60
 
25
- const _getParams = (countryId, regionId, subsiteId, page, pageSize) => {
26
- const countryIdParam = countryId ? `&country_id=${countryId}` : '';
27
- const regionIdParam = regionId ? `&region_id=${regionId}` : '';
28
- const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : '';
29
- const pageParam = page ? `&page=${page}` : '';
30
- const pageSizeParam = pageSize ? `&page_size=${pageSize}` : '';
31
- const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}`;
61
+ const _getParams = (countryId, regionId, subsiteId, page, orderBy, pageSize) => {
62
+ const countryIdParam = countryId ? `&country_id=${countryId}` : "";
63
+ const regionIdParam = regionId ? `&region_id=${regionId}` : "";
64
+ const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : "";
65
+ const pageParam = page ? `&page=${page}` : "";
66
+ const orderByParam = orderBy ? `&orderBy=${orderBy}` : "";
67
+ const pageSizeParam = pageSize ? `&page_size=${pageSize}` : "";
68
+ const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}${orderByParam}`;
32
69
 
33
70
  return `org_id=${orgId}&org_type=${orgType}${params}`;
34
71
  };
35
72
 
36
73
  return {
37
74
  get,
75
+ update,
76
+ remove
38
77
  };
39
78
  };
@@ -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 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
  /**