sahab-utils 2.1.8 → 2.1.9

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.
@@ -19,12 +19,16 @@ export declare function isPackageVisit(order?: Order): boolean;
19
19
  /** What the minimum is compared against: what the customer paid. A visit holds only a slice of the
20
20
  * package, so it uses the package price — otherwise later visits of a qualifying package would not stamp. */
21
21
  export declare function loyaltyQualifyingAmount(order: Order): number;
22
+ /** Why a finished order does not earn a stamp; the one place the reasons are decided, so the rule and the verdict kept on the order cannot disagree. */
23
+ export type LoyaltyRefusal = 'no_offer' | 'not_settled' | 'unpaid' | 'other_provider' | 'gift_redemption' | 'loyalty_redemption' | 'below_minimum';
24
+ /** The first reason `order` earns no stamp for `offer`, or undefined when it does. */
25
+ export declare function loyaltyStampRefusal(order: Order | undefined, offer: Discount | undefined): LoyaltyRefusal | undefined;
22
26
  /** Whether a finished order earns a stamp. For a multi-visit package this is asked per visit: every
23
27
  * completed visit earns one, not the package once. A redemption order earns none. */
24
28
  export declare function orderEarnsLoyaltyStamp(order: Order | undefined, offer: Discount | undefined): boolean;
25
29
  export type LoyaltyCheckResult = 'stamped' | 'not_eligible' | 'already_counted';
26
30
  /** One offer's verdict on a completed order, kept on `order.loyaltyChecks`. The offer can be edited later, so its
27
- * terms are copied; the parts with no Firestore dependency — the server adds `checkedAt`. */
31
+ * terms are copied. This part has no Firestore dependency; the server adds `checkedAt`. */
28
32
  export type LoyaltyCheckFields = {
29
33
  promoCodeId: string;
30
34
  buyQuantity: number;
@@ -32,6 +36,8 @@ export type LoyaltyCheckFields = {
32
36
  /** What the minimum was compared against (`loyaltyQualifyingAmount`). */
33
37
  qualifyingAmount: number;
34
38
  result: LoyaltyCheckResult;
39
+ /** Why it was not eligible; absent for the other results. */
40
+ reason?: LoyaltyRefusal;
35
41
  };
36
42
  /** The verdict to record for `order` against `offer`, with the terms in force right now. */
37
43
  export declare function loyaltyCheckFields(order: Order, offer: Discount, result: LoyaltyCheckResult): LoyaltyCheckFields;
@@ -8,6 +8,7 @@ exports.loyaltyOfferMatchesTestMode = loyaltyOfferMatchesTestMode;
8
8
  exports.isLoyaltyRedemption = isLoyaltyRedemption;
9
9
  exports.isPackageVisit = isPackageVisit;
10
10
  exports.loyaltyQualifyingAmount = loyaltyQualifyingAmount;
11
+ exports.loyaltyStampRefusal = loyaltyStampRefusal;
11
12
  exports.orderEarnsLoyaltyStamp = orderEarnsLoyaltyStamp;
12
13
  exports.loyaltyCheckFields = loyaltyCheckFields;
13
14
  exports.giftEarnsLoyaltyStamp = giftEarnsLoyaltyStamp;
@@ -78,39 +79,38 @@ function loyaltyQualifyingAmount(order) {
78
79
  return (0, utils_1.preventNaN)(order.originalPrice);
79
80
  return (0, calcs_1.getCustomerTotalToPay)(order);
80
81
  }
81
- /** Whether a finished order earns a stamp. For a multi-visit package this is asked per visit: every
82
- * completed visit earns one, not the package once. A redemption order earns none. */
83
- function orderEarnsLoyaltyStamp(order, offer) {
82
+ /** The first reason `order` earns no stamp for `offer`, or undefined when it does. */
83
+ function loyaltyStampRefusal(order, offer) {
84
84
  var _a, _b;
85
85
  if (!order || !(offer === null || offer === void 0 ? void 0 : offer.loyalty))
86
- return false;
86
+ return 'no_offer';
87
87
  if (!SETTLED_STATUSES.includes((_a = order.orderStatus) !== null && _a !== void 0 ? _a : types_1.OrderStatus.PENDING))
88
- return false;
88
+ return 'not_settled';
89
89
  if (!order.isPaid)
90
- return false;
90
+ return 'unpaid';
91
91
  if (!loyaltyCoversProvider(offer, order.providerId))
92
- return false;
92
+ return 'other_provider';
93
93
  // A gift redemption stamps the giver (who paid), not the recipient — see `giftEarnsLoyaltyStamp`.
94
94
  // Without this, the recipient's total is 0, so any programme with no minimum would wrongly stamp them.
95
95
  if ((0, gifts_1.isGiftRedemption)(order))
96
- return false;
96
+ return 'gift_redemption';
97
97
  if (isLoyaltyRedemption(order))
98
- return false;
98
+ return 'loyalty_redemption';
99
99
  const min = (0, utils_1.preventNaN)((_b = offer.loyalty.minOrderAmount) !== null && _b !== void 0 ? _b : 0);
100
100
  if (min > 0 && loyaltyQualifyingAmount(order) < min)
101
- return false;
102
- return true;
101
+ return 'below_minimum';
102
+ return undefined;
103
+ }
104
+ /** Whether a finished order earns a stamp. For a multi-visit package this is asked per visit: every
105
+ * completed visit earns one, not the package once. A redemption order earns none. */
106
+ function orderEarnsLoyaltyStamp(order, offer) {
107
+ return loyaltyStampRefusal(order, offer) === undefined;
103
108
  }
104
109
  /** The verdict to record for `order` against `offer`, with the terms in force right now. */
105
110
  function loyaltyCheckFields(order, offer, result) {
106
111
  var _a, _b, _c;
107
- return {
108
- promoCodeId: (_a = offer.promoCodeId) !== null && _a !== void 0 ? _a : '',
109
- buyQuantity: (0, utils_1.preventNaN)((_b = offer.loyalty) === null || _b === void 0 ? void 0 : _b.buyQuantity),
110
- minOrderAmount: (0, utils_1.preventNaN)((_c = offer.loyalty) === null || _c === void 0 ? void 0 : _c.minOrderAmount),
111
- qualifyingAmount: loyaltyQualifyingAmount(order),
112
- result,
113
- };
112
+ const reason = result === 'not_eligible' ? loyaltyStampRefusal(order, offer) : undefined;
113
+ return Object.assign({ promoCodeId: (_a = offer.promoCodeId) !== null && _a !== void 0 ? _a : '', buyQuantity: (0, utils_1.preventNaN)((_b = offer.loyalty) === null || _b === void 0 ? void 0 : _b.buyQuantity), minOrderAmount: (0, utils_1.preventNaN)((_c = offer.loyalty) === null || _c === void 0 ? void 0 : _c.minOrderAmount), qualifyingAmount: loyaltyQualifyingAmount(order), result }, (reason ? { reason } : {}));
114
114
  }
115
115
  /** Whether buying a gift earns the giver a stamp. Awarded at purchase, not redemption — the reward
116
116
  * shouldn't hinge on whether the recipient ever spends it. The minimum is fair here since the giver really paid it. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sahab-utils",
3
- "version": "2.1.8",
3
+ "version": "2.1.9",
4
4
  "description": "This package contains types and utils functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",