diff --git a/package.json b/package.json index 470f73d60..2e0159c5c 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@capacitor/core": "^2.4.7", "@capacitor/ios": "^2.5.0", "@hotwax/apps-theme": "^1.1.0", + "@hotwax/oms-api": "^1.3.0", "@ionic/core": "6.2.9", "@ionic/vue": "6.2.9", "@ionic/vue-router": "6.2.9", diff --git a/src/App.vue b/src/App.vue index 0dfae63e1..7f35f385e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -22,6 +22,8 @@ import OfflineHelper from "./offline-helper" import { useStore } from "./store"; import emitter from "@/event-bus" import { loadingController } from '@ionic/vue'; +import { init, resetConfig } from '@/adapter' +import { mapGetters } from 'vuex' export default defineComponent({ name: "App", @@ -33,7 +35,8 @@ export default defineComponent({ }, data() { return { - loader: null as any + loader: null as any, + maxAge: process.env.VUE_APP_CACHE_MAX_AGE ? parseInt(process.env.VUE_APP_CACHE_MAX_AGE) : 0 } }, methods: { @@ -64,10 +67,18 @@ export default defineComponent({ }); emitter.on('presentLoader', this.presentLoader); emitter.on('dismissLoader', this.dismissLoader); + init(this.userToken, this.instanceUrl, this.maxAge) }, unmounted() { emitter.off('presentLoader', this.presentLoader); emitter.off('dismissLoader', this.dismissLoader); + resetConfig(); + }, + computed: { + ...mapGetters({ + userToken: 'user/getUserToken', + instanceUrl: 'user/getInstanceUrl' + }) }, setup() { const store = useStore(); diff --git a/src/adapter/index.ts b/src/adapter/index.ts new file mode 100644 index 000000000..7c414a23b --- /dev/null +++ b/src/adapter/index.ts @@ -0,0 +1,11 @@ +import { fetchProductsStockAtFacility, init, resetConfig, Response, Stock, updateInstanceUrl, updateToken } from '@hotwax/oms-api' + +export { + fetchProductsStockAtFacility, + init, + resetConfig, + Response, + Stock, + updateInstanceUrl, + updateToken +} diff --git a/src/store/modules/stock/actions.ts b/src/store/modules/stock/actions.ts index 1e2e294b0..2d8a9f73c 100644 --- a/src/store/modules/stock/actions.ts +++ b/src/store/modules/stock/actions.ts @@ -4,12 +4,14 @@ import RootState from '@/store/RootState' import StockState from './StockState' import * as types from './mutation-types' import { hasError } from '@/utils' +import { fetchProductsStockAtFacility, Response, Stock } from '@/adapter' const actions: ActionTree = { /** * Add stocks of specific product */ + // TODO: remove this action as not used async addProduct ( { commit }, { productId }) { const resp: any = await StockService.checkInventory({ productId }); if (resp.status === 200) { @@ -24,17 +26,12 @@ const actions: ActionTree = { // There is a limitation at API level to handle only 100 records // but as we will always fetch data for the fetched records which will be as per the viewSize // assuming that the value will never be 100 to show - const resp: any = await StockService.checkInventory({ - "viewSize": productIds.length, - "filters": { - "productId": productIds, - "productId_op": "in", - }, - "fieldsToSelect": ["productId","atp"], - }); - if (resp.status === 200 && !hasError(resp)) { - // Handled empty response in case of failed query - if (resp.data) commit(types.STOCK_ADD_PRODUCTS, { products: resp.data.docs }) + + try { + const resp: Array | Response = await fetchProductsStockAtFacility(productIds); + commit(types.STOCK_ADD_PRODUCTS, resp) + } catch(err: Response) { + console.error(err.message) } } diff --git a/src/store/modules/stock/mutations.ts b/src/store/modules/stock/mutations.ts index 2ade95625..38e147702 100644 --- a/src/store/modules/stock/mutations.ts +++ b/src/store/modules/stock/mutations.ts @@ -1,6 +1,7 @@ import { MutationTree } from 'vuex' import StockState from './StockState' import * as types from './mutation-types' +import { Stock } from '@/adapter' const mutations: MutationTree = { [types.STOCK_ADD_PRODUCT] (state, payload) { @@ -8,8 +9,8 @@ const mutations: MutationTree = { }, [types.STOCK_ADD_PRODUCTS] (state, payload) { // TODO - payload.products.forEach((product: any) => { - state.products[product.productId] = product.atp + payload.forEach((productStock: Stock) => { + state.products[productStock.productId] = productStock.availableToPromiseTotal }); } } diff --git a/src/store/modules/user/actions.ts b/src/store/modules/user/actions.ts index 94f12effe..d6e4153b8 100644 --- a/src/store/modules/user/actions.ts +++ b/src/store/modules/user/actions.ts @@ -6,6 +6,7 @@ import * as types from './mutation-types' import { hasError, showToast } from '@/utils' import { translate } from '@/i18n' import "moment-timezone"; +import { resetConfig, updateInstanceUrl, updateToken } from '@/adapter' const actions: ActionTree = { @@ -31,6 +32,7 @@ const actions: ActionTree = { if (checkPermissionResponse.status === 200 && !hasError(checkPermissionResponse) && checkPermissionResponse.data && checkPermissionResponse.data.hasPermission) { commit(types.USER_TOKEN_CHANGED, { newToken: resp.data.token }) + updateToken(resp.data.token) dispatch('getProfile') if (resp.data._EVENT_MESSAGE_ && resp.data._EVENT_MESSAGE_.startsWith("Alert:")) { // TODO Internationalise text @@ -45,6 +47,7 @@ const actions: ActionTree = { } } else { commit(types.USER_TOKEN_CHANGED, { newToken: resp.data.token }) + updateToken(resp.data.token) dispatch('getProfile') return resp.data; } @@ -72,7 +75,7 @@ const actions: ActionTree = { async logout ({ commit }) { // TODO add any other tasks if need commit(types.USER_END_SESSION) - + resetConfig() }, /** @@ -157,6 +160,7 @@ const actions: ActionTree = { */ setUserInstanceUrl ({ commit }, payload){ commit(types.USER_INSTANCE_URL_UPDATED, payload) + updateInstanceUrl(payload) }, } export default actions; \ No newline at end of file