From 972e02a5b206c26e41b6765a7e7fcca9532eb139 Mon Sep 17 00:00:00 2001 From: Scott Kiener Date: Thu, 23 Jun 2022 15:23:30 -0400 Subject: [PATCH] CSR-706 | Re-work synchronicity of saveOrder Allow multiple saveOrders to run synchronously Allow saveOrder to be awaited before navigating to heritage Allow saveOrder to run on routine navigation if an email address is present Add saveOrderPromise member to the store --- src/constants/store-mutations.js | 3 +- .../heritage-integration/order-helper.js | 41 +++++++++++++------ src/router/index.js | 6 +-- src/store/index.js | 9 ++-- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/constants/store-mutations.js b/src/constants/store-mutations.js index 1924b0063..bbd6fc818 100644 --- a/src/constants/store-mutations.js +++ b/src/constants/store-mutations.js @@ -46,7 +46,8 @@ const storeMutations = { // OTHER MUTATIONS UPDATE_PAGE_DATA: "updatePageData", UPDATE_STATE_WITH_ORDER_INFORMATION: "updateStateWithOrderInformation", - UPDATE_SERVICE_LOCATION_WITH_VEHICLE_REGISTRATION: "updateServiceLocationWithVehicleRegistration" + UPDATE_SERVICE_LOCATION_WITH_VEHICLE_REGISTRATION: "updateServiceLocationWithVehicleRegistration", + UPDATE_SAVE_ORDER_PROMISE: "updateSaveOrderPromise", }; export { storeMutations }; diff --git a/src/helpers/heritage-integration/order-helper.js b/src/helpers/heritage-integration/order-helper.js index 962550592..af672f7cd 100644 --- a/src/helpers/heritage-integration/order-helper.js +++ b/src/helpers/heritage-integration/order-helper.js @@ -1,6 +1,8 @@ import { storeActions } from "@/constants/store-actions.js"; import { getFunnelCookie, updateOrCreateFunnelCookie, deleteFunnelCookie } from "@/helpers/heritage-integration/cookie-helper.js"; import baseMixin from "@/mixins/base-mixin"; +import store from "@/store"; +import { storeMutations } from "@/constants/store-mutations"; /* Will call API and hydrate state with data from API if present. If there is no order present @@ -34,18 +36,20 @@ export async function loadOrderIfPresent() { update the cookie. */ export async function saveOrder() { - const savedOrderInfo = await baseMixin.methods.dispatchStoreAction(storeActions.SAVE_ORDER); - - // Save the referral information back from the store. - await baseMixin.methods.dispatchStoreAction(storeActions.SET_REFERRAL_INFORMATION, { - referralNumber: savedOrderInfo.data.referralNumber.toString(), - referralCorrelationId: savedOrderInfo.data.referralCorrelationId, - referralDate: savedOrderInfo.data.referralDate, - accountNumber: savedOrderInfo.data.accountNumber.toString() - }, false); - - // Update the cookie with the referral information when saved. - updateOrCreateFunnelCookie(); + var saveOrderPromise; + if (store.getters.applicationUser.saveOrderPromise) { + // queue newest request after current saveOrderPromise resolves + saveOrderPromise = store.getters.applicationUser.saveOrderPromise.then(() => { + // get a new saveOrderPromise + return saveOrderHelper(); + }); + } else { + // create an initial saveOrderPromise + saveOrderPromise = saveOrderHelper(); + } + store.commit(storeMutations.UPDATE_SAVE_ORDER_PROMISE, saveOrderPromise); + // await here to allow for a caller to await and make the function synchronous + await saveOrderPromise; } @@ -65,4 +69,17 @@ async function loadOrder(referralNumber, referralDate, referralCorrelationId, ac }, false); return response; +} +async function saveOrderHelper() { + const savedOrderInfo = await baseMixin.methods.dispatchStoreAction(storeActions.SAVE_ORDER); + // Save the referral information back from the store. + await baseMixin.methods.dispatchStoreAction(storeActions.SET_REFERRAL_INFORMATION, { + referralNumber: savedOrderInfo.data.referralNumber.toString(), + referralCorrelationId: savedOrderInfo.data.referralCorrelationId, + referralDate: savedOrderInfo.data.referralDate, + accountNumber: savedOrderInfo.data.accountNumber.toString() + }, false); + + // Update the cookie with the referral information when saved. + updateOrCreateFunnelCookie(); } \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js index 2b2d09859..b3cc73be1 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -177,9 +177,9 @@ async function navigate(scenario, currentRoute, invalidateOnSave, optionalQuery // Append page data to the store for the NEXT page, if any. It will be an empty object if none is provided. baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData); - // if cookie and referralNumber/Date exists - if (getFunnelCookie()?.ReferralNumber && getFunnelCookie()?.ReferralDate) { - await saveOrder(); + // if cookie and referralNumber/Date exists OR an emailAddress has been saved + if ((getFunnelCookie()?.ReferralNumber && getFunnelCookie()?.ReferralDate) || store.getters.customer.emailAddress) { + saveOrder(); } router.push({ diff --git a/src/store/index.js b/src/store/index.js index 5730adf53..52b373aaa 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -62,7 +62,8 @@ const getDefaultState = () => { applicationUser: { eventBus: [], pageData: {}, - savedSessionTimeout: getDateForSavedSessionTimeout() + savedSessionTimeout: getDateForSavedSessionTimeout(), + saveOrderPromise: null, }, } }; @@ -166,7 +167,6 @@ export const mutations = { state.order.customer.emailAddress = customerEmailAddress; }, - // EVENT BUS MUTATIONS addEventToBus(state, event) { state.applicationUser.eventBus.push(event); @@ -269,7 +269,10 @@ export const mutations = { state.order.serviceLocation.city = state.order.vehicle.registration.city; state.order.serviceLocation.state = state.order.vehicle.registration.state; state.order.serviceLocation.zipCode = state.order.vehicle.registration.zipCode; - } + }, + updateSaveOrderPromise(state, saveOrderPromise){ + state.applicationUser.saveOrderPromise = saveOrderPromise; + }, } // Export Getters