Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: order routing test drive feature(#254) #286

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
39 changes: 39 additions & 0 deletions src/services/ProductService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { client } from "@/api";
import store from "@/store";

const fetchProducts = async (payload: any): Promise <any> => {
const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"];
let baseURL = omsRedirectionInfo.url;
baseURL = baseURL && baseURL.startsWith("http") ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "searchProducts",
method: "post",
baseURL: baseURL,
data: payload,
headers: {
Authorization: 'Bearer ' + omsRedirectionInfo.token,
'Content-Type': 'application/json'
}
});
}

const getInventoryAvailableByFacility = async (payload: any): Promise <any> => {
const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"];
let baseURL = omsRedirectionInfo.url;
baseURL = baseURL && baseURL.startsWith("http") ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "service/getInventoryAvailableByFacility",
method: "post",
baseURL: baseURL,
data: payload,
headers: {
Authorization: 'Bearer ' + omsRedirectionInfo.token,
'Content-Type': 'application/json'
}
});
}

export const ProductService = {
fetchProducts,
getInventoryAvailableByFacility
}
46 changes: 45 additions & 1 deletion src/services/RoutingService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api from "@/api"
import api, { client } from "@/api"
import store from "@/store";

const fetchRoutingGroups = async (payload: any): Promise<any> => {
return api({
Expand Down Expand Up @@ -163,7 +164,48 @@ const runNow = async (routingGroupId: string): Promise<any> => {
});
}

const findOrder = async (payload: any): Promise<any> => {
const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"];
let baseURL = omsRedirectionInfo.url;
baseURL = baseURL && baseURL.startsWith("http") ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "solr-query",
method: "post",
baseURL: baseURL,
data: payload,
headers: {
Authorization: 'Bearer ' + omsRedirectionInfo.token,
'Content-Type': 'application/json'
}
});
}

const brokerOrder = async(payload: any): Promise<any> => {
return api({
url: `groups/${payload.routingGroupId}/run`,
method: "POST",
data: payload
})
}

const getOrderFacilityChangeInfo = async (payload: any): Promise<any> => {
const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"];
let baseURL = omsRedirectionInfo.url;
baseURL = baseURL && baseURL.startsWith("http") ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "performFind",
method: "post",
baseURL: baseURL,
data: payload,
headers: {
Authorization: 'Bearer ' + omsRedirectionInfo.token,
'Content-Type': 'application/json'
}
});
}

export const OrderRoutingService = {
brokerOrder,
cloneGroup,
createOrderRouting,
cloneRouting,
Expand All @@ -180,6 +222,8 @@ export const OrderRoutingService = {
fetchRoutingHistory,
fetchRoutingScheduleInformation,
fetchRule,
findOrder,
getOrderFacilityChangeInfo,
runNow,
scheduleBrokering,
updateRouting,
Expand Down
39 changes: 37 additions & 2 deletions src/services/UtilService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api from "@/api"
import api, { client } from "@/api"
import store from "@/store";

const fetchEnums = async (payload: any): Promise<any> => {
return api({
Expand Down Expand Up @@ -55,12 +56,46 @@ const checkOmsConnection = async (): Promise<any> => {
});
}

const getCarrierInformation = async (payload: any): Promise<any> => {
const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"];
let baseURL = omsRedirectionInfo.url;
baseURL = baseURL && baseURL.startsWith("http") ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "performFind",
method: "post",
baseURL: baseURL,
data: payload,
headers: {
Authorization: 'Bearer ' + omsRedirectionInfo.token,
'Content-Type': 'application/json'
}
});
}

const getCarrierDeliveryDays = async (payload: any): Promise<any> => {
const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"];
let baseURL = omsRedirectionInfo.url;
baseURL = baseURL && baseURL.startsWith("http") ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "performFind",
method: "post",
baseURL: baseURL,
data: payload,
headers: {
Authorization: 'Bearer ' + omsRedirectionInfo.token,
'Content-Type': 'application/json'
}
});
}

export const UtilService = {
checkOmsConnection,
fetchEnums,
fetchFacilities,
fetchFacilityGroups,
fetchOmsEnums,
fetchShippingMethods,
fetchStatusInformation
fetchStatusInformation,
getCarrierInformation,
getCarrierDeliveryDays
}
6 changes: 4 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import userModule from "./modules/user";
import utilModule from "./modules/util"
import orderRoutingModule from "./modules/orderRouting"
import { setPermissions } from "@/authorization"
import productModule from "./modules/product"

// TODO check how to register it from the components only
// Handle same module registering multiple time on page refresh
Expand All @@ -16,7 +17,7 @@ import { setPermissions } from "@/authorization"
const state: any = {}

const persistState = createPersistedState({
paths: ["user", "util", "orderRouting.currentGroup", "orderRouting.currentRuleId"],
paths: ["user", "util", "orderRouting.currentGroup", "orderRouting.currentRuleId", "product"],
fetchBeforeUse: true
})

Expand All @@ -30,7 +31,8 @@ const store = createStore<RootState>({
modules: {
"user": userModule,
"util": utilModule,
"orderRouting": orderRoutingModule
"orderRouting": orderRoutingModule,
"product": productModule
},
})

Expand Down
4 changes: 4 additions & 0 deletions src/store/modules/product/ProductState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface ProductState {
products: any;
stock: any;
}
78 changes: 78 additions & 0 deletions src/store/modules/product/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { ActionTree } from "vuex"
import RootState from "@/store/RootState"
import ProductState from "./ProductState"
import logger from "@/logger"
import { hasError } from "@/utils"
import * as types from "./mutation-types"
import { ProductService } from "@/services/ProductService"

