@managesome/eposmatic-toolkit 0.2.339 → 0.2.342-beta.0
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.
- package/lib/constants/ENUMS.d.ts +35 -0
- package/lib/constants/ENUMS.js +48 -3
- package/lib/helpers/store/common-purchase-order-actions.d.ts +2 -2
- package/lib/helpers/store/purchase-orders-cart-actions.d.ts +7 -6
- package/lib/helpers/utils/code-redemption-helpers.d.ts +84 -0
- package/lib/helpers/utils/code-redemption-helpers.js +459 -0
- package/lib/schemas/models/index.d.ts +3 -0
- package/lib/schemas/models/index.js +3 -0
- package/lib/schemas/models/purchase_order_receipts.d.ts +165 -0
- package/lib/schemas/models/purchase_order_receipts.js +151 -0
- package/lib/schemas/models/purchase_order_rfq.d.ts +1938 -0
- package/lib/schemas/models/purchase_order_rfq.js +309 -0
- package/lib/schemas/models/purchase_order_vendor_returns.d.ts +234 -0
- package/lib/schemas/models/purchase_order_vendor_returns.js +188 -0
- package/lib/schemas/models/purchase_orders.d.ts +3 -0
- package/lib/schemas/models/purchase_orders.js +10 -1
- package/lib/schemas/models/void_purchase_order_items.js +2 -1
- package/lib/schemas/models/warehouse.d.ts +7 -1
- package/lib/schemas/models/warehouse.js +9 -0
- package/lib/schemas/models/warehouse_items.d.ts +19 -0
- package/lib/schemas/models/warehouse_items.js +29 -0
- package/lib/schemas/models/warehouse_vendors.d.ts +3 -3
- package/lib/schemas/types/index.d.ts +4 -0
- package/lib/schemas/types/index.js +4 -0
- package/lib/schemas/types/purchase_order_common.d.ts +39 -0
- package/lib/schemas/types/purchase_order_common.js +2 -0
- package/lib/schemas/types/purchase_order_receipts.d.ts +33 -0
- package/lib/schemas/types/purchase_order_receipts.js +2 -0
- package/lib/schemas/types/purchase_order_rfq.d.ts +25 -0
- package/lib/schemas/types/purchase_order_rfq.js +2 -0
- package/lib/schemas/types/purchase_order_vendor_returns.d.ts +42 -0
- package/lib/schemas/types/purchase_order_vendor_returns.js +2 -0
- package/lib/schemas/types/purchase_orders.d.ts +7 -43
- package/lib/schemas/types/warehouse.d.ts +3 -1
- package/lib/schemas/types/warehouse_items.d.ts +7 -1
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -38,6 +38,9 @@ export * from './operational_roles';
|
|
|
38
38
|
export * from './order';
|
|
39
39
|
export * from './printer_settings';
|
|
40
40
|
export * from './public_holidays';
|
|
41
|
+
export * from './purchase_order_receipts';
|
|
42
|
+
export * from './purchase_order_rfq';
|
|
43
|
+
export * from './purchase_order_vendor_returns';
|
|
41
44
|
export * from './purchase_orders';
|
|
42
45
|
export * from './reservations';
|
|
43
46
|
export * from './reviews';
|
|
@@ -54,6 +54,9 @@ __exportStar(require("./operational_roles"), exports);
|
|
|
54
54
|
__exportStar(require("./order"), exports);
|
|
55
55
|
__exportStar(require("./printer_settings"), exports);
|
|
56
56
|
__exportStar(require("./public_holidays"), exports);
|
|
57
|
+
__exportStar(require("./purchase_order_receipts"), exports);
|
|
58
|
+
__exportStar(require("./purchase_order_rfq"), exports);
|
|
59
|
+
__exportStar(require("./purchase_order_vendor_returns"), exports);
|
|
57
60
|
__exportStar(require("./purchase_orders"), exports);
|
|
58
61
|
__exportStar(require("./reservations"), exports);
|
|
59
62
|
__exportStar(require("./reviews"), exports);
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import { PURCHASE_ORDER_RECEIPT_STATUS_ENUM } from '../../constants';
|
|
3
|
+
export declare const PurchaseOrderReceiptsSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
|
|
4
|
+
timestamps: true;
|
|
5
|
+
versionKey: false;
|
|
6
|
+
minimize: false;
|
|
7
|
+
}, {
|
|
8
|
+
createdAt: NativeDate;
|
|
9
|
+
updatedAt: NativeDate;
|
|
10
|
+
} & {
|
|
11
|
+
_id: import("mongoose").Types.ObjectId;
|
|
12
|
+
createdAt_mins: number;
|
|
13
|
+
createdAt: NativeDate;
|
|
14
|
+
updatedAt: NativeDate;
|
|
15
|
+
items: import("mongoose").Types.DocumentArray<{
|
|
16
|
+
_id: import("mongoose").Types.ObjectId;
|
|
17
|
+
name: string;
|
|
18
|
+
price: number;
|
|
19
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
20
|
+
subtotal: number;
|
|
21
|
+
ordered_quantity: number;
|
|
22
|
+
received_quantity: number;
|
|
23
|
+
}, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
|
|
24
|
+
_id: import("mongoose").Types.ObjectId;
|
|
25
|
+
name: string;
|
|
26
|
+
price: number;
|
|
27
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
28
|
+
subtotal: number;
|
|
29
|
+
ordered_quantity: number;
|
|
30
|
+
received_quantity: number;
|
|
31
|
+
}> & {
|
|
32
|
+
_id: import("mongoose").Types.ObjectId;
|
|
33
|
+
name: string;
|
|
34
|
+
price: number;
|
|
35
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
36
|
+
subtotal: number;
|
|
37
|
+
ordered_quantity: number;
|
|
38
|
+
received_quantity: number;
|
|
39
|
+
}>;
|
|
40
|
+
warehouse_id: string;
|
|
41
|
+
vendor_id: import("mongoose").Types.ObjectId;
|
|
42
|
+
vendor_name: string;
|
|
43
|
+
purchase_order_id: import("mongoose").Types.ObjectId;
|
|
44
|
+
receipt_status: PURCHASE_ORDER_RECEIPT_STATUS_ENUM;
|
|
45
|
+
total_received_amount: number;
|
|
46
|
+
cancel_reason?: string | null | undefined;
|
|
47
|
+
notes?: string | null | undefined;
|
|
48
|
+
created_by?: {
|
|
49
|
+
_id: string;
|
|
50
|
+
name: string;
|
|
51
|
+
} | null | undefined;
|
|
52
|
+
validated_by?: {
|
|
53
|
+
_id: string;
|
|
54
|
+
name: string;
|
|
55
|
+
} | null | undefined;
|
|
56
|
+
validated_at?: NativeDate | null | undefined;
|
|
57
|
+
cancelled_at?: NativeDate | null | undefined;
|
|
58
|
+
}, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
|
|
59
|
+
createdAt: NativeDate;
|
|
60
|
+
updatedAt: NativeDate;
|
|
61
|
+
} & {
|
|
62
|
+
_id: import("mongoose").Types.ObjectId;
|
|
63
|
+
createdAt_mins: number;
|
|
64
|
+
createdAt: NativeDate;
|
|
65
|
+
updatedAt: NativeDate;
|
|
66
|
+
items: import("mongoose").Types.DocumentArray<{
|
|
67
|
+
_id: import("mongoose").Types.ObjectId;
|
|
68
|
+
name: string;
|
|
69
|
+
price: number;
|
|
70
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
71
|
+
subtotal: number;
|
|
72
|
+
ordered_quantity: number;
|
|
73
|
+
received_quantity: number;
|
|
74
|
+
}, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
|
|
75
|
+
_id: import("mongoose").Types.ObjectId;
|
|
76
|
+
name: string;
|
|
77
|
+
price: number;
|
|
78
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
79
|
+
subtotal: number;
|
|
80
|
+
ordered_quantity: number;
|
|
81
|
+
received_quantity: number;
|
|
82
|
+
}> & {
|
|
83
|
+
_id: import("mongoose").Types.ObjectId;
|
|
84
|
+
name: string;
|
|
85
|
+
price: number;
|
|
86
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
87
|
+
subtotal: number;
|
|
88
|
+
ordered_quantity: number;
|
|
89
|
+
received_quantity: number;
|
|
90
|
+
}>;
|
|
91
|
+
warehouse_id: string;
|
|
92
|
+
vendor_id: import("mongoose").Types.ObjectId;
|
|
93
|
+
vendor_name: string;
|
|
94
|
+
purchase_order_id: import("mongoose").Types.ObjectId;
|
|
95
|
+
receipt_status: PURCHASE_ORDER_RECEIPT_STATUS_ENUM;
|
|
96
|
+
total_received_amount: number;
|
|
97
|
+
cancel_reason?: string | null | undefined;
|
|
98
|
+
notes?: string | null | undefined;
|
|
99
|
+
created_by?: {
|
|
100
|
+
_id: string;
|
|
101
|
+
name: string;
|
|
102
|
+
} | null | undefined;
|
|
103
|
+
validated_by?: {
|
|
104
|
+
_id: string;
|
|
105
|
+
name: string;
|
|
106
|
+
} | null | undefined;
|
|
107
|
+
validated_at?: NativeDate | null | undefined;
|
|
108
|
+
cancelled_at?: NativeDate | null | undefined;
|
|
109
|
+
}>> & import("mongoose").FlatRecord<{
|
|
110
|
+
createdAt: NativeDate;
|
|
111
|
+
updatedAt: NativeDate;
|
|
112
|
+
} & {
|
|
113
|
+
_id: import("mongoose").Types.ObjectId;
|
|
114
|
+
createdAt_mins: number;
|
|
115
|
+
createdAt: NativeDate;
|
|
116
|
+
updatedAt: NativeDate;
|
|
117
|
+
items: import("mongoose").Types.DocumentArray<{
|
|
118
|
+
_id: import("mongoose").Types.ObjectId;
|
|
119
|
+
name: string;
|
|
120
|
+
price: number;
|
|
121
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
122
|
+
subtotal: number;
|
|
123
|
+
ordered_quantity: number;
|
|
124
|
+
received_quantity: number;
|
|
125
|
+
}, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
|
|
126
|
+
_id: import("mongoose").Types.ObjectId;
|
|
127
|
+
name: string;
|
|
128
|
+
price: number;
|
|
129
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
130
|
+
subtotal: number;
|
|
131
|
+
ordered_quantity: number;
|
|
132
|
+
received_quantity: number;
|
|
133
|
+
}> & {
|
|
134
|
+
_id: import("mongoose").Types.ObjectId;
|
|
135
|
+
name: string;
|
|
136
|
+
price: number;
|
|
137
|
+
item_id: import("mongoose").Types.ObjectId;
|
|
138
|
+
subtotal: number;
|
|
139
|
+
ordered_quantity: number;
|
|
140
|
+
received_quantity: number;
|
|
141
|
+
}>;
|
|
142
|
+
warehouse_id: string;
|
|
143
|
+
vendor_id: import("mongoose").Types.ObjectId;
|
|
144
|
+
vendor_name: string;
|
|
145
|
+
purchase_order_id: import("mongoose").Types.ObjectId;
|
|
146
|
+
receipt_status: PURCHASE_ORDER_RECEIPT_STATUS_ENUM;
|
|
147
|
+
total_received_amount: number;
|
|
148
|
+
cancel_reason?: string | null | undefined;
|
|
149
|
+
notes?: string | null | undefined;
|
|
150
|
+
created_by?: {
|
|
151
|
+
_id: string;
|
|
152
|
+
name: string;
|
|
153
|
+
} | null | undefined;
|
|
154
|
+
validated_by?: {
|
|
155
|
+
_id: string;
|
|
156
|
+
name: string;
|
|
157
|
+
} | null | undefined;
|
|
158
|
+
validated_at?: NativeDate | null | undefined;
|
|
159
|
+
cancelled_at?: NativeDate | null | undefined;
|
|
160
|
+
}> & Required<{
|
|
161
|
+
_id: import("mongoose").Types.ObjectId;
|
|
162
|
+
}> & {
|
|
163
|
+
__v: number;
|
|
164
|
+
}>;
|
|
165
|
+
export declare const PURCHASE_ORDER_RECEIPTS_COLLECTION_NAME = "purchase_order_receipts";
|