@managesome/eposmatic-toolkit 0.2.318 → 0.2.320

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.
@@ -44,19 +44,33 @@ function getOrderWalletRedemptions(walletRedemptions) {
44
44
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
45
45
  normalizedWalletRedemptions.cashback_amount = cashbackAmount;
46
46
  }
47
- const giftCardRedemption = walletRedemptions.gift_cards;
48
- if (giftCardRedemption != null) {
49
- const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
50
- const valueRedeemed = Number(giftCardRedemption.value_redeemed);
51
- if (giftCardId != null && giftCardId.length > 0 && Number.isFinite(valueRedeemed) && valueRedeemed > 0) {
52
- normalizedWalletRedemptions.gift_cards = {
47
+ const giftCardRedemptions = walletRedemptions.gift_cards;
48
+ if (Array.isArray(giftCardRedemptions)) {
49
+ const normalizedGiftCardRedemptionsMap = {};
50
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
51
+ const giftCardRedemption = giftCardRedemptions[i];
52
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
53
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
54
+ const rawRedeemedTimes = giftCardRedemption.redeemed_times ?? legacyValueRedeemed;
55
+ const redeemedTimes = Number(rawRedeemedTimes);
56
+ if (giftCardId == null || giftCardId.length === 0 || !Number.isFinite(redeemedTimes) || redeemedTimes <= 0) {
57
+ continue;
58
+ }
59
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
60
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
53
61
  gift_card_id: giftCardId,
54
- value_redeemed: valueRedeemed,
62
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + Math.floor(redeemedTimes),
55
63
  ...(giftCardRedemption.mystery_gift_type != null
56
64
  ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
57
- : {}),
65
+ : existingRedemption?.mystery_gift_type != null
66
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
67
+ : {}),
58
68
  };
59
69
  }
70
+ const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
71
+ if (normalizedGiftCardRedemptions.length > 0) {
72
+ normalizedWalletRedemptions.gift_cards = normalizedGiftCardRedemptions;
73
+ }
60
74
  }
61
75
  return Object.keys(normalizedWalletRedemptions).length > 0 ? normalizedWalletRedemptions : null;
62
76
  }
@@ -480,19 +480,32 @@ function getWalletRedemptionsMap(walletRedemptions) {
480
480
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
481
481
  normalized.cashback_amount = cashbackAmount;
482
482
  }
483
- const giftCardRedemption = walletRedemptions.gift_cards;
484
- if (giftCardRedemption != null) {
485
- const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
486
- const valueRedeemed = Number(giftCardRedemption.value_redeemed);
487
- if (giftCardId != null && giftCardId.length > 0 && Number.isFinite(valueRedeemed) && valueRedeemed > 0) {
488
- normalized.gift_cards = {
483
+ const giftCardRedemptions = walletRedemptions.gift_cards;
484
+ if (Array.isArray(giftCardRedemptions)) {
485
+ const normalizedGiftCardRedemptionsMap = {};
486
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
487
+ const giftCardRedemption = giftCardRedemptions[i];
488
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
489
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
490
+ const redeemedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
491
+ if (giftCardId == null || giftCardId.length === 0 || redeemedTimes <= 0) {
492
+ continue;
493
+ }
494
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
495
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
489
496
  gift_card_id: giftCardId,
490
- value_redeemed: valueRedeemed,
497
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + redeemedTimes,
491
498
  ...(giftCardRedemption.mystery_gift_type != null
492
499
  ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
493
- : {}),
500
+ : existingRedemption?.mystery_gift_type != null
501
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
502
+ : {}),
494
503
  };
495
504
  }
505
+ const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
506
+ if (normalizedGiftCardRedemptions.length > 0) {
507
+ normalized.gift_cards = normalizedGiftCardRedemptions;
508
+ }
496
509
  }
497
510
  return Object.keys(normalized).length > 0 ? normalized : null;
498
511
  }
