tripstorekrabi-shared 1.23.9 → 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 +25 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.23.9",
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;
@@ -116,10 +124,11 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
116
124
  const activeRevision = getActiveRevision(booking, pricing);
117
125
 
118
126
  if (!activeRevision) {
119
- 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;
120
129
  }
121
130
 
122
- let activeVolumes = activeRevision.volumes;
131
+ let activeVolumes = activeRevision.volumes || [];
123
132
 
124
133
  if (activeRevision.promotion) {
125
134
  const departureDate = booking.departureDate instanceof Date ? dayjs(booking.departureDate) : typeof booking.departureDate?.toDate === 'function' ? dayjs(booking.departureDate.toDate()) : null;
@@ -127,10 +136,15 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
127
136
  const endDate = activeRevision.promotion.endDate instanceof Date ? activeRevision.promotion.endDate : typeof activeRevision.promotion.endDate.toDate === 'function' ? activeRevision.promotion.endDate.toDate() : null;
128
137
 
129
138
  if (departureDate?.isValid() && departureDate?.isBetween(dayjs(startDate), dayjs(endDate), "day", "[]")) {
130
- activeVolumes = activeRevision.promotion.volumes;
139
+ activeVolumes = activeRevision.promotion.volumes || [];
131
140
  }
132
141
  }
133
142
 
143
+ if (activeVolumes.length === 0) {
144
+ console.warn("Pricing fail-safe triggered: Active revision has no volumes.");
145
+ return 0;
146
+ }
147
+
134
148
  const context = {
135
149
  ...pricing,
136
150
  volumes: activeVolumes
@@ -144,7 +158,7 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
144
158
  const volume = findVolume(booking, context);
145
159
  if (!volume) {
146
160
  console.error(`No volume price found for the total amount of ${booking.adults + booking.children} people`);
147
- return undefined;
161
+ return 0;
148
162
  } else {
149
163
  const bookingSourceKey = getBookingSourceKey(booking.otaPartnerRef);
150
164
 
@@ -184,7 +198,7 @@ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE
184
198
  const volume = findVolume(booking, context);
185
199
  if (!volume) {
186
200
  console.error(`No volume price found for the total amount of ${booking.adults + booking.children} people`);
187
- return undefined;
201
+ return 0;
188
202
  } else {
189
203
  const bookingSourceKey = getBookingSourceKey(booking.otaPartnerRef);
190
204
 
@@ -443,7 +457,8 @@ export function getExtraPaymentsOperatorPrice(extraPayments) {
443
457
 
444
458
  export function calculateAddOnsPrice(booking, product, options = { mode: SELLING_MODE }) {
445
459
  let total = 0;
446
- let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
460
+ let isSelling = [SELLING_MODE, PARTNER_MODE].includes(options.mode);
461
+
447
462
  for (const [id, addOn] of Object.entries(booking.addOns ?? {})) {
448
463
  let addOnDef = findAddOn(id, product.addOns ?? []);
449
464
  if (addOnDef) {
@@ -471,7 +486,7 @@ export function calculateStartEndPointPartnerNetPrice(people, startEndPoint) {
471
486
 
472
487
  function calculateStartEndPointPrice(people, startEndPoint, options = { mode: SELLING_MODE }) {
473
488
  let pricing = startEndPoint?.pricing;
474
- let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
489
+ let isSelling = [SELLING_MODE, PARTNER_MODE].includes(options.mode);
475
490
  if (pricing) {
476
491
  if (pricing.type === "group") {
477
492
  return (isSelling ? (pricing.sellingPrice ?? pricing.amount) : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);