diff --git a/src/lib/beds24.ts b/src/lib/beds24.ts index 3162b6d..f0783fa 100644 --- a/src/lib/beds24.ts +++ b/src/lib/beds24.ts @@ -184,7 +184,7 @@ async function fetchOffer( // is long-term stays (1–12+ months), we work around it by splitting longer // stays into consecutive chunks of at most this many nights each, querying // each separately, and summing the results. -const MAX_NIGHTS_PER_OFFER_REQUEST = 90; +const MAX_NIGHTS_PER_OFFER_REQUEST = 99; export async function getAvailability( params: Beds24AvailabilityParams @@ -242,13 +242,20 @@ async function getAvailabilityInChunks( params: Beds24AvailabilityParams, totalNights: number ): Promise { + // Split into as few chunks as possible, each ≤ 99 nights, and keep the + // chunk sizes as equal as possible. This avoids tiny final chunks that + // would fall below a room's minimum-stay rule and return no offer. + const numChunks = Math.ceil(totalNights / MAX_NIGHTS_PER_OFFER_REQUEST); + let baseChunkSize = Math.floor(totalNights / numChunks); + let remainder = totalNights % numChunks; + let cursor = params.start; - let remaining = totalNights; let totalPrice = 0; let currency = "EUR"; - while (remaining > 0) { - const chunkNights = Math.min(remaining, MAX_NIGHTS_PER_OFFER_REQUEST); + for (let i = 0; i < numChunks; i++) { + const chunkNights = baseChunkSize + (remainder > 0 ? 1 : 0); + remainder--; const chunkEnd = addDaysToDateString(cursor, chunkNights); const offer = await fetchOffer( @@ -273,7 +280,6 @@ async function getAvailabilityInChunks( totalPrice += offer.price; currency = offer.currency; cursor = chunkEnd; - remaining -= chunkNights; } return {