started to re-do cookie

This commit is contained in:
FrankRua 2022-03-21 18:46:21 -04:00
parent f7ddb5ff6c
commit aeca237b05
7 changed files with 88 additions and 57 deletions

View file

@ -1,5 +1,6 @@
const applicationConfig = { const applicationConfig = {
CONSUMER_APIGATEWAY_URL: process.env.VUE_APP_CONSUMER_API_GATEWAY, CONSUMER_APIGATEWAY_URL: process.env.VUE_APP_CONSUMER_API_GATEWAY,
SESSION_TIMEOUT_CONFIG: 30
}; };
export { applicationConfig }; export { applicationConfig };

View file

@ -1,5 +1,5 @@
const cookieNames = { const cookieNames = {
ORDER_INFO: "OrderInfo" CONCEPT_SESSION_INFO: "ConceptSessionInfo",
}; };
export { cookieNames }; export { cookieNames };

View file

@ -34,7 +34,7 @@ const storeMutations = {
// OTHER MUTATIONS // OTHER MUTATIONS
UPDATE_PAGE_DATA: "updatePageData", UPDATE_PAGE_DATA: "updatePageData",
SET_LOAD_ORDER_INFO: "setLoadOrderInformation" SET_LOAD_CONCEPT_SESSION_INFO: "setLoadOrderInformation"
}; };

View file

