@managesome/eposmatic-toolkit 0.2.338 → 0.2.341

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.
@@ -279,3 +279,8 @@ export interface IWorker_UpdateCustomerRedemptionsOnWalletAssignment {
279
279
  code_quantities: Record<string, number>;
280
280
  updated_phone_numbers: string[];
281
281
  }
282
+ export interface IWorker_CleanupRewardsAndAssetsOnSettleOrder {
283
+ order: IOrder;
284
+ brand_id: string;
285
+ phoneNumber: string;
286
+ }
@@ -0,0 +1,84 @@
1
+ import { GIFT_CARD_REWARD_TYPE_ENUM, MYSTERY_GIFT_TYPE_ENUM } from '../../constants';
2
+ import { IDiscountDetails, IGiftCard, IOffer, IOrder, IVoucher } from '../../schemas';
3
+ import { ICustomerRedemptionCount } from '../types';
4
+ type TCodeContextType = 'voucher' | 'gift-card' | null;
5
+ export interface IWalletGiftCardUsageGate {
6
+ blocked: boolean;
7
+ usedCount: number;
8
+ maxUsesPerCustomer: number | null;
9
+ message: string | null;
10
+ }
11
+ export declare function getDirectWalletGiftCardUsageGate({ giftCard, customerOfferRedemptionCounts, }: {
12
+ giftCard: IGiftCard;
13
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
14
+ }): IWalletGiftCardUsageGate;
15
+ export interface IGiftCardRewardVoucherAssignmentGate {
16
+ blocked: boolean;
17
+ message: string | null;
18
+ }
19
+ export declare function getGiftCardRewardVoucherAssignmentGate({ giftCard, vouchers, customerOfferRedemptionCounts, mysteryGiftType, currentCartAssignmentCount, claimsIncrement, }: {
20
+ giftCard: IGiftCard;
21
+ vouchers: IVoucher[];
22
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
23
+ mysteryGiftType?: GIFT_CARD_REWARD_TYPE_ENUM | MYSTERY_GIFT_TYPE_ENUM | null;
24
+ currentCartAssignmentCount?: number;
25
+ claimsIncrement?: number;
26
+ }): IGiftCardRewardVoucherAssignmentGate;
27
+ export interface IValidatedWalletGiftCardRedemptionClaimResult {
28
+ success: boolean;
29
+ walletPatch: Partial<Pick<IOrder, 'wallet_redemptions'>>;
30
+ errorMessage: string | null;
31
+ }
32
+ export declare function buildValidatedWalletGiftCardRedemptionClaimPatch({ currentWalletRedemptions, giftCard, vouchers, customerOfferRedemptionCounts, claimsIncrement, mysteryGiftType, }: {
33
+ currentWalletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
34
+ giftCard: IGiftCard;
35
+ vouchers: IVoucher[];
36
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
37
+ claimsIncrement?: number;
38
+ mysteryGiftType?: GIFT_CARD_REWARD_TYPE_ENUM | null;
39
+ }): IValidatedWalletGiftCardRedemptionClaimResult;
40
+ export declare function getRedeemedVoucherPathwayOfferIds(args: {
41
+ offers: IOffer[];
42
+ redeemedOffers: IOrder['redeemed_offers'] | null | undefined;
43
+ }): string[];
44
+ export interface IWalletGiftCardRedemptionStats {
45
+ gift_card_id: string;
46
+ code: string;
47
+ owned_count: number;
48
+ spendable_count: number;
49
+ used_count: number;
50
+ max_uses_per_customer: number | null;
51
+ current_cart_claim_count: number;
52
+ next_claim_increment: number;
53
+ is_usage_exhausted: boolean;
54
+ claimable: boolean;
55
+ claim_block_message: string | null;
56
+ is_applied: boolean;
57
+ }
58
+ export interface IWalletGiftCardClaimRequest {
59
+ giftCardId: string | null | undefined;
60
+ claimsIncrement?: number;
61
+ mysteryGiftType?: GIFT_CARD_REWARD_TYPE_ENUM | MYSTERY_GIFT_TYPE_ENUM | null;
62
+ }
63
+ export interface IWalletRedemptionsSourceOfTruth {
64
+ activeCode: string | null;
65
+ activeCodeType: TCodeContextType;
66
+ activeVoucherCode: string | null;
67
+ activeGiftCardCode: string | null;
68
+ activeGiftCardId: string | null;
69
+ activeVoucherOfferIds: string[];
70
+ appliedGiftCardIds: string[];
71
+ giftCardStatsById: Record<string, IWalletGiftCardRedemptionStats>;
72
+ }
73
+ export declare function getWalletRedemptionsSourceOfTruth({ offers, vouchers, giftCards, redeemedOffers, walletRedemptions, orderDiscountDetails, customerGiftCardCodes, customerOfferRedemptionCounts, pendingGiftCardClaim, }: {
74
+ offers: IOffer[];
75
+ vouchers: IVoucher[];
76
+ giftCards: IGiftCard[];
77
+ redeemedOffers: IOrder['redeemed_offers'] | null | undefined;
78
+ walletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
79
+ orderDiscountDetails: IDiscountDetails | null | undefined;
80
+ customerGiftCardCodes: Record<string, number> | null | undefined;
81
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
82
+ pendingGiftCardClaim?: IWalletGiftCardClaimRequest | null;
83
+ }): IWalletRedemptionsSourceOfTruth;
84
+ export {};
@@ -0,0 +1,459 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDirectWalletGiftCardUsageGate = getDirectWalletGiftCardUsageGate;
4
+ exports.getGiftCardRewardVoucherAssignmentGate = getGiftCardRewardVoucherAssignmentGate;
5
+ exports.buildValidatedWalletGiftCardRedemptionClaimPatch = buildValidatedWalletGiftCardRedemptionClaimPatch;
6
+ exports.getRedeemedVoucherPathwayOfferIds = getRedeemedVoucherPathwayOfferIds;
7
+ exports.getWalletRedemptionsSourceOfTruth = getWalletRedemptionsSourceOfTruth;
8
+ const constants_1 = require("../../constants");
9
+ const offers_helpers_1 = require("./offers-helpers");
10
+ const wallet_redemption_helpers_1 = require("./wallet-redemption-helpers");
11
+ function normalizeCode(code) {
12
+ if (code == null) {
13
+ return null;
14
+ }
15
+ const normalizedCode = code.trim().toUpperCase();
16
+ return normalizedCode.length > 0 ? normalizedCode : null;
17
+ }
18
+ function toPositiveInteger(value, fallback = 0) {
19
+ const parsedValue = Number(value);
20
+ if (!Number.isFinite(parsedValue) || parsedValue <= 0) {
21
+ return fallback;
22
+ }
23
+ return Math.floor(parsedValue);
24
+ }
25
+ function getCustomerRedemptionCount(customerOfferRedemptionCounts, type, id) {
26
+ const normalizedId = id?.trim();
27
+ if (normalizedId == null || normalizedId.length === 0 || customerOfferRedemptionCounts == null) {
28
+ return 0;
29
+ }
30
+ const typedCount = customerOfferRedemptionCounts[type]?.[normalizedId];
31
+ if (typedCount != null) {
32
+ return toPositiveInteger(typedCount, 0);
33
+ }
34
+ const legacyFlatCounts = customerOfferRedemptionCounts;
35
+ return toPositiveInteger(legacyFlatCounts[normalizedId], 0);
36
+ }
37
+ function getCustomerUsedCount(customerOfferRedemptionCounts, id) {
38
+ return getCustomerRedemptionCount(customerOfferRedemptionCounts, constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.USED, id);
39
+ }
40
+ function getCustomerAssignedCount(customerOfferRedemptionCounts, id) {
41
+ return getCustomerRedemptionCount(customerOfferRedemptionCounts, constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.ASSIGNED, id);
42
+ }
43
+ function getRewardMaxAssignmentsPerCustomer(rewardResource) {
44
+ return rewardResource.max_assignments_per_customer ?? rewardResource.max_uses_per_customer ?? null;
45
+ }
46
+ function isResourceExpired(expireAt) {
47
+ if (expireAt == null) {
48
+ return false;
49
+ }
50
+ const expiryTime = new Date(expireAt).getTime();
51
+ return Number.isFinite(expiryTime) && expiryTime <= Date.now();
52
+ }
53
+ function getDirectWalletGiftCardUsageGate({ giftCard, customerOfferRedemptionCounts, }) {
54
+ if (!giftCard.active || isResourceExpired(giftCard.expire_at)) {
55
+ return {
56
+ blocked: true,
57
+ usedCount: 0,
58
+ maxUsesPerCustomer: giftCard.max_uses_per_customer ?? null,
59
+ message: 'This gift card reward is not available right now.',
60
+ };
61
+ }
62
+ if (giftCard.max_uses != null && giftCard.used_count >= giftCard.max_uses) {
63
+ return {
64
+ blocked: true,
65
+ usedCount: 0,
66
+ maxUsesPerCustomer: giftCard.max_uses_per_customer ?? null,
67
+ message: 'This gift card has been fully used.',
68
+ };
69
+ }
70
+ const maxUsesPerCustomer = giftCard.max_uses_per_customer ?? null;
71
+ if (maxUsesPerCustomer == null) {
72
+ return {
73
+ blocked: false,
74
+ usedCount: getCustomerUsedCount(customerOfferRedemptionCounts, giftCard._id),
75
+ maxUsesPerCustomer: null,
76
+ message: null,
77
+ };
78
+ }
79
+ if (customerOfferRedemptionCounts == null) {
80
+ return {
81
+ blocked: true,
82
+ usedCount: 0,
83
+ maxUsesPerCustomer,
84
+ message: 'Unable to verify your gift card usage limit right now.',
85
+ };
86
+ }
87
+ const usedCount = getCustomerUsedCount(customerOfferRedemptionCounts, giftCard._id);
88
+ if (usedCount >= maxUsesPerCustomer) {
89
+ return {
90
+ blocked: true,
91
+ usedCount,
92
+ maxUsesPerCustomer,
93
+ message: 'You have reached your personal limit for this gift card.',
94
+ };
95
+ }
96
+ return {
97
+ blocked: false,
98
+ usedCount,
99
+ maxUsesPerCustomer,
100
+ message: null,
101
+ };
102
+ }
103
+ function getVoucherRewardIdsFromGiftCard(giftCard, mysteryGiftType) {
104
+ if (giftCard.reward_type === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.VOUCHER) {
105
+ return Object.keys(giftCard.reward_voucher?.voucher_ids ?? {});
106
+ }
107
+ if (giftCard.reward_type === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.MYSTERY_GIFT && giftCard.reward_mystery_gift != null) {
108
+ const normalizedMysteryGiftType = mysteryGiftType?.toString() ?? null;
109
+ const shouldUseVoucherReward = normalizedMysteryGiftType === constants_1.MYSTERY_GIFT_TYPE_ENUM.VOUCHER ||
110
+ giftCard.reward_mystery_gift.choice_of_rewards.includes(constants_1.MYSTERY_GIFT_TYPE_ENUM.VOUCHER);
111
+ if (!shouldUseVoucherReward) {
112
+ return [];
113
+ }
114
+ return Object.keys(giftCard.reward_mystery_gift.reward_voucher?.voucher_ids ?? {});
115
+ }
116
+ return [];
117
+ }
118
+ function getGiftCardRewardVoucherAssignmentGate({ giftCard, vouchers, customerOfferRedemptionCounts, mysteryGiftType, currentCartAssignmentCount = 0, claimsIncrement = 0, }) {
119
+ const voucherIds = getVoucherRewardIdsFromGiftCard(giftCard, mysteryGiftType);
120
+ if (voucherIds.length === 0) {
121
+ return {
122
+ blocked: false,
123
+ message: null,
124
+ };
125
+ }
126
+ for (let i = 0; i < voucherIds.length; i++) {
127
+ const voucherId = voucherIds[i];
128
+ const voucher = vouchers.find((voucherItem) => voucherItem._id === voucherId);
129
+ if (voucher == null || !voucher.active || isResourceExpired(voucher.expire_at)) {
130
+ return {
131
+ blocked: true,
132
+ message: 'This voucher reward is not available right now.',
133
+ };
134
+ }
135
+ if (voucher.max_assignments != null &&
136
+ voucher.assigned_count +
137
+ toPositiveInteger(currentCartAssignmentCount, 0) +
138
+ toPositiveInteger(claimsIncrement, 0) >
139
+ voucher.max_assignments) {
140
+ return {
141
+ blocked: true,
142
+ message: 'This voucher reward is no longer available.',
143
+ };
144
+ }
145
+ const maxAssignmentsPerCustomer = getRewardMaxAssignmentsPerCustomer(voucher);
146
+ if (maxAssignmentsPerCustomer == null) {
147
+ continue;
148
+ }
149
+ if (customerOfferRedemptionCounts == null) {
150
+ return {
151
+ blocked: true,
152
+ message: 'Unable to verify your personal limit for this reward right now.',
153
+ };
154
+ }
155
+ if (getCustomerAssignedCount(customerOfferRedemptionCounts, voucher._id) +
156
+ toPositiveInteger(currentCartAssignmentCount, 0) +
157
+ toPositiveInteger(claimsIncrement, 0) >
158
+ maxAssignmentsPerCustomer) {
159
+ return {
160
+ blocked: true,
161
+ message: 'You have reached your personal limit for this reward.',
162
+ };
163
+ }
164
+ }
165
+ return {
166
+ blocked: false,
167
+ message: null,
168
+ };
169
+ }
170
+ function getWalletGiftCardClaimGate({ currentWalletRedemptions, giftCard, vouchers, customerOfferRedemptionCounts, claimsIncrement = 1, mysteryGiftType, }) {
171
+ const safeClaimsIncrement = toPositiveInteger(claimsIncrement, 1);
172
+ const currentGiftCardClaimCount = (0, wallet_redemption_helpers_1.getWalletGiftCardRedemptionClaims)(currentWalletRedemptions?.gift_cards)
173
+ .filter((claim) => claim.gift_card_id === giftCard._id)
174
+ .reduce((total, claim) => total + claim.redeemed_times, 0);
175
+ const directUsageGate = getDirectWalletGiftCardUsageGate({
176
+ giftCard,
177
+ customerOfferRedemptionCounts,
178
+ });
179
+ if (directUsageGate.blocked) {
180
+ return {
181
+ blocked: true,
182
+ message: directUsageGate.message,
183
+ usedCount: directUsageGate.usedCount,
184
+ maxUsesPerCustomer: directUsageGate.maxUsesPerCustomer,
185
+ currentCartClaimCount: currentGiftCardClaimCount,
186
+ claimsIncrement: safeClaimsIncrement,
187
+ };
188
+ }
189
+ if (directUsageGate.maxUsesPerCustomer != null &&
190
+ directUsageGate.usedCount + currentGiftCardClaimCount + safeClaimsIncrement > directUsageGate.maxUsesPerCustomer) {
191
+ return {
192
+ blocked: true,
193
+ message: 'You have reached your personal limit for this gift card.',
194
+ usedCount: directUsageGate.usedCount,
195
+ maxUsesPerCustomer: directUsageGate.maxUsesPerCustomer,
196
+ currentCartClaimCount: currentGiftCardClaimCount,
197
+ claimsIncrement: safeClaimsIncrement,
198
+ };
199
+ }
200
+ const voucherRewardAssignmentGate = getGiftCardRewardVoucherAssignmentGate({
201
+ giftCard,
202
+ vouchers,
203
+ customerOfferRedemptionCounts,
204
+ mysteryGiftType,
205
+ currentCartAssignmentCount: currentGiftCardClaimCount,
206
+ claimsIncrement: safeClaimsIncrement,
207
+ });
208
+ if (voucherRewardAssignmentGate.blocked) {
209
+ return {
210
+ blocked: true,
211
+ message: voucherRewardAssignmentGate.message,
212
+ usedCount: directUsageGate.usedCount,
213
+ maxUsesPerCustomer: directUsageGate.maxUsesPerCustomer,
214
+ currentCartClaimCount: currentGiftCardClaimCount,
215
+ claimsIncrement: safeClaimsIncrement,
216
+ };
217
+ }
218
+ return {
219
+ blocked: false,
220
+ message: null,
221
+ usedCount: directUsageGate.usedCount,
222
+ maxUsesPerCustomer: directUsageGate.maxUsesPerCustomer,
223
+ currentCartClaimCount: currentGiftCardClaimCount,
224
+ claimsIncrement: safeClaimsIncrement,
225
+ };
226
+ }
227
+ function buildValidatedWalletGiftCardRedemptionClaimPatch({ currentWalletRedemptions, giftCard, vouchers, customerOfferRedemptionCounts, claimsIncrement = 1, mysteryGiftType, }) {
228
+ const safeClaimsIncrement = toPositiveInteger(claimsIncrement, 1);
229
+ const sourceOfTruth = getWalletRedemptionsSourceOfTruth({
230
+ offers: [],
231
+ vouchers,
232
+ giftCards: [giftCard],
233
+ redeemedOffers: null,
234
+ walletRedemptions: currentWalletRedemptions,
235
+ orderDiscountDetails: null,
236
+ customerGiftCardCodes: null,
237
+ customerOfferRedemptionCounts,
238
+ pendingGiftCardClaim: {
239
+ giftCardId: giftCard._id,
240
+ claimsIncrement: safeClaimsIncrement,
241
+ mysteryGiftType,
242
+ },
243
+ });
244
+ const giftCardStats = sourceOfTruth.giftCardStatsById[giftCard._id];
245
+ if (giftCardStats == null || !giftCardStats.claimable) {
246
+ return {
247
+ success: false,
248
+ walletPatch: {},
249
+ errorMessage: giftCardStats?.claim_block_message ?? 'You cannot apply this gift card right now.',
250
+ };
251
+ }
252
+ return {
253
+ success: true,
254
+ walletPatch: (0, offers_helpers_1.buildWalletGiftCardRedemptionClaimPatch)({
255
+ currentWalletRedemptions,
256
+ giftCardId: giftCard._id,
257
+ claimsIncrement: safeClaimsIncrement,
258
+ mysteryGiftType,
259
+ }),
260
+ errorMessage: null,
261
+ };
262
+ }
263
+ function getRedeemedVoucherPathwayOfferIdsInternal({ offers, redeemedOffers, }) {
264
+ if (redeemedOffers == null) {
265
+ return [];
266
+ }
267
+ const offerById = new Map();
268
+ for (let i = 0; i < offers.length; i++) {
269
+ offerById.set(offers[i]._id, offers[i]);
270
+ }
271
+ const offerIds = Object.keys(redeemedOffers);
272
+ const voucherPathwayOfferIds = [];
273
+ for (let i = 0; i < offerIds.length; i++) {
274
+ const offerId = offerIds[i];
275
+ const redeemedTimes = toPositiveInteger(redeemedOffers[offerId], 0);
276
+ if (redeemedTimes <= 0) {
277
+ continue;
278
+ }
279
+ const offer = offerById.get(offerId);
280
+ if (offer == null || offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
281
+ continue;
282
+ }
283
+ if (offer.pathway_to_unlock_type_vouchers == null) {
284
+ continue;
285
+ }
286
+ voucherPathwayOfferIds.push(offerId);
287
+ }
288
+ return voucherPathwayOfferIds;
289
+ }
290
+ function getRedeemedVoucherPathwayOfferIds(args) {
291
+ return getRedeemedVoucherPathwayOfferIdsInternal(args);
292
+ }
293
+ function getVoucherCodesForOffer(offer, vouchersById) {
294
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ||
295
+ offer.pathway_to_unlock_type_vouchers == null) {
296
+ return [];
297
+ }
298
+ const voucherCodes = [];
299
+ const voucherIds = offer.pathway_to_unlock_type_vouchers.voucher_ids;
300
+ for (let i = 0; i < voucherIds.length; i++) {
301
+ const voucher = vouchersById.get(voucherIds[i]);
302
+ if (voucher == null) {
303
+ continue;
304
+ }
305
+ const normalizedCode = normalizeCode(voucher.code);
306
+ if (normalizedCode == null) {
307
+ continue;
308
+ }
309
+ voucherCodes.push(normalizedCode);
310
+ }
311
+ return voucherCodes;
312
+ }
313
+ function getWalletRedemptionsSourceOfTruth({ offers, vouchers, giftCards, redeemedOffers, walletRedemptions, orderDiscountDetails, customerGiftCardCodes, customerOfferRedemptionCounts, pendingGiftCardClaim, }) {
314
+ const activeVoucherOfferIds = getRedeemedVoucherPathwayOfferIdsInternal({
315
+ offers,
316
+ redeemedOffers,
317
+ });
318
+ const offerById = new Map();
319
+ const vouchersById = new Map();
320
+ const giftCardsById = new Map();
321
+ for (let i = 0; i < offers.length; i++) {
322
+ offerById.set(offers[i]._id, offers[i]);
323
+ }
324
+ for (let i = 0; i < vouchers.length; i++) {
325
+ vouchersById.set(vouchers[i]._id, vouchers[i]);
326
+ }
327
+ for (let i = 0; i < giftCards.length; i++) {
328
+ giftCardsById.set(giftCards[i]._id, giftCards[i]);
329
+ }
330
+ const voucherCodesInUse = new Set();
331
+ for (let i = 0; i < activeVoucherOfferIds.length; i++) {
332
+ const offer = offerById.get(activeVoucherOfferIds[i]);
333
+ if (offer == null) {
334
+ continue;
335
+ }
336
+ const voucherCodes = getVoucherCodesForOffer(offer, vouchersById);
337
+ for (let j = 0; j < voucherCodes.length; j++) {
338
+ voucherCodesInUse.add(voucherCodes[j]);
339
+ }
340
+ }
341
+ const normalizedVoucherReferenceCode = normalizeCode(orderDiscountDetails?.voucher_reference_number);
342
+ const activeVoucherCode = voucherCodesInUse.size === 0
343
+ ? null
344
+ : normalizedVoucherReferenceCode != null && voucherCodesInUse.has(normalizedVoucherReferenceCode)
345
+ ? normalizedVoucherReferenceCode
346
+ : (Array.from(voucherCodesInUse.values()).sort((a, b) => a.localeCompare(b))[0] ?? null);
347
+ const normalizedWalletGiftCardClaims = (0, wallet_redemption_helpers_1.getWalletGiftCardRedemptionClaims)(walletRedemptions?.gift_cards);
348
+ const appliedGiftCardIds = normalizedWalletGiftCardClaims.map((walletGiftCardClaim) => walletGiftCardClaim.gift_card_id);
349
+ const normalizedLoyaltyDiscountId = orderDiscountDetails?.loyalty_discount_id?.trim() ?? null;
350
+ const isWalletCashbackLoyaltyDiscount = normalizedLoyaltyDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
351
+ let activeGiftCardId = null;
352
+ if (appliedGiftCardIds.length === 1) {
353
+ activeGiftCardId = appliedGiftCardIds[0];
354
+ }
355
+ else if (normalizedLoyaltyDiscountId != null &&
356
+ !isWalletCashbackLoyaltyDiscount &&
357
+ appliedGiftCardIds.includes(normalizedLoyaltyDiscountId)) {
358
+ activeGiftCardId = normalizedLoyaltyDiscountId;
359
+ }
360
+ else if (appliedGiftCardIds.length > 0) {
361
+ activeGiftCardId = appliedGiftCardIds[0];
362
+ }
363
+ else if (normalizedLoyaltyDiscountId != null &&
364
+ !isWalletCashbackLoyaltyDiscount &&
365
+ giftCardsById.has(normalizedLoyaltyDiscountId)) {
366
+ activeGiftCardId = normalizedLoyaltyDiscountId;
367
+ }
368
+ const activeGiftCardCode = activeGiftCardId == null ? null : normalizeCode(giftCardsById.get(activeGiftCardId)?.code);
369
+ const spendableGiftCardCodes = (0, offers_helpers_1.calculateCustomerSpendableGiftCardCodes)({
370
+ customerGiftCardCodes,
371
+ loyaltyReferenceNumber: orderDiscountDetails?.loyalty_reference_number,
372
+ loyaltyDiscountId: orderDiscountDetails?.loyalty_discount_id,
373
+ walletGiftCardRedemptions: walletRedemptions?.gift_cards,
374
+ giftCards,
375
+ }).giftCardCodes;
376
+ const normalizedCustomerGiftCardCodes = {};
377
+ if (customerGiftCardCodes != null) {
378
+ const entries = Object.entries(customerGiftCardCodes);
379
+ for (let i = 0; i < entries.length; i++) {
380
+ const [code, count] = entries[i];
381
+ const normalizedCode = normalizeCode(code);
382
+ if (normalizedCode == null) {
383
+ continue;
384
+ }
385
+ const parsedCount = toPositiveInteger(count, 0);
386
+ if (parsedCount <= 0) {
387
+ continue;
388
+ }
389
+ normalizedCustomerGiftCardCodes[normalizedCode] = parsedCount;
390
+ }
391
+ }
392
+ const appliedGiftCardIdSet = new Set(appliedGiftCardIds);
393
+ const giftCardStatsById = {};
394
+ const pendingGiftCardClaimId = pendingGiftCardClaim?.giftCardId?.toString().trim();
395
+ for (let i = 0; i < giftCards.length; i++) {
396
+ const giftCard = giftCards[i];
397
+ const normalizedGiftCardCode = normalizeCode(giftCard.code);
398
+ if (normalizedGiftCardCode == null) {
399
+ continue;
400
+ }
401
+ const isPendingGiftCardClaim = pendingGiftCardClaimId === giftCard._id;
402
+ const claimGate = getWalletGiftCardClaimGate({
403
+ currentWalletRedemptions: walletRedemptions,
404
+ giftCard,
405
+ vouchers,
406
+ customerOfferRedemptionCounts,
407
+ claimsIncrement: isPendingGiftCardClaim ? pendingGiftCardClaim?.claimsIncrement : 1,
408
+ mysteryGiftType: isPendingGiftCardClaim ? pendingGiftCardClaim?.mysteryGiftType : giftCard.reward_type,
409
+ });
410
+ giftCardStatsById[giftCard._id] = {
411
+ gift_card_id: giftCard._id,
412
+ code: normalizedGiftCardCode,
413
+ owned_count: normalizedCustomerGiftCardCodes[normalizedGiftCardCode] ?? 0,
414
+ spendable_count: spendableGiftCardCodes[normalizedGiftCardCode] ?? 0,
415
+ used_count: claimGate.usedCount,
416
+ max_uses_per_customer: claimGate.maxUsesPerCustomer,
417
+ current_cart_claim_count: claimGate.currentCartClaimCount,
418
+ next_claim_increment: claimGate.claimsIncrement,
419
+ is_usage_exhausted: claimGate.blocked,
420
+ claimable: !claimGate.blocked,
421
+ claim_block_message: claimGate.message,
422
+ is_applied: appliedGiftCardIdSet.has(giftCard._id) ||
423
+ (!isWalletCashbackLoyaltyDiscount && normalizedLoyaltyDiscountId === giftCard._id),
424
+ };
425
+ }
426
+ let activeCodeType = null;
427
+ if (activeVoucherCode != null && activeGiftCardCode != null) {
428
+ if (normalizedVoucherReferenceCode === activeVoucherCode) {
429
+ activeCodeType = 'voucher';
430
+ }
431
+ else if (!isWalletCashbackLoyaltyDiscount &&
432
+ normalizeCode(orderDiscountDetails?.loyalty_reference_number) === activeGiftCardCode) {
433
+ activeCodeType = 'gift-card';
434
+ }
435
+ else if (activeVoucherOfferIds.length > 0) {
436
+ activeCodeType = 'voucher';
437
+ }
438
+ else {
439
+ activeCodeType = 'gift-card';
440
+ }
441
+ }
442
+ else if (activeVoucherCode != null) {
443
+ activeCodeType = 'voucher';
444
+ }
445
+ else if (activeGiftCardCode != null) {
446
+ activeCodeType = 'gift-card';
447
+ }
448
+ const activeCode = activeCodeType === 'voucher' ? activeVoucherCode : activeCodeType === 'gift-card' ? activeGiftCardCode : null;
449
+ return {
450
+ activeCode,
451
+ activeCodeType,
452
+ activeVoucherCode,
453
+ activeGiftCardCode,
454
+ activeGiftCardId,
455
+ activeVoucherOfferIds,
456
+ appliedGiftCardIds,
457
+ giftCardStatsById,
458
+ };
459
+ }
@@ -1,6 +1,7 @@
1
1
  export * from './cart-helpers';
