DigitalConsumer.FixMyGlass/src/helpers/heritage-integration-helper.js

157 lines
5.7 KiB
JavaScript

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";
// Saves Referral if one is available and commits referral details to state.
export async function saveOrder() {
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,
referralCorrelationId: savedOrderInfo.data.referralCorrelationId,
referralDate: savedOrderInfo.data.referralDate,
}, 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) {
console.log("loading order...", referralNumber, referralDate, correlationId);
await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOAD_ORDER,
{ referralNumber: referralNumber.toString(), 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("calling load order from loadReferralIfPresent()...");
// 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().toUTCString(),
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=/`;
// Set up cookie with all the props.
setConceptCookieProperties({
LastTouched: new Date().toUTCString(),
DidHeritageFunnelUpdateLast: false,
ShouldResetState: 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;
console.log("has session expired -->", isMoreThanHalfHourAgo);
if (isMoreThanHalfHourAgo) {
return false;
}
return true;
}
}
// 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 */
export function getConceptCookie() {
const cookieJson = document.cookie
?.split("; ")
?.find(row => row.startsWith(`${cookieNames.CONCEPT_SESSION_INFO}=`))
?.split("=")[1];
try {
return JSON.parse(cookieJson);
} catch (error) {
return null;
}
}
// --------- PRIVATE FUNCTIONS ---------
function setConceptCookieProperties(properties) {
if (typeof properties == "object") {
let cookie = getConceptCookie();
if (cookie !== null) {
Object.keys(properties).forEach(key => {
cookie[key] = properties[key];
});
const cookieValueJson = JSON.stringify(cookie);
document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}=${cookieValueJson}; path=/`;
}
}
}
function deleteConceptCookie() {
// 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 */