DigitalConsumer.FixMyGlass/src/router/methods/before-each.js
2025-05-13 16:37:04 -04:00

106 lines
3.5 KiB
JavaScript

import { queryStrings } from "@/constants/query-strings";
import { sessionStorageKeyConstants } from "@/constants/session-storage";
import {
getFunnelCookie,
updateOrCreateFunnelCookie,
} from "@/helpers/heritage-integration/cookie-helper";
import { loadSessionIfPresent } from "@/helpers/heritage-integration/order-helper";
import { isSavedSessionStillActive } from "@/helpers/heritage-integration/session-helper";
import analyticsMixin from "@/mixins/analytics-mixin";
import { routeData, FUNNEL_START_PAGE } from "@/router/constants/routes";
import { consumeQueryFromStash } from "@/router/methods/helpers/querystring-stash";
import { runExperiments } from "@/router/methods/helpers/run-experiments";
import { bailout } from "@/router/methods/error";
import { checkPagePrerequisites } from "@/router/methods/page-prerequisites";
import { checkLogParam } from "@/helpers/debug-log-helper";
import { debugLog } from "@/helpers/debug-log-helper";
export async function beforeEach(to, from) {
try {
await analyticsMixin.methods.validateSession();
// prettier-ignore
{
debugLog(`--- before-each.js ${from?.name} start ---`);
debugLog(" funnel cookie init: ", getFunnelCookie());
}
if (getFunnelCookie()?.SuppressConceptFunnel) {
// Redirect to heritage.
return {
name: routeData.HERITAGE.name,
};
}
// Ensure session has not expired
if (getFunnelCookie() !== null && !isSavedSessionStillActive()) {
const errorPayload = {
cause: "expired session",
currentPage: from?.name,
nextPage: to?.name,
};
bailout(errorPayload, true);
return false;
}
// Block navigation if an order has been submitted.
if (window.sessionStorage.getItem(sessionStorageKeyConstants.SUBMITTED_STATE) !== null) {
if (to.name !== FUNNEL_START_PAGE.name && to.name !== routeData.CONFIRMATION.name) {
return false;
}
}
// Process funnel cookie.
updateOrCreateFunnelCookie();
// prettier-ignore
{
debugLog(`--- before-each.js ${from?.name} start ---`);
debugLog(" funnel cookie after: ", getFunnelCookie());
}
// If sent from heritage, need to load session.
const fromHeritageFlag = consumeQueryFromStash(queryStrings.FROM_HERITAGE);
if (fromHeritageFlag) {
await loadSessionIfPresent(true, routeData.SERVICE_LOCATION.name);
}
// Update if logging is enabled.
checkLogParam();
// Ensure page-prerequisites are fulfilled
const arePagePrerequisitesValid = await checkPagePrerequisites(to.name);
if (!arePagePrerequisitesValid) {
const errorPayload = {
cause: "invalid page prerequisites for page",
currentPage: from?.name,
nextPage: to?.name,
};
bailout(errorPayload);
return;
}
await runExperiments(to.name);
// prettier-ignore
{
debugLog("--- before-each.js end ---");
}
} catch (error) {
const errorPayload = {
cause: "Uncaught exception in `beforeEach`.",
currentPage: from?.name,
nextPage: to?.name,
};
console.log(error);
bailout(errorPayload);
return;
}
}