const actions: ActionTree<ProductState, RootState> = {
async fetchProducts({ commit, state }, productIds) {
const cachedProductIds = Object.keys(state.products);
const productIdFilter= productIds.reduce((filter: string, productId: any) => {
// If product already exist in cached products skip
if (cachedProductIds.includes(productId)) {
return filter;
} else {
// checking condition that if the filter is not empty then adding 'OR' to the filter
if (filter !== '') filter += ' OR '
return filter += productId;
}
}, '');

// If there are no products skip the API call
if (productIdFilter === '') return;

try {
// adding viewSize as by default we are having the viewSize of 10
const resp = await ProductService.fetchProducts({
"filters": ['productId: (' + productIdFilter + ')'],
"viewSize": productIds.length
})
if(resp.data.response && !hasError(resp)) {
const products = resp.data.response.docs.reduce((products: any, product: any) => {
products[product.productId] = product
return products;
}, {});
// Handled empty response in case of failed query
if(resp.data) commit(types.PRODUCT_LIST_UPDATED, products);
} else {
throw resp.data;
}
} catch(err) {
logger.error("Failed to fetch product information", err)
}
},

async fetchStock({ commit, state }, shipGroup) {
const productIds = shipGroup.map((item: any) => item.productId)
const facilityId = shipGroup[0].facilityId
for(const productId of productIds) {
if(state.stock[productId]?.[facilityId]) {
continue;
}

try {
const payload = {
productId,
facilityId
}

const resp: any = await ProductService.getInventoryAvailableByFacility(payload);
if (!hasError(resp)) {
commit(types.PRODUCT_STOCK_UPDATED, { productId: payload.productId, facilityId: facilityId, stock: resp.data })
} else {
throw resp.data;
}
} catch (err) {
logger.error(err)
}
}
},

async clearProductState({ commit }) {
commit(types.PRODUCT_CLEARED)
}
}

export default actions;
17 changes: 17 additions & 0 deletions src/store/modules/product/getters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { GetterTree } from "vuex"
import ProductState from "./ProductState"
import RootState from "@/store/RootState"

const getters: GetterTree<ProductState, RootState> = {
getProducts(state) {
return state.products
},
getProductById: (state) => (id: string) => {
return state.products[id] || {}
},
getProductStock: (state) => (productId: string, facilityId: string) => {
return state.stock[productId] ? state.stock[productId][facilityId] ? state.stock[productId][facilityId] : {} : {}
},
}

export default getters;
19 changes: 19 additions & 0 deletions src/store/modules/product/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import actions from "./actions"
import getters from "./getters"
import mutations from "./mutations"
import { Module } from "vuex"
import ProductState from "./ProductState"
import RootState from "@/store/RootState"

const productModule: Module<ProductState, RootState> = {
namespaced: true,
state: {
products: {},
stock: {}
},
getters,
actions,
mutations,
}

export default productModule;
5 changes: 5 additions & 0 deletions src/store/modules/product/mutation-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SN_PRODUCT = "product"
export const PRODUCT_LIST_UPDATED = SN_PRODUCT + "/LIST_UPDATED"
export const PRODUCT_STOCK_UPDATED = SN_PRODUCT + "/STOCK_UPDATED"
export const PRODUCT_CLEARED = SN_PRODUCT + "/CLEARED"

22 changes: 22 additions & 0 deletions src/store/modules/product/mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MutationTree } from "vuex"
import ProductState from "./ProductState"
import * as types from "./mutation-types"

const mutations: MutationTree<ProductState> = {
[types.PRODUCT_LIST_UPDATED](state, payload) {
state.products = payload
},
[types.PRODUCT_CLEARED](state) {
state.products = {}
},
[types.PRODUCT_STOCK_UPDATED] (state, payload) {
if(state.stock[payload.productId]) {
state.stock[payload.productId][payload.facilityId] = payload.stock
} else {
state.stock[payload.productId] = {
[payload.facilityId]: payload.stock
}
}
},
}
export default mutations;
5 changes: 3 additions & 2 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { Settings } from "luxon"
import { useAuthStore } from '@hotwax/dxp-components'
import { resetConfig } from '@/adapter'
import { getServerPermissionsFromRules, prepareAppPermissions, resetPermissions, setPermissions } from "@/authorization"

Check warning on line 13 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (18.x)

'setPermissions' is defined but never used

Check warning on line 13 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (20.x)

'setPermissions' is defined but never used

const actions: ActionTree<UserState, RootState> = {

Expand Down Expand Up @@ -61,13 +61,13 @@
Settings.defaultZone = userProfile.timeZone;
}

setPermissions(appPermissions);
// setPermissions(appPermissions);
if(omsRedirectionUrl && token) {
dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token })
}
commit(types.USER_TOKEN_CHANGED, { newToken: api_key })
commit(types.USER_INFO_UPDATED, userProfile);
commit(types.USER_PERMISSIONS_UPDATED, appPermissions);
// commit(types.USER_PERMISSIONS_UPDATED, appPermissions);
commit(types.USER_CURRENT_ECOM_STORE_UPDATED, userProfile.stores.length ? userProfile.stores[0] : {});
emitter.emit("dismissLoader")
} catch (err: any) {
Expand All @@ -90,6 +90,7 @@
commit(types.USER_END_SESSION)
this.dispatch("orderRouting/clearRouting")
this.dispatch("util/clearUtilState")
this.dispatch("product/clearProductState")
dispatch("setOmsRedirectionInfo", { url: "", token: "" })
resetConfig();
resetPermissions();
Expand Down
3 changes: 2 additions & 1 deletion src/store/modules/util/UtilState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default interface UtilState {
shippingMethods: object;
facilityGroups: object;
isOmsConnectionExist: boolean | undefined;
statuses: any
statuses: any;
carriers: any;
}
Loading
Loading