Files
fewo-stommer/app/Availability/CalendarClearance.php
LarsandClaude Sonnet 5.5 eafab8a2cb
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m38s
Buchungsprozess: Angebots-Snapshot, atomare Annahme, Auto-Angebot, Kanalfreigabe
- Angebotsbedingungen und Zahlungsdaten werden je Version eingefroren (offer_terms, offer_hash)
- Annahme in einer gesperrten Transaktion, idempotent, mit Nachweis; Mail- und Portalfehler
  machen eine gespeicherte Buchung nicht ungueltig (Postfach, Nachversand)
- Je Wohnung: manuelle/automatische Angebote, optionale Vorreservierung
- Automatische Freigabe abhaengig von aktuellen Importen und Feed-Abruf bei Sofortbuchungsportalen
- Deutsche Fehlerseiten, Buchungsseite zeigt Angebots- und Zustellstatus
- Dokumentation: BOOKING-E2E-*.md, BOOKING-COMPLIANCE-HANDOVER.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 12:01:02 +01:00

107 lines
4.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Availability;
use App\Actions\SyncChannel;
use App\Enums\ChannelBookingModel;
use App\Models\Channel;
use App\Models\ChannelExportRequest;
use App\Models\Property;
use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use Throwable;
/**
* Whether the calendars of the connected portals are good enough to commit to a stay without a human looking.
*
* Two kinds of evidence, both taken from what the synchronisation already records:
* - import: the last successful import of every importing channel is recent (otherwise one import is tried first);
* - export: a channel that can book instantly (booking model portal booking / mixed) has fetched OUR feed after the
* block was created. The feed is built from the database on every request, so a successful fetch of that channel's
* own token after the block existed contains it. This proves delivery, not that the portal has processed it –
* the remaining risk is documented in BOOKING-E2E-IMPLEMENTATION.md.
*
* Network access happens only in {@see refresh()} and never inside a database transaction.
*/
class CalendarClearance
{
public function __construct(private SyncChannel $sync) {}
/**
* @return Collection<int, Channel>
*/
public function staleImports(Property $property): Collection
{
$limit = now()->subMinutes((int) config('fewo.channels.fresh_within_minutes'));
return $property->channels()->active()->get()
->filter(fn (Channel $channel): bool => $channel->importsOccupations())
->filter(fn (Channel $channel): bool => $channel->last_success_at === null || $channel->last_success_at->lessThan($limit))
->values();
}
/**
* Imports the channels whose calendar is not recent (one attempt each); failures stay visible as stale.
*/
public function refresh(Property $property): void
{
foreach ($this->staleImports($property) as $channel) {
try {
$this->sync->handle($channel);
} catch (Throwable) {
// The sync log records the failure; the channel simply stays stale.
}
}
}
/**
* Instant-booking channels that have not fetched our feed since the block exists.
*
* @return Collection<int, Channel>
*/
public function unprovenInstantChannels(Property $property, CarbonInterface $blockedSince): Collection
{
return $property->channels()->active()->get()
->filter(fn (Channel $channel): bool => $this->canBookInstantly($channel))
->reject(fn (Channel $channel): bool => $channel->exportsOccupations() && $this->fetchedFeedSince($channel, $blockedSince))
->values();
}
public function canBookInstantly(Channel $channel): bool
{
return in_array($channel->booking_model, [ChannelBookingModel::PortalBooking, ChannelBookingModel::Mixed], true);
}
/**
* Reasons (German, for the admin) why the calendar state cannot be trusted yet; empty = cleared.
*
* @return list<string>
*/
public function reasons(Property $property, CarbonInterface $blockedSince): array
{
$reasons = [];
foreach ($this->staleImports($property) as $channel) {
$reasons[] = "Kalenderstand von {$channel->name} ist nicht aktuell (letzter erfolgreicher Import: ".($channel->last_success_at?->format('d.m.Y, H:i') ?? 'nie').').';
}
foreach ($this->unprovenInstantChannels($property, $blockedSince) as $channel) {
$reasons[] = $channel->exportsOccupations()
? "{$channel->name} (Sofortbuchung) hat unseren Kalender seit der Sperre noch nicht abgerufen."
: "Der Kalenderexport an {$channel->name} (Sofortbuchung) ist nicht aktiv.";
}
return $reasons;
}
private function fetchedFeedSince(Channel $channel, CarbonInterface $blockedSince): bool
{
return ChannelExportRequest::query()
->where('channel_id', $channel->id)
->where('requested_at', '>=', $blockedSince->copy()->addSecond())
->where('response_status', 200)
->where('export_state', 'live')
->exists();
}
}