From 3773f3e3e8f1146eea0b39853b6ec6d5c1f44677 Mon Sep 17 00:00:00 2001 From: k2maan Date: Wed, 4 Oct 2023 14:53:06 +0530 Subject: [PATCH 1/3] Implemented: services to set and get user preferences --- src/index.ts | 6 ++++-- src/modules/index.ts | 6 ++++-- src/modules/user/index.ts | 43 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index d81481c..752fdc0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { events, Product, Response, Stock, Order, OrderItem, OrderPart, OPERATOR, User } from './types' import api, { client, getConfig, init, initialise, resetConfig, updateToken, updateInstanceUrl } from './api' import { hasError, isError } from './util' -import { getNotificationEnumIds, getNotificationUserPrefTypeIds, getUserFacilities, fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStock, fetchProductsStockAtFacility, getOrderDetails, getProductIdentificationPref, getProfile, logout, removeClientRegistrationToken, setProductIdentificationPref, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, updateOrderStatus } from './modules' +import { getNotificationEnumIds, getNotificationUserPrefTypeIds, getUserFacilities, fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStock, fetchProductsStockAtFacility, getOrderDetails, getProductIdentificationPref, getProfile, logout, removeClientRegistrationToken, setProductIdentificationPref, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, updateOrderStatus, getUserPreference, setUserPreference } from './modules' export { api, @@ -40,5 +40,7 @@ export { storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, - User + User, + getUserPreference, + setUserPreference, } \ No newline at end of file diff --git a/src/modules/index.ts b/src/modules/index.ts index 22c8623..9f11868 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,6 +1,6 @@ import { getOrderDetails, updateOrderStatus } from '../modules/order' import { fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent } from '../modules/product' -import { getProductIdentificationPref, getProfile, getUserFacilities, logout, setProductIdentificationPref } from '../modules/user' +import { getProductIdentificationPref, getProfile, getUserFacilities, getUserPreference, logout, setProductIdentificationPref, setUserPreference } from '../modules/user' import { getNotificationEnumIds, getNotificationUserPrefTypeIds, removeClientRegistrationToken, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic } from '../modules/notification' import { fetchProductsStock, fetchProductsStockAtFacility } from '../modules/stock' @@ -22,5 +22,7 @@ export { subscribeTopic, unsubscribeTopic, getUserFacilities, - updateOrderStatus + updateOrderStatus, + getUserPreference, + setUserPreference } \ No newline at end of file diff --git a/src/modules/user/index.ts b/src/modules/user/index.ts index 259cfc2..1dcb5ee 100644 --- a/src/modules/user/index.ts +++ b/src/modules/user/index.ts @@ -233,10 +233,51 @@ async function getUserFacilities(token: any, baseURL: string, partyId: string, f } } +async function setUserPreference(payload: any): Promise { + try { + const resp: any = await api({ + url: "service/setUserPreference", + method: "post", + data: payload + }); + + if (!hasError(resp)) { + return Promise.resolve(resp.data) + } else { + throw resp.data + } + } catch (err) { + return Promise.reject(err) + } +} + +async function getUserPreference(token: any, baseURL: string, userPrefTypeId: string): Promise { + try { + const resp = await client({ + url: "service/getUserPreference", + method: "post", + data: { userPrefTypeId }, + baseURL, + headers: { + Authorization: 'Bearer ' + token, + 'Content-Type': 'application/json' + } + }); + if (hasError(resp)) { + return Promise.reject(resp.data); + } + return Promise.resolve(resp.data.userPrefValue ? JSON.parse(resp.data.userPrefValue) : {}); + } catch (err) { + return Promise.reject(err) + } +} + export { + getUserFacilities, + getUserPreference, getProductIdentificationPref, getProfile, logout, setProductIdentificationPref, - getUserFacilities + setUserPreference } \ No newline at end of file From 5981019a4ed35ee8208a6a1c218f8be4b5b4af32 Mon Sep 17 00:00:00 2001 From: k2maan Date: Wed, 4 Oct 2023 14:55:12 +0530 Subject: [PATCH 2/3] Improved: spacing and method sequence --- src/index.ts | 2 +- src/modules/user/index.ts | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index 752fdc0..ddb2ae5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { events, Product, Response, Stock, Order, OrderItem, OrderPart, OPERATOR, User } from './types' import api, { client, getConfig, init, initialise, resetConfig, updateToken, updateInstanceUrl } from './api' import { hasError, isError } from './util' -import { getNotificationEnumIds, getNotificationUserPrefTypeIds, getUserFacilities, fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStock, fetchProductsStockAtFacility, getOrderDetails, getProductIdentificationPref, getProfile, logout, removeClientRegistrationToken, setProductIdentificationPref, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, updateOrderStatus, getUserPreference, setUserPreference } from './modules' +import { getNotificationEnumIds, getNotificationUserPrefTypeIds, getUserFacilities, fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStock, fetchProductsStockAtFacility, getOrderDetails, getProductIdentificationPref, getProfile, logout, removeClientRegistrationToken, setProductIdentificationPref, storeClientRegistrationToken, subscribeTopic, unsubscribeTopic, updateOrderStatus, getUserPreference, setUserPreference } from './modules' export { api, diff --git a/src/modules/user/index.ts b/src/modules/user/index.ts index 1dcb5ee..2b0ba5e 100644 --- a/src/modules/user/index.ts +++ b/src/modules/user/index.ts @@ -233,24 +233,6 @@ async function getUserFacilities(token: any, baseURL: string, partyId: string, f } } -async function setUserPreference(payload: any): Promise { - try { - const resp: any = await api({ - url: "service/setUserPreference", - method: "post", - data: payload - }); - - if (!hasError(resp)) { - return Promise.resolve(resp.data) - } else { - throw resp.data - } - } catch (err) { - return Promise.reject(err) - } -} - async function getUserPreference(token: any, baseURL: string, userPrefTypeId: string): Promise { try { const resp = await client({ @@ -272,6 +254,24 @@ async function getUserPreference(token: any, baseURL: string, userPrefTypeId: st } } +async function setUserPreference(payload: any): Promise { + try { + const resp: any = await api({ + url: "service/setUserPreference", + method: "post", + data: payload + }); + + if (!hasError(resp)) { + return Promise.resolve(resp.data) + } else { + throw resp.data + } + } catch (err) { + return Promise.reject(err) + } +} + export { getUserFacilities, getUserPreference, From a5c97020383415adaf0bb8ee3f74bfa79e869665 Mon Sep 17 00:00:00 2001 From: k2maan Date: Wed, 4 Oct 2023 17:54:32 +0530 Subject: [PATCH 3/3] Improved: getUserPreference method by throwing resp.data on error --- src/modules/user/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/user/index.ts b/src/modules/user/index.ts index 2b0ba5e..0e7b02a 100644 --- a/src/modules/user/index.ts +++ b/src/modules/user/index.ts @@ -246,7 +246,7 @@ async function getUserPreference(token: any, baseURL: string, userPrefTypeId: st } }); if (hasError(resp)) { - return Promise.reject(resp.data); + throw resp.data } return Promise.resolve(resp.data.userPrefValue ? JSON.parse(resp.data.userPrefValue) : {}); } catch (err) {