CSR-1452 | Refactor and reformat

This commit is contained in:
Scott Kiener 2023-11-09 15:17:47 -05:00
parent 7eea3baa13
commit f72ce16936
3 changed files with 114 additions and 69 deletions

View file

@ -17,7 +17,7 @@ export const addableVapsPromoPartNumbers = [
];
const promoErrorCodes = {
CANNOT_COMBINE: "SimilarPromoAlreadyOnOrder"
CANNOT_COMBINE: "SimilarPromoAlreadyOnOrder",
};
export const pagesToStripPromoQueryStringFrom = ["quote", "payment-method"];
@ -114,70 +114,54 @@ export async function revalidatePromosAndValidateNewPromo(
return { validatePromoResponse, revalidatePromoResponse };
}
export function buildToastMessagesFromRevalidateOrValidatePromoResponse(promoResponse, oldActivePromos = [], oldInactivePromos = []) {
export function buildToastMessagesFromRevalidateOrValidatePromoResponse(
promoResponse,
oldActivePromos = [],
oldInactivePromos = []
) {
const alerts = [];
// Revalidate always has an "errors" array
if (promoResponse.errors) {
const oldPromoCodes = oldActivePromos.map(promo => getPromoCodeWithoutBundleIdentifier(promo.promoCode));
const activePromoCodesFromResponse = [];
// consolidate bundle promos to avoid displaying multiple toast messages for one bundle
promoResponse.promoLineItems.forEach(newPromo => {
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(newPromo.promoCode);
if (!activePromoCodesFromResponse.includes(promoCodeToDisplay)) {
activePromoCodesFromResponse.push(promoCodeToDisplay);
}
});
const inactivePromoCodesFromResponse = [];
promoResponse.errors.forEach(newInactive => {
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(newInactive.promoCode);
if (!inactivePromoCodesFromResponse.includes(promoCodeToDisplay)) {
inactivePromoCodesFromResponse.push(promoCodeToDisplay);
}
});
const newlyActivatedPromoCodes = activePromoCodesFromResponse.filter(newPromoCode => !oldPromoCodes.includes(newPromoCode));
newlyActivatedPromoCodes.forEach(newlyActivatedPromoCode => {
alerts.push({
messageHeadline: "Promo applied!",
messageCopy: `Promo "${newlyActivatedPromoCode}" was successfully applied to your cart.`,
type: "alert-success",
isDismissible: true,
shouldAutoFade: true,
});
const oldPromoCodes = oldActivePromos.map((promo) =>
getPromoCodeWithoutBundleIdentifier(promo.promoCode)
);
const activePromoCodesFromResponse = getPromoCodesFromPromoObjectsWithoutDuplicates(
promoResponse.promoLineItems
);
const newlyActivatedPromoCodes = activePromoCodesFromResponse.filter(
(newPromoCode) => !oldPromoCodes.includes(newPromoCode)
);
newlyActivatedPromoCodes.forEach((newlyActivatedPromoCode) => {
alerts.push(createPromoSuccessAlert(newlyActivatedPromoCode));
});
const newlyInactivatedPromos = inactivePromoCodesFromResponse.filter(errorPromoCode => !oldInactivePromos.includes(errorPromoCode));
newlyInactivatedPromos.forEach(newlyInactivatedPromo => {
alerts.push({
messageHeadline: "Promo error",
messageCopy: `Sorry, promo code "${newlyInactivatedPromo}" is not valid`,
type: "alert-danger",
isDismissible: true,
shouldAutoFade: false,
});
const inactivePromoCodesFromResponse = getPromoCodesFromPromoObjectsWithoutDuplicates(
promoResponse.errors
);
const newlyInactivatedPromos = inactivePromoCodesFromResponse.filter(
(errorPromoCode) => !oldInactivePromos.includes(errorPromoCode)
);
newlyInactivatedPromos.forEach((newlyInactivatedPromo) => {
alerts.push(createPromoErrorAlert(newlyInactivatedPromo));
});
} else {
}
// validate response
else {
if (promoResponse.orderPromos) {
let promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(promoResponse.orderPromos[0].promoCode);
alerts.push({
messageHeadline: "Promo applied!",
messageCopy: `Promo "${promoCodeToDisplay}" was successfully applied to your cart.`,
type: "alert-success",
isDismissible: true,
shouldAutoFade: true,
});
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(
promoResponse.orderPromos[0].promoCode
);
alerts.push(createPromoSuccessAlert(promoCodeToDisplay));
} else {
const alertToPush = {
messageHeadline: "Promo error",
type: "alert-danger",
shouldAutoFade: false,
isDismissible: true,
}
if (promoResponse.errorCode == promoErrorCodes.CANNOT_COMBINE) {
alertToPush.messageCopy = `Sorry, promo code "${promoResponse.promoCode}" cannot be combined with ${promoResponse.additionalInfo[0]}`;
} else {
alertToPush.messageCopy = `Sorry, promo code "${promoResponse.promoCode}" is not valid`;
}
alerts.push(alertToPush);
alerts.push(
createPromoErrorAlert(
promoResponse.promoCode,
promoResponse.errorCode,
promoResponse.additionalInfo
)
);
}
}
return alerts;
@ -241,3 +225,42 @@ function findLineItemsWithPartType(typeToFind, itemsToSearch) {
);
return partTypeMatches;
}
// returns a list of promo codes (string) only without duplicates or bundle identifiers
// <promos> parameter must be an array of objects with a 'promoCode' field
function getPromoCodesFromPromoObjectsWithoutDuplicates(promos) {
const promoCodes = [];
promos.forEach((promo) => {
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(promo.promoCode);
if (!promoCodes.includes(promoCodeToDisplay)) {
promoCodes.push(promoCodeToDisplay);
}
});
return promoCodes;
}
function createPromoSuccessAlert(promoCode) {
return {
messageHeadline: "Promo applied!",
messageCopy: `Promo "${promoCode}" was successfully applied to your cart.`,
type: "alert-success",
isDismissible: true,
shouldAutoFade: true,
};
}
function createPromoErrorAlert(promoCode, errorCode = null, additionalInfo) {
const alert = {
messageHeadline: "Promo error",
type: "alert-danger",
shouldAutoFade: false,
isDismissible: true,
};
if (errorCode == promoErrorCodes.CANNOT_COMBINE) {
alert.messageCopy = `Sorry, promo code "${promoCode}" cannot be combined with ${additionalInfo[0]}`;
} else {
alert.messageCopy = `Sorry, promo code "${promoCode}" is not valid`;
}
return alert;
}

View file

@ -214,15 +214,23 @@ export default {
vm.updateFooterButtonText(vm.customCtaCopy);
if (revalidatePromoResponse) {
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(revalidatePromoResponse, oldActivePromos, oldInactivePromos);
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(
revalidatePromoResponse,
oldActivePromos,
oldInactivePromos
);
revalidateAlerts.forEach(alert => {
revalidateAlerts.forEach((alert) => {
vm.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade);
});
}
if (validatePromoResponse) {
const validateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse);
vm.$refs.funnelHeader.pushGlobalAlert(validateAlerts[0], validateAlerts[0].shouldAutoFade);
const validateAlerts =
buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse);
vm.$refs.funnelHeader.pushGlobalAlert(
validateAlerts[0],
validateAlerts[0].shouldAutoFade
);
}
});
},
@ -317,8 +325,12 @@ export default {
"payment-method",
false
);
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(revalidatePromoResponse, this.lineItems.promos, this.inactivePromos);
revalidateAlerts.forEach(alert => {
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(
revalidatePromoResponse,
this.lineItems.promos,
this.inactivePromos
);
revalidateAlerts.forEach((alert) => {
this.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade);
});

View file

@ -80,8 +80,10 @@ import { Form, defineRule } from "vee-validate";
import { navigateToHeritageFunnel } from "@/helpers/heritage-integration/navigation-helper";
import { applicationConfig } from "@/constants/application-config";
import { payWithInsuranceStates } from "@/constants/pay-with-insurance-states";
import { revalidatePromosAndValidateNewPromo,
buildToastMessagesFromRevalidateOrValidatePromoResponse } from "@/helpers/promotions-helper";
import {
revalidatePromosAndValidateNewPromo,
buildToastMessagesFromRevalidateOrValidatePromoResponse,
} from "@/helpers/promotions-helper";
import { queryStrings } from "@/constants/query-strings";
import { getQuerystringParameter } from "@/helpers/querystring-helper";
@ -176,14 +178,22 @@ export default {
vm.newValidatedPromos = validatePromoResponse?.orderPromos;
if (revalidatePromoResponse) {
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(revalidatePromoResponse, oldActivePromos, oldInactivePromos);
revalidateAlerts.forEach(alert => {
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(
revalidatePromoResponse,
oldActivePromos,
oldInactivePromos
);
revalidateAlerts.forEach((alert) => {
vm.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade);
});
}
if (validatePromoResponse) {
const validateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse);
vm.$refs.funnelHeader.pushGlobalAlert(validateAlerts[0], validateAlerts[0].shouldAutoFade);
const validateAlerts =
buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse);
vm.$refs.funnelHeader.pushGlobalAlert(
validateAlerts[0],
validateAlerts[0].shouldAutoFade
);
}
});
},