@final-commerce/common 2.12.0-staging.1 → 2.12.0-staging.3

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/dist/index.mjs CHANGED
@@ -3464,6 +3464,11 @@ function occupancyIn(occupied, productId, startAt, bufferEndAt, shared) {
3464
3464
  foreignProductId: foreign?.productId
3465
3465
  };
3466
3466
  }
3467
+ var inEveryOutletPool = (entry) => !entry.outletId;
3468
+ function occupiedAtOutlet(occupied, rule, outletId) {
3469
+ if (bookingMethodOf(rule) !== "sessions" || !outletId) return occupied;
3470
+ return occupied.filter((entry) => inEveryOutletPool(entry) || entry.outletId === outletId);
3471
+ }
3467
3472
  var stateFor = (hasShift, offerable, free) => {
3468
3473
  if (offerable === 0) {
3469
3474
  return hasShift() ? "fullyBooked" /* FULLY_BOOKED */ : "noHoursInRange" /* NO_HOURS_IN_RANGE */;
@@ -3495,12 +3500,14 @@ function buildAvailability({
3495
3500
  bookingRulesId,
3496
3501
  rule,
3497
3502
  resources,
3498
- occupied,
3503
+ occupied: everyOutlet,
3504
+ outletId,
3499
3505
  from,
3500
3506
  to,
3501
3507
  now,
3502
3508
  timeZone
3503
3509
  }) {
3510
+ const occupied = occupiedAtOutlet(everyOutlet, rule, outletId);
3504
3511
  const shared = rule.slot.sharedAcrossProducts ?? false;
3505
3512
  const bookableUntil = windowEndsAtInZone(rule, now, timeZone ?? null);
3506
3513
  const parts = bookingRuleParts(rule);
@@ -3828,6 +3835,70 @@ function windowForPeriods({ rule, timeZone, startAt, periods, now }) {
3828
3835
  slotsForWindow(grid, startAt, endAt);
3829
3836
  return { startAt, endAt };
3830
3837
  }
3838
+ function rentalEnds(input) {
3839
+ const { rule, timeZone, startAt, now, untilDay } = input;
3840
+ const parts = bookingRuleParts(rule);
3841
+ if (parts.method !== "date-ranges") throw new BookingRulesError("Only date-range rules book a stay");
3842
+ const horizon = shopDayRange(untilDay, 1, timeZone).to;
3843
+ if (horizon <= startAt) throw new BookingRulesError(`untilDay ${untilDay} is before the stay starts`);
3844
+ const gridUntil = (end) => buildGrid(rule, timeZone, startAt, new Date(Math.min(end, startAt.getTime() + 366 * 1440 * 6e4)), now);
3845
+ let walk = walkRentalEnds(gridUntil(horizon.getTime() + 1440 * 6e4), input, parts, horizon);
3846
+ if (!walk.ends.length && walk.refusal && walk.ranOut)
3847
+ walk = walkRentalEnds(gridUntil(Infinity), input, parts, horizon);
3848
+ if (walk.ends.length) return walk.ends;
3849
+ throw walk.refusal ?? new BookingRulesError(
3850
+ `No stay from ${startAt.toISOString()} fits these rules: the bookable days run out before the shortest allowed stay`
3851
+ );
3852
+ }
3853
+ function walkRentalEnds(grid, { rule, timeZone, startAt, untilDay }, parts, horizon) {
3854
+ const startIndex = grid.findIndex((cell) => cell.startAt.getTime() === startAt.getTime());
3855
+ if (startIndex < 0) {
3856
+ throw new BookingRulesError(`${startAt.toISOString()} is not a bookable slot start for these rules`);
3857
+ }
3858
+ slotsForWindow(grid, startAt, grid[startIndex].endAt);
3859
+ const ends = [];
3860
+ let refusal;
3861
+ for (const cell of grid.slice(startIndex)) {
3862
+ if (cell.endAt > horizon && (ends.length || !refusal)) {
3863
+ refusal ??= new BookingRulesError(`No stay from ${startAt.toISOString()} ends on or before ${untilDay}`);
3864
+ return { ends, refusal, ranOut: false };
3865
+ }
3866
+ const slots = refusalOr(() => slotsForWindow(grid, startAt, cell.endAt));
3867
+ if (slots instanceof BookingRulesError) return { ends, refusal, ranOut: false };
3868
+ const periods = refusalOr(
3869
+ () => parts.wholePeriods ? wholePeriodsIn(rule.ratePeriod, timeZone, startAt, cell.endAt, slots.length) : slots.length
3870
+ );
3871
+ if (periods instanceof BookingRulesError) continue;
3872
+ const stay = refusalOr(() => checkStay(parts, timeZone, startAt, periods));
3873
+ if (stay instanceof BookingRulesError) {
3874
+ if (ends.length) return { ends, refusal, ranOut: false };
3875
+ refusal = stay;
3876
+ continue;
3877
+ }
3878
+ ends.push(rentalEndOf(slots, periods, timeZone));
3879
+ }
3880
+ return { ends, refusal, ranOut: true };
3881
+ }
3882
+ function rentalEndOf(slots, periods, timeZone) {
3883
+ const endAt = slots[slots.length - 1].endAt;
3884
+ return {
3885
+ periods,
3886
+ endAt: endAt.toISOString(),
3887
+ lastDayKey: shopDayKey(new Date(endAt.getTime() - 1), timeZone),
3888
+ coveredSlots: slots.map((slot) => ({
3889
+ startAt: slot.startAt.toISOString(),
3890
+ dayKey: shopDayKey(slot.startAt, timeZone)
3891
+ }))
3892
+ };
3893
+ }
3894
+ function refusalOr(run) {
3895
+ try {
3896
+ return run();
3897
+ } catch (error) {
3898
+ if (error instanceof BookingRulesError) return error;
3899
+ throw error;
3900
+ }
3901
+ }
3831
3902
  function sameDayMonthsLater(startAt, months, timeZone) {
3832
3903
  const { year, month, day } = zonedParts(startAt, timeZone);
3833
3904
  return instantFromLocal(...sameDayMonthsLaterDate(year, month, day, months), 0, timeZone);
@@ -5027,6 +5098,7 @@ export {
5027
5098
  normalizePostCode,
5028
5099
  normalizeProvince,
5029
5100
  occupancyIn,
5101
+ occupiedAtOutlet,
5030
5102
  oldMaxAdvanceOf,
5031
5103
  orderAssignments,
5032
5104
  parseAmountToMinorUnits,
@@ -5045,6 +5117,7 @@ export {
5045
5117
  priceForTotal,
5046
5118
  provincesForPostCode,
5047
5119
  removeFragment,
5120
+ rentalEnds,
5048
5121
  resolveFamily,
5049
5122
  resolveModifiersForProduct,
5050
5123
  resolveShipping,