@managesome/eposmatic-toolkit 0.2.317 → 0.2.318
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ORDER_STATUS_ENUM, PLATFORMS_ENUM, SORT_TYPE_ENUM } from '../../constants';
|
|
2
|
-
import { ICourse, IDiscountDetails, IGeneralItem, IItem, IModifier, IOrder, IOrderCategory, IOrderCourse, IOrderDeliveryDetails, IOrderItem, IOrderIteration } from '../../schemas';
|
|
1
|
+
import { GIFT_CARD_REWARD_TYPE_ENUM, ORDER_STATUS_ENUM, PLATFORMS_ENUM, SORT_TYPE_ENUM } from '../../constants';
|
|
2
|
+
import { ICourse, IDiscountDetails, IGeneralItem, IItem, IModifier, IOffer, IOrder, IOrderCategory, IOrderCourse, IOrderDeliveryDetails, IOrderItem, IOrderIteration } from '../../schemas';
|
|
3
3
|
import { ICartListnerSettings } from '../types';
|
|
4
4
|
import { ICartItemsObj } from '../types/cart-actions';
|
|
5
5
|
interface IGetItemQuantityChangeResultsParams {
|
|
@@ -38,13 +38,21 @@ interface ICalculateGstForItem {
|
|
|
38
38
|
apply_service_fee_on_original_price: boolean | undefined;
|
|
39
39
|
}
|
|
40
40
|
export declare function calculateGstForItem({ subtotal, totalOrderDiscount, applyGSTOnOriginalPrice, is_gst_inclusive, gst, totalItemsDiscount, grossAmount, netAmount, payablePrice, originalItemsPrice, surcharge, is_surcharge_taxable, is_surcharge_percentage, service_fee, is_service_fee_taxable, is_service_fee_percentage, apply_service_fee_on_original_price, }: ICalculateGstForItem): number;
|
|
41
|
-
export
|
|
41
|
+
export interface ICartOfferSyncContext {
|
|
42
|
+
unlockedOffersApplicableTimes: Record<string, number>;
|
|
43
|
+
offers: IOffer[];
|
|
44
|
+
}
|
|
45
|
+
export declare function calculateCartOrderInfoAndSyncData<T extends IOrder | Partial<IOrder>>(data: T, courses?: ICourse[], listnerSettings?: ICartListnerSettings, calculateItemMetadata?: boolean, syncContext?: ICartOfferSyncContext): T;
|
|
42
46
|
export interface ISetAddItemToCartActionPayload {
|
|
43
47
|
name: string;
|
|
44
48
|
category_id: string;
|
|
45
49
|
item: IOrderItem;
|
|
46
50
|
chosen_course_id: string | null;
|
|
47
51
|
replace_id?: string;
|
|
52
|
+
walletGiftCardClaim?: {
|
|
53
|
+
giftCardId: string;
|
|
54
|
+
mysteryGiftType?: GIFT_CARD_REWARD_TYPE_ENUM | null;
|
|
55
|
+
};
|
|
48
56
|
}
|
|
49
57
|
export declare const getOrderItemComaparableData: (item: IOrderItem) => Partial<IOrderItem>;
|
|
50
58
|
export declare function getAddItemToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
@@ -152,6 +160,7 @@ export interface IGetCartItemsObjPayload {
|
|
|
152
160
|
export declare function getCartItemsObj({ currentIterationCategories, currentDineinCourses, previousIterationCategories, previousDineinCourses, mappedItems, shouldSortItemsViaId, platform, isAnEdit, sortType, }: IGetCartItemsObjPayload): ICartItemsObj;
|
|
153
161
|
export declare function getRemoveOfferFromCartData(data: Partial<IOrder>, offer_id: string, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
154
162
|
export declare function getRemoveAllOffersFromCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offerIds: string[] | undefined): Partial<IOrder>;
|
|
163
|
+
export declare function getClearVoucherCodeAndOffersFromCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offerIds: string[] | undefined): Partial<IOrder>;
|
|
155
164
|
/**
|
|
156
165
|
* Calculate the eligible subtotal by excluding items/categories in excluded_refs
|
|
157
166
|
*/
|
|
@@ -27,11 +27,13 @@ exports.getChangeCourseStatusData = getChangeCourseStatusData;
|
|
|
27
27
|
exports.getCartItemsObj = getCartItemsObj;
|
|
28
28
|
exports.getRemoveOfferFromCartData = getRemoveOfferFromCartData;
|
|
29
29
|
exports.getRemoveAllOffersFromCartData = getRemoveAllOffersFromCartData;
|
|
30
|
+
exports.getClearVoucherCodeAndOffersFromCartData = getClearVoucherCodeAndOffersFromCartData;
|
|
30
31
|
exports.calculateEligibleOfferDiscountDetails = calculateEligibleOfferDiscountDetails;
|
|
31
32
|
const bson_objectid_1 = __importDefault(require("bson-objectid"));
|
|
32
33
|
const lodash_1 = require("lodash");
|
|
33
34
|
const constants_1 = require("../../constants");
|
|
34
35
|
const utils_1 = require("../utils");
|
|
36
|
+
const offers_helpers_1 = require("../utils/offers-helpers");
|
|
35
37
|
const platform_catalog_1 = require("../utils/platform-catalog");
|
|
36
38
|
function getItemQuantityChangeResults({ prevQuantity, newQuantity, prevItemDiscountDetails, }) {
|
|
37
39
|
let item_discount_details = prevItemDiscountDetails;
|
|
@@ -598,11 +600,467 @@ function getSyncedWalletCashbackOrderDiscountDetails({ orderDiscountDetails, wal
|
|
|
598
600
|
return {
|
|
599
601
|
...orderDiscountDetails,
|
|
600
602
|
loyalty_discount_id: constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID,
|
|
603
|
+
loyalty_discount_name: constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_NAME,
|
|
601
604
|
loyalty_discount: (0, utils_1.parseNum)(walletCashbackAmount),
|
|
602
605
|
is_loyalty_discount_percentage: false,
|
|
606
|
+
loyalty_reference_number: constants_1.WALLET_CASHBACK_LOYALTY_REFERENCE,
|
|
603
607
|
};
|
|
604
608
|
}
|
|
605
|
-
function
|
|
609
|
+
function getOrderSubtotalFromCategoriesForOfferSync(categories) {
|
|
610
|
+
if (!categories?.length)
|
|
611
|
+
return 0;
|
|
612
|
+
let subtotal = 0;
|
|
613
|
+
for (let i = 0; i < categories.length; i++) {
|
|
614
|
+
const items = categories[i].items;
|
|
615
|
+
for (let j = 0; j < items.length; j++) {
|
|
616
|
+
subtotal += items[j].subtotal ?? 0;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return subtotal;
|
|
620
|
+
}
|
|
621
|
+
function isRedeemedOffersMapNormalizedForOfferSync(redeemedOffers) {
|
|
622
|
+
if (redeemedOffers == null) {
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
const normalizedRedeemedOffers = getRedeemedOffersMap(redeemedOffers);
|
|
626
|
+
const rawOfferIds = Object.keys(redeemedOffers);
|
|
627
|
+
const normalizedOfferIds = Object.keys(normalizedRedeemedOffers ?? {});
|
|
628
|
+
if (rawOfferIds.length !== normalizedOfferIds.length) {
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
for (let i = 0; i < rawOfferIds.length; i++) {
|
|
632
|
+
const rawOfferId = rawOfferIds[i];
|
|
633
|
+
const normalizedOfferId = getNormalizedOfferId(rawOfferId);
|
|
634
|
+
const safeCount = toPositiveInteger(redeemedOffers[rawOfferId], 0);
|
|
635
|
+
if (normalizedOfferId == null || rawOfferId !== normalizedOfferId || safeCount <= 0) {
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
if ((normalizedRedeemedOffers?.[normalizedOfferId] ?? 0) !== safeCount) {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
644
|
+
function areRedeemedOffersEqualForOfferSync(currentRedeemedOffers, nextRedeemedOffers) {
|
|
645
|
+
const normalizedCurrentRedeemedOffers = getRedeemedOffersMap(currentRedeemedOffers);
|
|
646
|
+
const normalizedNextRedeemedOffers = getRedeemedOffersMap(nextRedeemedOffers);
|
|
647
|
+
if (normalizedCurrentRedeemedOffers == null || normalizedNextRedeemedOffers == null) {
|
|
648
|
+
return normalizedCurrentRedeemedOffers == null && normalizedNextRedeemedOffers == null;
|
|
649
|
+
}
|
|
650
|
+
const currentOfferIds = Object.keys(normalizedCurrentRedeemedOffers);
|
|
651
|
+
const nextOfferIds = Object.keys(normalizedNextRedeemedOffers);
|
|
652
|
+
if (currentOfferIds.length !== nextOfferIds.length) {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
for (let i = 0; i < currentOfferIds.length; i++) {
|
|
656
|
+
const offerId = currentOfferIds[i];
|
|
657
|
+
if (normalizedCurrentRedeemedOffers[offerId] !== normalizedNextRedeemedOffers[offerId]) {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return true;
|
|
662
|
+
}
|
|
663
|
+
function getItemRewardApplicableQuantityForOfferSync(offer) {
|
|
664
|
+
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
|
|
665
|
+
return toPositiveInteger(offer.reward_items?.num_of_items, 1);
|
|
666
|
+
}
|
|
667
|
+
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
|
|
668
|
+
return toPositiveInteger(offer.reward_discount_items?.num_of_items, 1);
|
|
669
|
+
}
|
|
670
|
+
return null;
|
|
671
|
+
}
|
|
672
|
+
function getTotalSelectedOfferItemsAgainstOfferForOfferSync(categories, offerId) {
|
|
673
|
+
let totalSelectedOfferItems = 0;
|
|
674
|
+
for (let i = 0; i < categories.length; i++) {
|
|
675
|
+
const category = categories[i];
|
|
676
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
677
|
+
const item = category.items[j];
|
|
678
|
+
const discountDetails = item.item_discount_details;
|
|
679
|
+
if (discountDetails == null)
|
|
680
|
+
continue;
|
|
681
|
+
const loyaltyDiscountId = getNormalizedOfferId(discountDetails.loyalty_discount_id);
|
|
682
|
+
const voucherDiscountId = getNormalizedOfferId(discountDetails.voucher_discount_id);
|
|
683
|
+
if (loyaltyDiscountId !== offerId && voucherDiscountId !== offerId)
|
|
684
|
+
continue;
|
|
685
|
+
totalSelectedOfferItems += toPositiveInteger(item.quantity, 0);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return totalSelectedOfferItems;
|
|
689
|
+
}
|
|
690
|
+
function getReconciledRedeemedOffersMapForOfferSync({ existingRedeemedOffers, categories, offers, removedOfferIds, unlockedOffersApplicableTimes, }) {
|
|
691
|
+
const nextRedeemedOffers = {
|
|
692
|
+
...(getRedeemedOffersMap(existingRedeemedOffers) ?? {}),
|
|
693
|
+
};
|
|
694
|
+
const removedIds = Array.from(removedOfferIds);
|
|
695
|
+
for (let i = 0; i < removedIds.length; i++) {
|
|
696
|
+
const normalizedOfferId = getNormalizedOfferId(removedIds[i]);
|
|
697
|
+
if (normalizedOfferId == null)
|
|
698
|
+
continue;
|
|
699
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
700
|
+
}
|
|
701
|
+
for (let i = 0; i < offers.length; i++) {
|
|
702
|
+
const offer = offers[i];
|
|
703
|
+
const normalizedOfferId = getNormalizedOfferId(offer._id);
|
|
704
|
+
if (normalizedOfferId == null)
|
|
705
|
+
continue;
|
|
706
|
+
const applicableQuantity = getItemRewardApplicableQuantityForOfferSync(offer);
|
|
707
|
+
if (applicableQuantity != null) {
|
|
708
|
+
const selectedItemsCount = getTotalSelectedOfferItemsAgainstOfferForOfferSync(categories, normalizedOfferId);
|
|
709
|
+
// Any partial usage of an item reward still counts as one redeemed slot.
|
|
710
|
+
const redeemedTimes = selectedItemsCount > 0 ? Math.ceil(selectedItemsCount / applicableQuantity) : 0;
|
|
711
|
+
if (redeemedTimes > 0) {
|
|
712
|
+
nextRedeemedOffers[normalizedOfferId] = redeemedTimes;
|
|
713
|
+
}
|
|
714
|
+
else {
|
|
715
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
716
|
+
}
|
|
717
|
+
continue;
|
|
718
|
+
}
|
|
719
|
+
const existingRedeemedTimes = toPositiveInteger(nextRedeemedOffers[normalizedOfferId], 0);
|
|
720
|
+
if (existingRedeemedTimes <= 0) {
|
|
721
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
const maxApplicableTimes = toPositiveInteger(unlockedOffersApplicableTimes?.[offer._id], 0);
|
|
725
|
+
if (maxApplicableTimes <= 0) {
|
|
726
|
+
// Keep non-item claim history when unlock context is temporarily unavailable.
|
|
727
|
+
nextRedeemedOffers[normalizedOfferId] = existingRedeemedTimes;
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
nextRedeemedOffers[normalizedOfferId] = Math.min(existingRedeemedTimes, maxApplicableTimes);
|
|
731
|
+
}
|
|
732
|
+
return getRedeemedOffersMap(nextRedeemedOffers);
|
|
733
|
+
}
|
|
734
|
+
function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
735
|
+
const unlockedOffersMap = {
|
|
736
|
+
...syncContext.unlockedOffersApplicableTimes,
|
|
737
|
+
};
|
|
738
|
+
const removedOfferIds = new Set();
|
|
739
|
+
let hasChanges = false;
|
|
740
|
+
const categories = data.categories ?? [];
|
|
741
|
+
const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategoriesForOfferSync(categories);
|
|
742
|
+
let order_discount_details = data.order_discount_details;
|
|
743
|
+
let redeemed_offers = getRedeemedOffersMap(data.redeemed_offers);
|
|
744
|
+
if (!isRedeemedOffersMapNormalizedForOfferSync(data.redeemed_offers)) {
|
|
745
|
+
hasChanges = true;
|
|
746
|
+
}
|
|
747
|
+
const hasOrderLevelLoyaltyDiscount = order_discount_details?.loyalty_discount_id != null;
|
|
748
|
+
const hasOrderLevelVoucherDiscount = order_discount_details?.voucher_discount_id != null;
|
|
749
|
+
// Empty carts should not keep offer-driven discount slots or redeemed-offers state.
|
|
750
|
+
if (orderLevelDiscountBaseAmount <= 0) {
|
|
751
|
+
if (hasOrderLevelLoyaltyDiscount && order_discount_details != null) {
|
|
752
|
+
order_discount_details = {
|
|
753
|
+
...order_discount_details,
|
|
754
|
+
loyalty_discount_id: undefined,
|
|
755
|
+
loyalty_discount_name: undefined,
|
|
756
|
+
loyalty_discount: undefined,
|
|
757
|
+
is_loyalty_discount_percentage: false,
|
|
758
|
+
loyalty_reference_number: undefined,
|
|
759
|
+
total_discount: 0,
|
|
760
|
+
};
|
|
761
|
+
hasChanges = true;
|
|
762
|
+
}
|
|
763
|
+
if (hasOrderLevelVoucherDiscount && order_discount_details != null) {
|
|
764
|
+
order_discount_details = {
|
|
765
|
+
...order_discount_details,
|
|
766
|
+
voucher_discount_id: undefined,
|
|
767
|
+
voucher_discount_name: undefined,
|
|
768
|
+
voucher_discount: undefined,
|
|
769
|
+
is_voucher_discount_percentage: false,
|
|
770
|
+
total_discount: 0,
|
|
771
|
+
};
|
|
772
|
+
hasChanges = true;
|
|
773
|
+
}
|
|
774
|
+
if (redeemed_offers != null) {
|
|
775
|
+
redeemed_offers = null;
|
|
776
|
+
hasChanges = true;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
const redeemedOffersPresenceMap = getReconciledRedeemedOffersMapForOfferSync({
|
|
780
|
+
existingRedeemedOffers: redeemed_offers,
|
|
781
|
+
categories,
|
|
782
|
+
offers: syncContext.offers,
|
|
783
|
+
removedOfferIds: new Set(),
|
|
784
|
+
unlockedOffersApplicableTimes: syncContext.unlockedOffersApplicableTimes,
|
|
785
|
+
});
|
|
786
|
+
const clearOrderLevelDiscount = (discountType) => {
|
|
787
|
+
if (order_discount_details == null) {
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
const appliedOfferId = discountType === 'loyalty'
|
|
791
|
+
? order_discount_details.loyalty_discount_id
|
|
792
|
+
: order_discount_details.voucher_discount_id;
|
|
793
|
+
const normalizedAppliedOfferId = getNormalizedOfferId(appliedOfferId);
|
|
794
|
+
if (normalizedAppliedOfferId != null) {
|
|
795
|
+
removedOfferIds.add(normalizedAppliedOfferId);
|
|
796
|
+
}
|
|
797
|
+
if (discountType === 'loyalty') {
|
|
798
|
+
order_discount_details = {
|
|
799
|
+
...order_discount_details,
|
|
800
|
+
loyalty_discount_id: undefined,
|
|
801
|
+
loyalty_discount_name: undefined,
|
|
802
|
+
loyalty_discount: undefined,
|
|
803
|
+
is_loyalty_discount_percentage: false,
|
|
804
|
+
loyalty_reference_number: undefined,
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
order_discount_details = {
|
|
809
|
+
...order_discount_details,
|
|
810
|
+
voucher_discount_id: undefined,
|
|
811
|
+
voucher_discount_name: undefined,
|
|
812
|
+
voucher_discount: undefined,
|
|
813
|
+
is_voucher_discount_percentage: false,
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
hasChanges = true;
|
|
817
|
+
};
|
|
818
|
+
if (order_discount_details != null) {
|
|
819
|
+
const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
|
|
820
|
+
if (loyaltyDiscountId != null) {
|
|
821
|
+
if (loyaltyDiscountId !== constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
|
|
822
|
+
const loyaltyOffer = syncContext.offers.find((o) => o._id === loyaltyDiscountId);
|
|
823
|
+
// Preserve non-offer loyalty discounts (e.g. gift-card discount IDs).
|
|
824
|
+
if (loyaltyOffer != null) {
|
|
825
|
+
const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
|
|
826
|
+
const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
|
|
827
|
+
if (!hasRedeemedEntryForLoyalty) {
|
|
828
|
+
clearOrderLevelDiscount('loyalty');
|
|
829
|
+
}
|
|
830
|
+
else if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
831
|
+
clearOrderLevelDiscount('loyalty');
|
|
832
|
+
}
|
|
833
|
+
else {
|
|
834
|
+
if (loyaltyOffer.reward_discount != null) {
|
|
835
|
+
const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = loyaltyOffer.reward_discount;
|
|
836
|
+
const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
|
|
837
|
+
? calculateEligibleOfferDiscountDetails(categories, excluded_refs)
|
|
838
|
+
: {
|
|
839
|
+
subtotal: orderLevelDiscountBaseAmount,
|
|
840
|
+
hasExcludedRefs: false,
|
|
841
|
+
};
|
|
842
|
+
if (hasExcludedRefs && !is_discount_percentage) {
|
|
843
|
+
clearOrderLevelDiscount('loyalty');
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
const hasRelevantCap = is_discount_percentage &&
|
|
847
|
+
max_discount_amount != null &&
|
|
848
|
+
Number.isFinite(max_discount_amount) &&
|
|
849
|
+
max_discount_amount >= 0;
|
|
850
|
+
const hasRelevantPercentageCap = !is_discount_percentage &&
|
|
851
|
+
max_discount_percentage != null &&
|
|
852
|
+
Number.isFinite(max_discount_percentage) &&
|
|
853
|
+
max_discount_percentage >= 0;
|
|
854
|
+
const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
|
|
855
|
+
const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
|
|
856
|
+
if (shouldNormalizeToStatic) {
|
|
857
|
+
if (discountBaseAmount <= 0) {
|
|
858
|
+
clearOrderLevelDiscount('loyalty');
|
|
859
|
+
}
|
|
860
|
+
else {
|
|
861
|
+
const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
|
|
862
|
+
baseAmount: discountBaseAmount,
|
|
863
|
+
discountValue: discount_value,
|
|
864
|
+
isDiscountPercentage: is_discount_percentage,
|
|
865
|
+
maxDiscountAmount: max_discount_amount,
|
|
866
|
+
maxDiscountPercentage: max_discount_percentage,
|
|
867
|
+
});
|
|
868
|
+
order_discount_details = {
|
|
869
|
+
...order_discount_details,
|
|
870
|
+
loyalty_discount: (0, utils_1.parseNum)(calculatedDiscount),
|
|
871
|
+
is_loyalty_discount_percentage: false,
|
|
872
|
+
};
|
|
873
|
+
hasChanges = true;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
unlockedOffersMap[loyaltyDiscountId] -= 1;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
const voucherDiscountId = order_discount_details.voucher_discount_id;
|
|
884
|
+
if (voucherDiscountId != null) {
|
|
885
|
+
const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
|
|
886
|
+
const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
|
|
887
|
+
if (!hasRedeemedEntryForVoucher) {
|
|
888
|
+
clearOrderLevelDiscount('voucher');
|
|
889
|
+
}
|
|
890
|
+
else if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
891
|
+
clearOrderLevelDiscount('voucher');
|
|
892
|
+
}
|
|
893
|
+
else {
|
|
894
|
+
const voucherOffer = syncContext.offers.find((o) => o._id === voucherDiscountId);
|
|
895
|
+
if (voucherOffer?.reward_discount != null) {
|
|
896
|
+
const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = voucherOffer.reward_discount;
|
|
897
|
+
const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
|
|
898
|
+
? calculateEligibleOfferDiscountDetails(categories, excluded_refs)
|
|
899
|
+
: {
|
|
900
|
+
subtotal: orderLevelDiscountBaseAmount,
|
|
901
|
+
hasExcludedRefs: false,
|
|
902
|
+
};
|
|
903
|
+
if (hasExcludedRefs && !is_discount_percentage) {
|
|
904
|
+
clearOrderLevelDiscount('voucher');
|
|
905
|
+
}
|
|
906
|
+
else {
|
|
907
|
+
const hasRelevantCap = is_discount_percentage &&
|
|
908
|
+
max_discount_amount != null &&
|
|
909
|
+
Number.isFinite(max_discount_amount) &&
|
|
910
|
+
max_discount_amount >= 0;
|
|
911
|
+
const hasRelevantPercentageCap = !is_discount_percentage &&
|
|
912
|
+
max_discount_percentage != null &&
|
|
913
|
+
Number.isFinite(max_discount_percentage) &&
|
|
914
|
+
max_discount_percentage >= 0;
|
|
915
|
+
const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
|
|
916
|
+
const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
|
|
917
|
+
if (shouldNormalizeToStatic) {
|
|
918
|
+
if (discountBaseAmount <= 0) {
|
|
919
|
+
clearOrderLevelDiscount('voucher');
|
|
920
|
+
}
|
|
921
|
+
else {
|
|
922
|
+
const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
|
|
923
|
+
baseAmount: discountBaseAmount,
|
|
924
|
+
discountValue: discount_value,
|
|
925
|
+
isDiscountPercentage: is_discount_percentage,
|
|
926
|
+
maxDiscountAmount: max_discount_amount,
|
|
927
|
+
maxDiscountPercentage: max_discount_percentage,
|
|
928
|
+
});
|
|
929
|
+
order_discount_details = {
|
|
930
|
+
...order_discount_details,
|
|
931
|
+
voucher_discount: (0, utils_1.parseNum)(calculatedDiscount),
|
|
932
|
+
is_voucher_discount_percentage: false,
|
|
933
|
+
};
|
|
934
|
+
hasChanges = true;
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
unlockedOffersMap[voucherDiscountId] -= 1;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
const newCategories = [];
|
|
944
|
+
for (let i = 0; i < categories.length; i++) {
|
|
945
|
+
const category = categories[i];
|
|
946
|
+
const newItems = [];
|
|
947
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
948
|
+
const item = category.items[j];
|
|
949
|
+
if (item.item_discount_details != null) {
|
|
950
|
+
const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
|
|
951
|
+
if (loyaltyDiscountId != null) {
|
|
952
|
+
const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
|
|
953
|
+
const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
|
|
954
|
+
if (!hasRedeemedEntryForLoyalty) {
|
|
955
|
+
hasChanges = true;
|
|
956
|
+
if (normalizedLoyaltyDiscountId != null) {
|
|
957
|
+
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
958
|
+
}
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
962
|
+
hasChanges = true;
|
|
963
|
+
if (normalizedLoyaltyDiscountId != null) {
|
|
964
|
+
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
965
|
+
}
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
unlockedOffersMap[loyaltyDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
969
|
+
}
|
|
970
|
+
const voucherDiscountId = item.item_discount_details.voucher_discount_id;
|
|
971
|
+
if (voucherDiscountId != null) {
|
|
972
|
+
const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
|
|
973
|
+
const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
|
|
974
|
+
if (!hasRedeemedEntryForVoucher) {
|
|
975
|
+
hasChanges = true;
|
|
976
|
+
if (normalizedVoucherDiscountId != null) {
|
|
977
|
+
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
978
|
+
}
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
982
|
+
hasChanges = true;
|
|
983
|
+
if (normalizedVoucherDiscountId != null) {
|
|
984
|
+
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
985
|
+
}
|
|
986
|
+
continue;
|
|
987
|
+
}
|
|
988
|
+
unlockedOffersMap[voucherDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
newItems.push(item);
|
|
992
|
+
}
|
|
993
|
+
newCategories.push({
|
|
994
|
+
...category,
|
|
995
|
+
items: newItems,
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
const reconciledRedeemedOffers = getReconciledRedeemedOffersMapForOfferSync({
|
|
999
|
+
existingRedeemedOffers: redeemed_offers,
|
|
1000
|
+
categories: newCategories,
|
|
1001
|
+
offers: syncContext.offers,
|
|
1002
|
+
removedOfferIds,
|
|
1003
|
+
unlockedOffersApplicableTimes: syncContext.unlockedOffersApplicableTimes,
|
|
1004
|
+
});
|
|
1005
|
+
if (!areRedeemedOffersEqualForOfferSync(redeemed_offers, reconciledRedeemedOffers)) {
|
|
1006
|
+
redeemed_offers = reconciledRedeemedOffers;
|
|
1007
|
+
hasChanges = true;
|
|
1008
|
+
}
|
|
1009
|
+
if (!hasChanges) {
|
|
1010
|
+
return null;
|
|
1011
|
+
}
|
|
1012
|
+
return {
|
|
1013
|
+
...data,
|
|
1014
|
+
categories: newCategories,
|
|
1015
|
+
order_discount_details,
|
|
1016
|
+
redeemed_offers,
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
function hasItemLevelLoyaltyDiscountForId(categories, discountId) {
|
|
1020
|
+
for (let categoryIndex = 0; categoryIndex < categories.length; categoryIndex++) {
|
|
1021
|
+
const items = categories[categoryIndex].items;
|
|
1022
|
+
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
1023
|
+
if (items[itemIndex].item_discount_details?.loyalty_discount_id === discountId) {
|
|
1024
|
+
return true;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
function getSyncedWalletRedemptionsAfterGiftCardItemRewardCleanup({ walletRedemptions, orderDiscountDetails, categories, }) {
|
|
1031
|
+
const claimedGiftCardId = walletRedemptions?.gift_cards?.gift_card_id?.trim();
|
|
1032
|
+
const claimedTimes = Number(walletRedemptions?.gift_cards?.value_redeemed ?? 0);
|
|
1033
|
+
if (claimedGiftCardId == null ||
|
|
1034
|
+
claimedGiftCardId.length === 0 ||
|
|
1035
|
+
!Number.isFinite(claimedTimes) ||
|
|
1036
|
+
claimedTimes <= 0) {
|
|
1037
|
+
return walletRedemptions;
|
|
1038
|
+
}
|
|
1039
|
+
const rewardType = walletRedemptions?.gift_cards?.mystery_gift_type;
|
|
1040
|
+
const isItemRewardType = rewardType === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.ITEMS || rewardType === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.DISCOUNT_ITEMS;
|
|
1041
|
+
if (!isItemRewardType) {
|
|
1042
|
+
return walletRedemptions;
|
|
1043
|
+
}
|
|
1044
|
+
const hasOrderLevelDiscount = getNormalizedOfferId(orderDiscountDetails?.loyalty_discount_id) === claimedGiftCardId;
|
|
1045
|
+
const hasItemLevelDiscount = hasItemLevelLoyaltyDiscountForId(categories, claimedGiftCardId);
|
|
1046
|
+
if (hasOrderLevelDiscount || hasItemLevelDiscount) {
|
|
1047
|
+
return walletRedemptions;
|
|
1048
|
+
}
|
|
1049
|
+
return getWalletRedemptionsMap({
|
|
1050
|
+
...walletRedemptions,
|
|
1051
|
+
gift_cards: undefined,
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calculateItemMetadata, syncContext) {
|
|
1055
|
+
if (syncContext?.offers != null && syncContext.unlockedOffersApplicableTimes != null) {
|
|
1056
|
+
const offerSynchronizedData = getOfferSynchronizedCartDataForCalculation({
|
|
1057
|
+
data,
|
|
1058
|
+
syncContext,
|
|
1059
|
+
});
|
|
1060
|
+
if (offerSynchronizedData != null) {
|
|
1061
|
+
return calculateCartOrderInfoAndSyncData(offerSynchronizedData, courses, listnerSettings, calculateItemMetadata);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
606
1064
|
const isCompleted = data.order_status === constants_1.ORDER_STATUS_ENUM.COMPLETED;
|
|
607
1065
|
/* ===============================><=============================== */
|
|
608
1066
|
let subtotal = 0;
|
|
@@ -680,6 +1138,11 @@ function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calcu
|
|
|
680
1138
|
cashback_amount: undefined,
|
|
681
1139
|
});
|
|
682
1140
|
}
|
|
1141
|
+
syncedWalletRedemptions = getSyncedWalletRedemptionsAfterGiftCardItemRewardCleanup({
|
|
1142
|
+
walletRedemptions: syncedWalletRedemptions,
|
|
1143
|
+
orderDiscountDetails: syncedOrderDiscountDetails,
|
|
1144
|
+
categories,
|
|
1145
|
+
});
|
|
683
1146
|
const walletGiftCardRedeemedTimes = Number(syncedWalletRedemptions?.gift_cards?.value_redeemed ?? 0);
|
|
684
1147
|
const hasWalletGiftCardMetadata = syncedWalletRedemptions?.gift_cards?.gift_card_id != null &&
|
|
685
1148
|
syncedWalletRedemptions.gift_cards.gift_card_id.trim().length > 0 &&
|
|
@@ -962,6 +1425,41 @@ function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calcu
|
|
|
962
1425
|
}
|
|
963
1426
|
return syncedData;
|
|
964
1427
|
}
|
|
1428
|
+
function getOrderDataWithSyncedWalletGiftCardClaim({ data, walletGiftCardClaim, }) {
|
|
1429
|
+
if (walletGiftCardClaim == null) {
|
|
1430
|
+
return data;
|
|
1431
|
+
}
|
|
1432
|
+
const claimedGiftCardId = walletGiftCardClaim.giftCardId?.toString().trim();
|
|
1433
|
+
if (claimedGiftCardId == null || claimedGiftCardId.length === 0) {
|
|
1434
|
+
return data;
|
|
1435
|
+
}
|
|
1436
|
+
const normalizedWalletRedemptions = getWalletRedemptionsMap(data.wallet_redemptions);
|
|
1437
|
+
const currentClaimGiftCardId = normalizedWalletRedemptions?.gift_cards?.gift_card_id?.trim();
|
|
1438
|
+
const currentClaimCount = toPositiveInteger(normalizedWalletRedemptions?.gift_cards?.value_redeemed, 0);
|
|
1439
|
+
const hasExistingClaimForGiftCard = currentClaimGiftCardId === claimedGiftCardId && currentClaimCount > 0;
|
|
1440
|
+
if (hasExistingClaimForGiftCard) {
|
|
1441
|
+
return data;
|
|
1442
|
+
}
|
|
1443
|
+
const nextWalletRedemptions = getWalletRedemptionsMap({
|
|
1444
|
+
...(normalizedWalletRedemptions ?? {}),
|
|
1445
|
+
gift_cards: {
|
|
1446
|
+
gift_card_id: claimedGiftCardId,
|
|
1447
|
+
value_redeemed: 1,
|
|
1448
|
+
...(walletGiftCardClaim.mysteryGiftType != null
|
|
1449
|
+
? {
|
|
1450
|
+
mystery_gift_type: walletGiftCardClaim.mysteryGiftType,
|
|
1451
|
+
}
|
|
1452
|
+
: {}),
|
|
1453
|
+
},
|
|
1454
|
+
});
|
|
1455
|
+
if ((0, lodash_1.isEqual)(nextWalletRedemptions, normalizedWalletRedemptions)) {
|
|
1456
|
+
return data;
|
|
1457
|
+
}
|
|
1458
|
+
return {
|
|
1459
|
+
...data,
|
|
1460
|
+
wallet_redemptions: nextWalletRedemptions,
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
965
1463
|
const getOrderItemComaparableData = (item) => ({
|
|
966
1464
|
item_id: item.item_id,
|
|
967
1465
|
menu_id: item.menu_id,
|
|
@@ -994,7 +1492,7 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
|
|
|
994
1492
|
modifiers: item.modifiers,
|
|
995
1493
|
};
|
|
996
1494
|
};
|
|
997
|
-
const { category_id, name, item, chosen_course_id, replace_id } = payload;
|
|
1495
|
+
const { category_id, name, item, chosen_course_id, replace_id, walletGiftCardClaim } = payload;
|
|
998
1496
|
const comparableItem = getComparableItem(item);
|
|
999
1497
|
let mainItemId = item._id;
|
|
1000
1498
|
const orderData = { ...data };
|
|
@@ -1156,7 +1654,11 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
|
|
|
1156
1654
|
...(iterations.length > 0 && { iterations }),
|
|
1157
1655
|
categories,
|
|
1158
1656
|
};
|
|
1159
|
-
const
|
|
1657
|
+
const orderWithSyncedWalletGiftCardClaim = getOrderDataWithSyncedWalletGiftCardClaim({
|
|
1658
|
+
data: order,
|
|
1659
|
+
walletGiftCardClaim,
|
|
1660
|
+
});
|
|
1661
|
+
const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedWalletGiftCardClaim, courses, listnerSettings);
|
|
1160
1662
|
return newData;
|
|
1161
1663
|
}
|
|
1162
1664
|
function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }) {
|
|
@@ -2265,6 +2767,13 @@ function getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds
|
|
|
2265
2767
|
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
2266
2768
|
return newData;
|
|
2267
2769
|
}
|
|
2770
|
+
function getClearVoucherCodeAndOffersFromCartData(data, courses, listnerSettings, offerIds) {
|
|
2771
|
+
const orderWithoutOffers = getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds);
|
|
2772
|
+
const nextOrderDiscountDetails = (0, offers_helpers_1.buildClearVoucherDiscountDetails)(orderWithoutOffers.order_discount_details);
|
|
2773
|
+
return getUpdateCartInfoData(orderWithoutOffers, {
|
|
2774
|
+
order_discount_details: nextOrderDiscountDetails,
|
|
2775
|
+
}, courses, listnerSettings);
|
|
2776
|
+
}
|
|
2268
2777
|
// ================================><================================= //
|
|
2269
2778
|
/**
|
|
2270
2779
|
* Calculate the eligible subtotal by excluding items/categories in excluded_refs
|
|
@@ -512,369 +512,8 @@ function getWalletRedemptionsMap(existingWalletRedemptions) {
|
|
|
512
512
|
}
|
|
513
513
|
function getSyncOffersWithCartData(data, courses, listnerSettings, unlockedOffersApplicableTimes, // Record<offer_id, times can be applied>
|
|
514
514
|
offers) {
|
|
515
|
-
|
|
516
|
-
...unlockedOffersApplicableTimes,
|
|
517
|
-
};
|
|
518
|
-
const removedOfferIds = new Set();
|
|
519
|
-
let hasChanges = false;
|
|
520
|
-
const categories = data.categories ?? [];
|
|
521
|
-
const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategories(categories);
|
|
522
|
-
let order_discount_details = data.order_discount_details;
|
|
523
|
-
let redeemed_offers = getNormalizedRedeemedOffersMap(data.redeemed_offers);
|
|
524
|
-
let wallet_redemptions = getNormalizedWalletRedemptionsMap(data.wallet_redemptions);
|
|
525
|
-
if (!isRedeemedOffersMapNormalized(data.redeemed_offers)) {
|
|
526
|
-
hasChanges = true;
|
|
527
|
-
}
|
|
528
|
-
const previousWalletCashbackAmount = (0, common_1.parseNum)(Number(wallet_redemptions?.cashback_amount ?? 0));
|
|
529
|
-
const hasWalletCashbackLoyaltyDiscount = isWalletCashbackLoyaltyDiscount(order_discount_details);
|
|
530
|
-
if (hasWalletCashbackLoyaltyDiscount) {
|
|
531
|
-
const requestedWalletCashbackAmount = getWalletCashbackAmountFromOrderData({
|
|
532
|
-
orderDiscountDetails: order_discount_details,
|
|
533
|
-
walletRedemptions: wallet_redemptions,
|
|
534
|
-
});
|
|
535
|
-
const maxApplicableWalletCashback = (0, common_1.parseNum)(Math.max(0, orderLevelDiscountBaseAmount));
|
|
536
|
-
const syncedWalletCashbackAmount = (0, common_1.parseNum)(Math.min(requestedWalletCashbackAmount, maxApplicableWalletCashback));
|
|
537
|
-
const prevLoyaltyDiscountId = order_discount_details?.loyalty_discount_id;
|
|
538
|
-
const prevLoyaltyDiscountAmount = (0, common_1.parseNum)(Number(order_discount_details?.loyalty_discount ?? 0));
|
|
539
|
-
const prevIsLoyaltyDiscountPercentage = order_discount_details?.is_loyalty_discount_percentage === true;
|
|
540
|
-
const prevLoyaltyReferenceNumber = order_discount_details?.loyalty_reference_number;
|
|
541
|
-
const nextOrderDiscountDetails = getSyncedWalletCashbackOrderDiscountDetails({
|
|
542
|
-
orderDiscountDetails: order_discount_details,
|
|
543
|
-
walletCashbackAmount: syncedWalletCashbackAmount,
|
|
544
|
-
});
|
|
545
|
-
const nextLoyaltyDiscountId = nextOrderDiscountDetails?.loyalty_discount_id;
|
|
546
|
-
const nextLoyaltyDiscountAmount = (0, common_1.parseNum)(Number(nextOrderDiscountDetails?.loyalty_discount ?? 0));
|
|
547
|
-
const nextIsLoyaltyDiscountPercentage = nextOrderDiscountDetails?.is_loyalty_discount_percentage === true;
|
|
548
|
-
const nextLoyaltyReferenceNumber = nextOrderDiscountDetails?.loyalty_reference_number;
|
|
549
|
-
if (prevLoyaltyDiscountId !== nextLoyaltyDiscountId ||
|
|
550
|
-
prevLoyaltyDiscountAmount !== nextLoyaltyDiscountAmount ||
|
|
551
|
-
prevIsLoyaltyDiscountPercentage !== nextIsLoyaltyDiscountPercentage ||
|
|
552
|
-
prevLoyaltyReferenceNumber !== nextLoyaltyReferenceNumber) {
|
|
553
|
-
hasChanges = true;
|
|
554
|
-
}
|
|
555
|
-
order_discount_details = nextOrderDiscountDetails;
|
|
556
|
-
wallet_redemptions = getNormalizedWalletRedemptionsMap({
|
|
557
|
-
...(wallet_redemptions ?? {}),
|
|
558
|
-
cashback_amount: syncedWalletCashbackAmount > 0 ? syncedWalletCashbackAmount : undefined,
|
|
559
|
-
});
|
|
560
|
-
const nextWalletCashbackAmount = (0, common_1.parseNum)(Number(wallet_redemptions?.cashback_amount ?? 0));
|
|
561
|
-
if (previousWalletCashbackAmount !== nextWalletCashbackAmount) {
|
|
562
|
-
hasChanges = true;
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
else if (previousWalletCashbackAmount > 0) {
|
|
566
|
-
// Offers can reward cashback into wallet, but cannot activate cashback discount on current order.
|
|
567
|
-
wallet_redemptions = getNormalizedWalletRedemptionsMap({
|
|
568
|
-
...(wallet_redemptions ?? {}),
|
|
569
|
-
cashback_amount: undefined,
|
|
570
|
-
});
|
|
571
|
-
hasChanges = true;
|
|
572
|
-
}
|
|
573
|
-
const hasOrderLevelLoyaltyDiscount = order_discount_details?.loyalty_discount_id != null;
|
|
574
|
-
const hasOrderLevelVoucherDiscount = order_discount_details?.voucher_discount_id != null;
|
|
575
|
-
const walletGiftCardRedeemedTimes = Number(wallet_redemptions?.gift_cards?.value_redeemed ?? 0);
|
|
576
|
-
const hasWalletGiftCardMetadata = wallet_redemptions?.gift_cards?.gift_card_id != null &&
|
|
577
|
-
wallet_redemptions.gift_cards.gift_card_id.trim().length > 0 &&
|
|
578
|
-
Number.isFinite(walletGiftCardRedeemedTimes) &&
|
|
579
|
-
walletGiftCardRedeemedTimes > 0;
|
|
580
|
-
// Empty carts must not retain reward-driven loyalty/voucher discounts or gift-card wallet claims.
|
|
581
|
-
if (orderLevelDiscountBaseAmount <= 0) {
|
|
582
|
-
if (hasOrderLevelLoyaltyDiscount && order_discount_details != null) {
|
|
583
|
-
order_discount_details = {
|
|
584
|
-
...order_discount_details,
|
|
585
|
-
loyalty_discount_id: undefined,
|
|
586
|
-
loyalty_discount_name: undefined,
|
|
587
|
-
loyalty_discount: undefined,
|
|
588
|
-
is_loyalty_discount_percentage: false,
|
|
589
|
-
loyalty_reference_number: undefined,
|
|
590
|
-
total_discount: 0,
|
|
591
|
-
};
|
|
592
|
-
hasChanges = true;
|
|
593
|
-
}
|
|
594
|
-
if (hasOrderLevelVoucherDiscount && order_discount_details != null) {
|
|
595
|
-
order_discount_details = {
|
|
596
|
-
...order_discount_details,
|
|
597
|
-
voucher_discount_id: undefined,
|
|
598
|
-
voucher_discount_name: undefined,
|
|
599
|
-
voucher_discount: undefined,
|
|
600
|
-
is_voucher_discount_percentage: false,
|
|
601
|
-
total_discount: 0,
|
|
602
|
-
};
|
|
603
|
-
hasChanges = true;
|
|
604
|
-
}
|
|
605
|
-
if (redeemed_offers != null) {
|
|
606
|
-
redeemed_offers = null;
|
|
607
|
-
hasChanges = true;
|
|
608
|
-
}
|
|
609
|
-
if (hasWalletGiftCardMetadata) {
|
|
610
|
-
wallet_redemptions = getNormalizedWalletRedemptionsMap({
|
|
611
|
-
...(wallet_redemptions ?? {}),
|
|
612
|
-
gift_cards: undefined,
|
|
613
|
-
});
|
|
614
|
-
hasChanges = true;
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
const redeemedOffersPresenceMap = getReconciledRedeemedOffersMap({
|
|
618
|
-
existingRedeemedOffers: redeemed_offers,
|
|
619
|
-
categories,
|
|
620
|
-
offers,
|
|
621
|
-
removedOfferIds: new Set(),
|
|
515
|
+
return (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(data, courses, listnerSettings, undefined, {
|
|
622
516
|
unlockedOffersApplicableTimes,
|
|
623
|
-
});
|
|
624
|
-
const clearOrderLevelDiscount = (discountType) => {
|
|
625
|
-
if (order_discount_details == null) {
|
|
626
|
-
return;
|
|
627
|
-
}
|
|
628
|
-
const appliedOfferId = discountType === 'loyalty'
|
|
629
|
-
? order_discount_details.loyalty_discount_id
|
|
630
|
-
: order_discount_details.voucher_discount_id;
|
|
631
|
-
const normalizedAppliedOfferId = getNormalizedOfferId(appliedOfferId);
|
|
632
|
-
if (normalizedAppliedOfferId != null) {
|
|
633
|
-
removedOfferIds.add(normalizedAppliedOfferId);
|
|
634
|
-
}
|
|
635
|
-
if (discountType === 'loyalty') {
|
|
636
|
-
order_discount_details = {
|
|
637
|
-
...order_discount_details,
|
|
638
|
-
loyalty_discount_id: undefined,
|
|
639
|
-
loyalty_discount_name: undefined,
|
|
640
|
-
loyalty_discount: undefined,
|
|
641
|
-
is_loyalty_discount_percentage: false,
|
|
642
|
-
loyalty_reference_number: undefined,
|
|
643
|
-
};
|
|
644
|
-
}
|
|
645
|
-
else {
|
|
646
|
-
order_discount_details = {
|
|
647
|
-
...order_discount_details,
|
|
648
|
-
voucher_discount_id: undefined,
|
|
649
|
-
voucher_discount_name: undefined,
|
|
650
|
-
voucher_discount: undefined,
|
|
651
|
-
is_voucher_discount_percentage: false,
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
hasChanges = true;
|
|
655
|
-
};
|
|
656
|
-
if (order_discount_details != null) {
|
|
657
|
-
// we check if either the loyalty_discount_id or voucher_discount_id
|
|
658
|
-
const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
|
|
659
|
-
if (loyaltyDiscountId != null) {
|
|
660
|
-
if (loyaltyDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
|
|
661
|
-
// Wallet cashback is synced separately above and does not participate in offer unlock accounting.
|
|
662
|
-
}
|
|
663
|
-
else {
|
|
664
|
-
const loyaltyOffer = offers.find((o) => o._id === loyaltyDiscountId);
|
|
665
|
-
if (loyaltyOffer == null) {
|
|
666
|
-
// Preserve non-offer loyalty discounts (for example gift-card discounts).
|
|
667
|
-
}
|
|
668
|
-
else {
|
|
669
|
-
const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
|
|
670
|
-
const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
|
|
671
|
-
if (!hasRedeemedEntryForLoyalty) {
|
|
672
|
-
clearOrderLevelDiscount('loyalty');
|
|
673
|
-
}
|
|
674
|
-
else if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
675
|
-
clearOrderLevelDiscount('loyalty');
|
|
676
|
-
}
|
|
677
|
-
else {
|
|
678
|
-
if (loyaltyOffer.reward_discount != null) {
|
|
679
|
-
const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = loyaltyOffer.reward_discount;
|
|
680
|
-
const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
|
|
681
|
-
? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
|
|
682
|
-
: {
|
|
683
|
-
subtotal: orderLevelDiscountBaseAmount,
|
|
684
|
-
hasExcludedRefs: false,
|
|
685
|
-
};
|
|
686
|
-
// Static discounts with exclusions are not supported - silently skip
|
|
687
|
-
if (hasExcludedRefs && !is_discount_percentage) {
|
|
688
|
-
clearOrderLevelDiscount('loyalty');
|
|
689
|
-
}
|
|
690
|
-
else {
|
|
691
|
-
const hasRelevantCap = is_discount_percentage &&
|
|
692
|
-
max_discount_amount != null &&
|
|
693
|
-
Number.isFinite(max_discount_amount) &&
|
|
694
|
-
max_discount_amount >= 0;
|
|
695
|
-
const hasRelevantPercentageCap = !is_discount_percentage &&
|
|
696
|
-
max_discount_percentage != null &&
|
|
697
|
-
Number.isFinite(max_discount_percentage) &&
|
|
698
|
-
max_discount_percentage >= 0;
|
|
699
|
-
const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
|
|
700
|
-
const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
|
|
701
|
-
if (shouldNormalizeToStatic) {
|
|
702
|
-
if (discountBaseAmount <= 0) {
|
|
703
|
-
clearOrderLevelDiscount('loyalty');
|
|
704
|
-
}
|
|
705
|
-
else {
|
|
706
|
-
const calculatedDiscount = (0, common_1.getCappedOfferDiscountAmount)({
|
|
707
|
-
baseAmount: discountBaseAmount,
|
|
708
|
-
discountValue: discount_value,
|
|
709
|
-
isDiscountPercentage: is_discount_percentage,
|
|
710
|
-
maxDiscountAmount: max_discount_amount,
|
|
711
|
-
maxDiscountPercentage: max_discount_percentage,
|
|
712
|
-
});
|
|
713
|
-
order_discount_details = {
|
|
714
|
-
...order_discount_details,
|
|
715
|
-
loyalty_discount: (0, common_1.parseNum)(calculatedDiscount),
|
|
716
|
-
is_loyalty_discount_percentage: false,
|
|
717
|
-
};
|
|
718
|
-
hasChanges = true;
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
unlockedOffersMap[loyaltyDiscountId] -= 1;
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
const voucherDiscountId = order_discount_details.voucher_discount_id;
|
|
729
|
-
if (voucherDiscountId != null) {
|
|
730
|
-
const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
|
|
731
|
-
const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
|
|
732
|
-
if (!hasRedeemedEntryForVoucher) {
|
|
733
|
-
clearOrderLevelDiscount('voucher');
|
|
734
|
-
}
|
|
735
|
-
else if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
736
|
-
clearOrderLevelDiscount('voucher');
|
|
737
|
-
}
|
|
738
|
-
else {
|
|
739
|
-
// Find the offer to check for excluded_refs
|
|
740
|
-
const voucherOffer = offers.find((o) => o._id === voucherDiscountId);
|
|
741
|
-
if (voucherOffer?.reward_discount != null) {
|
|
742
|
-
const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = voucherOffer.reward_discount;
|
|
743
|
-
const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
|
|
744
|
-
? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
|
|
745
|
-
: {
|
|
746
|
-
subtotal: orderLevelDiscountBaseAmount,
|
|
747
|
-
hasExcludedRefs: false,
|
|
748
|
-
};
|
|
749
|
-
// Static discounts with exclusions are not supported - silently skip
|
|
750
|
-
if (hasExcludedRefs && !is_discount_percentage) {
|
|
751
|
-
clearOrderLevelDiscount('voucher');
|
|
752
|
-
}
|
|
753
|
-
else {
|
|
754
|
-
const hasRelevantCap = is_discount_percentage &&
|
|
755
|
-
max_discount_amount != null &&
|
|
756
|
-
Number.isFinite(max_discount_amount) &&
|
|
757
|
-
max_discount_amount >= 0;
|
|
758
|
-
const hasRelevantPercentageCap = !is_discount_percentage &&
|
|
759
|
-
max_discount_percentage != null &&
|
|
760
|
-
Number.isFinite(max_discount_percentage) &&
|
|
761
|
-
max_discount_percentage >= 0;
|
|
762
|
-
const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
|
|
763
|
-
const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
|
|
764
|
-
if (shouldNormalizeToStatic) {
|
|
765
|
-
if (discountBaseAmount <= 0) {
|
|
766
|
-
clearOrderLevelDiscount('voucher');
|
|
767
|
-
}
|
|
768
|
-
else {
|
|
769
|
-
const calculatedDiscount = (0, common_1.getCappedOfferDiscountAmount)({
|
|
770
|
-
baseAmount: discountBaseAmount,
|
|
771
|
-
discountValue: discount_value,
|
|
772
|
-
isDiscountPercentage: is_discount_percentage,
|
|
773
|
-
maxDiscountAmount: max_discount_amount,
|
|
774
|
-
maxDiscountPercentage: max_discount_percentage,
|
|
775
|
-
});
|
|
776
|
-
order_discount_details = {
|
|
777
|
-
...order_discount_details,
|
|
778
|
-
voucher_discount: (0, common_1.parseNum)(calculatedDiscount),
|
|
779
|
-
is_voucher_discount_percentage: false,
|
|
780
|
-
};
|
|
781
|
-
hasChanges = true;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
unlockedOffersMap[voucherDiscountId] -= 1;
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
const newCategories = [];
|
|
791
|
-
for (let i = 0; i < categories.length; i++) {
|
|
792
|
-
const category = categories[i];
|
|
793
|
-
const newItems = [];
|
|
794
|
-
for (let j = 0; j < category.items.length; j++) {
|
|
795
|
-
const item = category.items[j];
|
|
796
|
-
// we check if either the loyalty_discount_id or voucher_discount_id
|
|
797
|
-
// is present in the item_discount_details
|
|
798
|
-
// if it is then we remove the items from cart
|
|
799
|
-
if (item.item_discount_details != null) {
|
|
800
|
-
const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
|
|
801
|
-
if (loyaltyDiscountId != null) {
|
|
802
|
-
const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
|
|
803
|
-
const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
|
|
804
|
-
if (!hasRedeemedEntryForLoyalty) {
|
|
805
|
-
hasChanges = true;
|
|
806
|
-
if (normalizedLoyaltyDiscountId != null) {
|
|
807
|
-
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
808
|
-
}
|
|
809
|
-
continue;
|
|
810
|
-
}
|
|
811
|
-
if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
812
|
-
hasChanges = true;
|
|
813
|
-
if (normalizedLoyaltyDiscountId != null) {
|
|
814
|
-
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
815
|
-
}
|
|
816
|
-
continue;
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
unlockedOffersMap[loyaltyDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
const voucherDiscountId = item.item_discount_details.voucher_discount_id;
|
|
823
|
-
if (voucherDiscountId != null) {
|
|
824
|
-
const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
|
|
825
|
-
const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
|
|
826
|
-
if (!hasRedeemedEntryForVoucher) {
|
|
827
|
-
hasChanges = true;
|
|
828
|
-
if (normalizedVoucherDiscountId != null) {
|
|
829
|
-
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
830
|
-
}
|
|
831
|
-
continue;
|
|
832
|
-
}
|
|
833
|
-
if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
834
|
-
hasChanges = true;
|
|
835
|
-
if (normalizedVoucherDiscountId != null) {
|
|
836
|
-
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
837
|
-
}
|
|
838
|
-
continue;
|
|
839
|
-
}
|
|
840
|
-
else {
|
|
841
|
-
unlockedOffersMap[voucherDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
newItems.push(item);
|
|
845
|
-
}
|
|
846
|
-
else {
|
|
847
|
-
newItems.push(item);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
newCategories.push({
|
|
851
|
-
...category,
|
|
852
|
-
items: newItems,
|
|
853
|
-
});
|
|
854
|
-
}
|
|
855
|
-
const reconciledRedeemedOffers = getReconciledRedeemedOffersMap({
|
|
856
|
-
existingRedeemedOffers: redeemed_offers,
|
|
857
|
-
categories: newCategories,
|
|
858
517
|
offers,
|
|
859
|
-
removedOfferIds,
|
|
860
|
-
unlockedOffersApplicableTimes,
|
|
861
518
|
});
|
|
862
|
-
if (!areRedeemedOffersEqual(redeemed_offers, reconciledRedeemedOffers)) {
|
|
863
|
-
redeemed_offers = reconciledRedeemedOffers;
|
|
864
|
-
hasChanges = true;
|
|
865
|
-
}
|
|
866
|
-
if (hasChanges) {
|
|
867
|
-
const order = {
|
|
868
|
-
...data,
|
|
869
|
-
categories: newCategories,
|
|
870
|
-
order_discount_details,
|
|
871
|
-
redeemed_offers,
|
|
872
|
-
wallet_redemptions: getWalletRedemptionsMap(wallet_redemptions),
|
|
873
|
-
};
|
|
874
|
-
const newData = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(order, courses, listnerSettings);
|
|
875
|
-
return newData;
|
|
876
|
-
}
|
|
877
|
-
else {
|
|
878
|
-
return data;
|
|
879
|
-
}
|
|
880
519
|
}
|