diff --git a/src/helpers/heritage-integration-helper.js b/src/helpers/heritage-integration-helper.js index 010d1f1cb..6a421a90b 100644 --- a/src/helpers/heritage-integration-helper.js +++ b/src/helpers/heritage-integration-helper.js @@ -7,71 +7,64 @@ import router from "@/router"; import baseMixin from "../mixins/base-mixin"; -// Read info from heritage funnel and reset state or load referral -export async function loadReferralFromHeritageFunnelIfPresent() { - // const orderInfo = getHeritageCookieValue(); - - // // Do nothing if there is no cookie or no correlation id. - // if (orderInfo === null || orderInfo.ReferralCorrelationId === undefined) { - // return; - // } - - // // Reset state if cookie says to. - // if (orderInfo.ShouldResetState) { - // baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.RESET_STATE); - // //deleteHeritageCookie(); - // return; - // } - - // // Load referral if there is a cookie, and it doesn't indicate it needs a state reset. - // await loadOrder(orderInfo.ReferralNumber, orderInfo.ReferralDate, orderInfo.ReferralCorrelationId); -} - -// Navigate to Heritage Funnel with the proper URL format. -// Will Save the referral if there is one in state, or create a new one if one is not in state. -export async function navigateToHeritageFunnel() { - await saveOrder(); - const referralCorrelationId = store.state.order.referralCorrelationId; - - router.navigate( - navigationScenarios.MOVE_TO_HERITAGE_FUNNEL, - router.currentRoute.value, - { - corid: referralCorrelationId, - src: "concept-funnel", - // TODO CSR-98 REMOVE THIS - cns: "all", - experiments: "RemoveServiceAreaPage=ServAreaRemoval_V7=ServAreaNoRemove_V7_TEST=true,ConceptFunnel=ConceptFunnel_V1=ConceptFunnel_TEST=true" - } - ); -} - // Saves Referral if one is available and commits referral details to state. -// Also sets cookie properties. export async function saveOrder() { - // const savedOrderInfo = await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SAVE_ORDER); + console.log("saving order..."); + const savedOrderInfo = await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SAVE_ORDER); - // // Save the referral information back from the store. - // await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SET_REFERRAL_INFORMATION, { - // referralNumber: savedOrderInfo.data.referralNumber, - // correlationId: savedOrderInfo.data.referralCorrelationId, - // referralDate: savedOrderInfo.data.referralDate, - // }, false); + // Save the referral information back from the store. + await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SET_REFERRAL_INFORMATION, { + referralNumber: savedOrderInfo.data.referralNumber, + correlationId: savedOrderInfo.data.referralCorrelationId, + referralDate: savedOrderInfo.data.referralDate, + }, false); - // // Set cookie properties. - // setHeritageCookieProperties({ - // DidHeritageFunnelUpdateLast: false - // }); + // Update the cookie with the referral information when saved. + updateOrCreateConceptCookie(); } // Loads order based on referral data in cookie, also loads the referral information into state. export async function loadOrder(referralNumber, referralDate, correlationId) { - const loadOrderResponse = await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOAD_ORDER, + console.log("loading order...", referralNumber, referralDate, correlationId); + await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOAD_ORDER, { referralNumber: referralNumber, referralDate: referralDate, correlationId: correlationId } , false); } +// Read info from heritage funnel and reset state or load referral +export async function loadReferralIfPresent() { + console.log("attempting to load referral...."); + const conceptCookie = getConceptCookie(); + + // Do nothing if there is no cookie or no correlation id. + if (conceptCookie === null || conceptCookie.ReferralCorrelationId === null) { + console.log("No referral found"); + return; + } + + // Reset state if cookie says to. + if (conceptCookie.ShouldResetState) { + console.log("Resetting state..."); + baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.RESET_STATE); + deleteConceptCookie(); + return; + } + + console.log("Loading order..."); + // Load referral if there is a cookie, and it doesn't indicate it needs a state reset. + await loadOrder(conceptCookie.ReferralNumber, conceptCookie.ReferralDate, conceptCookie.ReferralCorrelationId); +} + export function updateOrCreateConceptCookie() { + console.log("Updating cookie...", { + LastTouched: new Date(), + DidHeritageFunnelUpdateLast: false, + ShouldResetState: false, + ReferralNumber: store.state.order.referralNumber, + ReferralDate: store.state.order.referralDate, + ReferralCorrelationId: store.state.order.referralCorrelationId, + }); + // Create the cookie document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}={}; path=/`; @@ -79,6 +72,7 @@ export function updateOrCreateConceptCookie() { setConceptCookieProperties({ LastTouched: new Date(), DidHeritageFunnelUpdateLast: false, + ShouldResetState: false, ReferralNumber: store.state.order.referralNumber, ReferralDate: store.state.order.referralDate, ReferralCorrelationId: store.state.order.referralCorrelationId, @@ -87,12 +81,14 @@ export function updateOrCreateConceptCookie() { } export function isConceptSessionStillActive() { - if(getConceptCookie() !== null){ + if (getConceptCookie() !== null) { const lastTouchedValue = getConceptCookie().LastTouched; const timeoutAmount = applicationConfig.SESSION_TIMEOUT_CONFIG; const isMoreThanHalfHourAgo = ((new Date() - new Date(lastTouchedValue)) / 60000) > timeoutAmount; - if(isMoreThanHalfHourAgo){ + console.log("has session expired -->", isMoreThanHalfHourAgo); + + if (isMoreThanHalfHourAgo) { return false; } @@ -100,10 +96,27 @@ export function isConceptSessionStillActive() { } } -// --------- PRIVATE FUNCTIONS --------- +// Navigate to Heritage Funnel with the proper URL format. +// Will Save the referral if there is one in state, or create a new one if one is not in state. +export async function navigateToHeritageFunnel() { + + // Create the order (or save existing order) when navigating to Heritage Funnel. + await saveOrder(); + + router.navigate( + navigationScenarios.MOVE_TO_HERITAGE_FUNNEL, + router.currentRoute.value, + { + corid: store.state.order.referralCorrelationId, + src: "concept-funnel", + cns: "all", + experiments: "RemoveServiceAreaPage=ServAreaRemoval_V7=ServAreaNoRemove_V7_TEST=true,ConceptFunnel=ConceptFunnel_V1=ConceptFunnel_TEST=true" + } + ); +} /* Start cookie related functions */ -function getConceptCookie() { +export function getConceptCookie() { const cookieJson = document.cookie ?.split("; ") ?.find(row => row.startsWith(`${cookieNames.CONCEPT_SESSION_INFO}=`)) @@ -116,6 +129,11 @@ function getConceptCookie() { } } + + + +// --------- PRIVATE FUNCTIONS --------- + function setConceptCookieProperties(properties) { if (typeof properties == "object") { let cookie = getConceptCookie(); diff --git a/src/helpers/heritage-integration-helper.spec.js b/src/helpers/heritage-integration-helper.spec.js index a5f7fd437..eb1130c21 100644 --- a/src/helpers/heritage-integration-helper.spec.js +++ b/src/helpers/heritage-integration-helper.spec.js @@ -19,7 +19,7 @@ describe("saveOrder", () => { } } - describe("loadReferralFromHeritageFunnelIfPresent", () => { + describe("loadReferralIfPresent", () => { // test("ShouldResetState == true => heritage cookie is deleted", () => { // // Arrange // // TODO CSR-98 Can we not mock this?? @@ -42,7 +42,7 @@ describe("saveOrder", () => { // console.log(document.cookie) // // Act - // helper.loadReferralFromHeritageFunnelIfPresent(); + // helper.loadReferralIfPresent(); // // Assert // console.log(document.cookie) @@ -54,7 +54,7 @@ describe("saveOrder", () => { // helper.getHeritageCookieValue = jest.fn(x => x.ShouldResetState = false); // // Act - // helper.loadReferralFromHeritageFunnelIfPresent(); + // helper.loadReferralIfPresent(); // // Assert // expect(helper.getHeritageCookieValue).toHaveBeenCalled(); @@ -65,7 +65,7 @@ describe("saveOrder", () => { // helper.getHeritageCookieValue = jest.fn(x => x.ShouldResetState = true); // // Act - // helper.loadReferralFromHeritageFunnelIfPresent(); + // helper.loadReferralIfPresent(); // // Assert // expect(helper.getHeritageCookieValue).toHaveBeenCalled(); diff --git a/src/router/index.js b/src/router/index.js index 79db19b63..54a64c5d2 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -4,7 +4,7 @@ import { storeActions } from "@/constants/store-actions"; import { lazyLoadComponent } from "@/router/dynamic-routing/component-loader.js"; import { routingTable } from "@/router/router-constants/routing-table.js"; import { globalEvents, globalEventTypes } from "@/constants/events"; -import { updateOrCreateConceptCookie, loadReferralFromHeritageFunnelIfPresent, isConceptSessionStillActive} from "@/helpers/heritage-integration-helper"; +import * as integrationHelper from "@/helpers/heritage-integration-helper"; import baseMixin from "@/mixins/base-mixin"; import eventBus from "@/helpers/event-bus/event-bus"; @@ -35,13 +35,19 @@ const routes = [ } else { try { - // Check our 'Session' is still good, update the cookie. - isConceptSessionStillActive(); - updateOrCreateConceptCookie(); + // Check our 'Session' is still good. + // If not, reset state and go back to the start. + if (!integrationHelper.isConceptSessionStillActive()) { + baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.RESET_STATE); + await GoToFunnelStartOn404(next); + } + + // Create funnel cookie, or update it if it already exists. + integrationHelper.updateOrCreateConceptCookie(); // On entering the concept funnel "fresh", read cookie information, decide what to do next. if (from.redirectedFrom === undefined) { - await loadReferralFromHeritageFunnelIfPresent(); + await integrationHelper.loadReferralIfPresent(); } // If we already have our route, go to it. @@ -137,10 +143,10 @@ 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 exists - // if (getHeritageCookieValue()?.ReferralNumber) { - // await saveOrder(); - // } + // if cookie and referralNumber/Date exists + if (integrationHelper.getConceptCookie()?.ReferralNumber && integrationHelper.getConceptCookie()?.ReferralDate) { + await integrationHelper.saveOrder(); + } router.push({ name: "root",