@managesome/eposmatic-toolkit 0.2.261 → 0.2.263

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,9 +1993,7 @@ 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
+ const orderLevelDiscountBaseAmount = (0, utils_1.getOrderSubtotalFromCategories)(categories);
1999
1997
  let order_discount_details = data.order_discount_details;
2000
1998
  const clearOrderLevelDiscount = (discountType) => {
2001
1999
  if (order_discount_details == null) {
@@ -27,6 +27,7 @@ export * from './kds-helpers';
27
27
  export * from './location-helpers';
28
28
  export * from './menu-helpers';
29
29
  export * from './minified-catalog';
30
+ export * from './offer-apply-helpers';
30
31
  export * from './offers-helpers';
31
32
  export * from './operational-roles-helpers';
32
33
  export * from './order-engine';
@@ -43,6 +43,7 @@ __exportStar(require("./kds-helpers"), exports);
43
43
  __exportStar(require("./location-helpers"), exports);
44
44
  __exportStar(require("./menu-helpers"), exports);
45
45
  __exportStar(require("./minified-catalog"), exports);
46
+ __exportStar(require("./offer-apply-helpers"), exports);
46
47
  __exportStar(require("./offers-helpers"), exports);
47
48
  __exportStar(require("./operational-roles-helpers"), exports);
48
49
  __exportStar(require("./order-engine"), exports);
@@ -0,0 +1,34 @@
1
+ import { IDiscountDetails, IGiftCard, IOffer, IOrderCategory } from '../../schemas';
2
+ export declare function getOrderSubtotalFromCategories(categories: IOrderCategory[] | undefined): number;
3
+ export interface IApplyDiscountResult {
4
+ success: boolean;
5
+ discountDetails: IDiscountDetails | null;
6
+ errorMessage: string | null;
7
+ }
8
+ export declare function buildOfferDiscountUpdate({ offer, orderDiscountDetails, categories, }: {
9
+ offer: IOffer;
10
+ orderDiscountDetails: IDiscountDetails | null | undefined;
11
+ categories: IOrderCategory[] | undefined;
12
+ }): IApplyDiscountResult;
13
+ export declare function buildOfferPointsUpdate({ offer, orderDiscountDetails, }: {
14
+ offer: IOffer;
15
+ orderDiscountDetails: IDiscountDetails | null | undefined;
16
+ }): IApplyDiscountResult;
17
+ export declare function buildOfferCashbackUpdate({ offer, orderDiscountDetails, categories, }: {
18
+ offer: IOffer;
19
+ orderDiscountDetails: IDiscountDetails | null | undefined;
20
+ categories: IOrderCategory[] | undefined;
21
+ }): IApplyDiscountResult;
22
+ export declare function buildOfferRewardUpdate({ offer, orderDiscountDetails, }: {
23
+ offer: IOffer;
24
+ orderDiscountDetails: IDiscountDetails | null | undefined;
25
+ }): IApplyDiscountResult;
26
+ export declare function buildGiftCardDiscountUpdate({ giftCard, orderDiscountDetails, categories, }: {
27
+ giftCard: IGiftCard;
28
+ orderDiscountDetails: IDiscountDetails | null | undefined;
29
+ categories: IOrderCategory[] | undefined;
30
+ }): IApplyDiscountResult;
31
+ export declare function buildVoucherDiscountUpdate({ code, orderDiscountDetails, }: {
32
+ code: string;
33
+ orderDiscountDetails: IDiscountDetails | null | undefined;
34
+ }): IDiscountDetails;
@@ -0,0 +1,297 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getOrderSubtotalFromCategories = getOrderSubtotalFromCategories;
4
+ exports.buildOfferDiscountUpdate = buildOfferDiscountUpdate;
5
+ exports.buildOfferPointsUpdate = buildOfferPointsUpdate;
6
+ exports.buildOfferCashbackUpdate = buildOfferCashbackUpdate;
7
+ exports.buildOfferRewardUpdate = buildOfferRewardUpdate;
8
+ exports.buildGiftCardDiscountUpdate = buildGiftCardDiscountUpdate;
9
+ exports.buildVoucherDiscountUpdate = buildVoucherDiscountUpdate;
10
+ const constants_1 = require("../../constants");
11
+ const common_1 = require("./common");
12
+ const cart_actions_1 = require("../store/cart-actions");
13
+ // ================================><================================= //
14
+ // === Shared order subtotal for discount logic === //
15
+ // ================================><================================= //
16
+ function getOrderSubtotalFromCategories(categories) {
17
+ if (!categories?.length)
18
+ return 0;
19
+ let subtotal = 0;
20
+ for (let i = 0; i < categories.length; i++) {
21
+ const items = categories[i].items;
22
+ for (let j = 0; j < items.length; j++) {
23
+ subtotal += items[j].subtotal ?? 0;
24
+ }
25
+ }
26
+ return subtotal;
27
+ }
28
+ // ================================><================================= //
29
+ // === buildOfferDiscountUpdate === //
30
+ // ================================><================================= //
31
+ function buildOfferDiscountUpdate({ offer, orderDiscountDetails, categories, }) {
32
+ if (offer.reward_discount == null) {
33
+ return { success: false, discountDetails: null, errorMessage: null };
34
+ }
35
+ const discountSlot = offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ? 'loyalty' : 'voucher';
36
+ // Already-applied guard
37
+ if (orderDiscountDetails != null &&
38
+ (discountSlot === 'loyalty' ? orderDiscountDetails.loyalty_discount_id : orderDiscountDetails.voucher_discount_id)) {
39
+ return {
40
+ success: false,
41
+ discountDetails: null,
42
+ errorMessage: 'You already have an offer applied to your order.',
43
+ };
44
+ }
45
+ let { discount_value, is_discount_percentage } = offer.reward_discount;
46
+ const { max_discount_amount, max_discount_percentage, excluded_refs } = offer.reward_discount;
47
+ const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategories(categories);
48
+ // Handle excluded_refs
49
+ if (excluded_refs != null && excluded_refs.length > 0) {
50
+ const { subtotal: eligibleSubtotal, hasExcludedRefs } = (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs);
51
+ if (hasExcludedRefs) {
52
+ if (!is_discount_percentage) {
53
+ return { success: false, discountDetails: null, errorMessage: null };
54
+ }
55
+ if (eligibleSubtotal <= 0) {
56
+ return { success: false, discountDetails: null, errorMessage: null };
57
+ }
58
+ discount_value = (0, common_1.parseNum)((0, common_1.getCappedOfferDiscountAmount)({
59
+ baseAmount: eligibleSubtotal,
60
+ discountValue: discount_value,
61
+ isDiscountPercentage: is_discount_percentage,
62
+ maxDiscountAmount: max_discount_amount,
63
+ maxDiscountPercentage: max_discount_percentage,
64
+ }));
65
+ is_discount_percentage = false;
66
+ }
67
+ }
68
+ // Cap handling for non-excluded cases
69
+ const hasRelevantCap = is_discount_percentage &&
70
+ max_discount_amount != null &&
71
+ Number.isFinite(max_discount_amount) &&
72
+ max_discount_amount >= 0;
73
+ const hasRelevantPercentageCap = !is_discount_percentage &&
74
+ max_discount_percentage != null &&
75
+ Number.isFinite(max_discount_percentage) &&
76
+ max_discount_percentage >= 0;
77
+ if (hasRelevantCap || hasRelevantPercentageCap) {
78
+ discount_value = (0, common_1.parseNum)((0, common_1.getCappedOfferDiscountAmount)({
79
+ baseAmount: orderLevelDiscountBaseAmount,
80
+ discountValue: discount_value,
81
+ isDiscountPercentage: is_discount_percentage,
82
+ maxDiscountAmount: max_discount_amount,
83
+ maxDiscountPercentage: max_discount_percentage,
84
+ }));
85
+ is_discount_percentage = false;
86
+ }
87
+ const discountDetails = {
88
+ ...orderDiscountDetails,
89
+ ...(discountSlot === 'loyalty'
90
+ ? {
91
+ loyalty_discount_id: offer._id,
92
+ loyalty_discount_name: offer.name,
93
+ loyalty_discount: discount_value,
94
+ is_loyalty_discount_percentage: is_discount_percentage,
95
+ }
96
+ : {
97
+ voucher_discount_id: offer._id,
98
+ voucher_discount_name: offer.name,
99
+ voucher_discount: discount_value,
100
+ is_voucher_discount_percentage: is_discount_percentage,
101
+ }),
102
+ total_discount: 0,
103
+ };
104
+ return { success: true, discountDetails, errorMessage: null };
105
+ }
106
+ // ================================><================================= //
107
+ // === buildOfferPointsUpdate === //
108
+ // ================================><================================= //
109
+ function buildOfferPointsUpdate({ offer, orderDiscountDetails, }) {
110
+ if (offer.reward_points == null) {
111
+ return { success: false, discountDetails: null, errorMessage: null };
112
+ }
113
+ const discountSlot = offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ? 'loyalty' : 'voucher';
114
+ if (orderDiscountDetails != null &&
115
+ (discountSlot === 'loyalty' ? orderDiscountDetails.loyalty_discount_id : orderDiscountDetails.voucher_discount_id)) {
116
+ return {
117
+ success: false,
118
+ discountDetails: null,
119
+ errorMessage: 'You already have an offer applied to your order.',
120
+ };
121
+ }
122
+ const discountDetails = {
123
+ ...orderDiscountDetails,
124
+ ...(discountSlot === 'loyalty'
125
+ ? {
126
+ loyalty_discount_id: offer._id,
127
+ loyalty_discount_name: offer.name,
128
+ loyalty_discount: 0,
129
+ is_loyalty_discount_percentage: false,
130
+ }
131
+ : {
132
+ voucher_discount_id: offer._id,
133
+ voucher_discount_name: offer.name,
134
+ voucher_discount: 0,
135
+ is_voucher_discount_percentage: false,
136
+ }),
137
+ total_discount: 0,
138
+ };
139
+ return { success: true, discountDetails, errorMessage: null };
140
+ }
141
+ // ================================><================================= //
142
+ // === buildOfferCashbackUpdate === //
143
+ // ================================><================================= //
144
+ function buildOfferCashbackUpdate({ offer, orderDiscountDetails, categories, }) {
145
+ if (offer.reward_cashback == null) {
146
+ return { success: false, discountDetails: null, errorMessage: null };
147
+ }
148
+ // Validate excluded_refs
149
+ const excluded_refs = offer.reward_cashback.excluded_refs ?? [];
150
+ if (excluded_refs.length > 0) {
151
+ const { subtotal: eligibleSubtotal } = (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs);
152
+ if (eligibleSubtotal <= 0) {
153
+ return { success: false, discountDetails: null, errorMessage: null };
154
+ }
155
+ }
156
+ const discountSlot = offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ? 'loyalty' : 'voucher';
157
+ if (orderDiscountDetails != null &&
158
+ (discountSlot === 'loyalty' ? orderDiscountDetails.loyalty_discount_id : orderDiscountDetails.voucher_discount_id)) {
159
+ return {
160
+ success: false,
161
+ discountDetails: null,
162
+ errorMessage: 'You already have an offer applied to your order.',
163
+ };
164
+ }
165
+ const discountDetails = {
166
+ ...orderDiscountDetails,
167
+ ...(discountSlot === 'loyalty'
168
+ ? {
169
+ loyalty_discount_id: offer._id,
170
+ loyalty_discount_name: offer.name,
171
+ loyalty_discount: 0,
172
+ is_loyalty_discount_percentage: false,
173
+ }
174
+ : {
175
+ voucher_discount_id: offer._id,
176
+ voucher_discount_name: offer.name,
177
+ voucher_discount: 0,
178
+ is_voucher_discount_percentage: false,
179
+ }),
180
+ total_discount: 0,
181
+ };
182
+ return { success: true, discountDetails, errorMessage: null };
183
+ }
184
+ // ================================><================================= //
185
+ // === buildOfferRewardUpdate === //
186
+ // === (for GIFT_CARDS and VOUCHER reward types — post-order) === //
187
+ // ================================><================================= //
188
+ function buildOfferRewardUpdate({ offer, orderDiscountDetails, }) {
189
+ const discountSlot = offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ? 'loyalty' : 'voucher';
190
+ if (orderDiscountDetails != null &&
191
+ (discountSlot === 'loyalty' ? orderDiscountDetails.loyalty_discount_id : orderDiscountDetails.voucher_discount_id)) {
192
+ return {
193
+ success: false,
194
+ discountDetails: null,
195
+ errorMessage: 'You already have an offer applied to your order.',
196
+ };
197
+ }
198
+ const discountDetails = {
199
+ ...orderDiscountDetails,
200
+ ...(discountSlot === 'loyalty'
201
+ ? {
202
+ loyalty_discount_id: offer._id,
203
+ loyalty_discount_name: offer.name,
204
+ loyalty_discount: 0,
205
+ is_loyalty_discount_percentage: false,
206
+ }
207
+ : {
208
+ voucher_discount_id: offer._id,
209
+ voucher_discount_name: offer.name,
210
+ voucher_discount: 0,
211
+ is_voucher_discount_percentage: false,
212
+ }),
213
+ total_discount: 0,
214
+ };
215
+ return { success: true, discountDetails, errorMessage: null };
216
+ }
217
+ // ================================><================================= //
218
+ // === buildGiftCardDiscountUpdate === //
219
+ // ================================><================================= //
220
+ function buildGiftCardDiscountUpdate({ giftCard, orderDiscountDetails, categories, }) {
221
+ // Already-applied guard
222
+ if (orderDiscountDetails != null && orderDiscountDetails.special_discount_id) {
223
+ return {
224
+ success: false,
225
+ discountDetails: null,
226
+ errorMessage: 'You already have a gift card or special discount applied.',
227
+ };
228
+ }
229
+ const rewardType = giftCard.reward_type;
230
+ if (rewardType === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.DISCOUNT) {
231
+ const reward = giftCard.reward_discount;
232
+ if (!reward)
233
+ return { success: false, discountDetails: null, errorMessage: null };
234
+ const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage } = reward;
235
+ const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategories(categories);
236
+ const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
237
+ ? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
238
+ : { subtotal: orderLevelDiscountBaseAmount, hasExcludedRefs: false };
239
+ const hasRelevantCap = is_discount_percentage &&
240
+ max_discount_amount != null &&
241
+ Number.isFinite(max_discount_amount) &&
242
+ max_discount_amount >= 0;
243
+ const hasRelevantPercentageCap = !is_discount_percentage &&
244
+ max_discount_percentage != null &&
245
+ Number.isFinite(max_discount_percentage) &&
246
+ max_discount_percentage >= 0;
247
+ const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
248
+ const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
249
+ if (shouldNormalizeToStatic && discountBaseAmount <= 0) {
250
+ return {
251
+ success: false,
252
+ discountDetails: null,
253
+ errorMessage: 'Gift card is not applicable to current cart items.',
254
+ };
255
+ }
256
+ const resolvedDiscount = shouldNormalizeToStatic
257
+ ? (0, common_1.parseNum)((0, common_1.getCappedOfferDiscountAmount)({
258
+ baseAmount: discountBaseAmount,
259
+ discountValue: discount_value,
260
+ isDiscountPercentage: is_discount_percentage,
261
+ maxDiscountAmount: max_discount_amount,
262
+ maxDiscountPercentage: max_discount_percentage,
263
+ }))
264
+ : discount_value;
265
+ const discountDetails = {
266
+ ...orderDiscountDetails,
267
+ special_discount_id: giftCard._id,
268
+ special_discount_name: giftCard.name,
269
+ special_discount: resolvedDiscount,
270
+ is_special_discount_percentage: shouldNormalizeToStatic ? false : is_discount_percentage,
271
+ special_reference_number: giftCard.code,
272
+ total_discount: 0,
273
+ };
274
+ return { success: true, discountDetails, errorMessage: null };
275
+ }
276
+ // POINTS, CASHBACK, VOUCHER, ITEMS, DISCOUNT_ITEMS, MYSTERY_GIFT — tag for backend resolution
277
+ const discountDetails = {
278
+ ...orderDiscountDetails,
279
+ special_discount_id: giftCard._id,
280
+ special_discount_name: giftCard.name,
281
+ special_discount: 0,
282
+ is_special_discount_percentage: false,
283
+ special_reference_number: giftCard.code,
284
+ total_discount: 0,
285
+ };
286
+ return { success: true, discountDetails, errorMessage: null };
287
+ }
288
+ // ================================><================================= //
289
+ // === buildVoucherDiscountUpdate === //
290
+ // ================================><================================= //
291
+ function buildVoucherDiscountUpdate({ code, orderDiscountDetails, }) {
292
+ return {
293
+ ...orderDiscountDetails,
294
+ voucher_reference_number: code.trim().toUpperCase(),
295
+ total_discount: 0,
296
+ };
297
+ }
@@ -285,6 +285,7 @@ export interface ISettingsForCloud {
285
285
  rider_pickup_food_image_mandatory: ITenant['rider_pickup_food_image_mandatory'];
286
286
  rider_dropoff_food_image_mandatory: ITenant['rider_dropoff_food_image_mandatory'];
287
287
  cash_in_hand_limit: ITenant['cash_in_hand_limit'];
288
+ menu_source: ITenant['menu_source'];
288
289
  }
289
290
  export declare function getSettingsForCloud(settings: ITenant): ISettingsForCloud;
290
291
  export declare function getTenantOnlineOrderingMetadataFromTenant(tenant: ITenant): ITenantOnlineOrderingMetadata;
@@ -350,6 +350,7 @@ function getSettingsForCloud(settings) {
350
350
  rider_pickup_food_image_mandatory: settings.rider_pickup_food_image_mandatory,
351
351
  rider_dropoff_food_image_mandatory: settings.rider_dropoff_food_image_mandatory,
352
352
  cash_in_hand_limit: settings.cash_in_hand_limit,
353
+ menu_source: settings.menu_source,
353
354
  };
354
355
  }
355
356
  function getTenantOnlineOrderingMetadataFromTenant(tenant) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.261",
3
+ "version": "0.2.263",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",