Files
fewo-stommer/app/Actions/MonitorChannels.php
T
Lars StommerandClaude Opus 5.5 e5f9e0762f Schnellerkennung von Portalbuchungen per E-Mail
- IMAP-Abruf jede Minute (webklex/php-imap, ohne PHP-imap-Erweiterung) oder signierter Push von n8n
- Parser für Buchungs-, Storno- und Anfrage-Mails (de/en), Buchungsnummer, An-/Abreise
- Erkannte Buchungen werden sofort gesperrt und an handgepflegte Portale weitergegeben; Kalender-Abgleich übernimmt bzw. ersetzt Mail-Sperren
- Unklare Mails: Prüf-Mail + Admin-Liste Portal-E-Mails mit Aktion Sperre anlegen
- Überwachung des Postfachs in Monitor und /health/sync

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 11:29:26 +02:00

64 lines
1.8 KiB
PHP

<?php
namespace App\Actions;
use App\Channels\Mail\MailboxReader;
use App\Mail\ChannelSyncAlert;
use App\Mail\MailboxAlert;
use App\Models\Channel;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;
/**
* Alerts the landlord once per incident when a portal import or the mailbox fast path fails
* or has not succeeded for too long. The alert is reset by the next success.
*/
class MonitorChannels
{
private const MAILBOX_ALERTED_KEY = 'mailbox:alerted';
public function handle(): int
{
$channels = Channel::query()->active()->whereNull('alerted_at')->get()
->filter(fn (Channel $channel): bool => $channel->isStale());
foreach ($channels as $channel) {
Mail::to(config('fewo.notification_email'))->send(new ChannelSyncAlert($channel));
$channel->forceFill(['alerted_at' => now()])->save();
}
return $channels->count() + $this->monitorMailbox();
}
public static function mailboxIsStale(): bool
{
if (! MailboxReader::isConfigured()) {
return false;
}
$lastSuccess = Cache::get(MailboxReader::LAST_SUCCESS_KEY);
return $lastSuccess === null
|| CarbonImmutable::parse($lastSuccess)->lessThan(now()->subMinutes(config('fewo.mailbox.stale_after_minutes')));
}
private function monitorMailbox(): int
{
if (! self::mailboxIsStale()) {
Cache::forget(self::MAILBOX_ALERTED_KEY);
return 0;
}
if (Cache::has(self::MAILBOX_ALERTED_KEY)) {
return 0;
}
Mail::to(config('fewo.notification_email'))->send(new MailboxAlert(Cache::get(MailboxReader::LAST_ERROR_KEY)));
Cache::forever(self::MAILBOX_ALERTED_KEY, true);
return 1;
}
}