more integration helper changes

This commit is contained in:
FrankRua 2022-03-22 08:41:02 -04:00
parent c6dac0bb96
commit c77c8e12a2
3 changed files with 93 additions and 69 deletions

View file

@ -7,71 +7,64 @@ 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
export async function loadReferralFromHeritageFunnelIfPresent() {
// const orderInfo = getHeritageCookieValue();
// // 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;
// }
// // 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.
// 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() {
await saveOrder();
const referralCorrelationId = store.state.order.referralCorrelationId;
router.navigate(
navigationScenarios.MOVE_TO_HERITAGE_FUNNEL,
router.currentRoute.value,
{
corid: referralCorrelationId,
src: "concept-funnel",
// TODO CSR-98 REMOVE THIS
cns: "all",
experiments: "RemoveServiceAreaPage=ServAreaRemoval_V7=ServAreaNoRemove_V7_TEST=true,ConceptFunnel=ConceptFunnel_V1=ConceptFunnel_TEST=true"
}
);
}
// 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.
export async function saveOrder() { export async function saveOrder() {
// const savedOrderInfo = await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.SAVE_ORDER); console.log("saving 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. // Update the cookie with the referral information when saved.
// setHeritageCookieProperties({ updateOrCreateConceptCookie();
// 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.
export async function loadOrder(referralNumber, referralDate, correlationId) { export async function loadOrder(referralNumber, referralDate, correlationId) {
const loadOrderResponse = await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOAD_ORDER, console.log("loading order...", referralNumber, referralDate, correlationId);
await baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOAD_ORDER,
{ referralNumber: referralNumber, referralDate: referralDate, correlationId: correlationId } { referralNumber: referralNumber, referralDate: referralDate, correlationId: correlationId }
, false); , 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("Loading order...");
// 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() { export function updateOrCreateConceptCookie() {
console.log("Updating cookie...", {
LastTouched: new Date(),
DidHeritageFunnelUpdateLast: false,
ShouldResetState: false,
ReferralNumber: store.state.order.referralNumber,
ReferralDate: store.state.order.referralDate,
ReferralCorrelationId: store.state.order.referralCorrelationId,
});
// Create the cookie // Create the cookie
document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}={}; path=/`; document.cookie = `${cookieNames.CONCEPT_SESSION_INFO}={}; path=/`;
@ -79,6 +72,7 @@ export function updateOrCreateConceptCookie() {
setConceptCookieProperties({ setConceptCookieProperties({
LastTouched: new Date(), LastTouched: new Date(),
DidHeritageFunnelUpdateLast: false, DidHeritageFunnelUpdateLast: false,
ShouldResetState: false,
ReferralNumber: store.state.order.referralNumber, ReferralNumber: store.state.order.referralNumber,
ReferralDate: store.state.order.referralDate, ReferralDate: store.state.order.referralDate,
ReferralCorrelationId: store.state.order.referralCorrelationId, ReferralCorrelationId: store.state.order.referralCorrelationId,
@ -92,6 +86,8 @@ export function isConceptSessionStillActive() {
const timeoutAmount = applicationConfig.SESSION_TIMEOUT_CONFIG; const timeoutAmount = applicationConfig.SESSION_TIMEOUT_CONFIG;
const isMoreThanHalfHourAgo = ((new Date() - new Date(lastTouchedValue)) / 60000) > timeoutAmount; const isMoreThanHalfHourAgo = ((new Date() - new Date(lastTouchedValue)) / 60000) > timeoutAmount;
console.log("has session expired -->", isMoreThanHalfHourAgo);
if (isMoreThanHalfHourAgo) { if (isMoreThanHalfHourAgo) {
return false; return false;
} }
@ -100,10 +96,27 @@ export function isConceptSessionStillActive() {
} }
} }
// --------- PRIVATE FUNCTIONS --------- // 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 */ /* Start cookie related functions */
function getConceptCookie() { export function getConceptCookie() {
const cookieJson = document.cookie const cookieJson = document.cookie
?.split("; ") ?.split("; ")
?.find(row => row.startsWith(`${cookieNames.CONCEPT_SESSION_INFO}=`)) ?.find(row => row.startsWith(`${cookieNames.CONCEPT_SESSION_INFO}=`))
@ -116,6 +129,11 @@ function getConceptCookie() {
} }
} }
// --------- PRIVATE FUNCTIONS ---------
function setConceptCookieProperties(properties) { function setConceptCookieProperties(properties) {
if (typeof properties == "object") { if (typeof properties == "object") {
let cookie = getConceptCookie(); let cookie = getConceptCookie();

View file

@ -19,7 +19,7 @@ describe("saveOrder", () => {
} }
} }
describe("loadReferralFromHeritageFunnelIfPresent", () => { describe("loadReferralIfPresent", () => {
// test("ShouldResetState == true => heritage cookie is deleted", () => { // test("ShouldResetState == true => heritage cookie is deleted", () => {
// // Arrange // // Arrange
// // TODO CSR-98 Can we not mock this?? // // TODO CSR-98 Can we not mock this??
@ -42,7 +42,7 @@ describe("saveOrder", () => {
// console.log(document.cookie) // console.log(document.cookie)
// // Act // // Act
// helper.loadReferralFromHeritageFunnelIfPresent(); // helper.loadReferralIfPresent();
// // Assert // // Assert
// console.log(document.cookie) // console.log(document.cookie)
@ -54,7 +54,7 @@ describe("saveOrder", () => {
// helper.getHeritageCookieValue = jest.fn(x => x.ShouldResetState = false); // helper.getHeritageCookieValue = jest.fn(x => x.ShouldResetState = false);
// // Act // // Act
// helper.loadReferralFromHeritageFunnelIfPresent(); // helper.loadReferralIfPresent();
// // Assert // // Assert
// expect(helper.getHeritageCookieValue).toHaveBeenCalled(); // expect(helper.getHeritageCookieValue).toHaveBeenCalled();
@ -65,7 +65,7 @@ describe("saveOrder", () => {
// helper.getHeritageCookieValue = jest.fn(x => x.ShouldResetState = true); // helper.getHeritageCookieValue = jest.fn(x => x.ShouldResetState = true);
// // Act // // Act
// helper.loadReferralFromHeritageFunnelIfPresent(); // helper.loadReferralIfPresent();
// // Assert // // Assert
// expect(helper.getHeritageCookieValue).toHaveBeenCalled(); // expect(helper.getHeritageCookieValue).toHaveBeenCalled();

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 { updateOrCreateConceptCookie, loadReferralFromHeritageFunnelIfPresent, isConceptSessionStillActive} from "@/helpers/heritage-integration-helper"; import * as integrationHelper 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,13 +35,19 @@ const routes = [
} else { } else {
try { try {
// Check our 'Session' is still good, update the cookie. // Check our 'Session' is still good.
isConceptSessionStillActive(); // If not, reset state and go back to the start.
updateOrCreateConceptCookie(); if (!integrationHelper.isConceptSessionStillActive()) {
baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.RESET_STATE);
await GoToFunnelStartOn404(next);
}
// Create funnel cookie, or update it if it already exists.
integrationHelper.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 integrationHelper.loadReferralIfPresent();
} }
// If we already have our route, go to it. // If we already have our route, go to it.
@ -137,10 +143,10 @@ async function navigate(scenario, currentRoute, invalidateOnSave, optionalQuery
// Append page data to the store for the NEXT page, if any. It will be an empty object if none is provided. // Append page data to the store for the NEXT page, if any. It will be an empty object if none is provided.
baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData); baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData);
// if cookie and referralNumber exists // if cookie and referralNumber/Date exists
// if (getHeritageCookieValue()?.ReferralNumber) { if (integrationHelper.getConceptCookie()?.ReferralNumber && integrationHelper.getConceptCookie()?.ReferralDate) {
// await saveOrder(); await integrationHelper.saveOrder();
// } }
router.push({ router.push({
name: "root", name: "root",