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>
This commit is contained in:
Markus
2026-08-24 22:04:47 +02:00
co-authored by Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent 74592a9e14
commit 48b44f6ccd
+11 -5
View File
@@ -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<Beds24AvailabilityResponse> {
// 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 {