@managesome/eposmatic-toolkit 0.2.258 → 0.2.260

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.
@@ -1993,60 +1993,82 @@ offers) {
1993
1993
  };
1994
1994
  let hasChanges = false;
1995
1995
  const categories = data.categories ?? [];
1996
+ const orderLevelDiscountBaseAmount = categories.reduce((subtotal, category) => {
1997
+ return subtotal + category.items.reduce((itemSubtotal, item) => itemSubtotal + (item.subtotal ?? 0), 0);
1998
+ }, 0);
1996
1999
  let order_discount_details = data.order_discount_details;
2000
+ const clearOrderLevelDiscount = (discountType) => {
2001
+ if (order_discount_details == null) {
2002
+ return;
2003
+ }
2004
+ if (discountType === 'loyalty') {
2005
+ order_discount_details = {
2006
+ ...order_discount_details,
2007
+ loyalty_discount_id: undefined,
2008
+ loyalty_discount_name: undefined,
2009
+ loyalty_discount: undefined,
2010
+ is_loyalty_discount_percentage: false,
2011
+ };
2012
+ }
2013
+ else {
2014
+ order_discount_details = {
2015
+ ...order_discount_details,
2016
+ voucher_discount_id: undefined,
2017
+ voucher_discount_name: undefined,
2018
+ voucher_discount: undefined,
2019
+ is_voucher_discount_percentage: false,
2020
+ };
2021
+ }
2022
+ hasChanges = true;
2023
+ };
1997
2024
  if (order_discount_details != null) {
1998
2025
  // we check if either the loyalty_discount_id or voucher_discount_id
1999
2026
  const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
2000
2027
  if (loyaltyDiscountId != null) {
2001
2028
  if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
2002
- order_discount_details = {
2003
- ...order_discount_details,
2004
- loyalty_discount_id: undefined,
2005
- loyalty_discount_name: undefined,
2006
- loyalty_discount: undefined,
2007
- is_loyalty_discount_percentage: false,
2008
- };
2009
- hasChanges = true;
2029
+ clearOrderLevelDiscount('loyalty');
2010
2030
  }
2011
2031
  else {
2012
2032
  // Find the offer to check for excluded_refs
2013
2033
  const loyaltyOffer = offers.find((o) => o._id === loyaltyDiscountId);
2014
2034
  if (loyaltyOffer?.reward_discount != null) {
2015
- let discount_value = loyaltyOffer.reward_discount.discount_value;
2016
- const { excluded_refs, is_discount_percentage } = loyaltyOffer.reward_discount;
2017
- // Handle excluded_refs - items/categories that should not receive discount
2018
- if (excluded_refs != null && excluded_refs.length > 0) {
2019
- const { subtotal: eligibleSubtotal, hasExcludedRefs } = calculateEligibleOfferDiscountDetails(categories, excluded_refs);
2020
- if (hasExcludedRefs) {
2021
- // Static discounts with exclusions are not supported - silently skip
2022
- if (!is_discount_percentage) {
2023
- order_discount_details = {
2024
- ...order_discount_details,
2025
- loyalty_discount_id: undefined,
2026
- loyalty_discount_name: undefined,
2027
- loyalty_discount: undefined,
2028
- is_loyalty_discount_percentage: false,
2029
- };
2030
- hasChanges = true;
2031
- }
2032
- else if (eligibleSubtotal <= 0) {
2033
- // If no eligible items, silently skip
2034
- order_discount_details = {
2035
- ...order_discount_details,
2036
- loyalty_discount_id: undefined,
2037
- loyalty_discount_name: undefined,
2038
- loyalty_discount: undefined,
2039
- is_loyalty_discount_percentage: false,
2040
- };
2041
- hasChanges = true;
2035
+ const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = loyaltyOffer.reward_discount;
2036
+ const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
2037
+ ? calculateEligibleOfferDiscountDetails(categories, excluded_refs)
2038
+ : {
2039
+ subtotal: orderLevelDiscountBaseAmount,
2040
+ hasExcludedRefs: false,
2041
+ };
2042
+ // Static discounts with exclusions are not supported - silently skip
2043
+ if (hasExcludedRefs && !is_discount_percentage) {
2044
+ clearOrderLevelDiscount('loyalty');
2045
+ }
2046
+ else {
2047
+ const hasRelevantCap = is_discount_percentage &&
2048
+ max_discount_amount != null &&
2049
+ Number.isFinite(max_discount_amount) &&
2050
+ max_discount_amount >= 0;
2051
+ const hasRelevantPercentageCap = !is_discount_percentage &&
2052
+ max_discount_percentage != null &&
2053
+ Number.isFinite(max_discount_percentage) &&
2054
+ max_discount_percentage >= 0;
2055
+ const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
2056
+ const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
2057
+ if (shouldNormalizeToStatic) {
2058
+ if (discountBaseAmount <= 0) {
2059
+ clearOrderLevelDiscount('loyalty');
2042
2060
  }
2043
2061
  else {
2044
- // Convert percentage to static value based on eligible subtotal
2045
- discount_value = (eligibleSubtotal * discount_value) / 100;
2046
- discount_value = Math.min(discount_value, eligibleSubtotal);
2062
+ const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
2063
+ baseAmount: discountBaseAmount,
2064
+ discountValue: discount_value,
2065
+ isDiscountPercentage: is_discount_percentage,
2066
+ maxDiscountAmount: max_discount_amount,
2067
+ maxDiscountPercentage: max_discount_percentage,
2068
+ });
2047
2069
  order_discount_details = {
2048
2070
  ...order_discount_details,
2049
- loyalty_discount: (0, utils_1.parseNum)(discount_value),
2071
+ loyalty_discount: (0, utils_1.parseNum)(calculatedDiscount),
2050
2072
  is_loyalty_discount_percentage: false,
2051
2073
  };
2052
2074
  hasChanges = true;
@@ -2060,54 +2082,49 @@ offers) {
2060
2082
  const voucherDiscountId = order_discount_details.voucher_discount_id;
2061
2083
  if (voucherDiscountId != null) {
2062
2084
  if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
2063
- order_discount_details = {
2064
- ...order_discount_details,
2065
- voucher_discount_id: undefined,
2066
- voucher_discount_name: undefined,
2067
- voucher_discount: undefined,
2068
- is_voucher_discount_percentage: false,
2069
- };
2070
- hasChanges = true;
2085
+ clearOrderLevelDiscount('voucher');
2071
2086
  }
2072
2087
  else {
2073
2088
  // Find the offer to check for excluded_refs
2074
2089
  const voucherOffer = offers.find((o) => o._id === voucherDiscountId);
2075
2090
  if (voucherOffer?.reward_discount != null) {
2076
- let discount_value = voucherOffer.reward_discount.discount_value;
2077
- const { excluded_refs, is_discount_percentage } = voucherOffer.reward_discount;
2078
- // Handle excluded_refs - items/categories that should not receive discount
2079
- if (excluded_refs != null && excluded_refs.length > 0) {
2080
- const { subtotal: eligibleSubtotal, hasExcludedRefs } = calculateEligibleOfferDiscountDetails(categories, excluded_refs);
2081
- if (hasExcludedRefs) {
2082
- // Static discounts with exclusions are not supported - silently skip
2083
- if (!is_discount_percentage) {
2084
- order_discount_details = {
2085
- ...order_discount_details,
2086
- voucher_discount_id: undefined,
2087
- voucher_discount_name: undefined,
2088
- voucher_discount: undefined,
2089
- is_voucher_discount_percentage: false,
2090
- };
2091
- hasChanges = true;
2092
- }
2093
- else if (eligibleSubtotal <= 0) {
2094
- // If no eligible items, silently skip
2095
- order_discount_details = {
2096
- ...order_discount_details,
2097
- voucher_discount_id: undefined,
2098
- voucher_discount_name: undefined,
2099
- voucher_discount: undefined,
2100
- is_voucher_discount_percentage: false,
2101
- };
2102
- hasChanges = true;
2091
+ const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = voucherOffer.reward_discount;
2092
+ const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
2093
+ ? calculateEligibleOfferDiscountDetails(categories, excluded_refs)
2094
+ : {
2095
+ subtotal: orderLevelDiscountBaseAmount,
2096
+ hasExcludedRefs: false,
2097
+ };
2098
+ // Static discounts with exclusions are not supported - silently skip
2099
+ if (hasExcludedRefs && !is_discount_percentage) {
2100
+ clearOrderLevelDiscount('voucher');
2101
+ }
2102
+ else {
2103
+ const hasRelevantCap = is_discount_percentage &&
2104
+ max_discount_amount != null &&
2105
+ Number.isFinite(max_discount_amount) &&
2106
+ max_discount_amount >= 0;
2107
+ const hasRelevantPercentageCap = !is_discount_percentage &&
2108
+ max_discount_percentage != null &&
2109
+ Number.isFinite(max_discount_percentage) &&
2110
+ max_discount_percentage >= 0;
2111
+ const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
2112
+ const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
2113
+ if (shouldNormalizeToStatic) {
2114
+ if (discountBaseAmount <= 0) {
2115
+ clearOrderLevelDiscount('voucher');
2103
2116
  }
2104
2117
  else {
2105
- // Convert percentage to static value based on eligible subtotal
2106
- discount_value = (eligibleSubtotal * discount_value) / 100;
2107
- discount_value = Math.min(discount_value, eligibleSubtotal);
2118
+ const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
2119
+ baseAmount: discountBaseAmount,
2120
+ discountValue: discount_value,
2121
+ isDiscountPercentage: is_discount_percentage,
2122
+ maxDiscountAmount: max_discount_amount,
2123
+ maxDiscountPercentage: max_discount_percentage,
2124
+ });
2108
2125
  order_discount_details = {
2109
2126
  ...order_discount_details,
2110
- voucher_discount: (0, utils_1.parseNum)(discount_value),
2127
+ voucher_discount: (0, utils_1.parseNum)(calculatedDiscount),
2111
2128
  is_voucher_discount_percentage: false,
2112
2129
  };
2113
2130
  hasChanges = true;
@@ -34,3 +34,12 @@ export declare function formattedStoreName(name: string): string;
34
34
  export declare const formatFieldName: (field: string) => string;
35
35
  export declare function getPercentage(total: number, num: number): number;
36
36
  export declare function getPercentageAmount(total: number, percent: number): number;
37
+ interface IGetCappedOfferDiscountAmount {
38
+ baseAmount: number;
39
+ discountValue: number;
40
+ isDiscountPercentage: boolean;
41
+ maxDiscountAmount?: number | null;
42
+ maxDiscountPercentage?: number | null;
43
+ }
44
+ export declare function getCappedOfferDiscountAmount({ baseAmount, discountValue, isDiscountPercentage, maxDiscountAmount, maxDiscountPercentage, }: IGetCappedOfferDiscountAmount): number;
45
+ export {};
@@ -28,6 +28,7 @@ exports.getFormattedCancellationReason = getFormattedCancellationReason;
28
28
  exports.formattedStoreName = formattedStoreName;
29
29
  exports.getPercentage = getPercentage;
30
30
  exports.getPercentageAmount = getPercentageAmount;
31
+ exports.getCappedOfferDiscountAmount = getCappedOfferDiscountAmount;
31
32
  const google_libphonenumber_1 = require("google-libphonenumber");
32
33
  const moment_1 = __importDefault(require("moment"));
33
34
  const constants_1 = require("../../constants");
@@ -330,3 +331,23 @@ function getPercentage(total, num) {
330
331
  function getPercentageAmount(total, percent) {
331
332
  return (total * percent) / 100;
332
333
  }
334
+ function getCappedOfferDiscountAmount({ baseAmount, discountValue, isDiscountPercentage, maxDiscountAmount, maxDiscountPercentage, }) {
335
+ const normalizedBaseAmount = Number.isFinite(baseAmount) ? Math.max(0, baseAmount) : 0;
336
+ const normalizedDiscountValue = Number.isFinite(discountValue) ? Math.max(0, discountValue) : 0;
337
+ if (normalizedBaseAmount <= 0 || normalizedDiscountValue <= 0) {
338
+ return 0;
339
+ }
340
+ if (isDiscountPercentage) {
341
+ let amount = getPercentageAmount(normalizedBaseAmount, normalizedDiscountValue);
342
+ if (maxDiscountAmount != null && Number.isFinite(maxDiscountAmount) && maxDiscountAmount >= 0) {
343
+ amount = Math.min(amount, maxDiscountAmount);
344
+ }
345
+ return Math.min(amount, normalizedBaseAmount);
346
+ }
347
+ let amount = normalizedDiscountValue;
348
+ if (maxDiscountPercentage != null && Number.isFinite(maxDiscountPercentage) && maxDiscountPercentage >= 0) {
349
+ const maxAmountFromPercentage = getPercentageAmount(normalizedBaseAmount, maxDiscountPercentage);
350
+ amount = Math.min(amount, maxAmountFromPercentage);
351
+ }
352
+ return Math.min(amount, normalizedBaseAmount);
353
+ }
@@ -22,6 +22,8 @@ export interface IRewardPreview {
22
22
  label: string;
23
23
  discount_value?: number;
24
24
  is_percentage?: boolean;
25
+ max_discount_amount?: number | null;
26
+ max_discount_percentage?: number | null;
25
27
  free_items_count?: number;
26
28
  points_value?: number;
27
29
  cashback_value?: number;
@@ -100,6 +100,14 @@ function getOfferWithPrice(offer, writeable) {
100
100
  delete newOffer.reward_cashback;
101
101
  delete newOffer.reward_gift_cards;
102
102
  }
103
+ if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT && newOffer.reward_discount != null) {
104
+ if (newOffer.reward_discount.is_discount_percentage) {
105
+ newOffer.reward_discount.max_discount_percentage = null;
106
+ }
107
+ else {
108
+ newOffer.reward_discount.max_discount_amount = null;
109
+ }
110
+ }
103
111
  }
104
112
  return {
105
113
  ...newOffer,
@@ -151,6 +159,12 @@ function getOfferWithPrice(offer, writeable) {
151
159
  reward_discount: {
152
160
  ...newOffer.reward_discount,
153
161
  discount_value: convertToNum(newOffer.reward_discount.discount_value),
162
+ max_discount_amount: newOffer.reward_discount.max_discount_amount != null
163
+ ? convertToNum(newOffer.reward_discount.max_discount_amount)
164
+ : null,
165
+ max_discount_percentage: newOffer.reward_discount.max_discount_percentage != null
166
+ ? convertToNum(newOffer.reward_discount.max_discount_percentage)
167
+ : null,
154
168
  },
155
169
  }
156
170
  : {}),
@@ -506,12 +520,21 @@ function buildRewardPreview(offer) {
506
520
  case constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT: {
507
521
  if (offer.reward_discount == null)
508
522
  return null;
509
- const { discount_value, is_discount_percentage } = offer.reward_discount;
523
+ const { discount_value, is_discount_percentage, max_discount_amount, max_discount_percentage } = offer.reward_discount;
524
+ let label = is_discount_percentage ? `${discount_value}% off` : `${discount_value} off`;
525
+ if (is_discount_percentage && max_discount_amount != null) {
526
+ label += ` (max ${max_discount_amount})`;
527
+ }
528
+ if (!is_discount_percentage && max_discount_percentage != null) {
529
+ label += ` (max ${max_discount_percentage}%)`;
530
+ }
510
531
  return {
511
532
  type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
512
- label: is_discount_percentage ? `${discount_value}% off` : `${discount_value} off`,
533
+ label,
513
534
  discount_value,
514
535
  is_percentage: is_discount_percentage,
536
+ max_discount_amount,
537
+ max_discount_percentage,
515
538
  };
516
539
  }
517
540
  case constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS: {
@@ -356,6 +356,8 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
356
356
  is_discount_percentage: boolean;
357
357
  excluded_refs: string[];
358
358
  discount_value: number;
359
+ max_discount_amount?: number | null | undefined;
360
+ max_discount_percentage?: number | null | undefined;
359
361
  } | null | undefined;
360
362
  reward_discount_items?: {
361
363
  is_discount_percentage: boolean;
@@ -743,6 +745,8 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
743
745
  is_discount_percentage: boolean;
744
746
  excluded_refs: string[];
745
747
  discount_value: number;
748
+ max_discount_amount?: number | null | undefined;
749
+ max_discount_percentage?: number | null | undefined;
746
750
  } | null | undefined;
747
751
  reward_discount_items?: {
748
752
  is_discount_percentage: boolean;
@@ -1130,6 +1134,8 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
1130
1134
  is_discount_percentage: boolean;
1131
1135
  excluded_refs: string[];
1132
1136
  discount_value: number;
1137
+ max_discount_amount?: number | null | undefined;
1138
+ max_discount_percentage?: number | null | undefined;
1133
1139
  } | null | undefined;
1134
1140
  reward_discount_items?: {
1135
1141
  is_discount_percentage: boolean;
@@ -168,6 +168,16 @@ const RewardDiscountSchema = new mongoose_1.Schema({
168
168
  type: Boolean,
169
169
  required: true,
170
170
  },
171
+ max_discount_amount: {
172
+ type: Number,
173
+ required: false,
174
+ default: null,
175
+ },
176
+ max_discount_percentage: {
177
+ type: Number,
178
+ required: false,
179
+ default: null,
180
+ },
171
181
  excluded_refs: {
172
182
  type: [String],
173
183
  required: true,
@@ -8,10 +8,11 @@ export declare const OfferFingerprintSchema: Schema<any, import("mongoose").Mode
8
8
  updatedAt: NativeDate;
9
9
  } & {
10
10
  _id: import("mongoose").Types.ObjectId;
11
- tenant_id: string;
11
+ tenants: string[];
12
12
  createdAt_mins: number;
13
13
  createdAt: NativeDate;
14
14
  updatedAt: NativeDate;
15
+ brand_id: string;
15
16
  customer_phone: string;
16
17
  offer_id: import("mongoose").Types.ObjectId;
17
18
  redeemed_times: number;
@@ -21,10 +22,11 @@ export declare const OfferFingerprintSchema: Schema<any, import("mongoose").Mode
21
22
  updatedAt: NativeDate;
22
23
  } & {
23
24
  _id: import("mongoose").Types.ObjectId;
24
- tenant_id: string;
25
+ tenants: string[];
25
26
  createdAt_mins: number;
26
27
  createdAt: NativeDate;
27
28
  updatedAt: NativeDate;
29
+ brand_id: string;
28
30
  customer_phone: string;
29
31
  offer_id: import("mongoose").Types.ObjectId;
30
32
  redeemed_times: number;
@@ -34,10 +36,11 @@ export declare const OfferFingerprintSchema: Schema<any, import("mongoose").Mode
34
36
  updatedAt: NativeDate;
35
37
  } & {
36
38
  _id: import("mongoose").Types.ObjectId;
37
- tenant_id: string;
39
+ tenants: string[];
38
40
  createdAt_mins: number;
39
41
  createdAt: NativeDate;
40
42
  updatedAt: NativeDate;
43
+ brand_id: string;
41
44
  customer_phone: string;
42
45
  offer_id: import("mongoose").Types.ObjectId;
43
46
  redeemed_times: number;
@@ -8,11 +8,15 @@ exports.OfferFingerprintSchema = new mongoose_1.Schema({
8
8
  required: true,
9
9
  immutable: true,
10
10
  },
11
- tenant_id: {
11
+ brand_id: {
12
12
  type: String,
13
13
  required: true,
14
14
  immutable: true,
15
- ref: 'tenants',
15
+ ref: 'brands',
16
+ },
17
+ tenants: {
18
+ type: [String],
19
+ default: [],
16
20
  },
17
21
  offer_id: {
18
22
  type: mongoose_1.Schema.Types.ObjectId,
@@ -74,6 +74,8 @@ export interface IRewardTypeDiscount {
74
74
  */
75
75
  discount_value: number;
76
76
  is_discount_percentage: boolean;
77
+ max_discount_amount: number | null;
78
+ max_discount_percentage: number | null;
77
79
  excluded_refs: string[];
78
80
  }
79
81
  export interface IRewardTypeDiscountItems {
@@ -1,6 +1,7 @@
1
1
  export interface IOfferFingerprint {
2
2
  _id: string;
3
- tenant_id: string;
3
+ brand_id: string;
4
+ tenants: string[];
4
5
  offer_id: string;
5
6
  customer_phone: string;
6
7
  redeemed_times: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.258",
3
+ "version": "0.2.260",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",