@managesome/eposmatic-toolkit 0.2.306 → 0.2.308

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.
@@ -36,6 +36,30 @@ function getOrderTakeAwayDetails(takeaway_details) {
36
36
  estimated_takeaway_time: takeaway_details.estimated_takeaway_time != null ? new Date(takeaway_details.estimated_takeaway_time) : null,
37
37
  };
38
38
  }
39
+ function getOrderWalletRedemptions(walletRedemptions) {
40
+ if (walletRedemptions == null)
41
+ return null;
42
+ const normalizedWalletRedemptions = {};
43
+ const cashbackAmount = Number(walletRedemptions.cashback_amount);
44
+ if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
45
+ normalizedWalletRedemptions.cashback_amount = cashbackAmount;
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 = {
53
+ gift_card_id: giftCardId,
54
+ value_redeemed: valueRedeemed,
55
+ ...(giftCardRedemption.mystery_gift_type != null
56
+ ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
57
+ : {}),
58
+ };
59
+ }
60
+ }
61
+ return Object.keys(normalizedWalletRedemptions).length > 0 ? normalizedWalletRedemptions : null;
62
+ }
39
63
  function generateOrderCategories(categories) {
40
64
  if (!categories)
41
65
  return [];
@@ -143,7 +167,8 @@ function generateOrderOnCreate(order, withIterations) {
143
167
  if (!newOrder.redeemed_offers || Object.keys(newOrder.redeemed_offers).length === 0) {
144
168
  delete newOrder.redeemed_offers;
145
169
  }
146
- if (!newOrder.wallet_redemptions || Object.keys(newOrder.wallet_redemptions).length === 0) {
170
+ newOrder.wallet_redemptions = getOrderWalletRedemptions(newOrder.wallet_redemptions);
171
+ if (newOrder.wallet_redemptions == null) {
147
172
  delete newOrder.wallet_redemptions;
148
173
  }
149
174
  if (!newOrder.surcharge) {
@@ -333,9 +358,12 @@ function generateOrderOnUpdate(partialUpdatedOrder) {
333
358
  delete set.wallet_redemptions;
334
359
  unset['wallet_redemptions'] = '';
335
360
  }
336
- else if (set.wallet_redemptions != null && Object.keys(set.wallet_redemptions).length === 0) {
337
- delete set.wallet_redemptions;
338
- unset['wallet_redemptions'] = '';
361
+ else if (set.wallet_redemptions != null) {
362
+ set.wallet_redemptions = getOrderWalletRedemptions(set.wallet_redemptions);
363
+ if (set.wallet_redemptions == null) {
364
+ delete set.wallet_redemptions;
365
+ unset['wallet_redemptions'] = '';
366
+ }
339
367
  }
340
368
  if (set.surcharge === 0) {
341
369
  delete set.surcharge;
@@ -109,7 +109,7 @@ export declare function getChangeCourseStatusData(data: Partial<IOrder>, course_
109
109
  payment_method_expected?: import("../../constants").PAYMENT_METHOD_EXPECTED_ENUM | undefined;
110
110
  order_taker_details?: (import("../../schemas").IStaffDetails | null) | undefined;
111
111
  redeemed_offers?: (Record<string, number> | null) | undefined;
112
- wallet_redemptions?: (Record<string, number> | null) | undefined;
112
+ wallet_redemptions?: (import("../../schemas").IWalletRedemptions | null) | undefined;
113
113
  order_settled_at?: Date | undefined;
114
114
  order_settled_at_mins?: number | undefined;
115
115
  createdAt_mins?: number | undefined;
@@ -388,6 +388,29 @@ function getNormalizedRedeemedOffersMap(redeemedOffers) {
388
388
  }
389
389
  return Object.keys(normalizedRedeemedOffers).length > 0 ? normalizedRedeemedOffers : null;
390
390
  }
391
+ function isRedeemedOffersMapNormalized(redeemedOffers) {
392
+ if (redeemedOffers == null) {
393
+ return true;
394
+ }
395
+ const normalizedRedeemedOffers = getNormalizedRedeemedOffersMap(redeemedOffers);
396
+ const rawOfferIds = Object.keys(redeemedOffers);
397
+ const normalizedOfferIds = Object.keys(normalizedRedeemedOffers ?? {});
398
+ if (rawOfferIds.length !== normalizedOfferIds.length) {
399
+ return false;
400
+ }
401
+ for (let i = 0; i < rawOfferIds.length; i++) {
402
+ const rawOfferId = rawOfferIds[i];
403
+ const normalizedOfferId = getNormalizedOfferId(rawOfferId);
404
+ const safeCount = toPositiveInteger(redeemedOffers[rawOfferId], 0);
405
+ if (normalizedOfferId == null || rawOfferId !== normalizedOfferId || safeCount <= 0) {
406
+ return false;
407
+ }
408
+ if ((normalizedRedeemedOffers?.[normalizedOfferId] ?? 0) !== safeCount) {
409
+ return false;
410
+ }
411
+ }
412
+ return true;
413
+ }
391
414
  function areRedeemedOffersEqual(currentRedeemedOffers, nextRedeemedOffers) {
392
415
  const normalizedCurrentRedeemedOffers = getNormalizedRedeemedOffersMap(currentRedeemedOffers);
393
416
  const normalizedNextRedeemedOffers = getNormalizedRedeemedOffersMap(nextRedeemedOffers);
@@ -465,11 +488,13 @@ function getReconciledRedeemedOffersMap({ existingRedeemedOffers, categories, of
465
488
  }
466
489
  const existingRedeemedTimes = toPositiveInteger(nextRedeemedOffers[normalizedOfferId], 0);
467
490
  if (existingRedeemedTimes <= 0) {
491
+ delete nextRedeemedOffers[normalizedOfferId];
468
492
  continue;
469
493
  }
470
494
  const maxApplicableTimes = toPositiveInteger(unlockedOffersApplicableTimes?.[offer._id], 0);
471
495
  if (maxApplicableTimes <= 0) {
472
- delete nextRedeemedOffers[normalizedOfferId];
496
+ // Preserve non-item claims as positive claimed-times when unlock state is temporarily unavailable.
497
+ nextRedeemedOffers[normalizedOfferId] = existingRedeemedTimes;
473
498
  continue;
474
499
  }
475
500
  nextRedeemedOffers[normalizedOfferId] = Math.min(existingRedeemedTimes, maxApplicableTimes);
@@ -483,16 +508,23 @@ function getNormalizedWalletRedemptionsMap(walletRedemptions) {
483
508
  if (walletRedemptions == null)
484
509
  return null;
485
510
  const normalized = {};
486
- const walletIds = Object.keys(walletRedemptions);
487
- for (let i = 0; i < walletIds.length; i++) {
488
- const walletId = walletIds[i];
489
- const trimmedId = walletId?.toString().trim();
490
- if (!trimmedId)
491
- continue;
492
- const amount = Number(walletRedemptions[walletId]);
493
- if (!Number.isFinite(amount) || amount <= 0)
494
- continue;
495
- normalized[trimmedId] = amount;
511
+ const cashbackAmount = Number(walletRedemptions.cashback_amount);
512
+ if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
513
+ normalized.cashback_amount = cashbackAmount;
514
+ }
515
+ const giftCardRedemption = walletRedemptions.gift_cards;
516
+ if (giftCardRedemption != null) {
517
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
518
+ const valueRedeemed = Number(giftCardRedemption.value_redeemed);
519
+ if (giftCardId != null && giftCardId.length > 0 && Number.isFinite(valueRedeemed) && valueRedeemed > 0) {
520
+ normalized.gift_cards = {
521
+ gift_card_id: giftCardId,
522
+ value_redeemed: valueRedeemed,
523
+ ...(giftCardRedemption.mystery_gift_type != null
524
+ ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
525
+ : {}),
526
+ };
527
+ }
496
528
  }
497
529
  return Object.keys(normalized).length > 0 ? normalized : null;
498
530
  }
@@ -2132,6 +2164,9 @@ offers) {
2132
2164
  const orderLevelDiscountBaseAmount = (0, utils_1.getOrderSubtotalFromCategories)(categories);
2133
2165
  let order_discount_details = data.order_discount_details;
2134
2166
  let redeemed_offers = getNormalizedRedeemedOffersMap(data.redeemed_offers);
2167
+ if (!isRedeemedOffersMapNormalized(data.redeemed_offers)) {
2168
+ hasChanges = true;
2169
+ }
2135
2170
  const redeemedOffersPresenceMap = getReconciledRedeemedOffersMap({
2136
2171
  existingRedeemedOffers: redeemed_offers,
2137
2172
  categories,
@@ -1286,6 +1286,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1286
1286
  if (stats.claimable) {
1287
1287
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
1288
1288
  }
1289
+ stats.applicable_quantity = 0;
1290
+ unlockedOffersApplicableTimes[offer._id] = 0;
1289
1291
  stats.claimable = false;
1290
1292
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
1291
1293
  }
@@ -1317,7 +1319,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1317
1319
  const totalItemsSelected = totalOfferItemsSelected + bachelorItems;
1318
1320
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1319
1321
  const unlockTimesForItems = Math.floor(totalItemsSelected / (num_of_items || 1));
1320
- const unlockTimesForAmount = Math.floor(orderValue / (total_amount || 1));
1322
+ const unlockTimesForAmount = total_amount != null && total_amount > 0 ? Math.floor(orderValue / total_amount) : unlockTimesForItems;
1321
1323
  const totalUnlockableClaims = Math.min(unlockTimesForItems, unlockTimesForAmount, maxClaimsPerOrder);
1322
1324
  const itemsProgress = getPreUnlockProgressPercentage(bachelorItems, num_of_items || 1);
1323
1325
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
@@ -1543,6 +1545,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1543
1545
  if (stats.claimable) {
1544
1546
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
1545
1547
  }
1548
+ stats.applicable_quantity = 0;
1549
+ unlockedOffersApplicableTimes[offer._id] = 0;
1546
1550
  stats.claimable = false;
1547
1551
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
1548
1552
  }
@@ -1603,7 +1607,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1603
1607
  }
1604
1608
  if (stats.progress >= 100) {
1605
1609
  const pointBurnTimes = Math.floor(effectiveLoyaltyPointsForCurrentOffer / (num_of_points || 1));
1606
- const amountTimes = Math.floor(orderValue / (total_amount || 1));
1610
+ const amountTimes = total_amount != null && total_amount > 0 ? Math.floor(orderValue / total_amount) : pointBurnTimes;
1607
1611
  const totalUnlockableClaims = Math.min(pointBurnTimes, amountTimes, maxClaimsPerOrder);
1608
1612
  if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
1609
1613
  if (offer.reward_discount == null)
@@ -1660,7 +1664,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1660
1664
  stats.message = `🎉 You have unlocked this offer 🎉`;
1661
1665
  }
1662
1666
  else {
1663
- stats.message = `This offer is redeemed ✅`;
1667
+ stats.message = redeemedTimes > 0 ? `This offer is redeemed ✅` : `🎉 You have unlocked this offer 🎉`;
1664
1668
  }
1665
1669
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1666
1670
  }
@@ -1798,7 +1802,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1798
1802
  stats.message = `🎉 You have unlocked this offer 🎉`;
1799
1803
  }
1800
1804
  else {
1801
- stats.message = `This offer is redeemed ✅`;
1805
+ stats.message = redeemedTimes > 0 ? `This offer is redeemed ✅` : `🎉 You have unlocked this offer 🎉`;
1802
1806
  }
1803
1807
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1804
1808
  }
@@ -1807,6 +1811,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1807
1811
  if (stats.claimable) {
1808
1812
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
1809
1813
  }
1814
+ stats.applicable_quantity = 0;
1815
+ unlockedOffersApplicableTimes[offer._id] = 0;
1810
1816
  stats.claimable = false;
1811
1817
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
1812
1818
  }
@@ -2082,6 +2088,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2082
2088
  if (stats.claimable) {
2083
2089
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
2084
2090
  }
2091
+ stats.applicable_quantity = 0;
2092
+ unlockedOffersApplicableTimes[offer._id] = 0;
2085
2093
  stats.claimable = false;
2086
2094
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2087
2095
  }
@@ -5185,7 +5185,14 @@ export declare const OrderSchema: Schema<any, import("mongoose").Model<any, any,
5185
5185
  is_gst_inclusive?: boolean | null | undefined;
5186
5186
  tax_invoice_provider_charges?: number | null | undefined;
5187
5187
  redeemed_offers?: Map<string, number> | null | undefined;
5188
- wallet_redemptions?: Map<string, number> | null | undefined;
5188
+ wallet_redemptions?: {
5189
+ gift_cards?: {
5190
+ gift_card_id: string;
5191
+ value_redeemed: number;
5192
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
5193
+ } | null | undefined;
5194
+ cashback_amount?: number | null | undefined;
5195
+ } | null | undefined;
5189
5196
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
5190
5197
  createdAt: NativeDate;
5191
5198
  updatedAt: NativeDate;
@@ -10284,7 +10291,14 @@ export declare const OrderSchema: Schema<any, import("mongoose").Model<any, any,
10284
10291
  is_gst_inclusive?: boolean | null | undefined;
10285
10292
  tax_invoice_provider_charges?: number | null | undefined;
10286
10293
  redeemed_offers?: Map<string, number> | null | undefined;
10287
- wallet_redemptions?: Map<string, number> | null | undefined;
10294
+ wallet_redemptions?: {
10295
+ gift_cards?: {
10296
+ gift_card_id: string;
10297
+ value_redeemed: number;
10298
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
10299
+ } | null | undefined;
10300
+ cashback_amount?: number | null | undefined;
10301
+ } | null | undefined;
10288
10302
  }>> & import("mongoose").FlatRecord<{
10289
10303
  createdAt: NativeDate;
10290
10304
  updatedAt: NativeDate;
@@ -15383,7 +15397,14 @@ export declare const OrderSchema: Schema<any, import("mongoose").Model<any, any,
15383
15397
  is_gst_inclusive?: boolean | null | undefined;
15384
15398
  tax_invoice_provider_charges?: number | null | undefined;
15385
15399
  redeemed_offers?: Map<string, number> | null | undefined;
15386
- wallet_redemptions?: Map<string, number> | null | undefined;
15400
+ wallet_redemptions?: {
15401
+ gift_cards?: {
15402
+ gift_card_id: string;
15403
+ value_redeemed: number;
15404
+ mystery_gift_type?: import("../../constants").GIFT_CARD_REWARD_TYPE_ENUM | null | undefined;
15405
+ } | null | undefined;
15406
+ cashback_amount?: number | null | undefined;
15407
+ } | null | undefined;
15387
15408
  }> & Required<{
15388
15409
  _id: import("mongoose").Types.ObjectId;
15389
15410
  }> & {
@@ -228,6 +228,33 @@ const DiscountDetailsSchema = new mongoose_1.Schema({
228
228
  required: true,
229
229
  },
230
230
  }, { _id: false, minimize: false });
231
+ const GiftCardRedemptionSchema = new mongoose_1.Schema({
232
+ gift_card_id: {
233
+ type: String,
234
+ required: true,
235
+ },
236
+ mystery_gift_type: {
237
+ type: String,
238
+ enum: constants_1.ALL_GIFT_CARD_REWARD_TYPES,
239
+ required: false,
240
+ },
241
+ value_redeemed: {
242
+ type: Number,
243
+ min: 0,
244
+ required: true,
245
+ },
246
+ }, { _id: false, minimize: false });
247
+ const WalletRedemptionsSchema = new mongoose_1.Schema({
248
+ cashback_amount: {
249
+ type: Number,
250
+ min: 0,
251
+ required: false,
252
+ },
253
+ gift_cards: {
254
+ type: GiftCardRedemptionSchema,
255
+ required: false,
256
+ },
257
+ }, { _id: false, minimize: false });
231
258
  const OptionsSchema = new mongoose_1.Schema({
232
259
  _id: {
233
260
  type: String,
@@ -814,8 +841,7 @@ exports.OrderSchema = new mongoose_1.Schema({
814
841
  required: false,
815
842
  },
816
843
  wallet_redemptions: {
817
- type: Map,
818
- of: Number,
844
+ type: WalletRedemptionsSchema,
819
845
  required: false,
820
846
  },
821
847
  order_settled_at: {
@@ -1,4 +1,4 @@
1
- import { ORDER_ITEM_TYPE_ENUM, ORDER_STATUS_ENUM, PAYMENT_METHOD_EXPECTED_ENUM, PAYMENT_PROVIDERS_ENUM, PAYMENT_STATUS_ENUM, PAYMENT_TYPE_ENUM, PLATFORMS_ENUM, SPLIT_BILL_TYPE_ENUM } from '../../constants';
1
+ import { GIFT_CARD_REWARD_TYPE_ENUM, ORDER_ITEM_TYPE_ENUM, ORDER_STATUS_ENUM, PAYMENT_METHOD_EXPECTED_ENUM, PAYMENT_PROVIDERS_ENUM, PAYMENT_STATUS_ENUM, PAYMENT_TYPE_ENUM, PLATFORMS_ENUM, SPLIT_BILL_TYPE_ENUM } from '../../constants';
2
2
  export interface IOrderCourse {
3
3
  name: string;
4
4
  course_id: string;
@@ -121,6 +121,15 @@ export interface IOrderDineInDetails {
121
121
  export interface IOrderTakeawayDetails {
122
122
  estimated_takeaway_time: Date | null;
123
123
  }
124
+ export interface IGiftCardRedemption {
125
+ gift_card_id: string;
126
+ mystery_gift_type?: GIFT_CARD_REWARD_TYPE_ENUM | null;
127
+ value_redeemed: number;
128
+ }
129
+ export interface IWalletRedemptions {
130
+ cashback_amount?: number | null;
131
+ gift_cards?: IGiftCardRedemption | null;
132
+ }
124
133
  interface CommonOptionalFields {
125
134
  delivery_details?: IOrderDeliveryDetails | null;
126
135
  dinein_details?: IOrderDineInDetails | null;
@@ -223,7 +232,7 @@ export interface IOrder extends CommonOptionalFields {
223
232
  payment_method_expected?: PAYMENT_METHOD_EXPECTED_ENUM;
224
233
  order_taker_details?: IStaffDetails | null;
225
234
  redeemed_offers?: Record<string, number> | null;
226
- wallet_redemptions?: Record<string, number> | null;
235
+ wallet_redemptions?: IWalletRedemptions | null;
227
236
  order_settled_at: Date;
228
237
  order_settled_at_mins: number;
229
238
  createdAt_mins: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.306",
3
+ "version": "0.2.308",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",