sahab-utils 2.1.7 → 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.
@@ -1,6 +1,7 @@
1
1
  import { DocumentReference, GeoPoint, Timestamp } from 'firebase/firestore';
2
2
  import type { TapLicenseType } from '../ts/tap';
3
3
  import type { OrderSizeAdjustmentFields } from '../ts/orderAdjustment';
4
+ import type { LoyaltyCheckFields } from '../ts/loyalty';
4
5
  export type GeoPointType = GeoPoint;
5
6
  export type TimestampType = Timestamp;
6
7
  export type DistenceDueration = {
@@ -183,6 +184,9 @@ export type Order = {
183
184
  linkDeadline?: TimestampType;
184
185
  paidAt?: TimestampType;
185
186
  }>;
187
+ loyaltyChecks?: Array<LoyaltyCheckFields & {
188
+ checkedAt?: TimestampType;
189
+ }>;
186
190
  };
187
191
  export type ReduxOrder = {
188
192
  orderId?: string;
@@ -19,9 +19,28 @@ 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;
29
+ export type LoyaltyCheckResult = 'stamped' | 'not_eligible' | 'already_counted';
30
+ /** One offer's verdict on a completed order, kept on `order.loyaltyChecks`. The offer can be edited later, so its
31
+ * terms are copied. This part has no Firestore dependency; the server adds `checkedAt`. */
32
+ export type LoyaltyCheckFields = {
33
+ promoCodeId: string;
34
+ buyQuantity: number;
35
+ minOrderAmount: number;
36
+ /** What the minimum was compared against (`loyaltyQualifyingAmount`). */
37
+ qualifyingAmount: number;
38
+ result: LoyaltyCheckResult;
39
+ /** Why it was not eligible; absent for the other results. */
40
+ reason?: LoyaltyRefusal;
41
+ };
42
+ /** The verdict to record for `order` against `offer`, with the terms in force right now. */
43
+ export declare function loyaltyCheckFields(order: Order, offer: Discount, result: LoyaltyCheckResult): LoyaltyCheckFields;
25
44
  /** Whether buying a gift earns the giver a stamp. Awarded at purchase, not redemption — the reward
26
45
  * shouldn't hinge on whether the recipient ever spends it. The minimum is fair here since the giver really paid it. */
27
46
  export declare function giftEarnsLoyaltyStamp(gift: Gift | undefined, offer: Discount | undefined): boolean;
@@ -8,7 +8,9 @@ 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;
13
+ exports.loyaltyCheckFields = loyaltyCheckFields;
12
14
  exports.giftEarnsLoyaltyStamp = giftEarnsLoyaltyStamp;
13
15
  exports.emptyLoyaltyProgress = emptyLoyaltyProgress;
14
16
  exports.applyLoyaltyStamp = applyLoyaltyStamp;
@@ -77,28 +79,38 @@ function loyaltyQualifyingAmount(order) {
77
79
  return (0, utils_1.preventNaN)(order.originalPrice);
78
80
  return (0, calcs_1.getCustomerTotalToPay)(order);
79
81
  }
80
- /** Whether a finished order earns a stamp. For a multi-visit package this is asked per visit: every
81
- * completed visit earns one, not the package once. A redemption order earns none. */
82
- function orderEarnsLoyaltyStamp(order, offer) {
82
+ /** The first reason `order` earns no stamp for `offer`, or undefined when it does. */
83
+ function loyaltyStampRefusal(order, offer) {
83
84
  var _a, _b;
84
85
  if (!order || !(offer === null || offer === void 0 ? void 0 : offer.loyalty))
85
- return false;
86
+ return 'no_offer';
86
87
  if (!SETTLED_STATUSES.includes((_a = order.orderStatus) !== null && _a !== void 0 ? _a : types_1.OrderStatus.PENDING))
87
- return false;
88
+ return 'not_settled';
88
89
  if (!order.isPaid)
89
- return false;
90
+ return 'unpaid';
90
91
  if (!loyaltyCoversProvider(offer, order.providerId))
91
- return false;
92
+ return 'other_provider';
92
93
  // A gift redemption stamps the giver (who paid), not the recipient — see `giftEarnsLoyaltyStamp`.
93
94
  // Without this, the recipient's total is 0, so any programme with no minimum would wrongly stamp them.
94
95
  if ((0, gifts_1.isGiftRedemption)(order))
95
- return false;
96
+ return 'gift_redemption';
96
97
  if (isLoyaltyRedemption(order))
97
- return false;
98
+ return 'loyalty_redemption';
98
99
  const min = (0, utils_1.preventNaN)((_b = offer.loyalty.minOrderAmount) !== null && _b !== void 0 ? _b : 0);
99
100
  if (min > 0 && loyaltyQualifyingAmount(order) < min)
100
- return false;
101
- 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;
108
+ }
109
+ /** The verdict to record for `order` against `offer`, with the terms in force right now. */
110
+ function loyaltyCheckFields(order, offer, result) {
111
+ var _a, _b, _c;
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 } : {}));
102
114
  }
103
115
  /** Whether buying a gift earns the giver a stamp. Awarded at purchase, not redemption — the reward
104
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.7",
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",