From aeca237b0538bd1e1c818297bd5d6710f32638c6 Mon Sep 17 00:00:00 2001 From: FrankRua Date: Mon, 21 Mar 2022 18:46:21 -0400 Subject: [PATCH] started to re-do cookie --- src/constants/application-config.js | 1 + src/constants/cookie-names.js | 2 +- src/constants/store-mutations.js | 2 +- src/helpers/heritage-integration-helper.js | 114 +++++++++++------- .../heritage-integration-helper.spec.js | 12 +- src/router/index.js | 12 +- src/store/index.js | 2 +- 7 files changed, 88 insertions(+), 57 deletions(-) diff --git a/src/constants/application-config.js b/src/constants/application-config.js index 4b80d25d1..8c033d4cd 100644 --- a/src/constants/application-config.js +++ b/src/constants/application-config.js @@ -1,5 +1,6 @@ const applicationConfig = { CONSUMER_APIGATEWAY_URL: process.env.VUE_APP_CONSUMER_API_GATEWAY, + SESSION_TIMEOUT_CONFIG: 30 }; export { applicationConfig }; diff --git a/src/constants/cookie-names.js b/src/constants/cookie-names.js index 33a76ae43..6b1e3c909 100644 --- a/src/constants/cookie-names.js +++ b/src/constants/cookie-names.js @@ -1,5 +1,5 @@ const cookieNames = { - ORDER_INFO: "OrderInfo" + CONCEPT_SESSION_INFO: "ConceptSessionInfo", }; export { cookieNames }; diff --git a/src/constants/store-mutations.js b/src/constants/store-mutations.js index 2636464bd..00ac66d58 100644 --- a/src/constants/store-mutations.js +++ b/src/constants/store-mutations.js @@ -34,7 +34,7 @@ const storeMutations = { // OTHER MUTATIONS UPDATE_PAGE_DATA: "updatePageData", - SET_LOAD_ORDER_INFO: "setLoadOrderInformation" + SET_LOAD_CONCEPT_SESSION_INFO: "setLoadOrderInformation" }; diff --git a/src/helpers/heritage-integration-helper.js b/src/helpers/heritage-integration-helper.js index 61e410604..01950fdce 100644 --- a/src/helpers/heritage-integration-helper.js +++ b/src/helpers/heritage-integration-helper.js @@ -1,28 +1,30 @@ import { storeActions } from "@/constants/store-actions.js"; import { cookieNames } from "@/constants/cookie-names"; import { navigationScenarios } from "@/router/router-constants/navigation-scenarios"; +import { applicationConfig } from "@/constants/application-config"; import store from "@/store"; 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(); + // const orderInfo = getHeritageCookieValue(); - // Do nothing if there is no cookie or no correlation id. - if (orderInfo === null || orderInfo.ReferralCorrelationId === undefined) { - return; - } + // // 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; - } + // // 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); + // // 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. @@ -47,19 +49,19 @@ export async function navigateToHeritageFunnel() { // 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); + // 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 - }); + // // Set cookie properties. + // setHeritageCookieProperties({ + // DidHeritageFunnelUpdateLast: false + // }); } // Loads order based on referral data in cookie, also loads the referral information into state. @@ -69,14 +71,42 @@ export async function loadOrder(referralNumber, referralDate, correlationId) { , false); } +export function updateOrCreateConceptCookie() { + // Create the cookie + document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}={}; path=/`; + + // Set up cookie with all the props. + setConceptCookieProperties({ + LastTouched: new Date(), + DidHeritageFunnelUpdateLast: false, + ReferralNumber: store.state.order.referralNumber, + ReferralDate: store.state.order.referralDate, + ReferralCorrelationId: store.state.order.referralCorrelationId, + }); + +} + +export function isConceptSessionStillActive() { + if(getConceptCookie() !== null){ + const lastTouchedValue = getConceptCookie().LastTouched; + const timeoutAmount = applicationConfig.SESSION_TIMEOUT_CONFIG; + const isMoreThanHalfHourAgo = ((new Date() - new Date(lastTouchedValue)) / 60000) > timeoutAmount; + + if(isMoreThanHalfHourAgo){ + return false; + } + + return true; + } +} + // --------- PRIVATE FUNCTIONS --------- /* Start cookie related functions */ -export function getHeritageCookieValue() { - console.log("getHeritageCookieValue called"); +export function getConceptCookie() { const cookieJson = document.cookie ?.split("; ") - ?.find(row => row.startsWith(`${cookieNames.ORDER_INFO}=`)) + ?.find(row => row.startsWith(`${cookieNames.CONCEPT_SESSION_INFO}=`)) ?.split("=")[1]; try { @@ -86,31 +116,27 @@ export function getHeritageCookieValue() { } } -function deleteHeritageCookie() { - // If this cookie is ever created from the concept funnel, will need to add another - // line with the path=/fmg/ - document.cookie = `${cookieNames.ORDER_INFO}=; Max-Age=0; path=/; domain=${location.hostname}`; -} - -function setHeritageCookieValue(cookieValue) { - let cookieValueJson = cookieValue; - if (typeof cookieValue == "object") - cookieValueJson = JSON.stringify(cookieValue); - - document.cookie = `${cookieNames.ORDER_INFO}=${cookieValueJson}; path=/`; -} - -function setHeritageCookieProperties(properties) { +function setConceptCookieProperties(properties) { if (typeof properties == "object") { - let cookie = getHeritageCookieValue(); + let cookie = getConceptCookie(); if (cookie !== null) { Object.keys(properties).forEach(key => { cookie[key] = properties[key]; }); - setHeritageCookieValue(cookie); + const cookieValueJson = JSON.stringify(cookie); + + document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}=${cookieValueJson}; path=/`; } } } + +// function deleteHeritageCookie() { +// // If this cookie is ever created from the concept funnel, will need to add another +// // line with the path=/fmg/ +// document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}=; Max-Age=0; path=/; domain=${location.hostname}`; +// } + + /* End cookie related functions */ diff --git a/src/helpers/heritage-integration-helper.spec.js b/src/helpers/heritage-integration-helper.spec.js index f99187474..a5f7fd437 100644 --- a/src/helpers/heritage-integration-helper.spec.js +++ b/src/helpers/heritage-integration-helper.spec.js @@ -29,16 +29,16 @@ describe("saveOrder", () => { // ShouldResetState: testShouldResetState // } - // console.log(cookieNames.ORDER_INFO); + // console.log(cookieNames.CONCEPT_SESSION_INFO); // console.log(testCookieValue) // console.log(JSON.stringify(testCookieValue)); // console.log(location.hostname) - // let myString = `${cookieNames.ORDER_INFO}=${JSON.stringify(testCookieValue)};domain=${location.hostname};path=/;`; + // let myString = `${cookieNames.CONCEPT_SESSION_INFO}=${JSON.stringify(testCookieValue)};domain=${location.hostname};path=/;`; // console.log(myString) // document.cookie = 'OrderInfo={"Hahahah":true};domain=localhost;path=/;'; // console.log(document.cookie) // document.cookie = "Ahhh=AHHH;" - // document.cookie = `${cookieNames.ORDER_INFO}=; Max-Age=0; domain=${location.hostname};`; + // document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}=; Max-Age=0; domain=${location.hostname};`; // console.log(document.cookie) // // Act @@ -217,7 +217,7 @@ describe.skip("cookies", () => { const cookies = { "orderconfirmation": 1565980, "optimizelyEndUserId": "oeu1610051865958r0.07937134272718804", - [cookieNames.ORDER_INFO]: `{"ReferralNumber":"1566818","ReferralDate":"2022-03-15T10:56:24.597","ReferralCorrelationId":"404d2b04-f86e-45c3-b373-127b6217b060","ShouldResetState":false,"DidHeritageFunnelUpdateLast":true}`, + [cookieNames.CONCEPT_SESSION_INFO]: `{"ReferralNumber":"1566818","ReferralDate":"2022-03-15T10:56:24.597","ReferralCorrelationId":"404d2b04-f86e-45c3-b373-127b6217b060","ShouldResetState":false,"DidHeritageFunnelUpdateLast":true}`, "UNIQUE_SESSION_ID": "33756020-b58e-4ec7-b8b8-3f1576719c40", "_ga": "GA1.1.1631274579.1622658999", "da_lid": "B10847599A73EA95861EBB99009A283E27|0|0|0 clientTag=null", @@ -227,9 +227,9 @@ const cookies = { function setupCookies({ heritageCookieValue = "", includeHeritageCookie = true }) { console.log(heritageCookieValue) Object.keys(cookies).forEach(key => { - const cookieValue = key == cookieNames.ORDER_INFO ? heritageCookieValue : cookies[key]; + const cookieValue = key == cookieNames.CONCEPT_SESSION_INFO ? heritageCookieValue : cookies[key]; - if (includeHeritageCookie || key != cookieNames.ORDER_INFO) + if (includeHeritageCookie || key != cookieNames.CONCEPT_SESSION_INFO) document.cookie = `${key}=${cookieValue}`; }); } diff --git a/src/router/index.js b/src/router/index.js index 2dd8e8486..79db19b63 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 { loadReferralFromHeritageFunnelIfPresent, saveOrder, getHeritageCookieValue } from "@/helpers/heritage-integration-helper"; +import { updateOrCreateConceptCookie, loadReferralFromHeritageFunnelIfPresent, isConceptSessionStillActive} from "@/helpers/heritage-integration-helper"; import baseMixin from "@/mixins/base-mixin"; import eventBus from "@/helpers/event-bus/event-bus"; @@ -35,6 +35,10 @@ const routes = [ } else { try { + // Check our 'Session' is still good, update the cookie. + isConceptSessionStillActive(); + updateOrCreateConceptCookie(); + // On entering the concept funnel "fresh", read cookie information, decide what to do next. if (from.redirectedFrom === undefined) { await loadReferralFromHeritageFunnelIfPresent(); @@ -134,9 +138,9 @@ async function navigate(scenario, currentRoute, invalidateOnSave, optionalQuery baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData); // if cookie and referralNumber exists - if (getHeritageCookieValue()?.ReferralNumber) { - await saveOrder(); - } + // if (getHeritageCookieValue()?.ReferralNumber) { + // await saveOrder(); + // } router.push({ name: "root", diff --git a/src/store/index.js b/src/store/index.js index 62de9805c..af280675e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -367,7 +367,7 @@ export const actions = { correlationId: correlationId }, }).then( (response) => { - context.commit(storeMutations.SET_LOAD_ORDER_INFO, response.data); + context.commit(storeMutations.SET_LOAD_CONCEPT_SESSION_INFO, response.data); return response; }); }