@final-commerce/common 2.11.1-staging2.1 → 2.12.0-staging.1
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.d.mts +216 -24
- package/dist/index.d.ts +216 -24
- package/dist/index.js +884 -162
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +856 -162
- package/dist/index.mjs.map +1 -1
- package/dist/pos-types/index.d.mts +49 -8
- package/dist/pos-types/index.d.ts +49 -8
- package/dist/pos-types/index.js +7 -2
- package/dist/pos-types/index.js.map +1 -1
- package/dist/pos-types/index.mjs +5 -1
- package/dist/pos-types/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -454,21 +454,37 @@ var PaymentProcessor = /* @__PURE__ */ ((PaymentProcessor2) => {
|
|
|
454
454
|
})(PaymentProcessor || {});
|
|
455
455
|
|
|
456
456
|
// enums/booking.enum.ts
|
|
457
|
-
var BookingType = /* @__PURE__ */ ((
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return
|
|
457
|
+
var BookingType = /* @__PURE__ */ ((BookingType2) => {
|
|
458
|
+
BookingType2["RENTAL"] = "rental";
|
|
459
|
+
BookingType2["APPOINTMENT"] = "appointment";
|
|
460
|
+
BookingType2["OVERNIGHT"] = "overnight";
|
|
461
|
+
BookingType2["GROUP"] = "group";
|
|
462
|
+
return BookingType2;
|
|
463
463
|
})(BookingType || {});
|
|
464
464
|
var BookingRatePeriod = /* @__PURE__ */ ((BookingRatePeriod2) => {
|
|
465
465
|
BookingRatePeriod2["SLOT"] = "slot";
|
|
466
|
+
BookingRatePeriod2["HOUR"] = "hour";
|
|
467
|
+
BookingRatePeriod2["SESSION"] = "session";
|
|
466
468
|
BookingRatePeriod2["DAY"] = "day";
|
|
467
469
|
BookingRatePeriod2["NIGHT"] = "night";
|
|
468
470
|
BookingRatePeriod2["WEEK"] = "week";
|
|
469
471
|
BookingRatePeriod2["MONTH"] = "month";
|
|
470
472
|
return BookingRatePeriod2;
|
|
471
473
|
})(BookingRatePeriod || {});
|
|
474
|
+
var BookingDurationUnit = /* @__PURE__ */ ((BookingDurationUnit2) => {
|
|
475
|
+
BookingDurationUnit2["MINUTE"] = "minute";
|
|
476
|
+
BookingDurationUnit2["HOUR"] = "hour";
|
|
477
|
+
BookingDurationUnit2["DAY"] = "day";
|
|
478
|
+
BookingDurationUnit2["NIGHT"] = "night";
|
|
479
|
+
BookingDurationUnit2["WEEK"] = "week";
|
|
480
|
+
BookingDurationUnit2["MONTH"] = "month";
|
|
481
|
+
return BookingDurationUnit2;
|
|
482
|
+
})(BookingDurationUnit || {});
|
|
483
|
+
var BookingSessionSale = /* @__PURE__ */ ((BookingSessionSale2) => {
|
|
484
|
+
BookingSessionSale2["SESSION"] = "session";
|
|
485
|
+
BookingSessionSale2["COURSE"] = "course";
|
|
486
|
+
return BookingSessionSale2;
|
|
487
|
+
})(BookingSessionSale || {});
|
|
472
488
|
var BookingResourceKind = /* @__PURE__ */ ((BookingResourceKind2) => {
|
|
473
489
|
BookingResourceKind2["ASSET"] = "asset";
|
|
474
490
|
BookingResourceKind2["ROOM"] = "room";
|
|
@@ -3329,6 +3345,7 @@ var ORDER_STATE_EVENTS = {
|
|
|
3329
3345
|
};
|
|
3330
3346
|
|
|
3331
3347
|
// booking/types.ts
|
|
3348
|
+
var UNLIMITED_PLACES = Number.MAX_SAFE_INTEGER;
|
|
3332
3349
|
var BookingRulesError = class extends Error {
|
|
3333
3350
|
constructor(message) {
|
|
3334
3351
|
super(message);
|
|
@@ -3397,153 +3414,39 @@ function atZonedMinute(dayStart, minutes, timeZone) {
|
|
|
3397
3414
|
const { year, month, day } = zonedParts(dayStart, timeZone);
|
|
3398
3415
|
return instantFromLocal(year, month, day, minutes, timeZone);
|
|
3399
3416
|
}
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
var asTime = (value, field) => {
|
|
3405
|
-
if (value == null) return void 0;
|
|
3406
|
-
const time = new Date(value).getTime();
|
|
3407
|
-
if (Number.isNaN(time)) throw new BookingRulesError(`Booking rules: ${field} is not a date, got "${String(value)}"`);
|
|
3408
|
-
return time;
|
|
3409
|
-
};
|
|
3410
|
-
function parseHhMm(value, field) {
|
|
3411
|
-
const [hours, minutes] = value.split(":").map(Number);
|
|
3412
|
-
if (!Number.isInteger(hours) || !Number.isInteger(minutes)) {
|
|
3413
|
-
throw new BookingRulesError(`Booking rules: ${field} must be "HH:mm", got "${value}"`);
|
|
3414
|
-
}
|
|
3415
|
-
return hours * 60 + minutes;
|
|
3416
|
-
}
|
|
3417
|
-
function windowEndsAt(rule, now) {
|
|
3418
|
-
const { maxAdvanceDays, bookableTo } = rule.window;
|
|
3419
|
-
const ends = Math.min(
|
|
3420
|
-
maxAdvanceDays ? now.getTime() + maxAdvanceDays * 1440 * MINUTE2 : Number.POSITIVE_INFINITY,
|
|
3421
|
-
asTime(bookableTo, "window.bookableTo") ?? Number.POSITIVE_INFINITY
|
|
3422
|
-
);
|
|
3423
|
-
return Number.isFinite(ends) ? new Date(ends) : null;
|
|
3417
|
+
function sameDayMonthsLaterDate(year, month, day, months) {
|
|
3418
|
+
const target = new Date(Date.UTC(year, month - 1 + months, 1));
|
|
3419
|
+
const lastDay = new Date(Date.UTC(target.getUTCFullYear(), target.getUTCMonth() + 1, 0)).getUTCDate();
|
|
3420
|
+
return [target.getUTCFullYear(), target.getUTCMonth() + 1, Math.min(day, lastDay)];
|
|
3424
3421
|
}
|
|
3425
|
-
function
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
if (ratePeriod === "night" /* NIGHT */) {
|
|
3429
|
-
if (!checkInTime || !checkOutTime) {
|
|
3430
|
-
throw new BookingRulesError("Overnight booking rules need both checkInTime and checkOutTime");
|
|
3431
|
-
}
|
|
3432
|
-
const checkIn = parseHhMm(checkInTime, "checkInTime");
|
|
3433
|
-
const checkOut = parseHhMm(checkOutTime, "checkOutTime");
|
|
3434
|
-
return {
|
|
3435
|
-
startAt: at(checkIn),
|
|
3436
|
-
endAt: at(1440 + checkOut),
|
|
3437
|
-
// Housekeeping sits between check-out and the next check-in: it costs no night.
|
|
3438
|
-
bufferEndAt: at(1440 + checkIn),
|
|
3439
|
-
canStart: true
|
|
3440
|
-
};
|
|
3441
|
-
}
|
|
3442
|
-
const from = ratePeriod === "day" /* DAY */ ? openMinute ?? 0 : 0;
|
|
3443
|
-
const to = ratePeriod === "day" /* DAY */ ? closeMinute ?? 1440 : 1440;
|
|
3444
|
-
return {
|
|
3445
|
-
startAt: at(from),
|
|
3446
|
-
endAt: at(to),
|
|
3447
|
-
bufferEndAt: at(to + (slot.bufferMinutes ?? 0)),
|
|
3448
|
-
canStart: true
|
|
3449
|
-
};
|
|
3450
|
-
}
|
|
3451
|
-
function freeGaps(openAt, closeAt, busy) {
|
|
3452
|
-
const inside = busy.map(({ startAt, bufferEndAt }) => ({ from: +startAt, to: +bufferEndAt })).filter(({ from, to }) => to > openAt && from < closeAt).sort((first, second) => first.from - second.from);
|
|
3453
|
-
const gaps = [];
|
|
3454
|
-
let cursor = openAt;
|
|
3455
|
-
for (const { from, to } of inside) {
|
|
3456
|
-
if (from > cursor) gaps.push({ from: cursor, to: from, endsAtBooking: true });
|
|
3457
|
-
cursor = Math.max(cursor, to);
|
|
3458
|
-
if (cursor >= closeAt) break;
|
|
3459
|
-
}
|
|
3460
|
-
if (cursor < closeAt) gaps.push({ from: cursor, to: closeAt, endsAtBooking: false });
|
|
3461
|
-
return gaps;
|
|
3462
|
-
}
|
|
3463
|
-
function buildGrid(rule, timeZone, from, to, now, busy = []) {
|
|
3464
|
-
if (to.getTime() - from.getTime() > MAX_RANGE_DAYS * 1440 * MINUTE2) {
|
|
3465
|
-
throw new BookingRulesError(`A booking range may span at most ${MAX_RANGE_DAYS} days`);
|
|
3466
|
-
}
|
|
3467
|
-
const { availability, slot, window, ratePeriod } = rule;
|
|
3468
|
-
const periodMode = ratePeriod !== "slot" /* SLOT */;
|
|
3469
|
-
const weeklyHours = availability.weeklyHours ?? [];
|
|
3470
|
-
const notBefore = Math.max(
|
|
3471
|
-
now.getTime() + (window.minNoticeMinutes ?? 0) * MINUTE2,
|
|
3472
|
-
asTime(window.bookableFrom, "window.bookableFrom") ?? 0,
|
|
3473
|
-
asTime(availability.availableFrom, "availability.availableFrom") ?? 0
|
|
3474
|
-
);
|
|
3475
|
-
const notAfter = windowEndsAt(rule, now)?.getTime() ?? Number.POSITIVE_INFINITY;
|
|
3476
|
-
const blocked = (availability.offMarket ?? []).map(({ from: blockFrom, to: blockTo }) => ({
|
|
3477
|
-
from: asTime(blockFrom, "availability.offMarket.from"),
|
|
3478
|
-
to: asTime(blockTo, "availability.offMarket.to")
|
|
3479
|
-
}));
|
|
3480
|
-
const arrivalDays = availability.arrivalDays;
|
|
3481
|
-
const step = slot.lengthMinutes + (slot.bufferMinutes ?? 0);
|
|
3482
|
-
const slots = [];
|
|
3483
|
-
const lastDay = startOfZonedDay(to, timeZone).getTime();
|
|
3484
|
-
let dayStart = startOfZonedDay(from, timeZone);
|
|
3485
|
-
while (dayStart.getTime() <= lastDay) {
|
|
3486
|
-
const weekday = zonedParts(dayStart, timeZone).weekday;
|
|
3487
|
-
const rows = weeklyHours.filter((entry) => entry.weekday === weekday);
|
|
3488
|
-
if (periodMode) {
|
|
3489
|
-
const openToday = weeklyHours.length === 0 || rows.length > 0;
|
|
3490
|
-
if (openToday) {
|
|
3491
|
-
const built = periodSlot(dayStart, timeZone, rule, rows[0]?.openMinute, rows[0]?.closeMinute);
|
|
3492
|
-
slots.push({ ...built, canStart: !arrivalDays?.length || arrivalDays.includes(weekday) });
|
|
3493
|
-
}
|
|
3494
|
-
} else {
|
|
3495
|
-
for (const row of rows) {
|
|
3496
|
-
const openAt = +atZonedMinute(dayStart, row.openMinute, timeZone);
|
|
3497
|
-
const closeAt = +atZonedMinute(dayStart, row.closeMinute, timeZone);
|
|
3498
|
-
for (const gap of freeGaps(openAt, closeAt, busy)) {
|
|
3499
|
-
const mustFit = (gap.endsAtBooking ? step : slot.lengthMinutes) * MINUTE2;
|
|
3500
|
-
for (let start = gap.from; start + mustFit <= gap.to; start += step * MINUTE2) {
|
|
3501
|
-
slots.push({
|
|
3502
|
-
startAt: new Date(start),
|
|
3503
|
-
endAt: new Date(start + slot.lengthMinutes * MINUTE2),
|
|
3504
|
-
bufferEndAt: new Date(start + step * MINUTE2),
|
|
3505
|
-
canStart: true
|
|
3506
|
-
});
|
|
3507
|
-
}
|
|
3508
|
-
}
|
|
3509
|
-
}
|
|
3510
|
-
}
|
|
3511
|
-
dayStart = nextZonedDay(dayStart, timeZone);
|
|
3512
|
-
}
|
|
3513
|
-
const askedFrom = from.getTime();
|
|
3514
|
-
const askedTo = to.getTime();
|
|
3515
|
-
return slots.filter(({ startAt, bufferEndAt }) => {
|
|
3516
|
-
const start = startAt.getTime();
|
|
3517
|
-
if (start < askedFrom || start >= askedTo) return false;
|
|
3518
|
-
if (start < notBefore || start > notAfter) return false;
|
|
3519
|
-
return !blocked.some(
|
|
3520
|
-
({ from: blockFrom, to: blockTo }) => start < blockTo && bufferEndAt.getTime() > blockFrom
|
|
3521
|
-
);
|
|
3522
|
-
}).sort((first, second) => +first.startAt - +second.startAt);
|
|
3523
|
-
}
|
|
3524
|
-
function slotsForWindow(grid, startAt, endAt) {
|
|
3525
|
-
const startIndex = grid.findIndex((entry) => entry.startAt.getTime() === startAt.getTime());
|
|
3526
|
-
if (startIndex < 0) {
|
|
3527
|
-
throw new BookingRulesError(`${startAt.toISOString()} is not a bookable slot start for these rules`);
|
|
3528
|
-
}
|
|
3529
|
-
if (!grid[startIndex].canStart) {
|
|
3530
|
-
throw new BookingRulesError(`${startAt.toISOString()} is not an arrival day for these rules`);
|
|
3531
|
-
}
|
|
3532
|
-
const covered = [];
|
|
3533
|
-
for (let index = startIndex; index < grid.length; index++) {
|
|
3534
|
-
const entry = grid[index];
|
|
3535
|
-
const previous = covered[covered.length - 1];
|
|
3536
|
-
if (previous && entry.startAt.getTime() !== previous.bufferEndAt.getTime()) break;
|
|
3537
|
-
covered.push(entry);
|
|
3538
|
-
if (entry.endAt.getTime() === endAt.getTime()) return covered;
|
|
3539
|
-
if (entry.endAt.getTime() > endAt.getTime()) break;
|
|
3540
|
-
}
|
|
3422
|
+
function addClockDuration(from, { count, unit: unit2 }) {
|
|
3423
|
+
if (unit2 === "minute" /* MINUTE */) return new Date(from.getTime() + count * MINUTE);
|
|
3424
|
+
if (unit2 === "hour" /* HOUR */) return new Date(from.getTime() + count * 60 * MINUTE);
|
|
3541
3425
|
throw new BookingRulesError(
|
|
3542
|
-
|
|
3426
|
+
`Booking rules: ${count} ${unit2} is not minutes or hours \u2014 it needs the shop's time zone`
|
|
3543
3427
|
);
|
|
3544
3428
|
}
|
|
3429
|
+
var DAYS_IN = {
|
|
3430
|
+
["day" /* DAY */]: 1,
|
|
3431
|
+
["night" /* NIGHT */]: 1,
|
|
3432
|
+
["week" /* WEEK */]: 7
|
|
3433
|
+
};
|
|
3434
|
+
function addDuration(from, duration, timeZone) {
|
|
3435
|
+
const { count, unit: unit2 } = duration;
|
|
3436
|
+
if (unit2 === "minute" /* MINUTE */ || unit2 === "hour" /* HOUR */) return addClockDuration(from, duration);
|
|
3437
|
+
const days = DAYS_IN[unit2];
|
|
3438
|
+
if (days === void 0 && unit2 !== "month" /* MONTH */) {
|
|
3439
|
+
throw new BookingRulesError(`Booking rules: "${unit2}" is not a duration unit`);
|
|
3440
|
+
}
|
|
3441
|
+
if (!Number.isInteger(count)) throw new BookingRulesError(`Booking rules: ${count} ${unit2} is not a whole number`);
|
|
3442
|
+
const { year, month, day, hour, minute, second } = zonedParts(from, timeZone);
|
|
3443
|
+
const [toYear, toMonth, toDay] = days === void 0 ? sameDayMonthsLaterDate(year, month, day, count) : [year, month, day + days * count];
|
|
3444
|
+
const wallClock = instantFromLocal(toYear, toMonth, toDay, hour * 60 + minute, timeZone);
|
|
3445
|
+
return new Date(wallClock.getTime() + second * 1e3 + from.getUTCMilliseconds());
|
|
3446
|
+
}
|
|
3545
3447
|
|
|
3546
3448
|
// booking/availability.ts
|
|
3449
|
+
var placesOnTheWire = (entry) => Number.isFinite(entry.capacity) ? entry : { ...entry, capacity: UNLIMITED_PLACES, free: UNLIMITED_PLACES, unlimited: true };
|
|
3547
3450
|
var withinPresence = (slot, timeZone, presence) => {
|
|
3548
3451
|
if (!presence?.length) return true;
|
|
3549
3452
|
const dayStart = startOfZonedDay(slot.startAt, timeZone);
|
|
@@ -3590,25 +3493,33 @@ function shopDayRange(fromDayKey, count, timeZone) {
|
|
|
3590
3493
|
function buildAvailability({
|
|
3591
3494
|
productId,
|
|
3592
3495
|
bookingRulesId,
|
|
3593
|
-
bookingType,
|
|
3594
3496
|
rule,
|
|
3595
3497
|
resources,
|
|
3596
3498
|
occupied,
|
|
3597
3499
|
from,
|
|
3598
3500
|
to,
|
|
3599
|
-
now
|
|
3501
|
+
now,
|
|
3502
|
+
timeZone
|
|
3600
3503
|
}) {
|
|
3601
3504
|
const shared = rule.slot.sharedAcrossProducts ?? false;
|
|
3602
|
-
const bookableUntil =
|
|
3505
|
+
const bookableUntil = windowEndsAtInZone(rule, now, timeZone ?? null);
|
|
3506
|
+
const parts = bookingRuleParts(rule);
|
|
3603
3507
|
const header = {
|
|
3604
3508
|
productId,
|
|
3605
3509
|
bookingRulesId,
|
|
3606
|
-
bookingType,
|
|
3607
3510
|
ratePeriod: rule.ratePeriod,
|
|
3511
|
+
// Time slots / sessions / date ranges — so a reader never re-classifies `ratePeriod` itself.
|
|
3512
|
+
bookingMethod: bookingMethodOf(rule),
|
|
3608
3513
|
// Passed through for the UI to label: a group slot under minimumToRun may not run.
|
|
3609
3514
|
minimumToRun: rule.slot.minimumToRun,
|
|
3610
3515
|
minStay: rule.slot.minStay,
|
|
3611
3516
|
maxStay: rule.slot.maxStay,
|
|
3517
|
+
// The same limits with their own units, both rule shapes. `minStay` / `maxStay` above go at B13 step 9.
|
|
3518
|
+
minStayLength: parts.method === "date-ranges" ? parts.minStay : void 0,
|
|
3519
|
+
maxStayLength: parts.method === "date-ranges" ? parts.maxStay : void 0,
|
|
3520
|
+
// The per-booking limits, so a stepper is bounded by the rules (G7) instead of a number in the flow.
|
|
3521
|
+
maxQtyPerBooking: rule.slot.maxQtyPerBooking,
|
|
3522
|
+
maxPartySize: rule.slot.maxPartySize,
|
|
3612
3523
|
// Why a day can come back empty. Past this instant the rule refuses everything, so the UI
|
|
3613
3524
|
// can say "beyond the booking window" where it would otherwise claim "nothing free".
|
|
3614
3525
|
bookableUntil,
|
|
@@ -3632,7 +3543,7 @@ function buildAvailability({
|
|
|
3632
3543
|
const byInstant = /* @__PURE__ */ new Map();
|
|
3633
3544
|
const freeSlotsPerResource = /* @__PURE__ */ new Map();
|
|
3634
3545
|
for (const { resource, slots: slots2 } of grids) {
|
|
3635
|
-
const capacity = resource
|
|
3546
|
+
const capacity = capacityOf(rule, resource);
|
|
3636
3547
|
const mine = byResource.get(resource.id) ?? [];
|
|
3637
3548
|
for (const slot of slots2) {
|
|
3638
3549
|
const { taken, foreignProductId } = occupancyIn(mine, productId, slot.startAt, slot.bufferEndAt, shared);
|
|
@@ -3649,7 +3560,9 @@ function buildAvailability({
|
|
|
3649
3560
|
entry.booked += foreignProductId ? capacity : taken;
|
|
3650
3561
|
entry.free += free;
|
|
3651
3562
|
entry.canStart = entry.canStart || slot.canStart;
|
|
3652
|
-
entry.resources.push(
|
|
3563
|
+
entry.resources.push(
|
|
3564
|
+
placesOnTheWire({ resourceId: resource.id, name: resource.name, capacity, booked: taken, free })
|
|
3565
|
+
);
|
|
3653
3566
|
if (free > 0) freeSlotsPerResource.set(resource.id, (freeSlotsPerResource.get(resource.id) ?? 0) + 1);
|
|
3654
3567
|
byInstant.set(+slot.startAt, entry);
|
|
3655
3568
|
}
|
|
@@ -3671,9 +3584,10 @@ function buildAvailability({
|
|
|
3671
3584
|
)
|
|
3672
3585
|
};
|
|
3673
3586
|
});
|
|
3674
|
-
const slots = [...byInstant.values()].sort((first, second) => +first.startAt - +second.startAt);
|
|
3587
|
+
const slots = [...byInstant.values()].map(placesOnTheWire).map((slot) => withMaxQtyNow(slot, slot.resources, rule.slot.maxQtyPerBooking)).sort((first, second) => +first.startAt - +second.startAt);
|
|
3675
3588
|
return {
|
|
3676
3589
|
...header,
|
|
3590
|
+
course: soldAsCourse(rule) ? courseAvailability(rule, resources, byResource, productId, shared, now) : void 0,
|
|
3677
3591
|
resources: inWindow,
|
|
3678
3592
|
slots,
|
|
3679
3593
|
/**
|
|
@@ -3685,6 +3599,51 @@ function buildAvailability({
|
|
|
3685
3599
|
days: [...new Set(slots.map((slot) => slot.dayKey))]
|
|
3686
3600
|
};
|
|
3687
3601
|
}
|
|
3602
|
+
function courseAvailability(rule, resources, byResource, productId, shared, now) {
|
|
3603
|
+
if (resources.length === 0) return void 0;
|
|
3604
|
+
const sessions = courseSessions(rule, resources[0].timeZone);
|
|
3605
|
+
const alreadyStarted = courseStartedInZone(rule, sessions, now, resources[0].timeZone);
|
|
3606
|
+
const free = resources.map((resource) => {
|
|
3607
|
+
const mine = byResource.get(resource.id) ?? [];
|
|
3608
|
+
const everySession = sessions.every((session) => withinPresence(session, resource.timeZone, resource.presence));
|
|
3609
|
+
const freePerSession = sessions.map((session) => {
|
|
3610
|
+
const { taken, foreignProductId } = occupancyIn(mine, productId, session.startAt, session.bufferEndAt, shared);
|
|
3611
|
+
return foreignProductId ? 0 : Math.max(capacityOf(rule, resource) - taken, 0);
|
|
3612
|
+
});
|
|
3613
|
+
const fewest = everySession && !alreadyStarted ? Math.min(...freePerSession) : 0;
|
|
3614
|
+
return Number.isFinite(fewest) ? { resourceId: resource.id, free: fewest } : { resourceId: resource.id, free: UNLIMITED_PLACES, unlimited: true };
|
|
3615
|
+
});
|
|
3616
|
+
return withMaxQtyNow(
|
|
3617
|
+
{
|
|
3618
|
+
sessions: sessions.length,
|
|
3619
|
+
alreadyStarted,
|
|
3620
|
+
sessionTimes: sessions.map(({ startAt, endAt }) => ({ startAt, endAt })),
|
|
3621
|
+
startAt: sessions[0].startAt,
|
|
3622
|
+
endAt: sessions[sessions.length - 1].endAt,
|
|
3623
|
+
resources: free
|
|
3624
|
+
},
|
|
3625
|
+
free,
|
|
3626
|
+
rule.slot.maxQtyPerBooking
|
|
3627
|
+
);
|
|
3628
|
+
}
|
|
3629
|
+
function pickFreeResource(slot, qty = 1) {
|
|
3630
|
+
return slot.resources.find((entry) => entry.free >= qty);
|
|
3631
|
+
}
|
|
3632
|
+
function withMaxQtyNow(entry, resources, maxQtyPerBooking) {
|
|
3633
|
+
const largest = resources.some((resource) => resource.unlimited) ? Number.POSITIVE_INFINITY : Math.max(0, ...resources.map((resource) => resource.free));
|
|
3634
|
+
const maxQtyNow = Math.min(largest, maxQtyPerBooking ?? Number.POSITIVE_INFINITY);
|
|
3635
|
+
return Number.isFinite(maxQtyNow) ? { ...entry, maxQtyNow } : entry;
|
|
3636
|
+
}
|
|
3637
|
+
function fewestFreeOver(slots, from, to) {
|
|
3638
|
+
const cells = slots.filter(({ startAt, endAt }) => +startAt >= +from && +endAt <= +to);
|
|
3639
|
+
if (cells.length === 0) return [];
|
|
3640
|
+
return cells[0].resources.map((resource) => ({
|
|
3641
|
+
...resource,
|
|
3642
|
+
free: Math.min(
|
|
3643
|
+
...cells.map((cell) => cell.resources.find(({ resourceId }) => resourceId === resource.resourceId)?.free ?? 0)
|
|
3644
|
+
)
|
|
3645
|
+
}));
|
|
3646
|
+
}
|
|
3688
3647
|
function presentAt(resources, rows, outletId) {
|
|
3689
3648
|
const byResource = /* @__PURE__ */ new Map();
|
|
3690
3649
|
for (const row of rows) {
|
|
@@ -3698,6 +3657,107 @@ function presentAt(resources, rows, outletId) {
|
|
|
3698
3657
|
});
|
|
3699
3658
|
}
|
|
3700
3659
|
|
|
3660
|
+
// booking/durations.ts
|
|
3661
|
+
var { MINUTE: MINUTE2, HOUR, DAY, NIGHT, WEEK, MONTH } = BookingDurationUnit;
|
|
3662
|
+
var NONE = [];
|
|
3663
|
+
var CLOCK = [MINUTE2, HOUR];
|
|
3664
|
+
var everyType = (units) => Object.fromEntries(Object.values(BookingRatePeriod).map((ratePeriod) => [ratePeriod, units]));
|
|
3665
|
+
var STAY = {
|
|
3666
|
+
slot: NONE,
|
|
3667
|
+
session: NONE,
|
|
3668
|
+
hour: [HOUR, DAY, WEEK],
|
|
3669
|
+
night: [NIGHT, WEEK, MONTH],
|
|
3670
|
+
day: [DAY, WEEK, MONTH],
|
|
3671
|
+
week: [WEEK],
|
|
3672
|
+
month: [MONTH]
|
|
3673
|
+
};
|
|
3674
|
+
var ALLOWED_UNITS = {
|
|
3675
|
+
"timeSlots.length": { ...everyType(NONE), slot: CLOCK },
|
|
3676
|
+
"slot.buffer": { ...everyType(CLOCK), night: NONE },
|
|
3677
|
+
"window.hold": everyType(CLOCK),
|
|
3678
|
+
"window.minNotice": everyType([MINUTE2, HOUR, DAY, WEEK]),
|
|
3679
|
+
"window.maxAdvance": everyType([MINUTE2, HOUR, DAY, WEEK, MONTH]),
|
|
3680
|
+
"dateRanges.minStay": STAY,
|
|
3681
|
+
"dateRanges.maxStay": STAY
|
|
3682
|
+
};
|
|
3683
|
+
function allowedDurationUnits(rule, field) {
|
|
3684
|
+
const units = ALLOWED_UNITS[field]?.[rule.ratePeriod];
|
|
3685
|
+
if (!units) throw new BookingRulesError(`Booking rules: no duration "${field}" for rate period "${rule.ratePeriod}"`);
|
|
3686
|
+
return units;
|
|
3687
|
+
}
|
|
3688
|
+
var DURATION_AT = {
|
|
3689
|
+
"timeSlots.length": (rule) => rule.timeSlots?.length,
|
|
3690
|
+
"slot.buffer": (rule) => rule.slot.buffer,
|
|
3691
|
+
"window.hold": (rule) => rule.window.hold,
|
|
3692
|
+
"window.minNotice": (rule) => rule.window.minNotice,
|
|
3693
|
+
"window.maxAdvance": (rule) => rule.window.maxAdvance,
|
|
3694
|
+
"dateRanges.minStay": (rule) => rule.dateRanges?.minStay,
|
|
3695
|
+
"dateRanges.maxStay": (rule) => rule.dateRanges?.maxStay
|
|
3696
|
+
};
|
|
3697
|
+
function durationProblems(rule) {
|
|
3698
|
+
const problems = Object.keys(DURATION_AT).flatMap((field) => {
|
|
3699
|
+
const duration = DURATION_AT[field](rule);
|
|
3700
|
+
const problem = duration && problemOf(field, duration, allowedDurationUnits(rule, field), rule.ratePeriod);
|
|
3701
|
+
return problem ? [problem] : [];
|
|
3702
|
+
});
|
|
3703
|
+
if (problems.length > 0) return problems;
|
|
3704
|
+
return [stayAboveMax(rule.dateRanges?.minStay, rule.dateRanges?.maxStay), noticeAboveAdvance(rule.window)].filter(
|
|
3705
|
+
(problem) => !!problem
|
|
3706
|
+
);
|
|
3707
|
+
}
|
|
3708
|
+
function problemOf(field, duration, allowed, ratePeriod) {
|
|
3709
|
+
if (!Number.isInteger(duration.count) || duration.count < 1) {
|
|
3710
|
+
const message = `${field}: count must be a whole number of at least 1, got ${String(duration.count)}`;
|
|
3711
|
+
return { field, kind: "count", message };
|
|
3712
|
+
}
|
|
3713
|
+
if (allowed.length === 0) {
|
|
3714
|
+
return { field, kind: "not-for-type", message: `${field}: does not apply to ${ratePeriod} rules` };
|
|
3715
|
+
}
|
|
3716
|
+
if (!allowed.includes(duration.unit)) {
|
|
3717
|
+
return { field, kind: "unit", allowed, message: `${field}: takes ${allowed.join(", ")}, got ${duration.unit}` };
|
|
3718
|
+
}
|
|
3719
|
+
return void 0;
|
|
3720
|
+
}
|
|
3721
|
+
var MINUTES_RANGE = {
|
|
3722
|
+
minute: [1, 1],
|
|
3723
|
+
hour: [60, 60],
|
|
3724
|
+
day: [1440, 1440],
|
|
3725
|
+
night: [1440, 1440],
|
|
3726
|
+
week: [7 * 1440, 7 * 1440],
|
|
3727
|
+
month: [28 * 1440, 31 * 1440]
|
|
3728
|
+
};
|
|
3729
|
+
var clockShiftOf = (unit2) => unit2 === MINUTE2 || unit2 === HOUR ? 0 : 60;
|
|
3730
|
+
var shortestMinutesOf = ({ count, unit: unit2 }) => count * MINUTES_RANGE[unit2][0] - clockShiftOf(unit2);
|
|
3731
|
+
var longestMinutesOf = ({ count, unit: unit2 }) => count * MINUTES_RANGE[unit2][1] + clockShiftOf(unit2);
|
|
3732
|
+
var durationText = ({ count, unit: unit2 }) => `${count} ${unit2}${count === 1 ? "" : "s"}`;
|
|
3733
|
+
var DAYS_IN_UNIT = { day: 1, night: 1, week: 7 };
|
|
3734
|
+
var MINUTES_IN_UNIT = { minute: 1, hour: 60 };
|
|
3735
|
+
function exactly(min, max) {
|
|
3736
|
+
if (min.unit === max.unit) return [min.count, max.count];
|
|
3737
|
+
for (const table of [DAYS_IN_UNIT, MINUTES_IN_UNIT]) {
|
|
3738
|
+
const [minSize, maxSize] = [table[min.unit], table[max.unit]];
|
|
3739
|
+
if (minSize && maxSize) return [min.count * minSize, max.count * maxSize];
|
|
3740
|
+
}
|
|
3741
|
+
return void 0;
|
|
3742
|
+
}
|
|
3743
|
+
function minLongerThanMax(min, max) {
|
|
3744
|
+
const exact = exactly(min, max);
|
|
3745
|
+
return exact ? exact[0] > exact[1] : longestMinutesOf(min) > shortestMinutesOf(max);
|
|
3746
|
+
}
|
|
3747
|
+
function stayAboveMax(min, max) {
|
|
3748
|
+
if (!min || !max || !minLongerThanMax(min, max)) return void 0;
|
|
3749
|
+
const message = `dateRanges.maxStay: Max ${durationText(max)} can be shorter than Min ${durationText(min)}`;
|
|
3750
|
+
return { field: "dateRanges.maxStay", kind: "min-above-max", min, max, message };
|
|
3751
|
+
}
|
|
3752
|
+
function noticeAboveAdvance({
|
|
3753
|
+
minNotice,
|
|
3754
|
+
maxAdvance
|
|
3755
|
+
}) {
|
|
3756
|
+
if (!minNotice || !maxAdvance || !minLongerThanMax(minNotice, maxAdvance)) return void 0;
|
|
3757
|
+
const message = `window.maxAdvance: ${durationText(maxAdvance)} ahead can be shorter than the notice of ${durationText(minNotice)}`;
|
|
3758
|
+
return { field: "window.maxAdvance", kind: "min-above-max", min: minNotice, max: maxAdvance, message };
|
|
3759
|
+
}
|
|
3760
|
+
|
|
3701
3761
|
// booking/plan.ts
|
|
3702
3762
|
var DEFAULT_HOLD_MINUTES = 10;
|
|
3703
3763
|
function planWindow({ rule, timeZone, presence, startAt, endAt, now, busy = [] }) {
|
|
@@ -3705,17 +3765,623 @@ function planWindow({ rule, timeZone, presence, startAt, endAt, now, busy = [] }
|
|
|
3705
3765
|
(slot) => withinPresence(slot, timeZone, presence)
|
|
3706
3766
|
);
|
|
3707
3767
|
const slots = slotsForWindow(grid, startAt, endAt);
|
|
3708
|
-
const
|
|
3709
|
-
|
|
3710
|
-
|
|
3768
|
+
const last = slots[slots.length - 1];
|
|
3769
|
+
const parts = bookingRuleParts(rule);
|
|
3770
|
+
const wholePeriods = parts.method === "date-ranges" && parts.wholePeriods;
|
|
3771
|
+
const periods = wholePeriods ? wholePeriodsIn(rule.ratePeriod, timeZone, startAt, last.endAt, slots.length) : slots.length;
|
|
3772
|
+
checkStay(parts, timeZone, startAt, periods);
|
|
3773
|
+
const { buffer } = ruleDurationsOf(rule);
|
|
3774
|
+
const bufferEndAt = rule.ratePeriod === "hour" /* HOUR */ || wholePeriods ? buffer ? addDuration(last.endAt, buffer, timeZone) : new Date(last.endAt.getTime()) : last.bufferEndAt;
|
|
3775
|
+
return { slots, periods, endAt: last.endAt, bufferEndAt };
|
|
3776
|
+
}
|
|
3777
|
+
function checkStay(parts, timeZone, startAt, periods) {
|
|
3778
|
+
if (parts.method !== "date-ranges") return;
|
|
3779
|
+
const stay = { count: periods, unit: periodUnitOf(parts) };
|
|
3780
|
+
const length = (duration) => stayLength(parts.ratePeriod, duration, startAt, timeZone);
|
|
3781
|
+
const { minStay, maxStay } = parts;
|
|
3782
|
+
if (minStay && length(stay) < length(minStay)) {
|
|
3783
|
+
throw new BookingRulesError(
|
|
3784
|
+
`These rules ask for a stay of at least ${durationText(minStay)}, got ${durationText(stay)}`
|
|
3785
|
+
);
|
|
3711
3786
|
}
|
|
3712
|
-
if (maxStay &&
|
|
3713
|
-
throw new BookingRulesError(
|
|
3787
|
+
if (maxStay && length(stay) > length(maxStay)) {
|
|
3788
|
+
throw new BookingRulesError(
|
|
3789
|
+
`These rules allow a stay of at most ${durationText(maxStay)}, got ${durationText(stay)}`
|
|
3790
|
+
);
|
|
3714
3791
|
}
|
|
3715
|
-
const last = slots[slots.length - 1];
|
|
3716
|
-
return { slots, endAt: last.endAt, bufferEndAt: last.bufferEndAt };
|
|
3717
3792
|
}
|
|
3718
|
-
|
|
3793
|
+
function stayLength(ratePeriod, duration, startAt, timeZone) {
|
|
3794
|
+
if (ratePeriod === "hour" /* HOUR */) return addDuration(startAt, duration, timeZone).getTime();
|
|
3795
|
+
const { count, unit: unit2 } = duration;
|
|
3796
|
+
if (unit2 === "day" /* DAY */ || unit2 === "night" /* NIGHT */) return count;
|
|
3797
|
+
if (unit2 === "week" /* WEEK */) return 7 * count;
|
|
3798
|
+
if (unit2 !== "month" /* MONTH */) {
|
|
3799
|
+
throw new BookingRulesError(`Booking rules: a ${ratePeriod} stay cannot be counted in ${unit2}s`);
|
|
3800
|
+
}
|
|
3801
|
+
const { year, month, day } = zonedParts(startAt, timeZone);
|
|
3802
|
+
const [toYear, toMonth, toDay] = sameDayMonthsLaterDate(year, month, day, count);
|
|
3803
|
+
return (Date.UTC(toYear, toMonth - 1, toDay) - Date.UTC(year, month - 1, day)) / 864e5;
|
|
3804
|
+
}
|
|
3805
|
+
function windowForPeriods({ rule, timeZone, startAt, periods, now }) {
|
|
3806
|
+
if (!Number.isInteger(periods) || periods < 1) {
|
|
3807
|
+
throw new BookingRulesError(`periods must be a whole number of at least 1, got ${String(periods)}`);
|
|
3808
|
+
}
|
|
3809
|
+
const parts = bookingRuleParts(rule);
|
|
3810
|
+
checkStay(parts, timeZone, startAt, periods);
|
|
3811
|
+
const wholePeriods = parts.method === "date-ranges" && parts.wholePeriods;
|
|
3812
|
+
const monthsEnd = wholePeriods && rule.ratePeriod === "month" /* MONTH */ ? sameDayMonthsLater(startAt, periods, timeZone) : null;
|
|
3813
|
+
const cells = wholePeriods && rule.ratePeriod === "week" /* WEEK */ ? periods * 7 : periods;
|
|
3814
|
+
const lastDay = monthsEnd ?? new Date(startAt.getTime() + (cells + 2) * 1440 * 6e4);
|
|
3815
|
+
const grid = buildGrid(
|
|
3816
|
+
rule,
|
|
3817
|
+
timeZone,
|
|
3818
|
+
startAt,
|
|
3819
|
+
new Date(Math.min(lastDay.getTime() + 1440 * 6e4, startAt.getTime() + 366 * 1440 * 6e4)),
|
|
3820
|
+
now
|
|
3821
|
+
);
|
|
3822
|
+
const startIndex = grid.findIndex((cell) => cell.startAt.getTime() === startAt.getTime());
|
|
3823
|
+
if (startIndex < 0)
|
|
3824
|
+
throw new BookingRulesError(`${startAt.toISOString()} is not a bookable slot start for these rules`);
|
|
3825
|
+
const endAt = monthsEnd ?? grid[startIndex + cells - 1]?.endAt;
|
|
3826
|
+
if (!endAt)
|
|
3827
|
+
throw new BookingRulesError(`${periods} period(s) from ${startAt.toISOString()} run past the bookable grid`);
|
|
3828
|
+
slotsForWindow(grid, startAt, endAt);
|
|
3829
|
+
return { startAt, endAt };
|
|
3830
|
+
}
|
|
3831
|
+
function sameDayMonthsLater(startAt, months, timeZone) {
|
|
3832
|
+
const { year, month, day } = zonedParts(startAt, timeZone);
|
|
3833
|
+
return instantFromLocal(...sameDayMonthsLaterDate(year, month, day, months), 0, timeZone);
|
|
3834
|
+
}
|
|
3835
|
+
function wholePeriodsIn(ratePeriod, timeZone, startAt, endAt, days) {
|
|
3836
|
+
if (ratePeriod === "week" /* WEEK */) {
|
|
3837
|
+
if (days % 7 !== 0) throw new BookingRulesError(`Book whole weeks: ${days} day(s) is not a number of weeks`);
|
|
3838
|
+
return days / 7;
|
|
3839
|
+
}
|
|
3840
|
+
if (ratePeriod !== "month" /* MONTH */) {
|
|
3841
|
+
throw new BookingRulesError("Whole periods are for week and month rules only");
|
|
3842
|
+
}
|
|
3843
|
+
const start = zonedParts(startAt, timeZone);
|
|
3844
|
+
const end = zonedParts(endAt, timeZone);
|
|
3845
|
+
const months = (end.year - start.year) * 12 + (end.month - start.month);
|
|
3846
|
+
const lastDayOfEndMonth = new Date(Date.UTC(end.year, end.month, 0)).getUTCDate();
|
|
3847
|
+
if (months < 1 || end.day !== Math.min(start.day, lastDayOfEndMonth)) {
|
|
3848
|
+
throw new BookingRulesError("Book whole months: the stay must end on the same day of a later month");
|
|
3849
|
+
}
|
|
3850
|
+
return months;
|
|
3851
|
+
}
|
|
3852
|
+
function courseSessions(rule, timeZone) {
|
|
3853
|
+
const { bookableFrom, bookableTo } = rule.window;
|
|
3854
|
+
if (!bookableFrom || !bookableTo) throw new BookingRulesError("A course needs both course dates");
|
|
3855
|
+
const sessions = sessionCells(rule, timeZone, new Date(bookableFrom), new Date(bookableTo)).sort(
|
|
3856
|
+
(first, second) => +first.startAt - +second.startAt
|
|
3857
|
+
);
|
|
3858
|
+
if (sessions.length === 0) throw new BookingRulesError("These course dates hold no session");
|
|
3859
|
+
return sessions;
|
|
3860
|
+
}
|
|
3861
|
+
var courseStarted = (rule, sessions, now) => sessions[0].startAt.getTime() < now.getTime() + (rule.window.minNoticeMinutes ?? 0) * 6e4;
|
|
3862
|
+
function courseStartedInZone(rule, sessions, now, timeZone) {
|
|
3863
|
+
const { minNotice } = ruleDurationsOf(rule);
|
|
3864
|
+
return sessions[0].startAt.getTime() < (minNotice ? addDuration(now, minNotice, timeZone) : now).getTime();
|
|
3865
|
+
}
|
|
3866
|
+
function planCourse({ rule, timeZone, presence, now }) {
|
|
3867
|
+
const sessions = courseSessions(rule, timeZone);
|
|
3868
|
+
if (!sessions.every((session) => withinPresence(session, timeZone, presence))) {
|
|
3869
|
+
throw new BookingRulesError("This resource is not there for every session of the course");
|
|
3870
|
+
}
|
|
3871
|
+
if (courseStartedInZone(rule, sessions, now, timeZone))
|
|
3872
|
+
throw new BookingRulesError("This course has already started");
|
|
3873
|
+
return sessions;
|
|
3874
|
+
}
|
|
3875
|
+
function holdExpiresAt(rule, now) {
|
|
3876
|
+
return addClockDuration(now, holdOf(rule) ?? DEFAULT_HOLD);
|
|
3877
|
+
}
|
|
3878
|
+
var DEFAULT_HOLD = { count: DEFAULT_HOLD_MINUTES, unit: "minute" /* MINUTE */ };
|
|
3879
|
+
function holdOf(rule) {
|
|
3880
|
+
const { hold } = ruleDurationsOf(rule);
|
|
3881
|
+
if (!hold) return void 0;
|
|
3882
|
+
const field = rule.window.hold ? "window.hold" : "window.holdMinutes";
|
|
3883
|
+
if (!Number.isInteger(hold.count)) {
|
|
3884
|
+
throw new BookingRulesError(`${field} must be a whole number of ${hold.unit}s, got ${hold.count}`);
|
|
3885
|
+
}
|
|
3886
|
+
if (hold.count < 1) throw new BookingRulesError(`${field} must be at least 1 ${hold.unit}, got ${hold.count}`);
|
|
3887
|
+
return hold;
|
|
3888
|
+
}
|
|
3889
|
+
function checkQtyPerBooking(rule, qty) {
|
|
3890
|
+
const max = rule.slot.maxQtyPerBooking;
|
|
3891
|
+
if (max !== void 0 && qty > max) {
|
|
3892
|
+
throw new BookingRulesError(`These rules allow at most ${max} per booking, got ${qty}`);
|
|
3893
|
+
}
|
|
3894
|
+
}
|
|
3895
|
+
function checkPartySize(rule, partySize) {
|
|
3896
|
+
const max = rule.slot.maxPartySize;
|
|
3897
|
+
if (max !== void 0 && partySize > max) {
|
|
3898
|
+
throw new BookingRulesError(`These rules allow at most ${max} people per booking, got ${partySize}`);
|
|
3899
|
+
}
|
|
3900
|
+
}
|
|
3901
|
+
function bookingMethodOf(rule) {
|
|
3902
|
+
if (rule.ratePeriod === "slot" /* SLOT */) return "time-slots";
|
|
3903
|
+
if (rule.ratePeriod === "session" /* SESSION */) return "sessions";
|
|
3904
|
+
return "date-ranges";
|
|
3905
|
+
}
|
|
3906
|
+
var soldAsCourse = (rule) => rule.ratePeriod === "session" /* SESSION */ && rule.sessions?.sold === "course" /* COURSE */;
|
|
3907
|
+
function capacityOf(rule, resource) {
|
|
3908
|
+
if (rule.ratePeriod !== "session" /* SESSION */) return resource.seats ?? 1;
|
|
3909
|
+
if (!rule.sessions) throw new BookingRulesError("Scheduled-session rules need their sessions");
|
|
3910
|
+
return rule.sessions.spots ?? Number.POSITIVE_INFINITY;
|
|
3911
|
+
}
|
|
3912
|
+
function bookingRuleParts(rule) {
|
|
3913
|
+
const method = bookingMethodOf(rule);
|
|
3914
|
+
if (method === "sessions") {
|
|
3915
|
+
if (!rule.sessions) throw new BookingRulesError("Scheduled-session rules need their sessions");
|
|
3916
|
+
return { method, sessions: rule.sessions };
|
|
3917
|
+
}
|
|
3918
|
+
if (method === "time-slots") {
|
|
3919
|
+
const timeSlots = rule.timeSlots ? newTimeSlotsOf(rule.timeSlots) : oldTimeSlotsOf(rule);
|
|
3920
|
+
const { count } = timeSlots.length;
|
|
3921
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
3922
|
+
const field = rule.timeSlots ? "timeSlots.length" : "slot.lengthMinutes";
|
|
3923
|
+
throw new BookingRulesError(`Booking rules: ${field} must be a whole number of at least 1, got ${count}`);
|
|
3924
|
+
}
|
|
3925
|
+
return { method, ...timeSlots };
|
|
3926
|
+
}
|
|
3927
|
+
if (!rule.dateRanges) return { method, ratePeriod: rule.ratePeriod, ...oldDateRangesOf(rule) };
|
|
3928
|
+
const wholePeriods = rule.ratePeriod === "week" /* WEEK */ || rule.ratePeriod === "month" /* MONTH */;
|
|
3929
|
+
return { method, ratePeriod: rule.ratePeriod, wholePeriods, ...rule.dateRanges };
|
|
3930
|
+
}
|
|
3931
|
+
function newTimeSlotsOf({ length, weeklyHours }) {
|
|
3932
|
+
if (!weeklyHours?.length) throw new BookingRulesError("Booking rules: timeSlots.weeklyHours needs at least one day");
|
|
3933
|
+
return { length, weeklyHours };
|
|
3934
|
+
}
|
|
3935
|
+
function oldTimeSlotsOf({ slot, availability }) {
|
|
3936
|
+
if (slot.lengthMinutes == null) throw new BookingRulesError("Time-slot rules need their slot length");
|
|
3937
|
+
return {
|
|
3938
|
+
length: { count: slot.lengthMinutes, unit: "minute" /* MINUTE */ },
|
|
3939
|
+
weeklyHours: availability.weeklyHours ?? []
|
|
3940
|
+
};
|
|
3941
|
+
}
|
|
3942
|
+
function oldDateRangesOf(rule) {
|
|
3943
|
+
const { slot, availability } = rule;
|
|
3944
|
+
const wholePeriods = slot.wholePeriods ?? false;
|
|
3945
|
+
const stay = (count) => count ? { count, unit: periodUnitOf({ ratePeriod: rule.ratePeriod, wholePeriods }) } : void 0;
|
|
3946
|
+
return {
|
|
3947
|
+
// Old `[]` meant "open every day" — the new form says that by having no rows.
|
|
3948
|
+
weeklyHours: availability.weeklyHours?.length ? availability.weeklyHours : void 0,
|
|
3949
|
+
arrivalDays: availability.arrivalDays,
|
|
3950
|
+
checkInTime: rule.checkInTime,
|
|
3951
|
+
checkOutTime: rule.checkOutTime,
|
|
3952
|
+
checkTimesByWeekday: rule.checkTimesByWeekday,
|
|
3953
|
+
minStay: stay(slot.minStay),
|
|
3954
|
+
maxStay: stay(slot.maxStay),
|
|
3955
|
+
wholePeriods
|
|
3956
|
+
};
|
|
3957
|
+
}
|
|
3958
|
+
function periodUnitOf({
|
|
3959
|
+
ratePeriod,
|
|
3960
|
+
wholePeriods
|
|
3961
|
+
}) {
|
|
3962
|
+
const units = {
|
|
3963
|
+
["hour" /* HOUR */]: "hour" /* HOUR */,
|
|
3964
|
+
["night" /* NIGHT */]: "night" /* NIGHT */,
|
|
3965
|
+
["day" /* DAY */]: "day" /* DAY */,
|
|
3966
|
+
["week" /* WEEK */]: wholePeriods ? "week" /* WEEK */ : "day" /* DAY */,
|
|
3967
|
+
["month" /* MONTH */]: wholePeriods ? "month" /* MONTH */ : "day" /* DAY */
|
|
3968
|
+
};
|
|
3969
|
+
const unit2 = units[ratePeriod];
|
|
3970
|
+
if (!unit2) throw new BookingRulesError(`Booking rules: "${ratePeriod}" is not a date-range rate period`);
|
|
3971
|
+
return unit2;
|
|
3972
|
+
}
|
|
3973
|
+
var OLD_BUFFER = {
|
|
3974
|
+
path: "slot.bufferMinutes",
|
|
3975
|
+
home: {
|
|
3976
|
+
"time-slots": "slot.buffer",
|
|
3977
|
+
sessions: "slot.buffer",
|
|
3978
|
+
// The old night grid never read it: housekeeping is the gap between check-out and check-in (O-1).
|
|
3979
|
+
"date-ranges": ({ ratePeriod }) => ratePeriod === "night" /* NIGHT */ ? { unread: "nights clean between check-out and check-in" } : "slot.buffer"
|
|
3980
|
+
}
|
|
3981
|
+
};
|
|
3982
|
+
var OLD_FIELDS = [
|
|
3983
|
+
{
|
|
3984
|
+
path: "slot.lengthMinutes",
|
|
3985
|
+
home: {
|
|
3986
|
+
"time-slots": "timeSlots.length",
|
|
3987
|
+
sessions: { unread: "placeholder" },
|
|
3988
|
+
"date-ranges": { unread: "placeholder" }
|
|
3989
|
+
}
|
|
3990
|
+
},
|
|
3991
|
+
OLD_BUFFER,
|
|
3992
|
+
{
|
|
3993
|
+
path: "slot.minStay",
|
|
3994
|
+
home: {
|
|
3995
|
+
"time-slots": { dropped: "time-slots rules have no stay limits" },
|
|
3996
|
+
sessions: { dropped: "sessions rules have no stay limits" },
|
|
3997
|
+
"date-ranges": "dateRanges.minStay"
|
|
3998
|
+
}
|
|
3999
|
+
},
|
|
4000
|
+
{
|
|
4001
|
+
path: "slot.maxStay",
|
|
4002
|
+
home: {
|
|
4003
|
+
"time-slots": { dropped: "time-slots rules have no stay limits" },
|
|
4004
|
+
sessions: { dropped: "sessions rules have no stay limits" },
|
|
4005
|
+
"date-ranges": "dateRanges.maxStay"
|
|
4006
|
+
}
|
|
4007
|
+
},
|
|
4008
|
+
{
|
|
4009
|
+
path: "window.holdMinutes",
|
|
4010
|
+
home: { "time-slots": "window.hold", sessions: "window.hold", "date-ranges": "window.hold" }
|
|
4011
|
+
},
|
|
4012
|
+
{
|
|
4013
|
+
path: "window.minNoticeMinutes",
|
|
4014
|
+
home: { "time-slots": "window.minNotice", sessions: "window.minNotice", "date-ranges": "window.minNotice" }
|
|
4015
|
+
},
|
|
4016
|
+
// The two old max-advance fields are one fact, as the old readers took it: minutes, else days × 1440.
|
|
4017
|
+
...["window.maxAdvanceMinutes", "window.maxAdvanceDays"].map((path) => ({
|
|
4018
|
+
path,
|
|
4019
|
+
carried: maxAdvanceMinutesOf,
|
|
4020
|
+
home: { "time-slots": "window.maxAdvance", sessions: "window.maxAdvance", "date-ranges": "window.maxAdvance" }
|
|
4021
|
+
})),
|
|
4022
|
+
{
|
|
4023
|
+
path: "availability.weeklyHours",
|
|
4024
|
+
home: {
|
|
4025
|
+
"time-slots": "timeSlots.weeklyHours",
|
|
4026
|
+
sessions: { unread: "sessions read their timetable" },
|
|
4027
|
+
"date-ranges": "dateRanges.weeklyHours"
|
|
4028
|
+
}
|
|
4029
|
+
},
|
|
4030
|
+
...["availability.arrivalDays", "checkInTime", "checkOutTime", "checkTimesByWeekday"].map((path) => ({
|
|
4031
|
+
path,
|
|
4032
|
+
home: {
|
|
4033
|
+
"time-slots": { unread: "date ranges only" },
|
|
4034
|
+
sessions: { unread: "date ranges only" },
|
|
4035
|
+
"date-ranges": `dateRanges.${path.replace("availability.", "")}`
|
|
4036
|
+
}
|
|
4037
|
+
}))
|
|
4038
|
+
];
|
|
4039
|
+
var homeOf = (field, rule) => {
|
|
4040
|
+
const home = field.home[bookingMethodOf(rule)];
|
|
4041
|
+
return typeof home === "function" ? home(rule) : home;
|
|
4042
|
+
};
|
|
4043
|
+
var carriedValueOf = (rule, field) => field.carried ? field.carried(rule) : valueAt(rule, field.path);
|
|
4044
|
+
var carries = (value) => value != null && value !== 0 && !(Array.isArray(value) && value.length === 0);
|
|
4045
|
+
function ruleInNewShape(rule) {
|
|
4046
|
+
const set = { ...typePartInNewShape(rule), ...durationsInNewShape(rule) };
|
|
4047
|
+
const unset = [...OLD_FIELDS.map(({ path }) => path), "slot.wholePeriods"].filter(
|
|
4048
|
+
(path) => valueAt(rule, path) !== void 0
|
|
4049
|
+
);
|
|
4050
|
+
const written = writtenRule(rule, set, unset);
|
|
4051
|
+
bookingRuleParts(written);
|
|
4052
|
+
refuseLostValues(rule, written);
|
|
4053
|
+
const changesBehaviour = sellsOneDayOfWeekOrMonth(rule) ? "whole-periods" : void 0;
|
|
4054
|
+
const notes = [
|
|
4055
|
+
...droppedFieldNotes(rule, written),
|
|
4056
|
+
...changesBehaviour ? [`${rule.ratePeriod} rule now sells whole ${rule.ratePeriod}s, was one day per booking`] : []
|
|
4057
|
+
];
|
|
4058
|
+
return { set, unset, problems: durationProblems(written), notes, ...changesBehaviour && { changesBehaviour } };
|
|
4059
|
+
}
|
|
4060
|
+
function typePartInNewShape(rule) {
|
|
4061
|
+
const method = bookingMethodOf(rule);
|
|
4062
|
+
if (method === "sessions") return {};
|
|
4063
|
+
if (method === "time-slots") return { timeSlots: newTimeSlotsOf(rule.timeSlots ?? oldTimeSlotsOf(rule)) };
|
|
4064
|
+
if (rule.dateRanges) return { dateRanges: rule.dateRanges };
|
|
4065
|
+
const dateRanges = oldDateRangesOf(rule);
|
|
4066
|
+
delete dateRanges.wholePeriods;
|
|
4067
|
+
return { dateRanges: definedOnly(dateRanges) };
|
|
4068
|
+
}
|
|
4069
|
+
function durationsInNewShape(rule) {
|
|
4070
|
+
const { buffer, minNotice } = ruleDurationsOf(rule);
|
|
4071
|
+
return definedOnly({
|
|
4072
|
+
"slot.buffer": rule.slot.buffer ?? (typeof homeOf(OLD_BUFFER, rule) === "string" ? buffer : void 0),
|
|
4073
|
+
"window.hold": holdOf(rule) ?? DEFAULT_HOLD,
|
|
4074
|
+
"window.minNotice": minNotice,
|
|
4075
|
+
"window.maxAdvance": ruleDurationsOf(rule).maxAdvance
|
|
4076
|
+
});
|
|
4077
|
+
}
|
|
4078
|
+
var sellsOneDayOfWeekOrMonth = ({ ratePeriod, slot, dateRanges }) => !dateRanges && (ratePeriod === "week" /* WEEK */ || ratePeriod === "month" /* MONTH */) && !slot.wholePeriods;
|
|
4079
|
+
function droppedFieldNotes(rule, written) {
|
|
4080
|
+
const weekOrMonth = rule.ratePeriod === "week" /* WEEK */ || rule.ratePeriod === "month" /* MONTH */;
|
|
4081
|
+
return [
|
|
4082
|
+
...OLD_FIELDS.flatMap((field) => {
|
|
4083
|
+
const home = homeOf(field, rule);
|
|
4084
|
+
const dropped = typeof home === "object" && "dropped" in home && carries(carriedValueOf(rule, field));
|
|
4085
|
+
return dropped ? [`${field.path} is dropped: ${home.dropped}`] : [];
|
|
4086
|
+
}),
|
|
4087
|
+
...rule.slot.wholePeriods && !weekOrMonth ? ["slot.wholePeriods is dropped: only week and month rules have it"] : [],
|
|
4088
|
+
...rule.ratePeriod === "hour" /* HOUR */ && !written.dateRanges?.weeklyHours?.length ? ["hour rule has no opening hours: nothing can be booked"] : [],
|
|
4089
|
+
...rule.window.holdMinutes == null && !rule.window.hold ? [`window.hold is set to the default ${DEFAULT_HOLD_MINUTES} minutes`] : []
|
|
4090
|
+
];
|
|
4091
|
+
}
|
|
4092
|
+
function writtenRule(rule, set, unset) {
|
|
4093
|
+
const written = {
|
|
4094
|
+
...rule,
|
|
4095
|
+
slot: { ...rule.slot },
|
|
4096
|
+
window: { ...rule.window },
|
|
4097
|
+
availability: { ...rule.availability }
|
|
4098
|
+
};
|
|
4099
|
+
for (const path of unset) {
|
|
4100
|
+
const [head, leaf] = path.split(".");
|
|
4101
|
+
if (leaf) delete written[head][leaf];
|
|
4102
|
+
else delete written[head];
|
|
4103
|
+
}
|
|
4104
|
+
for (const [path, value] of Object.entries(set)) {
|
|
4105
|
+
const [head, leaf] = path.split(".");
|
|
4106
|
+
if (leaf) written[head][leaf] = value;
|
|
4107
|
+
else written[head] = value;
|
|
4108
|
+
}
|
|
4109
|
+
return written;
|
|
4110
|
+
}
|
|
4111
|
+
function refuseLostValues(rule, written) {
|
|
4112
|
+
for (const field of OLD_FIELDS) {
|
|
4113
|
+
const home = homeOf(field, rule);
|
|
4114
|
+
if (typeof home === "string" && carries(carriedValueOf(rule, field)) && valueAt(written, home) == null) {
|
|
4115
|
+
throw new BookingRulesError(`Booking rules: ${home} is missing \u2014 the rule's old value would be lost`);
|
|
4116
|
+
}
|
|
4117
|
+
}
|
|
4118
|
+
}
|
|
4119
|
+
var definedOnly = (value) => Object.fromEntries(Object.entries(value).filter(([, entry]) => entry != null));
|
|
4120
|
+
var valueAt = (rule, path) => path.split(".").reduce((node, key) => node?.[key], rule);
|
|
4121
|
+
|
|
4122
|
+
// booking/grid.ts
|
|
4123
|
+
var MINUTE3 = 6e4;
|
|
4124
|
+
var HOUR_MINUTES = 60;
|
|
4125
|
+
var MAX_RANGE_DAYS = 366;
|
|
4126
|
+
var asTime = (value, field) => {
|
|
4127
|
+
if (value == null) return void 0;
|
|
4128
|
+
const time = new Date(value).getTime();
|
|
4129
|
+
if (Number.isNaN(time)) throw new BookingRulesError(`Booking rules: ${field} is not a date, got "${String(value)}"`);
|
|
4130
|
+
return time;
|
|
4131
|
+
};
|
|
4132
|
+
function parseHhMm(value, field) {
|
|
4133
|
+
const [hours, minutes] = value.split(":").map(Number);
|
|
4134
|
+
if (!Number.isInteger(hours) || !Number.isInteger(minutes)) {
|
|
4135
|
+
throw new BookingRulesError(`Booking rules: ${field} must be "HH:mm", got "${value}"`);
|
|
4136
|
+
}
|
|
4137
|
+
return hours * 60 + minutes;
|
|
4138
|
+
}
|
|
4139
|
+
function windowEndsAt(rule, now) {
|
|
4140
|
+
const maxAdvanceMinutes = maxAdvanceMinutesOf(rule);
|
|
4141
|
+
const ends = Math.min(
|
|
4142
|
+
maxAdvanceMinutes ? now.getTime() + maxAdvanceMinutes * MINUTE3 : Number.POSITIVE_INFINITY,
|
|
4143
|
+
asTime(rule.window.bookableTo, "window.bookableTo") ?? Number.POSITIVE_INFINITY
|
|
4144
|
+
);
|
|
4145
|
+
return Number.isFinite(ends) ? new Date(ends) : null;
|
|
4146
|
+
}
|
|
4147
|
+
function windowEndsAtInZone(rule, now, timeZone) {
|
|
4148
|
+
const { maxAdvance } = ruleDurationsOf(rule);
|
|
4149
|
+
const ends = Math.min(
|
|
4150
|
+
maxAdvance ? addInZone(now, maxAdvance, timeZone, "window.maxAdvance").getTime() : Number.POSITIVE_INFINITY,
|
|
4151
|
+
asTime(rule.window.bookableTo, "window.bookableTo") ?? Number.POSITIVE_INFINITY
|
|
4152
|
+
);
|
|
4153
|
+
return Number.isFinite(ends) ? new Date(ends) : null;
|
|
4154
|
+
}
|
|
4155
|
+
function addInZone(from, duration, timeZone, field) {
|
|
4156
|
+
if (timeZone) return addDuration(from, duration, timeZone);
|
|
4157
|
+
if (duration.unit === "minute" /* MINUTE */ || duration.unit === "hour" /* HOUR */) {
|
|
4158
|
+
return addClockDuration(from, duration);
|
|
4159
|
+
}
|
|
4160
|
+
throw new BookingRulesError(`Booking rules: ${field} in ${duration.unit}s needs the shop's time zone`);
|
|
4161
|
+
}
|
|
4162
|
+
function maxAdvanceMinutesOf(rule) {
|
|
4163
|
+
const { maxAdvanceMinutes, maxAdvanceDays } = rule.window;
|
|
4164
|
+
return maxAdvanceMinutes ?? (maxAdvanceDays ?? 0) * 1440;
|
|
4165
|
+
}
|
|
4166
|
+
var asMinutes = (minutes) => minutes == null ? void 0 : { count: minutes, unit: "minute" /* MINUTE */ };
|
|
4167
|
+
var inMinutes = (minutes) => minutes === 0 ? void 0 : asMinutes(minutes);
|
|
4168
|
+
function notNegative(duration, field) {
|
|
4169
|
+
if (duration && !(Number.isFinite(duration.count) && duration.count >= 0)) {
|
|
4170
|
+
throw new BookingRulesError(`Booking rules: ${field} must be a number of at least 0, got ${duration.count}`);
|
|
4171
|
+
}
|
|
4172
|
+
return duration;
|
|
4173
|
+
}
|
|
4174
|
+
function ruleDurationsOf(rule) {
|
|
4175
|
+
const { slot, window } = rule;
|
|
4176
|
+
return {
|
|
4177
|
+
buffer: notNegative(
|
|
4178
|
+
slot.buffer ?? inMinutes(slot.bufferMinutes),
|
|
4179
|
+
slot.buffer ? "slot.buffer" : "slot.bufferMinutes"
|
|
4180
|
+
),
|
|
4181
|
+
hold: window.hold ?? asMinutes(window.holdMinutes),
|
|
4182
|
+
minNotice: notNegative(
|
|
4183
|
+
window.minNotice ?? inMinutes(window.minNoticeMinutes),
|
|
4184
|
+
window.minNotice ? "window.minNotice" : "window.minNoticeMinutes"
|
|
4185
|
+
),
|
|
4186
|
+
maxAdvance: window.maxAdvance ?? oldMaxAdvanceOf(window)
|
|
4187
|
+
};
|
|
4188
|
+
}
|
|
4189
|
+
function oldMaxAdvanceOf({
|
|
4190
|
+
maxAdvanceMinutes,
|
|
4191
|
+
maxAdvanceDays
|
|
4192
|
+
}) {
|
|
4193
|
+
const days = maxAdvanceDays ?? 0;
|
|
4194
|
+
if (maxAdvanceMinutes == null || days > 0 && maxAdvanceMinutes === days * 1440) {
|
|
4195
|
+
return days > 0 ? { count: days, unit: "day" /* DAY */ } : void 0;
|
|
4196
|
+
}
|
|
4197
|
+
return maxAdvanceMinutes ? { count: maxAdvanceMinutes, unit: "minute" /* MINUTE */ } : void 0;
|
|
4198
|
+
}
|
|
4199
|
+
function periodSlot(dayStart, timeZone, parts, buffer, openMinute, closeMinute) {
|
|
4200
|
+
const { ratePeriod } = parts;
|
|
4201
|
+
const at = (minutes) => atZonedMinute(dayStart, minutes, timeZone);
|
|
4202
|
+
if (ratePeriod === "night" /* NIGHT */) {
|
|
4203
|
+
const arrival = zonedParts(dayStart, timeZone).weekday;
|
|
4204
|
+
const departure = (arrival + 1) % 7;
|
|
4205
|
+
return {
|
|
4206
|
+
startAt: at(checkTimesOf(parts, arrival).checkIn),
|
|
4207
|
+
endAt: at(1440 + checkTimesOf(parts, departure).checkOut),
|
|
4208
|
+
// Housekeeping sits between check-out and the next check-in: it costs no night.
|
|
4209
|
+
bufferEndAt: at(1440 + checkTimesOf(parts, departure).checkIn),
|
|
4210
|
+
canStart: true
|
|
4211
|
+
};
|
|
4212
|
+
}
|
|
4213
|
+
const from = ratePeriod === "day" /* DAY */ ? openMinute ?? 0 : 0;
|
|
4214
|
+
const to = ratePeriod === "day" /* DAY */ ? closeMinute ?? 1440 : 1440;
|
|
4215
|
+
const turnaround = parts.wholePeriods ? void 0 : buffer;
|
|
4216
|
+
return {
|
|
4217
|
+
startAt: at(from),
|
|
4218
|
+
endAt: at(to),
|
|
4219
|
+
bufferEndAt: turnaround ? addDuration(at(to), turnaround, timeZone) : at(to),
|
|
4220
|
+
canStart: true
|
|
4221
|
+
};
|
|
4222
|
+
}
|
|
4223
|
+
function checkTimesOf(rule, weekday) {
|
|
4224
|
+
const row = rule.checkTimesByWeekday?.find((entry) => entry.weekday === weekday);
|
|
4225
|
+
const checkInTime = row?.checkInTime ?? rule.checkInTime;
|
|
4226
|
+
const checkOutTime = row?.checkOutTime ?? rule.checkOutTime;
|
|
4227
|
+
if (!checkInTime || !checkOutTime) {
|
|
4228
|
+
throw new BookingRulesError("Overnight booking rules need both checkInTime and checkOutTime");
|
|
4229
|
+
}
|
|
4230
|
+
return { checkIn: parseHhMm(checkInTime, "checkInTime"), checkOut: parseHhMm(checkOutTime, "checkOutTime") };
|
|
4231
|
+
}
|
|
4232
|
+
function freeGaps(openAt, closeAt, busy) {
|
|
4233
|
+
const inside = busy.map(({ startAt, bufferEndAt }) => ({ from: +startAt, to: +bufferEndAt })).filter(({ from, to }) => to > openAt && from < closeAt).sort((first, second) => first.from - second.from);
|
|
4234
|
+
const gaps = [];
|
|
4235
|
+
let cursor = openAt;
|
|
4236
|
+
for (const { from, to } of inside) {
|
|
4237
|
+
if (from > cursor) gaps.push({ from: cursor, to: from, endsAtBooking: true });
|
|
4238
|
+
cursor = Math.max(cursor, to);
|
|
4239
|
+
if (cursor >= closeAt) break;
|
|
4240
|
+
}
|
|
4241
|
+
if (cursor < closeAt) gaps.push({ from: cursor, to: closeAt, endsAtBooking: false });
|
|
4242
|
+
return gaps;
|
|
4243
|
+
}
|
|
4244
|
+
function sessionCells(rule, timeZone, from, to) {
|
|
4245
|
+
if (!rule.sessions) throw new BookingRulesError("Scheduled-session rules need their sessions");
|
|
4246
|
+
const { weekly = [], dates = [] } = rule.sessions;
|
|
4247
|
+
const { buffer } = ruleDurationsOf(rule);
|
|
4248
|
+
const cell = (startAt, endAt) => ({
|
|
4249
|
+
startAt: new Date(startAt),
|
|
4250
|
+
endAt: new Date(endAt),
|
|
4251
|
+
bufferEndAt: buffer ? addDuration(new Date(endAt), buffer, timeZone) : new Date(endAt),
|
|
4252
|
+
canStart: true
|
|
4253
|
+
});
|
|
4254
|
+
const cells = dates.map(({ date, startMinute, endMinute }) => {
|
|
4255
|
+
const [year, month, day] = calendarDate(date);
|
|
4256
|
+
return cell(
|
|
4257
|
+
+instantFromLocal(year, month, day, startMinute, timeZone),
|
|
4258
|
+
+instantFromLocal(year, month, day, endMinute, timeZone)
|
|
4259
|
+
);
|
|
4260
|
+
});
|
|
4261
|
+
const lastDay = startOfZonedDay(to, timeZone).getTime();
|
|
4262
|
+
for (let day = startOfZonedDay(from, timeZone); day.getTime() <= lastDay; day = nextZonedDay(day, timeZone)) {
|
|
4263
|
+
const weekday = zonedParts(day, timeZone).weekday;
|
|
4264
|
+
for (const row of weekly.filter((entry) => entry.weekday === weekday)) {
|
|
4265
|
+
cells.push(cell(+atZonedMinute(day, row.startMinute, timeZone), +atZonedMinute(day, row.endMinute, timeZone)));
|
|
4266
|
+
}
|
|
4267
|
+
}
|
|
4268
|
+
const blocked = offMarketOf(rule);
|
|
4269
|
+
return cells.filter(({ startAt, bufferEndAt }) => {
|
|
4270
|
+
const start = startAt.getTime();
|
|
4271
|
+
return start >= from.getTime() && start <= to.getTime() && !isBlocked(blocked, start, bufferEndAt.getTime());
|
|
4272
|
+
});
|
|
4273
|
+
}
|
|
4274
|
+
function calendarDate(value) {
|
|
4275
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value ?? "");
|
|
4276
|
+
const [year, month, day] = match ? match.slice(1).map(Number) : [];
|
|
4277
|
+
const real = match && new Date(Date.UTC(year, month - 1, day)).toISOString().slice(0, 10) === value;
|
|
4278
|
+
if (!real) throw new BookingRulesError(`Booking rules: "${value}" is not a date (YYYY-MM-DD)`);
|
|
4279
|
+
return [year, month, day];
|
|
4280
|
+
}
|
|
4281
|
+
var offMarketOf = (rule) => (rule.availability.offMarket ?? []).map(({ from: blockFrom, to: blockTo }) => ({
|
|
4282
|
+
from: asTime(blockFrom, "availability.offMarket.from"),
|
|
4283
|
+
to: asTime(blockTo, "availability.offMarket.to")
|
|
4284
|
+
}));
|
|
4285
|
+
var isBlocked = (blocked, start, bufferEnd) => blocked.some(({ from, to }) => start < to && bufferEnd > from);
|
|
4286
|
+
function hourCells(openAt, closeAt) {
|
|
4287
|
+
const cells = [];
|
|
4288
|
+
for (let start = openAt; start + HOUR_MINUTES * MINUTE3 <= closeAt; start += HOUR_MINUTES * MINUTE3) {
|
|
4289
|
+
const endAt = new Date(start + HOUR_MINUTES * MINUTE3);
|
|
4290
|
+
cells.push({ startAt: new Date(start), endAt, bufferEndAt: endAt, canStart: true });
|
|
4291
|
+
}
|
|
4292
|
+
return cells;
|
|
4293
|
+
}
|
|
4294
|
+
function cellsOf(rule, parts, timeZone, from, to, busy) {
|
|
4295
|
+
if (parts.method === "sessions") return sessionCells(rule, timeZone, from, to);
|
|
4296
|
+
const { buffer } = ruleDurationsOf(rule);
|
|
4297
|
+
const cells = [];
|
|
4298
|
+
const lastDay = startOfZonedDay(to, timeZone).getTime();
|
|
4299
|
+
for (let day = startOfZonedDay(from, timeZone); day.getTime() <= lastDay; day = nextZonedDay(day, timeZone)) {
|
|
4300
|
+
cells.push(
|
|
4301
|
+
...parts.method === "time-slots" ? timeSlotCells(parts, buffer, day, timeZone, busy) : periodCells(parts, buffer, day, timeZone)
|
|
4302
|
+
);
|
|
4303
|
+
}
|
|
4304
|
+
return cells;
|
|
4305
|
+
}
|
|
4306
|
+
function timeSlotCells(parts, buffer, dayStart, timeZone, busy) {
|
|
4307
|
+
const weekday = zonedParts(dayStart, timeZone).weekday;
|
|
4308
|
+
return parts.weeklyHours.filter((entry) => entry.weekday === weekday).flatMap(
|
|
4309
|
+
(row) => freeGaps(
|
|
4310
|
+
+atZonedMinute(dayStart, row.openMinute, timeZone),
|
|
4311
|
+
+atZonedMinute(dayStart, row.closeMinute, timeZone),
|
|
4312
|
+
busy
|
|
4313
|
+
).flatMap((gap) => cellsInGap(gap, parts.length, buffer, timeZone))
|
|
4314
|
+
);
|
|
4315
|
+
}
|
|
4316
|
+
function cellsInGap(gap, length, buffer, timeZone) {
|
|
4317
|
+
const cells = [];
|
|
4318
|
+
for (let start = gap.from; ; ) {
|
|
4319
|
+
const endAt = addDuration(new Date(start), length, timeZone);
|
|
4320
|
+
const bufferEndAt = buffer ? addDuration(endAt, buffer, timeZone) : new Date(endAt.getTime());
|
|
4321
|
+
if ((gap.endsAtBooking ? bufferEndAt : endAt).getTime() > gap.to) return cells;
|
|
4322
|
+
cells.push({ startAt: new Date(start), endAt, bufferEndAt, canStart: true });
|
|
4323
|
+
start = bufferEndAt.getTime();
|
|
4324
|
+
}
|
|
4325
|
+
}
|
|
4326
|
+
function periodCells(parts, buffer, dayStart, timeZone) {
|
|
4327
|
+
const weekday = zonedParts(dayStart, timeZone).weekday;
|
|
4328
|
+
const rows = (parts.weeklyHours ?? []).filter((entry) => entry.weekday === weekday);
|
|
4329
|
+
if (parts.ratePeriod === "hour" /* HOUR */) {
|
|
4330
|
+
return rows.flatMap(
|
|
4331
|
+
(row) => hourCells(
|
|
4332
|
+
+atZonedMinute(dayStart, row.openMinute, timeZone),
|
|
4333
|
+
+atZonedMinute(dayStart, row.closeMinute, timeZone)
|
|
4334
|
+
)
|
|
4335
|
+
);
|
|
4336
|
+
}
|
|
4337
|
+
if (parts.weeklyHours?.length && rows.length === 0) return [];
|
|
4338
|
+
const built = periodSlot(dayStart, timeZone, parts, buffer, rows[0]?.openMinute, rows[0]?.closeMinute);
|
|
4339
|
+
return [{ ...built, canStart: !parts.arrivalDays?.length || parts.arrivalDays.includes(weekday) }];
|
|
4340
|
+
}
|
|
4341
|
+
function buildGrid(rule, timeZone, from, to, now, busy = []) {
|
|
4342
|
+
if (to.getTime() - from.getTime() > MAX_RANGE_DAYS * 1440 * MINUTE3) {
|
|
4343
|
+
throw new BookingRulesError(`A booking range may span at most ${MAX_RANGE_DAYS} days`);
|
|
4344
|
+
}
|
|
4345
|
+
const { availability, window } = rule;
|
|
4346
|
+
const { minNotice } = ruleDurationsOf(rule);
|
|
4347
|
+
const notBefore = Math.max(
|
|
4348
|
+
(minNotice ? addDuration(now, minNotice, timeZone) : now).getTime(),
|
|
4349
|
+
asTime(window.bookableFrom, "window.bookableFrom") ?? 0,
|
|
4350
|
+
asTime(availability.availableFrom, "availability.availableFrom") ?? 0
|
|
4351
|
+
);
|
|
4352
|
+
const notAfter = windowEndsAtInZone(rule, now, timeZone)?.getTime() ?? Number.POSITIVE_INFINITY;
|
|
4353
|
+
const blocked = offMarketOf(rule);
|
|
4354
|
+
const slots = cellsOf(rule, bookingRuleParts(rule), timeZone, from, to, busy);
|
|
4355
|
+
const askedFrom = from.getTime();
|
|
4356
|
+
const askedTo = to.getTime();
|
|
4357
|
+
return slots.filter(({ startAt, bufferEndAt }) => {
|
|
4358
|
+
const start = startAt.getTime();
|
|
4359
|
+
if (start < askedFrom || start >= askedTo) return false;
|
|
4360
|
+
if (start < notBefore || start > notAfter) return false;
|
|
4361
|
+
return !isBlocked(blocked, start, bufferEndAt.getTime());
|
|
4362
|
+
}).sort((first, second) => +first.startAt - +second.startAt);
|
|
4363
|
+
}
|
|
4364
|
+
function slotsForWindow(grid, startAt, endAt) {
|
|
4365
|
+
const startIndex = grid.findIndex((entry) => entry.startAt.getTime() === startAt.getTime());
|
|
4366
|
+
if (startIndex < 0) {
|
|
4367
|
+
throw new BookingRulesError(`${startAt.toISOString()} is not a bookable slot start for these rules`);
|
|
4368
|
+
}
|
|
4369
|
+
if (!grid[startIndex].canStart) {
|
|
4370
|
+
throw new BookingRulesError(`${startAt.toISOString()} is not an arrival day for these rules`);
|
|
4371
|
+
}
|
|
4372
|
+
const covered = [];
|
|
4373
|
+
for (let index = startIndex; index < grid.length; index++) {
|
|
4374
|
+
const entry = grid[index];
|
|
4375
|
+
const previous = covered[covered.length - 1];
|
|
4376
|
+
if (previous && entry.startAt.getTime() !== previous.bufferEndAt.getTime()) break;
|
|
4377
|
+
covered.push(entry);
|
|
4378
|
+
if (entry.endAt.getTime() === endAt.getTime()) return covered;
|
|
4379
|
+
if (entry.endAt.getTime() > endAt.getTime()) break;
|
|
4380
|
+
}
|
|
4381
|
+
throw new BookingRulesError(
|
|
4382
|
+
`${startAt.toISOString()} \u2192 ${endAt.toISOString()} does not line up with the bookable grid`
|
|
4383
|
+
);
|
|
4384
|
+
}
|
|
3719
4385
|
|
|
3720
4386
|
// shipping/fold-text.ts
|
|
3721
4387
|
function foldText(value) {
|
|
@@ -4151,10 +4817,12 @@ export {
|
|
|
4151
4817
|
AttributeType,
|
|
4152
4818
|
BNPL_METHOD_STATUS,
|
|
4153
4819
|
BNPL_SKIP_REASON,
|
|
4820
|
+
BookingDurationUnit,
|
|
4154
4821
|
BookingRatePeriod,
|
|
4155
4822
|
BookingResourceKind,
|
|
4156
4823
|
BookingResourceStatus,
|
|
4157
4824
|
BookingRulesError,
|
|
4825
|
+
BookingSessionSale,
|
|
4158
4826
|
BookingType,
|
|
4159
4827
|
CANADA_PROVINCES,
|
|
4160
4828
|
CONDITION_OPERATORS,
|
|
@@ -4244,6 +4912,7 @@ export {
|
|
|
4244
4912
|
StripeStatus,
|
|
4245
4913
|
TimePeriod,
|
|
4246
4914
|
TopMetericsSort,
|
|
4915
|
+
UNLIMITED_PLACES,
|
|
4247
4916
|
UserTypes,
|
|
4248
4917
|
addAmounts,
|
|
4249
4918
|
addressPlaceKeys,
|
|
@@ -4252,6 +4921,7 @@ export {
|
|
|
4252
4921
|
adyenStoreRequiresStateOrProvince,
|
|
4253
4922
|
adyenSupportsTaxNumberAbsent,
|
|
4254
4923
|
adyenTaxNumberFormat,
|
|
4924
|
+
allowedDurationUnits,
|
|
4255
4925
|
amountToString,
|
|
4256
4926
|
ancestorChain,
|
|
4257
4927
|
applyAssignment,
|
|
@@ -4261,15 +4931,21 @@ export {
|
|
|
4261
4931
|
baseFulfillmentDefinitions,
|
|
4262
4932
|
baseOf,
|
|
4263
4933
|
basePaymentDefinitions,
|
|
4934
|
+
bookingMethodOf,
|
|
4935
|
+
bookingRuleParts,
|
|
4264
4936
|
buildAvailability,
|
|
4265
4937
|
buildGrid,
|
|
4266
4938
|
buildOrderContext,
|
|
4267
4939
|
buildPosPresetConfig,
|
|
4268
4940
|
buildResolvedConfig,
|
|
4269
4941
|
calculatePercentage,
|
|
4942
|
+
calendarDate,
|
|
4270
4943
|
canTransition,
|
|
4944
|
+
capacityOf,
|
|
4271
4945
|
cheapestCompositeItem,
|
|
4272
4946
|
cheapestCompositePrice,
|
|
4947
|
+
checkPartySize,
|
|
4948
|
+
checkQtyPerBooking,
|
|
4273
4949
|
compositeItemCost,
|
|
4274
4950
|
compositeItemUnavailable,
|
|
4275
4951
|
compositeMakeable,
|
|
@@ -4278,6 +4954,9 @@ export {
|
|
|
4278
4954
|
compositeVariantInStock,
|
|
4279
4955
|
conditionSummary,
|
|
4280
4956
|
convertFixedFeeToLocalCurrency,
|
|
4957
|
+
courseSessions,
|
|
4958
|
+
courseStarted,
|
|
4959
|
+
courseStartedInZone,
|
|
4281
4960
|
createAmount,
|
|
4282
4961
|
crossAxisRuleApplies,
|
|
4283
4962
|
currentAssignees,
|
|
@@ -4286,10 +4965,13 @@ export {
|
|
|
4286
4965
|
deriveUnitId,
|
|
4287
4966
|
displayRuleMatchesPair,
|
|
4288
4967
|
divideAmount,
|
|
4968
|
+
durationProblems,
|
|
4969
|
+
durationText,
|
|
4289
4970
|
evaluateCondition,
|
|
4290
4971
|
evaluateConditionSet,
|
|
4291
4972
|
extendPrice,
|
|
4292
4973
|
extendPriceFor,
|
|
4974
|
+
fewestFreeOver,
|
|
4293
4975
|
formatCurrency,
|
|
4294
4976
|
formatInUnit,
|
|
4295
4977
|
formatMinorUnitsToString,
|
|
@@ -4331,7 +5013,9 @@ export {
|
|
|
4331
5013
|
listCountries,
|
|
4332
5014
|
listProvinces,
|
|
4333
5015
|
listShippingCountries,
|
|
5016
|
+
longestMinutesOf,
|
|
4334
5017
|
mapToLegacyStatus,
|
|
5018
|
+
maxAdvanceMinutesOf,
|
|
4335
5019
|
maxPrecisionFor,
|
|
4336
5020
|
mergeFragment,
|
|
4337
5021
|
modifiersSellable,
|
|
@@ -4343,11 +5027,14 @@ export {
|
|
|
4343
5027
|
normalizePostCode,
|
|
4344
5028
|
normalizeProvince,
|
|
4345
5029
|
occupancyIn,
|
|
5030
|
+
oldMaxAdvanceOf,
|
|
4346
5031
|
orderAssignments,
|
|
4347
5032
|
parseAmountToMinorUnits,
|
|
4348
5033
|
parseCurrencyCode,
|
|
4349
5034
|
parseStripeCurrencyCode,
|
|
5035
|
+
pickFreeResource,
|
|
4350
5036
|
placeKey,
|
|
5037
|
+
planCourse,
|
|
4351
5038
|
planWindow,
|
|
4352
5039
|
poolDemand,
|
|
4353
5040
|
poolOf,
|
|
@@ -4364,13 +5051,18 @@ export {
|
|
|
4364
5051
|
resolveTemplate,
|
|
4365
5052
|
resolveUnit,
|
|
4366
5053
|
roundToPrecision,
|
|
5054
|
+
ruleDurationsOf,
|
|
5055
|
+
ruleInNewShape,
|
|
4367
5056
|
sameFamily,
|
|
5057
|
+
sessionCells,
|
|
4368
5058
|
setOrderState,
|
|
4369
5059
|
shipsToCountry,
|
|
4370
5060
|
shopDayKey,
|
|
4371
5061
|
shopDayRange,
|
|
5062
|
+
shortestMinutesOf,
|
|
4372
5063
|
signReportForDisplay,
|
|
4373
5064
|
slotsForWindow,
|
|
5065
|
+
soldAsCourse,
|
|
4374
5066
|
stringifyMinorUnits,
|
|
4375
5067
|
subtractAmounts,
|
|
4376
5068
|
toBase,
|
|
@@ -4381,6 +5073,8 @@ export {
|
|
|
4381
5073
|
validateShippingZone,
|
|
4382
5074
|
violatesFinancialInvariant,
|
|
4383
5075
|
windowEndsAt,
|
|
5076
|
+
windowEndsAtInZone,
|
|
5077
|
+
windowForPeriods,
|
|
4384
5078
|
withinPresence
|
|
4385
5079
|
};
|
|
4386
5080
|
//# sourceMappingURL=index.mjs.map
|