From 48b44f6ccd5bdde4a691becd97c690ba4a63c498 Mon Sep 17 00:00:00 2001 From: Markus Date: Mon, 24 Aug 2026 22:04:47 +0200 Subject: [PATCH] Fix 3-month availability by raising Beds24 chunk limit and balancing chunks. The /inventory/rooms/offers endpoint allows up to 99 nights, but the chunking threshold was set to 90. This caused 90-99 night stays to be split unnecessarily, often leaving a 1- or 2-night final chunk that fell below the room's minimum stay and produced a false "unavailable" result. Now the limit is 99 and chunks are split into equal-sized pieces, so stays around 3 months (and longer) avoid tiny trailing chunks. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- src/lib/beds24.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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 {