tripstorekrabi-shared 1.23.8 → 1.24.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/price.js +30 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.23.8",
3
+ "version": "1.24.0",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
package/price.js CHANGED
@@ -36,19 +36,27 @@ export function getActiveRevision(booking, pricing) {
36
36
  return dayjs(dateField);
37
37
  };
38
38
 
39
+ const getSortedActive = () => [...pricing.revisions]
40
+ .filter(r => r.isActive !== false)
41
+ .sort((a, b) => getSafeDate(a.startDate)?.valueOf() - getSafeDate(b.startDate)?.valueOf());
42
+
39
43
  const bookingDate = getSafeDate(booking.date)?.startOf('day');
40
- if (!bookingDate || !bookingDate.isValid()) return null;
44
+
45
+ if (!bookingDate || !bookingDate.isValid()) {
46
+ return getSortedActive()[0] || pricing.revisions[0];
47
+ }
41
48
 
42
49
  const candidateRevisions = pricing.revisions.filter((rev) => {
43
50
  if (rev.isActive === false) return false;
44
-
45
51
  const revStart = getSafeDate(rev.startDate)?.startOf('day');
46
52
  if (!revStart || !revStart.isValid()) return false;
47
53
 
48
54
  return revStart.isSame(bookingDate) || revStart.isBefore(bookingDate);
49
55
  });
50
56
 
51
- if (candidateRevisions.length === 0) return null;
57
+ if (candidateRevisions.length === 0) {
58
+ return getSortedActive()[0] || pricing.revisions[0];
59
+ }
52
60
 
53
61
  return candidateRevisions.sort((a, b) => {
54
62
  const aTime = getSafeDate(a.startDate)?.valueOf() || 0;
@@ -60,9 +68,13 @@ export function getActiveRevision(booking, pricing) {
60
68
  export function getActivePricing(booking, pricing) {
61
69
  const activeRevision = getActiveRevision(booking, pricing);
62
70
 
71
+ if (!activeRevision) {
72
+ return pricing;
73
+ }
74
+
63
75
  return {
64
76
  ...pricing,
65
- ...activeRevision.pricing
77
+ ...activeRevision
66
78
  };
67
79
  }
68
80
 
@@ -112,10 +124,11 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
112
124
  const activeRevision = getActiveRevision(booking, pricing);
113
125
 
114
126
  if (!activeRevision) {
115
- throw new Error("No active pricing revision found for this booking date.");
127
+ console.warn("Pricing fail-safe triggered: No active pricing revision found.");
128
+ return 0;
116
129
  }
117
130
 
118
- let activeVolumes = activeRevision.volumes;
131
+ let activeVolumes = activeRevision.volumes || [];
119
132
 
120
133
  if (activeRevision.promotion) {
121
134
  const departureDate = booking.departureDate instanceof Date ? dayjs(booking.departureDate) : typeof booking.departureDate?.toDate === 'function' ? dayjs(booking.departureDate.toDate()) : null;
@@ -123,10 +136,15 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
123
136
  const endDate = activeRevision.promotion.endDate instanceof Date ? activeRevision.promotion.endDate : typeof activeRevision.promotion.endDate.toDate === 'function' ? activeRevision.promotion.endDate.toDate() : null;
124
137
 
125
138
  if (departureDate?.isValid() && departureDate?.isBetween(dayjs(startDate), dayjs(endDate), "day", "[]")) {
126
- activeVolumes = activeRevision.promotion.volumes;
139
+ activeVolumes = activeRevision.promotion.volumes || [];
127
140
  }
128
141
  }
129
142
 
143
+ if (activeVolumes.length === 0) {
144
+ console.warn("Pricing fail-safe triggered: Active revision has no volumes.");
145
+ return 0;
146
+ }
147
+
130
148
  const context = {
131
149
  ...pricing,
132
150
  volumes: activeVolumes
@@ -140,7 +158,7 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
140
158
  const volume = findVolume(booking, context);
141
159
  if (!volume) {
142
160
  console.error(`No volume price found for the total amount of ${booking.adults + booking.children} people`);
143
- return undefined;
161
+ return 0;
144
162
  } else {
145
163
  const bookingSourceKey = getBookingSourceKey(booking.otaPartnerRef);
146
164
 
@@ -180,7 +198,7 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
180
198
  const volume = findVolume(booking, context);
181
199
  if (!volume) {
182
200
  console.error(`No volume price found for the total amount of ${booking.adults + booking.children} people`);
183
- return undefined;
201
+ return 0;
184
202
  } else {
185
203
  const bookingSourceKey = getBookingSourceKey(booking.otaPartnerRef);
186
204
 
@@ -439,7 +457,8 @@ export function getExtraPaymentsOperatorPrice(extraPayments) {
439
457
 
440
458
  export function calculateAddOnsPrice(booking, product, options = { mode: SELLING_MODE }) {
441
459
  let total = 0;
442
- let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
460
+ let isSelling = [SELLING_MODE, PARTNER_MODE].includes(options.mode);
461
+
443
462
  for (const [id, addOn] of Object.entries(booking.addOns ?? {})) {
444
463
  let addOnDef = findAddOn(id, product.addOns ?? []);
445
464
  if (addOnDef) {
@@ -467,7 +486,7 @@ export function calculateStartEndPointPartnerNetPrice(people, startEndPoint) {
467
486
 
468
487
  function calculateStartEndPointPrice(people, startEndPoint, options = { mode: SELLING_MODE }) {
469
488
  let pricing = startEndPoint?.pricing;
470
- let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
489
+ let isSelling = [SELLING_MODE, PARTNER_MODE].includes(options.mode);
471
490
  if (pricing) {
472
491
  if (pricing.type === "group") {
473
492
  return (isSelling ? (pricing.sellingPrice ?? pricing.amount) : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);