diff --git a/src/helpers/promotions-helper.js b/src/helpers/promotions-helper.js index 996ca1aa6..1c5170f14 100644 --- a/src/helpers/promotions-helper.js +++ b/src/helpers/promotions-helper.js @@ -246,6 +246,17 @@ export function getVapsThatNeedToBeAddedToSatisfyPromos( } } +export function removeCurrentlyActivePromoCodesFromInactivePromos(activePromoObjects, inactivePromos) { + activePromoObjects = activePromoObjects ?? []; + inactivePromos = inactivePromos ?? []; + const activePromoCodes = activePromoObjects.map((promoObject) => + getPromoCodeWithoutBundleIdentifier(promoObject.promoCode).toUpperCase() + ); + return inactivePromos.filter((inactivePromo) => { + return !activePromoCodes.includes(inactivePromo.toUpperCase()); + }); +} + // bundle promos look like: "bundlePromo/###"" for each promo, this returns "bundlePromo" only export function getPromoCodeWithoutBundleIdentifier(promoCode) { return promoCode.split("/")[0]; diff --git a/src/store/index.js b/src/store/index.js index 4b4c4ca6d..678a48cbb 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -23,7 +23,8 @@ import { getDisplayTextForDurationLength, } from "@/layouts/schedule/helpers/schedule-helper"; import { paymentMethods } from "@/constants/payment-method-constants"; -import { getPromoCodeWithoutBundleIdentifier } from "@/helpers/promotions-helper"; +import { getPromoCodeWithoutBundleIdentifier, + removeCurrentlyActivePromoCodesFromInactivePromos } from "@/helpers/promotions-helper"; import { getDateDifferenceInDays } from "@/helpers/date-helper"; // Export State const getDefaultState = () => { @@ -2120,14 +2121,6 @@ export const actions = { addGuidToLineItemsIfNotAlreadyThere(vaps); context.commit(storeMutations.UPDATE_VAPS, vaps); }, - // COMBINE THESE - savePromos(context, promos) { - addGuidToLineItemsIfNotAlreadyThere(promos); - context.commit(storeMutations.UPDATE_PROMOS, promos); - }, - saveInactivePromos(context, inactivePromos) { - context.commit(storeMutations.UPDATE_INACTIVE_PROMOS, inactivePromos); - }, // Manage promo saving to ensure a promoCode never ends up in both active and inactive saveActiveAndOrInactivePromos(context, { activePromos = null, inactivePromos = null }) { let activePromosToSave; @@ -2139,12 +2132,7 @@ export const actions = { } else if (activePromos && inactivePromos) { // Prioritize active promos when both inactive and active are supplied activePromosToSave = activePromos; - const activePromoCodes = activePromos.map((promoObject) => - getPromoCodeWithoutBundleIdentifier(promoObject.promoCode) - ); - inactivePromosToSave = inactivePromos.filter((inactivePromo) => { - return !activePromoCodes.includes(inactivePromo); - }); + inactivePromosToSave = removeCurrentlyActivePromoCodesFromInactivePromos(activePromosToSave, inactivePromos); } else if (!activePromos) { // Only inactivePromos supplied inactivePromosToSave = inactivePromos; @@ -2156,14 +2144,7 @@ export const actions = { } else if (!inactivePromos) { // Only activePromos supplied activePromosToSave = activePromos; - const activePromoCodes = activePromos.map((promoObject) => - getPromoCodeWithoutBundleIdentifier(promoObject.promoCode) - ); - inactivePromosToSave = (context.getters.payment.inactivePromos ?? []).filter( - (inactivePromo) => { - return !activePromoCodes.includes(inactivePromo); - } - ); + inactivePromosToSave = removeCurrentlyActivePromoCodesFromInactivePromos(activePromosToSave, context.getters.payment.inactivePromos ?? []) } context.commit(storeMutations.UPDATE_PROMOS, activePromosToSave); context.commit(storeMutations.UPDATE_INACTIVE_PROMOS, inactivePromosToSave); @@ -2361,10 +2342,12 @@ export const actions = { const order = context.getters.order; activePromosToUse = activePromosToUse ?? order.lineItems.promos; - inactivePromosToUse = inactivePromosToUse ?? order.payment.inactivePromos; lineItemsToUse = lineItemsToUse ? deepClone(lineItemsToUse) : deepClone(order.lineItems); lineItemsToUse.promos = activePromosToUse; + inactivePromosToUse = inactivePromosToUse ?? order.payment.inactivePromos; + inactivePromosToUse = removeCurrentlyActivePromoCodesFromInactivePromos(lineItemsToUse.promos, inactivePromosToUse); + let requestObject = { inactivePromos: inactivePromosToUse, order: {