@ -1,28 +1,30 @@
import { storeActions } from "@/constants/store-actions.js"; import { storeActions } from "@/constants/store-actions.js";
import { cookieNames } from "@/constants/cookie-names"; import { cookieNames } from "@/constants/cookie-names";
import { navigationScenarios } from "@/router/router-constants/navigation-scenarios"; import { navigationScenarios } from "@/router/router-constants/navigation-scenarios";
import { applicationConfig } from "@/constants/application-config";
import store from "@/store"; import store from "@/store";
import router from "@/router"; import router from "@/router";
import baseMixin from "../mixins/base-mixin"; import baseMixin from "../mixins/base-mixin";
// Read info from heritage funnel and reset state or load referral // Read info from heritage funnel and reset state or load referral
export async function loadReferralFromHeritageFunnelIfPresent() { export async function loadReferralFromHeritageFunnelIfPresent() {
const orderInfo = getHeritageCookieValue(); // const orderInfo = getHeritageCookieValue();
// Do nothing if there is no cookie or no correlation id. // // Do nothing if there is no cookie or no correlation id.
if (orderInfo === null || orderInfo.ReferralCorrelationId === undefined) { // if (orderInfo === null || orderInfo.ReferralCorrelationId === undefined) {
return; // return;
} // }
// Reset state if cookie says to. // // Reset state if cookie says to.
if (orderInfo.ShouldResetState) { // if (orderInfo.ShouldResetState) {
baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.RESET_STATE); // baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.RESET_STATE);
deleteHeritageCookie(); // //deleteHeritageCookie();
return; // return;
} // }
// Load referral if there is a cookie, and it doesn't indicate it needs a state reset. // // 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); // await loadOrder(orderInfo.ReferralNumber, orderInfo.ReferralDate, orderInfo.ReferralCorrelationId);
} }
// Navigate to Heritage Funnel with the proper URL format. // 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. // Saves Referral if one is available and commits referral details to state.
// Also sets cookie properties. // Also sets cookie properties.
export async function saveOrder() { 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. // // Save the referral information back from the store.
await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SET_REFERRAL_INFORMATION, { // await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SET_REFERRAL_INFORMATION, {
referralNumber: savedOrderInfo.data.referralNumber, // referralNumber: savedOrderInfo.data.referralNumber,
correlationId: savedOrderInfo.data.referralCorrelationId, // correlationId: savedOrderInfo.data.referralCorrelationId,
referralDate: savedOrderInfo.data.referralDate, // referralDate: savedOrderInfo.data.referralDate,
}, false); // }, false);
// Set cookie properties. // // Set cookie properties.
setHeritageCookieProperties({ // setHeritageCookieProperties({
DidHeritageFunnelUpdateLast: false // DidHeritageFunnelUpdateLast: false
}); // });
} }
// Loads order based on referral data in cookie, also loads the referral information into state. // 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); , 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 --------- // --------- PRIVATE FUNCTIONS ---------
/* Start cookie related functions */ /* Start cookie related functions */
export function getHeritageCookieValue() { export function getConceptCookie() {
console.log("getHeritageCookieValue called");
const cookieJson = document.cookie const cookieJson = document.cookie
?.split("; ") ?.split("; ")
?.find(row => row.startsWith(`${cookieNames.ORDER_INFO}=`)) ?.find(row => row.startsWith(`${cookieNames.CONCEPT_SESSION_INFO}=`))
?.split("=")[1]; ?.split("=")[1];
try { try {
@ -86,31 +116,27 @@ export function getHeritageCookieValue() {
} }
} }
function deleteHeritageCookie() { function setConceptCookieProperties(properties) {
// 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) {
if (typeof properties == "object") { if (typeof properties == "object") {
let cookie = getHeritageCookieValue(); let cookie = getConceptCookie();
if (cookie !== null) { if (cookie !== null) {
Object.keys(properties).forEach(key => { Object.keys(properties).forEach(key => {
cookie[key] = properties[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 */ /* End cookie related functions */

View file

@ -29,16 +29,16 @@ describe("saveOrder", () => {
// ShouldResetState: testShouldResetState // ShouldResetState: testShouldResetState
// } // }
// console.log(cookieNames.ORDER_INFO); // console.log(cookieNames.CONCEPT_SESSION_INFO);
// console.log(testCookieValue) // console.log(testCookieValue)
// console.log(JSON.stringify(testCookieValue)); // console.log(JSON.stringify(testCookieValue));
// console.log(location.hostname) // 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) // console.log(myString)
// document.cookie = 'OrderInfo={"Hahahah":true};domain=localhost;path=/;'; // document.cookie = 'OrderInfo={"Hahahah":true};domain=localhost;path=/;';
// console.log(document.cookie) // console.log(document.cookie)
// document.cookie = "Ahhh=AHHH;" // 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) // console.log(document.cookie)
// // Act // // Act
@ -217,7 +217,7 @@ describe.skip("cookies", () => {
const cookies = { const cookies = {
"orderconfirmation": 1565980, "orderconfirmation": 1565980,
"optimizelyEndUserId": "oeu1610051865958r0.07937134272718804", "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", "UNIQUE_SESSION_ID": "33756020-b58e-4ec7-b8b8-3f1576719c40",
"_ga": "GA1.1.1631274579.1622658999", "_ga": "GA1.1.1631274579.1622658999",
"da_lid": "B10847599A73EA95861EBB99009A283E27|0|0|0 clientTag=null", "da_lid": "B10847599A73EA95861EBB99009A283E27|0|0|0 clientTag=null",
@ -227,9 +227,9 @@ const cookies = {
function setupCookies({ heritageCookieValue = "", includeHeritageCookie = true }) { function setupCookies({ heritageCookieValue = "", includeHeritageCookie = true }) {
console.log(heritageCookieValue) console.log(heritageCookieValue)
Object.keys(cookies).forEach(key => { 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}`; document.cookie = `${key}=${cookieValue}`;
}); });
} }

View file

@ -4,7 +4,7 @@ import { storeActions } from "@/constants/store-actions";
import { lazyLoadComponent } from "@/router/dynamic-routing/component-loader.js"; import { lazyLoadComponent } from "@/router/dynamic-routing/component-loader.js";
import { routingTable } from "@/router/router-constants/routing-table.js"; import { routingTable } from "@/router/router-constants/routing-table.js";
import { globalEvents, globalEventTypes } from "@/constants/events"; 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 baseMixin from "@/mixins/base-mixin";
import eventBus from "@/helpers/event-bus/event-bus"; import eventBus from "@/helpers/event-bus/event-bus";
@ -35,6 +35,10 @@ const routes = [
} else { } else {
try { 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. // On entering the concept funnel "fresh", read cookie information, decide what to do next.
if (from.redirectedFrom === undefined) { if (from.redirectedFrom === undefined) {
await loadReferralFromHeritageFunnelIfPresent(); await loadReferralFromHeritageFunnelIfPresent();
@ -134,9 +138,9 @@ async function navigate(scenario, currentRoute, invalidateOnSave, optionalQuery
baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData); baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData);
// if cookie and referralNumber exists // if cookie and referralNumber exists
if (getHeritageCookieValue()?.ReferralNumber) { // if (getHeritageCookieValue()?.ReferralNumber) {
await saveOrder(); // await saveOrder();
} // }
router.push({ router.push({
name: "root", name: "root",

View file

@ -367,7 +367,7 @@ export const actions = {
correlationId: correlationId correlationId: correlationId
}, },
}).then( (response) => { }).then( (response) => {
context.commit(storeMutations.SET_LOAD_ORDER_INFO, response.data); context.commit(storeMutations.SET_LOAD_CONCEPT_SESSION_INFO, response.data);
return response; return response;
}); });
} }