2
2
  export * from './cash_in_hand_transactions';
3
3
  export * from './catalog-conversion';
4
+ export * from './code-redemption-helpers';
4
5
  export * from './color-helpers';
5
6
  export * from './common';
6
7
  export * from './crypt';
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./cart-helpers"), exports);
18
18
  __exportStar(require("./cash_in_hand_transactions"), exports);
19
19
  __exportStar(require("./catalog-conversion"), exports);
20
+ __exportStar(require("./code-redemption-helpers"), exports);
20
21
  __exportStar(require("./color-helpers"), exports);
21
22
  __exportStar(require("./common"), exports);
22
23
  __exportStar(require("./crypt"), exports);
@@ -34,5 +34,67 @@ export declare function buildVoucherDiscountUpdate({ code, orderDiscountDetails,
34
34
  orderDiscountDetails: IDiscountDetails | null | undefined;
35
35
  }): IDiscountDetails;
36
36
  export declare function getSyncOffersWithCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, unlockedOffersApplicableTimes: Record<string, number>, // Record<offer_id, times can be applied>
37
- offers: IOffer[]): Partial<IOrder>;
37
+ offers: IOffer[]): {
38
+ order_discount_details: IDiscountDetails | null | undefined;
39
+ redeemed_offers: Record<string, number> | null | undefined;
40
+ wallet_redemptions: import("../../schemas").IWalletRedemptions | null | undefined;
41
+ _id?: string | undefined;
42
+ tenant_id?: string | undefined;
43
+ consumer?: import("../../schemas").IConsumer | undefined;
44
+ platform?: import("../../constants").PLATFORMS_ENUM | undefined;
45
+ tip_amount?: number | undefined;
46
+ categories?: IOrderCategory[] | undefined;
47
+ iterations?: import("../../schemas").IOrderIteration[] | undefined;
48
+ special_instructions?: string | undefined;
49
+ payments?: import("../../schemas").IOrderPayment[] | undefined;
50
+ split_parent_id?: string | null | undefined;
51
+ split_child_ids?: string[] | null | undefined;
52
+ split_type?: (import("../../constants").SPLIT_BILL_TYPE_ENUM | null) | undefined;
53
+ order_status?: import("../../constants").ORDER_STATUS_ENUM | undefined;
54
+ tax_number?: string | undefined;
55
+ tax_amount?: number | undefined;
56
+ price?: number | undefined;
57
+ amount_collected?: number | undefined;
58
+ amount_earned?: number | undefined;
59
+ grand_total?: number | undefined;
60
+ order_number?: number | null | undefined;
61
+ delete_reason?: string | null | undefined;
62
+ cancel_reason?: string | null | undefined;
63
+ tax_invoice_id?: string | null | undefined;
64
+ is_deleted?: boolean | undefined;
65
+ order_status_updated_at?: Date | undefined;
66
+ rating?: number | null | undefined;
67
+ review?: string | null | undefined;
68
+ is_reopened?: boolean | undefined;
69
+ deferred_settlement?: boolean | undefined;
70
+ payment_method_expected?: import("../../constants").PAYMENT_METHOD_EXPECTED_ENUM | undefined;
71
+ order_taker_details?: (import("../../schemas").IStaffDetails | null) | undefined;
72
+ order_settled_at?: Date | undefined;
73
+ order_settled_at_mins?: number | undefined;
74
+ createdAt_mins?: number | undefined;
75
+ createdAt?: Date | undefined;
76
+ updatedAt?: Date | undefined;
77
+ delivery_details?: (import("../../schemas").IOrderDeliveryDetails | null) | undefined;
78
+ dinein_details?: (import("../../schemas").IOrderDineInDetails | null) | undefined;
79
+ takeaway_details?: (import("../../schemas").IOrderTakeawayDetails | null) | undefined;
80
+ staff_details?: (import("../../schemas").IStaffDetails | null) | undefined;
81
+ total_discount_applied?: number | undefined;
82
+ is_surcharge_percentage?: boolean | undefined;
83
+ is_surcharge_taxable?: boolean | undefined;
84
+ is_service_fee_percentage?: boolean | undefined;
85
+ is_service_fee_taxable?: boolean | undefined;
86
+ exclude_discount_from_gst?: boolean | undefined;
87
+ apply_service_fee_on_original_price?: boolean | undefined;
88
+ is_gst_inclusive?: boolean | undefined;
89
+ surcharge?: number | undefined;
90
+ surcharge_amount?: number | undefined;
91
+ service_fee?: number | undefined;
92
+ service_fee_amount?: number | undefined;
93
+ gst?: number | undefined;
94
+ gst_amount?: number | undefined;
95
+ packaging_charges?: number | undefined;
96
+ adjustment_amount?: number | null | undefined;
97
+ adjustment_reason?: string | null | undefined;
98
+ tax_invoice_provider_charges?: number | undefined;
99
+ };
38
100
  export {};