Skip to content

Commit

Permalink
Implemented: logic to create moqui token using the ofbiz token and st…
Browse files Browse the repository at this point in the history
…ore them in state (hotwax#293)
  • Loading branch information
amansinghbais committed Aug 6, 2024
1 parent b7fb46e commit 89626f4
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 29 deletions.
94 changes: 70 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"@casl/ability": "^6.0.0",
"@hotwax/app-version-info": "^1.0.0",
"@hotwax/apps-theme": "^1.2.6",
"@hotwax/dxp-components": "1.13.0",
"@hotwax/oms-api": "1.14.0",
"@hotwax/dxp-components": "1.15.2",
"@hotwax/oms-api": "1.15.0",
"@ionic/core": "^7.6.0",
"@ionic/vue": "^7.6.0",
"@ionic/vue-router": "^7.6.0",
Expand Down
29 changes: 29 additions & 0 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ const login = async (username: string, password: string): Promise <any> => {
});
}

const moquiLogin = async (omsRedirectionUrl: string, token: string): Promise <any> => {
const baseURL = omsRedirectionUrl.startsWith('http') ? omsRedirectionUrl.includes('/rest/s1/order-routing') ? omsRedirectionUrl : `${omsRedirectionUrl}/rest/s1/order-routing/` : `https://${omsRedirectionUrl}.hotwax.io/rest/s1/order-routing/`;
let api_key = ""

try {
const resp = await client({
url: "login",
method: "post",
baseURL,
params: {
token
},
headers: {
"Content-Type": "application/json"
}
}) as any;

if(!hasError(resp) && (resp.data.api_key || resp.data.token)) {
api_key = resp.data.api_key || resp.data.token
} else {
throw "Sorry, login failed. Please try again";
}
} catch(err) {
return Promise.resolve("Sorry, login failed. Please try again");
}
return Promise.resolve(api_key)
}

const setUserPreference = async (payload: any): Promise<any> => {
return api({
url: "service/setUserPreference",
Expand Down Expand Up @@ -195,5 +223,6 @@ export const UserService = {
getUserProfile,
getUserPermissions,
login,
moquiLogin,
setUserPreference,
}
4 changes: 4 additions & 0 deletions src/store/modules/user/UserState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export default interface UserState {
pwaState: any;
instanceUrl: string;
currentEComStore: object;
omsRedirectionInfo: {
url: string;
token: string;
}
}
18 changes: 16 additions & 2 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const actions: ActionTree<UserState, RootState> = {
*/
async login ({ commit, dispatch }, payload) {

const { token, oms } = payload;
const { token, oms, omsRedirectionUrl } = payload;
dispatch("setUserInstanceUrl", oms);
try {
if (token) {
Expand Down Expand Up @@ -66,6 +66,15 @@ const actions: ActionTree<UserState, RootState> = {
Settings.defaultZone = userProfile.userTimeZone;
}

if(omsRedirectionUrl) {
const api_key = await UserService.moquiLogin(omsRedirectionUrl, token)
if(api_key) {
dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token: api_key })
}
} else {
showToast(translate("Some functionality of this app might not work properly, Since moqui is not setup for this account."))
}

// TODO user single mutation
commit(types.USER_CURRENT_ECOM_STORE_UPDATED, preferredStore);
commit(types.USER_INFO_UPDATED, userProfile);
Expand All @@ -83,7 +92,7 @@ const actions: ActionTree<UserState, RootState> = {
/**
* Logout user
*/
async logout ({ commit }, payload) {
async logout ({ commit, dispatch }, payload) {
// store the url on which we need to redirect the user after logout api completes in case of SSO enabled
let redirectionUrl = ''

Expand Down Expand Up @@ -118,6 +127,7 @@ const actions: ActionTree<UserState, RootState> = {
this.dispatch("order/resetOrderQuery")
this.dispatch("job/clearCtgryAndBrkrngJobs")
this.dispatch("util/clearInvConfigs")
dispatch("setOmsRedirectionInfo", { url: "", token: "" })
resetPermissions();

// reset plugin state on logout
Expand Down Expand Up @@ -157,6 +167,10 @@ const actions: ActionTree<UserState, RootState> = {
});
},

setOmsRedirectionInfo({ commit }, payload) {
commit(types.USER_OMS_REDIRECTION_INFO_UPDATED, payload)
},

/**
* Set User Instance Url
*/
Expand Down
3 changes: 3 additions & 0 deletions src/store/modules/user/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const getters: GetterTree <UserState, RootState> = {
},
getCurrentEComStore(state) {
return state.currentEComStore
},
getOmsRedirectionInfo(state) {
return state.omsRedirectionInfo;
}
}
export default getters;
4 changes: 4 additions & 0 deletions src/store/modules/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const userModule: Module<UserState, RootState> = {
pwaState: {
updateExists: false,
registration: null,
},
omsRedirectionInfo: {
url: "",
token: ""
}
},
getters,
Expand Down
3 changes: 2 additions & 1 deletion src/store/modules/user/mutation-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const USER_INSTANCE_URL_UPDATED = SN_USER + '/INSTANCE_URL_UPDATED'
export const USER_PERMISSIONS_UPDATED = SN_USER + '/PERMISSIONS_UPDATED'
export const USER_CURRENT_ECOM_STORE_UPDATED = SN_USER + '/CURRENT_ECOM_STORE_UPDATED'
export const USER_STORE_RSRV_INV_STATUS_UPDATED = SN_USER + '/STORE_RSRV_INV_STATUS_UPDATED'
export const USER_PWA_STATE_UPDATED = SN_USER + '/PWA_STATE_UPDATED'
export const USER_PWA_STATE_UPDATED = SN_USER + '/PWA_STATE_UPDATED'
export const USER_OMS_REDIRECTION_INFO_UPDATED = SN_USER + '/OMS_REDIRECTION_INFO_UPDATED'
3 changes: 3 additions & 0 deletions src/store/modules/user/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ const mutations: MutationTree <UserState> = {
state.pwaState.registration = payload.registration;
state.pwaState.updateExists = payload.updateExists;
},
[types.USER_OMS_REDIRECTION_INFO_UPDATED](state, payload) {
state.omsRedirectionInfo = payload;
}
}
export default mutations;

0 comments on commit 89626f4

Please sign in to comment.