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 = {
CONSUMER_APIGATEWAY_URL: process.env.VUE_APP_CONSUMER_API_GATEWAY,
SESSION_TIMEOUT_CONFIG: 30
};
export { applicationConfig };

View file

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

View file

@ -34,7 +34,7 @@ const storeMutations = {
// OTHER MUTATIONS
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 { 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 */

View file

@ -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}`;
});
}

View file

@ -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",

View file

@ -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;
});
}