@@ -505,17 +518,23 @@ function getSyncedWalletRedemptionsAfterOrderDiscountOverflowCleanup({ walletRed
505
518
  if (previousLoyaltyDiscountId == null || previousLoyaltyDiscountId === nextLoyaltyDiscountId) {
506
519
  return walletRedemptions;
507
520
  }
508
- const walletGiftCardDiscountId = getNormalizedOfferId(walletRedemptions?.gift_cards?.gift_card_id);
521
+ const normalizedWalletRedemptions = getWalletRedemptionsMap(walletRedemptions);
522
+ const normalizedGiftCardRedemptions = normalizedWalletRedemptions?.gift_cards ?? [];
523
+ const filteredGiftCardRedemptions = normalizedGiftCardRedemptions.filter((giftCardRedemption) => getNormalizedOfferId(giftCardRedemption.gift_card_id) !== previousLoyaltyDiscountId);
509
524
  const shouldClearWalletCashback = previousLoyaltyDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID &&
510
525
  Number(walletRedemptions.cashback_amount ?? 0) > 0;
511
- const shouldClearGiftCardRedemption = walletGiftCardDiscountId != null && walletGiftCardDiscountId === previousLoyaltyDiscountId;
526
+ const shouldClearGiftCardRedemption = filteredGiftCardRedemptions.length !== normalizedGiftCardRedemptions.length;
512
527
  if (!shouldClearWalletCashback && !shouldClearGiftCardRedemption) {
513
528
  return walletRedemptions;
514
529
  }
515
530
  return getWalletRedemptionsMap({
516
- ...walletRedemptions,
531
+ ...(normalizedWalletRedemptions ?? {}),
517
532
  ...(shouldClearWalletCashback ? { cashback_amount: undefined } : {}),
518
- ...(shouldClearGiftCardRedemption ? { gift_cards: undefined } : {}),
533
+ ...(shouldClearGiftCardRedemption
534
+ ? {
535
+ gift_cards: filteredGiftCardRedemptions.length > 0 ? filteredGiftCardRedemptions : undefined,
536
+ }
537
+ : {}),
519
538
  });
520
539
  }
521
540
  function clearSpecialDiscountFields(orderDiscountDetails) {
@@ -538,8 +557,10 @@ function getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({ orderDiscou
538
557
  return orderDiscountDetails;
539
558
  }
540
559
  const hasRedeemedOfferForSpecialDiscount = redeemedOffers?.[normalizedSpecialDiscountId] != null;
541
- const normalizedGiftCardId = getNormalizedOfferId(walletRedemptions?.gift_cards?.gift_card_id);
542
- const isGiftCardRewardDiscount = normalizedGiftCardId != null && normalizedGiftCardId === normalizedSpecialDiscountId;
560
+ const normalizedGiftCardIds = (walletRedemptions?.gift_cards ?? [])
561
+ .map((giftCardRedemption) => getNormalizedOfferId(giftCardRedemption.gift_card_id))
562
+ .filter((giftCardId) => giftCardId != null);
563
+ const isGiftCardRewardDiscount = normalizedGiftCardIds.includes(normalizedSpecialDiscountId);
543
564
  const isWalletCashbackRewardDiscount = normalizedSpecialDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
544
565
  const isRewardLinkedSpecialDiscount = hasRedeemedOfferForSpecialDiscount || isGiftCardRewardDiscount || isWalletCashbackRewardDiscount;
545
566
  if (!isRewardLinkedSpecialDiscount) {
@@ -1028,27 +1049,38 @@ function hasItemLevelLoyaltyDiscountForId(categories, discountId) {
1028
1049
  return false;
1029
1050
  }
1030
1051
  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) {
1052
+ const giftCardRedemptions = walletRedemptions?.gift_cards;
1053
+ if (!Array.isArray(giftCardRedemptions) || giftCardRedemptions.length === 0) {
1042
1054
  return walletRedemptions;
1043
1055
  }
1044
- const hasOrderLevelDiscount = getNormalizedOfferId(orderDiscountDetails?.loyalty_discount_id) === claimedGiftCardId;
1045
- const hasItemLevelDiscount = hasItemLevelLoyaltyDiscountForId(categories, claimedGiftCardId);
1046
- if (hasOrderLevelDiscount || hasItemLevelDiscount) {
1056
+ let hasRemovedGiftCardRedemptions = false;
1057
+ const nextGiftCardRedemptions = giftCardRedemptions.filter((giftCardRedemption) => {
1058
+ const claimedGiftCardId = giftCardRedemption.gift_card_id?.trim();
1059
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
1060
+ const claimedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
1061
+ if (claimedGiftCardId == null || claimedGiftCardId.length === 0 || claimedTimes <= 0) {
1062
+ hasRemovedGiftCardRedemptions = true;
1063
+ return false;
1064
+ }
1065
+ const rewardType = giftCardRedemption.mystery_gift_type;
1066
+ const isItemRewardType = rewardType === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.ITEMS || rewardType === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.DISCOUNT_ITEMS;
1067
+ if (!isItemRewardType) {
1068
+ return true;
1069
+ }
1070
+ const hasOrderLevelDiscount = getNormalizedOfferId(orderDiscountDetails?.loyalty_discount_id) === claimedGiftCardId;
1071
+ const hasItemLevelDiscount = hasItemLevelLoyaltyDiscountForId(categories, claimedGiftCardId);
1072
+ if (hasOrderLevelDiscount || hasItemLevelDiscount) {
1073
+ return true;
1074
+ }
1075
+ hasRemovedGiftCardRedemptions = true;
1076
+ return false;
1077
+ });
1078
+ if (!hasRemovedGiftCardRedemptions) {
1047
1079
  return walletRedemptions;
1048
1080
  }
1049
1081
  return getWalletRedemptionsMap({
1050
1082
  ...walletRedemptions,
1051
- gift_cards: undefined,
1083
+ gift_cards: nextGiftCardRedemptions.length > 0 ? nextGiftCardRedemptions : undefined,
1052
1084
  });
1053
1085
  }
1054
1086
  function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calculateItemMetadata, syncContext) {
@@ -1143,11 +1175,7 @@ function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calcu
1143
1175
  orderDiscountDetails: syncedOrderDiscountDetails,
1144
1176
  categories,
1145
1177
  });
1146
- const walletGiftCardRedeemedTimes = Number(syncedWalletRedemptions?.gift_cards?.value_redeemed ?? 0);
1147
- const hasWalletGiftCardMetadata = syncedWalletRedemptions?.gift_cards?.gift_card_id != null &&
1148
- syncedWalletRedemptions.gift_cards.gift_card_id.trim().length > 0 &&
1149
- Number.isFinite(walletGiftCardRedeemedTimes) &&
1150
- walletGiftCardRedeemedTimes > 0;
1178
+ const hasWalletGiftCardMetadata = (syncedWalletRedemptions?.gift_cards?.length ?? 0) > 0;
1151
1179
  // Empty carts must not retain offer-redemption state or gift-card wallet claims.
1152
1180
  if (subtotal <= 0) {
1153
1181
  if (syncedRedeemedOffers != null) {
@@ -1434,23 +1462,37 @@ function getOrderDataWithSyncedWalletGiftCardClaim({ data, walletGiftCardClaim,
1434
1462
  return data;
1435
1463
  }
1436
1464
  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: {
1465
+ const currentGiftCardRedemptions = normalizedWalletRedemptions?.gift_cards ?? [];
1466
+ let hasUpdatedExistingClaim = false;
1467
+ const nextGiftCardRedemptions = currentGiftCardRedemptions.map((giftCardRedemption) => {
1468
+ if (giftCardRedemption.gift_card_id !== claimedGiftCardId) {
1469
+ return giftCardRedemption;
1470
+ }
1471
+ hasUpdatedExistingClaim = true;
1472
+ return {
1473
+ ...giftCardRedemption,
1474
+ redeemed_times: toPositiveInteger(giftCardRedemption.redeemed_times, 0) + 1,
1475
+ ...(walletGiftCardClaim.mysteryGiftType != null
1476
+ ? {
1477
+ mystery_gift_type: walletGiftCardClaim.mysteryGiftType,
1478
+ }
1479
+ : {}),
1480
+ };
1481
+ });
1482
+ if (!hasUpdatedExistingClaim) {
1483
+ nextGiftCardRedemptions.push({
1446
1484
  gift_card_id: claimedGiftCardId,
1447
- value_redeemed: 1,
1485
+ redeemed_times: 1,
1448
1486
  ...(walletGiftCardClaim.mysteryGiftType != null
1449
1487
  ? {
1450
1488
  mystery_gift_type: walletGiftCardClaim.mysteryGiftType,
1451
1489
  }
1452
1490
  : {}),
1453
- },
1491
+ });
1492
+ }
1493
+ const nextWalletRedemptions = getWalletRedemptionsMap({
1494
+ ...(normalizedWalletRedemptions ?? {}),
1495
+ gift_cards: nextGiftCardRedemptions,
1454
1496
  });
1455
1497
  if ((0, lodash_1.isEqual)(nextWalletRedemptions, normalizedWalletRedemptions)) {
1456
1498
  return data;
@@ -9,6 +9,7 @@ export declare function getOrderItemModifiersWithPrice(modifiers: IOrderModifier
9
9
  export declare function getOrderItemsWithPrice(items: IOrderItem[] | undefined, convertToPrice: (price: number) => number): IOrderItem[];
10
10
  export declare function getOrderCategoriesWithPrice(categories: IOrderCategory[] | undefined, convertToPrice: (price: number) => number): IOrderCategory[];
11
11
  export declare function getOrderDiscountDetailsWithPrice(discount: IDiscountDetails | null | undefined, convertToPrice: (price: number) => number): IDiscountDetails | null;
12
+ export declare function getOrderWalletRedemptionsWithPrice(walletRedemptions: IOrder['wallet_redemptions'] | null | undefined, convertToPrice: (price: number) => number): IOrder['wallet_redemptions'];
12
13
  export declare function sortByCreatedAt<T extends {
13
14
  createdAt: Date;
14
15
  }>(a: T, b: T): number;
@@ -11,6 +11,7 @@ exports.getOrderItemModifiersWithPrice = getOrderItemModifiersWithPrice;
11
11
  exports.getOrderItemsWithPrice = getOrderItemsWithPrice;
12
12
  exports.getOrderCategoriesWithPrice = getOrderCategoriesWithPrice;
13
13
  exports.getOrderDiscountDetailsWithPrice = getOrderDiscountDetailsWithPrice;
14
+ exports.getOrderWalletRedemptionsWithPrice = getOrderWalletRedemptionsWithPrice;
14
15
  exports.sortByCreatedAt = sortByCreatedAt;
15
16
  exports.sortByIdTimestamp = sortByIdTimestamp;
16
17
  exports.getOrderWithPrice = getOrderWithPrice;
@@ -108,6 +109,43 @@ function getOrderDiscountDetailsWithPrice(discount, convertToPrice) {
108
109
  total_discount: convertToPrice(total_discount ?? 0),
109
110
  };
110
111
  }
112
+ function getOrderWalletRedemptionsWithPrice(walletRedemptions, convertToPrice) {
113
+ if (walletRedemptions == null)
114
+ return null;
115
+ const normalizedGiftCardRedemptions = (() => {
116
+ const giftCardRedemptions = walletRedemptions.gift_cards;
117
+ if (!Array.isArray(giftCardRedemptions))
118
+ return null;
119
+ const normalizedGiftCardRedemptionsMap = {};
120
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
121
+ const giftCardRedemption = giftCardRedemptions[i];
122
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
123
+ const redeemedTimes = Number(giftCardRedemption.redeemed_times);
124
+ if (giftCardId == null || giftCardId.length === 0 || !Number.isFinite(redeemedTimes) || redeemedTimes <= 0) {
125
+ continue;
126
+ }
127
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
128
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
129
+ gift_card_id: giftCardId,
130
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + Math.floor(redeemedTimes),
131
+ ...(giftCardRedemption.mystery_gift_type != null
132
+ ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
133
+ : existingRedemption?.mystery_gift_type != null
134
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
135
+ : {}),
136
+ };
137
+ }
138
+ const dedupedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
139
+ return dedupedGiftCardRedemptions.length > 0 ? dedupedGiftCardRedemptions : null;
140
+ })();
141
+ return {
142
+ ...walletRedemptions,
143
+ ...(walletRedemptions.cashback_amount != null
144
+ ? { cashback_amount: convertToPrice(walletRedemptions.cashback_amount) }
145
+ : {}),
146
+ ...(normalizedGiftCardRedemptions != null ? { gift_cards: normalizedGiftCardRedemptions } : {}),
147
+ };
148
+ }
111
149
  function sortByCreatedAt(a, b) {
112
150
  return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
113
151
  }
@@ -167,6 +205,9 @@ function getOrderWithPrice(order, toCents) {
167
205
  },
168
206
  }
169
207
  : {}),
208
+ ...(order.wallet_redemptions != null
209
+ ? { wallet_redemptions: getOrderWalletRedemptionsWithPrice(order.wallet_redemptions, convertToPrice) }
210
+ : {}),
170
211
  };
171
212
  if (toCents) {
172
213
  const orderType = order.platform != null ? order.platform.split('_')[1] : null;
@@ -68,6 +68,7 @@ export * from './void-warehouse-invoice-items-helpers';
68
68
  export * from './void-warehouse-invoice-items-report-helpers';
69
69
  export * from './voidations-summary-report-helpers';
70
70
  export * from './wallet-helpers';
71
+ export * from './wallet-redemption-helpers';
71
72
  export * from './warehouse-catalog-conversion';
72
73
  export * from './warehouse-inventory';
73
74
  export * from './warehouse-invoice-order-engine';
@@ -84,6 +84,7 @@ __exportStar(require("./void-warehouse-invoice-items-helpers"), exports);
84
84
  __exportStar(require("./void-warehouse-invoice-items-report-helpers"), exports);
85
85
  __exportStar(require("./voidations-summary-report-helpers"), exports);
86
86
  __exportStar(require("./wallet-helpers"), exports);
87
+ __exportStar(require("./wallet-redemption-helpers"), exports);
87
88
  __exportStar(require("./warehouse-catalog-conversion"), exports);
88
89
  __exportStar(require("./warehouse-inventory"), exports);
89
90
  __exportStar(require("./warehouse-invoice-order-engine"), exports);
@@ -491,19 +491,32 @@ function getNormalizedWalletRedemptionsMap(walletRedemptions) {
491
491
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
492
492
  normalized.cashback_amount = cashbackAmount;
493
493
  }
494
- const giftCardRedemption = walletRedemptions.gift_cards;
495
- if (giftCardRedemption != null) {
496
- const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
497
- const redeemedTimes = toPositiveInteger(giftCardRedemption.value_redeemed, 0);
498
- if (giftCardId != null && giftCardId.length > 0 && redeemedTimes > 0) {
499
- normalized.gift_cards = {
494
+ const giftCardRedemptions = walletRedemptions.gift_cards;
495
+ if (Array.isArray(giftCardRedemptions)) {
496
+ const normalizedGiftCardRedemptionsMap = {};
497
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
498
+ const giftCardRedemption = giftCardRedemptions[i];
499
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
500
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
501
+ const redeemedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
502
+ if (giftCardId == null || giftCardId.length === 0 || redeemedTimes <= 0) {
503
+ continue;
504
+ }
505
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
506
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
500
507
  gift_card_id: giftCardId,
501
- value_redeemed: redeemedTimes,
508
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + redeemedTimes,
502
509
  ...(giftCardRedemption.mystery_gift_type != null
503
510
  ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
504
- : {}),
511
+ : existingRedemption?.mystery_gift_type != null
512
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
513
+ : {}),
505
514
  };
506
515
  }
516
+ const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
517
+ if (normalizedGiftCardRedemptions.length > 0) {
518
+ normalized.gift_cards = normalizedGiftCardRedemptions;
519
+ }
507
520
  }
508
521
  return Object.keys(normalized).length > 0 ? normalized : null;
509
522
  }
@@ -123,7 +123,7 @@ export declare function getWalletCashbackRecommendedValues({ maxApplicableCashba
123
123
  maxApplicableCashback: number | null | undefined;
124
124
  percentageSteps?: readonly number[];
125
125
  }): number[];
126
- type TGiftCardRedemptionPayload = NonNullable<NonNullable<IOrder['wallet_redemptions']>['gift_cards']>;
126
+ type TGiftCardRedemptionPayload = NonNullable<NonNullable<IOrder['wallet_redemptions']>['gift_cards']>[number];
127
127
  export declare function buildWalletRedemptionsPatch({ walletRedemptions, }: {
128
128
  walletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
129
129
  }): Partial<Pick<IOrder, 'wallet_redemptions'>>;
@@ -137,7 +137,7 @@ export declare function buildWalletCashbackRedemptionPatch({ currentWalletRedemp
137
137
  }): Partial<Pick<IOrder, 'wallet_redemptions'>>;
138
138
  export declare function buildWalletGiftCardRedemptionPatch({ currentWalletRedemptions, giftCardRedemption, }: {
139
139
  currentWalletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
140
- giftCardRedemption: TGiftCardRedemptionPayload | null | undefined;
140
+ giftCardRedemption: TGiftCardRedemptionPayload | TGiftCardRedemptionPayload[] | null | undefined;
141
141
  }): Partial<Pick<IOrder, 'wallet_redemptions'>>;
142
142
  export declare function buildWalletGiftCardRedemptionClaimPatch({ currentWalletRedemptions, giftCardId, claimsIncrement, mysteryGiftType, }: {
143
143
  currentWalletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
@@ -826,19 +826,32 @@ function normalizeWalletRedemptions(walletRedemptions) {
826
826
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
827
827
  normalizedWalletRedemptions.cashback_amount = (0, common_1.parseNum)(cashbackAmount);
828
828
  }
829
- const giftCardRedemption = walletRedemptions.gift_cards;
830
- if (giftCardRedemption != null) {
831
- const giftCardId = normalizeGiftCardId(giftCardRedemption.gift_card_id);
832
- const redeemedTimes = toPositiveInteger(giftCardRedemption.value_redeemed, 0);
833
- if (giftCardId != null && redeemedTimes > 0) {
834
- normalizedWalletRedemptions.gift_cards = {
829
+ const giftCardRedemptions = walletRedemptions.gift_cards;
830
+ if (Array.isArray(giftCardRedemptions)) {
831
+ const normalizedGiftCardRedemptionsMap = {};
832
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
833
+ const giftCardRedemption = giftCardRedemptions[i];
834
+ const giftCardId = normalizeGiftCardId(giftCardRedemption.gift_card_id);
835
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
836
+ const redeemedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
837
+ if (giftCardId == null || redeemedTimes <= 0) {
838
+ continue;
839
+ }
840
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
841
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
835
842
  gift_card_id: giftCardId,
836
- value_redeemed: redeemedTimes,
843
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + redeemedTimes,
837
844
  ...(giftCardRedemption.mystery_gift_type != null
838
845
  ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
839
- : {}),
846
+ : existingRedemption?.mystery_gift_type != null
847
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
848
+ : {}),
840
849
  };
841
850
  }
851
+ const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
852
+ if (normalizedGiftCardRedemptions.length > 0) {
853
+ normalizedWalletRedemptions.gift_cards = normalizedGiftCardRedemptions;
854
+ }
842
855
  }
843
856
  return Object.keys(normalizedWalletRedemptions).length > 0 ? normalizedWalletRedemptions : null;
844
857
  }
@@ -866,10 +879,15 @@ function buildWalletCashbackRedemptionPatch({ currentWalletRedemptions, cashback
866
879
  });
867
880
  }
868
881
  function buildWalletGiftCardRedemptionPatch({ currentWalletRedemptions, giftCardRedemption, }) {
882
+ const giftCardRedemptionsPatch = giftCardRedemption == null
883
+ ? undefined
884
+ : Array.isArray(giftCardRedemption)
885
+ ? giftCardRedemption
886
+ : [giftCardRedemption];
869
887
  return buildWalletRedemptionsMergePatch({
870
888
  currentWalletRedemptions,
871
889
  walletRedemptionsPatch: {
872
- gift_cards: giftCardRedemption,
890
+ gift_cards: giftCardRedemptionsPatch,
873
891
  },
874
892
  });
875
893
  }
@@ -880,17 +898,29 @@ function buildWalletGiftCardRedemptionClaimPatch({ currentWalletRedemptions, gif
880
898
  }
881
899
  const safeClaimsIncrement = toPositiveInteger(claimsIncrement, 1);
882
900
  const normalizedCurrentWalletRedemptions = normalizeWalletRedemptions(currentWalletRedemptions);
883
- const existingGiftCardRedemption = normalizedCurrentWalletRedemptions?.gift_cards;
884
- const currentRedeemedTimes = existingGiftCardRedemption?.gift_card_id === normalizedGiftCardId
885
- ? toPositiveInteger(existingGiftCardRedemption.value_redeemed, 0)
886
- : 0;
887
- return buildWalletGiftCardRedemptionPatch({
888
- currentWalletRedemptions,
889
- giftCardRedemption: {
901
+ const existingGiftCardRedemptions = normalizedCurrentWalletRedemptions?.gift_cards ?? [];
902
+ let hasUpdatedGiftCardClaim = false;
903
+ const nextGiftCardRedemptions = existingGiftCardRedemptions.map((existingGiftCardRedemption) => {
904
+ if (existingGiftCardRedemption.gift_card_id !== normalizedGiftCardId) {
905
+ return existingGiftCardRedemption;
906
+ }
907
+ hasUpdatedGiftCardClaim = true;
908
+ return {
909
+ ...existingGiftCardRedemption,
910
+ redeemed_times: toPositiveInteger(existingGiftCardRedemption.redeemed_times, 0) + safeClaimsIncrement,
911
+ ...(mysteryGiftType != null ? { mystery_gift_type: mysteryGiftType } : {}),
912
+ };
913
+ });
914
+ if (!hasUpdatedGiftCardClaim) {
915
+ nextGiftCardRedemptions.push({
890
916
  gift_card_id: normalizedGiftCardId,
891
- value_redeemed: currentRedeemedTimes + safeClaimsIncrement,
917
+ redeemed_times: safeClaimsIncrement,
892
918
  ...(mysteryGiftType != null ? { mystery_gift_type: mysteryGiftType } : {}),
893
- },
919
+ });
920
+ }
921
+ return buildWalletGiftCardRedemptionPatch({
922
+ currentWalletRedemptions,
923
+ giftCardRedemption: nextGiftCardRedemptions,
894
924
  });
895
925
  }
896
926
  function buildClearWalletRedemptionsPatch({ currentWalletRedemptions, clearCashbackAmount = false, clearGiftCardRedemption = false, clearAll = false, }) {
@@ -0,0 +1,18 @@
1
+ import { GIFT_CARD_REWARD_TYPE_ENUM } from '../../constants';
2
+ export interface IWalletGiftCardRedemptionCompat {
3
+ gift_card_id?: string | null;
4
+ mystery_gift_type?: GIFT_CARD_REWARD_TYPE_ENUM | null;
5
+ redeemed_times?: number | null;
6
+ value_redeemed?: number | null;
7
+ }
8
+ export type TWalletGiftCardRedemptionsCompatInput = IWalletGiftCardRedemptionCompat | IWalletGiftCardRedemptionCompat[] | null | undefined;
9
+ export interface IWalletGiftCardClaim {
10
+ gift_card_id: string;
11
+ redeemed_times: number;
12
+ mystery_gift_type?: GIFT_CARD_REWARD_TYPE_ENUM | null;
13
+ }
14
+ export declare function getWalletGiftCardRedemptionClaims(walletGiftCardRedemptions: TWalletGiftCardRedemptionsCompatInput): IWalletGiftCardClaim[];
15
+ export declare function buildWalletGiftCardRedemptionCompatPayload({ currentWalletGiftCardRedemptions, nextWalletGiftCardClaims, }: {
16
+ currentWalletGiftCardRedemptions: TWalletGiftCardRedemptionsCompatInput;
17
+ nextWalletGiftCardClaims: IWalletGiftCardClaim[];
18
+ }): IWalletGiftCardRedemptionCompat | IWalletGiftCardClaim[] | null;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getWalletGiftCardRedemptionClaims = getWalletGiftCardRedemptionClaims;
4
+ exports.buildWalletGiftCardRedemptionCompatPayload = buildWalletGiftCardRedemptionCompatPayload;
5
+ function getWalletGiftCardRedemptionClaims(walletGiftCardRedemptions) {
6
+ const normalizedWalletGiftCardRedemptions = Array.isArray(walletGiftCardRedemptions)
7
+ ? walletGiftCardRedemptions
8
+ : walletGiftCardRedemptions != null
9
+ ? [walletGiftCardRedemptions]
10
+ : [];
11
+ const claimsByGiftCardId = {};
12
+ for (let i = 0; i < normalizedWalletGiftCardRedemptions.length; i++) {
13
+ const walletGiftCardRedemption = normalizedWalletGiftCardRedemptions[i];
14
+ const giftCardId = walletGiftCardRedemption.gift_card_id?.trim();
15
+ const redeemedTimes = Number(walletGiftCardRedemption.redeemed_times ?? walletGiftCardRedemption.value_redeemed ?? 0);
16
+ if (giftCardId == null || giftCardId.length === 0 || !Number.isFinite(redeemedTimes) || redeemedTimes <= 0) {
17
+ continue;
18
+ }
19
+ const existingClaim = claimsByGiftCardId[giftCardId];
20
+ claimsByGiftCardId[giftCardId] = {
21
+ gift_card_id: giftCardId,
22
+ redeemed_times: (existingClaim?.redeemed_times ?? 0) + Math.floor(redeemedTimes),
23
+ ...(walletGiftCardRedemption.mystery_gift_type != null
24
+ ? {
25
+ mystery_gift_type: walletGiftCardRedemption.mystery_gift_type,
26
+ }
27
+ : existingClaim?.mystery_gift_type != null
28
+ ? {
29
+ mystery_gift_type: existingClaim.mystery_gift_type,
30
+ }
31
+ : {}),
32
+ };
33
+ }
34
+ return Object.values(claimsByGiftCardId);
35
+ }
36
+ function buildWalletGiftCardRedemptionCompatPayload({ currentWalletGiftCardRedemptions, nextWalletGiftCardClaims, }) {
37
+ if (Array.isArray(currentWalletGiftCardRedemptions)) {
38
+ return nextWalletGiftCardClaims;
39
+ }
40
+ const nextWalletGiftCardClaim = nextWalletGiftCardClaims[0];
41
+ if (nextWalletGiftCardClaim == null) {
42
+ return null;
43
+ }
44
+ return {
45
+ gift_card_id: nextWalletGiftCardClaim.gift_card_id,
46
+ mystery_gift_type: nextWalletGiftCardClaim.mystery_gift_type,
47
+ value_redeemed: nextWalletGiftCardClaim.redeemed_times,
48
+ redeemed_times: nextWalletGiftCardClaim.redeemed_times,
49
+ };
50
+ }
@@ -5186,11 +5186,19 @@ export declare const OrderSchema: Schema<any, import("mongoose").Model<any, any,
5186
5186
  tax_invoice_provider_charges?: number | null | undefined;
5187
5187
  redeemed_offers?: Map<string, number> | null | undefined;
5188
5188
  wallet_redemptions?: {
5189
- gift_cards?: {
5189
+ gift_cards?: import("mongoose").Types.DocumentArray<{
5190
+ redeemed_times: number;
5190
5191
  gift_card_id: string;
5191
- value_redeemed: number;
5192
5192
  mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
5193
- } | null | undefined;
5193
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
5194
+ redeemed_times: number;
5195
+ gift_card_id: string;
5196
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
5197
+ }> & {
5198
+ redeemed_times: number;
5199
+ gift_card_id: string;
5200
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
5201
+ }> | null | undefined;
5194
5202
  cashback_amount?: number | null | undefined;
5195
5203
  } | null | undefined;
5196
5204
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
@@ -10292,11 +10300,19 @@ export declare const OrderSchema: Schema<any, import("mongoose").Model<any, any,
10292
10300
  tax_invoice_provider_charges?: number | null | undefined;
10293
10301
  redeemed_offers?: Map<string, number> | null | undefined;
10294
10302
  wallet_redemptions?: {
10295
- gift_cards?: {
10303
+ gift_cards?: import("mongoose").Types.DocumentArray<{
10304
+ redeemed_times: number;
10296
10305
  gift_card_id: string;
10297
- value_redeemed: number;
10298
10306
  mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
10299
- } | null | undefined;
10307
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
10308
+ redeemed_times: number;
10309
+ gift_card_id: string;
10310
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
10311
+ }> & {
10312
+ redeemed_times: number;
10313
+ gift_card_id: string;
10314
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
10315
+ }> | null | undefined;
10300
10316
  cashback_amount?: number | null | undefined;
10301
10317
  } | null | undefined;
10302
10318
  }>> & import("mongoose").FlatRecord<{
@@ -15398,11 +15414,19 @@ export declare const OrderSchema: Schema<any, import("mongoose").Model<any, any,
15398
15414
  tax_invoice_provider_charges?: number | null | undefined;
15399
15415
  redeemed_offers?: Map<string, number> | null | undefined;
15400
15416
  wallet_redemptions?: {
15401
- gift_cards?: {
15417
+ gift_cards?: import("mongoose").Types.DocumentArray<{
15418
+ redeemed_times: number;
15402
15419
  gift_card_id: string;
15403
- value_redeemed: number;
15404
15420
  mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
15405
- } | null | undefined;
15421
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
15422
+ redeemed_times: number;
15423
+ gift_card_id: string;
15424
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
15425
+ }> & {
15426
+ redeemed_times: number;
15427
+ gift_card_id: string;
15428
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
15429
+ }> | null | undefined;
15406
15430
  cashback_amount?: number | null | undefined;
15407
15431
  } | null | undefined;
15408
15432
  }> & Required<{
@@ -238,7 +238,7 @@ const GiftCardRedemptionSchema = new mongoose_1.Schema({
238
238
  enum: constants_1.ALL_GIFT_CARD_REWARD_TYPES,
239
239
  required: false,
240
240
  },
241
- value_redeemed: {
241
+ redeemed_times: {
242
242
  type: Number,
243
243
  min: 0,
244
244
  required: true,
@@ -251,7 +251,7 @@ const WalletRedemptionsSchema = new mongoose_1.Schema({
251
251
  required: false,
252
252
  },
253
253
  gift_cards: {
254
- type: GiftCardRedemptionSchema,
254
+ type: [GiftCardRedemptionSchema],
255
255
  required: false,
256
256
  },
257
257
  }, { _id: false, minimize: false });
@@ -124,11 +124,11 @@ export interface IOrderTakeawayDetails {
124
124
  export interface IGiftCardRedemption {
125
125
  gift_card_id: string;
126
126
  mystery_gift_type?: GIFT_CARD_REWARD_TYPE_ENUM | null;
127
- value_redeemed: number;
127
+ redeemed_times: number;
128
128
  }
129
129
  export interface IWalletRedemptions {
130
130
  cashback_amount?: number | null;
131
- gift_cards?: IGiftCardRedemption | null;
131
+ gift_cards?: IGiftCardRedemption[] | null;
132
132
  }
133
133
  interface CommonOptionalFields {
134
134
  delivery_details?: IOrderDeliveryDetails | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.318",
3
+ "version": "0.2.320",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",