*/ 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 */ 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 */ 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(); } }