mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 21:08:12 +00:00
add script to seed freight contracts across flow variants
This commit is contained in:
@@ -169,31 +169,44 @@ export function isRoundTheClock(hours: OfficeHours): boolean {
|
||||
* single EAT day and never wraps past midnight (enforced when global rules are
|
||||
* saved). openHour === closeHour is the 24-hour desk, handled first.
|
||||
*/
|
||||
/**
|
||||
* The EAT instant a booking cycle would open if it became ready at `readyAt`,
|
||||
* honouring the daily office window but WITHOUT any departure bound:
|
||||
*
|
||||
* • round-the-clock desk → opens at `readyAt` (no day break)
|
||||
* • ready before openHour → opens at openHour that EAT morning
|
||||
* • ready inside office hours → opens at `readyAt`
|
||||
* • ready at/after closeHour → opens at openHour the next morning
|
||||
*
|
||||
* `nextCycleOpensAt` layers the "before departure" gate on top of this; the first
|
||||
* import window uses it directly and lets its own departure cap apply.
|
||||
*/
|
||||
export function officeHoursOpen(readyAt: Date, hours: OfficeHours): Date {
|
||||
if (isRoundTheClock(hours)) {
|
||||
return readyAt;
|
||||
}
|
||||
const { hour, minute } = eatParts(readyAt);
|
||||
const readyMinutes = hour * 60 + minute;
|
||||
const openMinutes = hours.windowOpenHour * 60;
|
||||
const closeMinutes = hours.windowCloseHour * 60;
|
||||
if (readyMinutes < openMinutes) {
|
||||
// Ready before the desk opens on its own EAT calendar day → open this morning.
|
||||
return eatDayToUtc(eatDay(readyAt), hours.windowOpenHour);
|
||||
}
|
||||
if (readyMinutes < closeMinutes) {
|
||||
// Inside office hours → open as soon as ready.
|
||||
return readyAt;
|
||||
}
|
||||
// Desk shut for the day → open tomorrow morning.
|
||||
return eatDayToUtc(shiftEatDay(eatDay(readyAt), 1), hours.windowOpenHour);
|
||||
}
|
||||
|
||||
export function nextCycleOpensAt(
|
||||
earliestNextOpen: Date,
|
||||
hours: OfficeHours,
|
||||
departure: Date,
|
||||
): Date | null {
|
||||
let opensAt: Date;
|
||||
if (isRoundTheClock(hours)) {
|
||||
opensAt = earliestNextOpen;
|
||||
} else {
|
||||
const { hour, minute } = eatParts(earliestNextOpen);
|
||||
const readyMinutes = hour * 60 + minute;
|
||||
const openMinutes = hours.windowOpenHour * 60;
|
||||
const closeMinutes = hours.windowCloseHour * 60;
|
||||
if (readyMinutes < openMinutes) {
|
||||
// Ready before the desk opens on its own EAT calendar day → open this morning.
|
||||
opensAt = eatDayToUtc(eatDay(earliestNextOpen), hours.windowOpenHour);
|
||||
} else if (readyMinutes < closeMinutes) {
|
||||
// Inside office hours → open as soon as ready.
|
||||
opensAt = earliestNextOpen;
|
||||
} else {
|
||||
// Desk shut for the day → open tomorrow morning.
|
||||
const tomorrow = shiftEatDay(eatDay(earliestNextOpen), 1);
|
||||
opensAt = eatDayToUtc(tomorrow, hours.windowOpenHour);
|
||||
}
|
||||
}
|
||||
const opensAt = officeHoursOpen(earliestNextOpen, hours);
|
||||
return opensAt.getTime() < departure.getTime() ? opensAt : null;
|
||||
}
|
||||
|
||||
@@ -203,27 +216,53 @@ export interface InitialWindowTimes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Import booking-day window: opens at `windowOpenHour` EAT on departure-day minus
|
||||
* `importWindowLeadDays`, for `windowDurationHours`. A schedule created after its
|
||||
* computed window has fully passed gets a same-day window starting now instead,
|
||||
* capped at departure.
|
||||
* Import booking-day window. The natural anchor is `windowOpenHour` EAT on
|
||||
* departure-day minus `importWindowLeadDays`. When `now` is at/before that anchor
|
||||
* (we're still before the lead window) the window opens at the anchor — the normal
|
||||
* morning wait.
|
||||
*
|
||||
* Once `now` is PAST the anchor we're already inside the lead window, so the desk's
|
||||
* office hours decide the open the same way a reopen cycle does (via
|
||||
* `nextCycleOpensAt`):
|
||||
*
|
||||
* • 24-hour desk (open === close) → opens at `now`, any hour, day or night
|
||||
* • `now` inside [openHour, closeHour) → opens at `now` (desk is open right now)
|
||||
* • `now` before openHour that EAT day → opens at openHour that morning
|
||||
* • `now` at/after closeHour → desk shut; opens openHour next morning
|
||||
*
|
||||
* `windowDurationHours` extends from that open, capped at departure.
|
||||
*/
|
||||
export function computeImportWindowTimes(
|
||||
departure: Date,
|
||||
cfg: {
|
||||
importWindowLeadDays: number;
|
||||
windowOpenHour: number;
|
||||
windowCloseHour: number;
|
||||
windowDurationHours: number;
|
||||
},
|
||||
now: Date,
|
||||
): InitialWindowTimes {
|
||||
const windowDay = shiftEatDay(eatDay(departure), -cfg.importWindowLeadDays);
|
||||
let opensAt = eatDayToUtc(windowDay, cfg.windowOpenHour);
|
||||
let closesAt = new Date(opensAt.getTime() + cfg.windowDurationHours * 3_600_000);
|
||||
if (closesAt.getTime() <= now.getTime()) {
|
||||
opensAt = now;
|
||||
closesAt = new Date(now.getTime() + cfg.windowDurationHours * 3_600_000);
|
||||
const anchor = eatDayToUtc(windowDay, cfg.windowOpenHour);
|
||||
|
||||
let opensAt: Date;
|
||||
if (now.getTime() <= anchor.getTime()) {
|
||||
// Before the lead window → normal morning wait at the anchor.
|
||||
opensAt = anchor;
|
||||
} else {
|
||||
// Inside the lead window → the office-hours rule decides the open, exactly as a
|
||||
// reopen cycle does: open now if the desk is open now (or round-the-clock),
|
||||
// else at the next open hour. We use the same primitive as reopen cycles but
|
||||
// WITHOUT its `< departure` null-gate — when the next open lands on/after
|
||||
// departure the shared cap below clamps the (zero-length) window to departure,
|
||||
// which is truthful, rather than masking it as "open now".
|
||||
opensAt = officeHoursOpen(now, {
|
||||
windowOpenHour: cfg.windowOpenHour,
|
||||
windowCloseHour: cfg.windowCloseHour,
|
||||
});
|
||||
}
|
||||
|
||||
let closesAt = new Date(opensAt.getTime() + cfg.windowDurationHours * 3_600_000);
|
||||
if (closesAt.getTime() > departure.getTime()) {
|
||||
closesAt = departure;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user