Files
fewo-stommer/app/Console/Commands/FetchPortalMail.php
T
LarsandClaude Opus 5.5 dff9e2344e v0.11 U5: Posteingang, deterministische Zuordnung, Klärungsansicht
- mailboxes (Modell für mehrere, eines aktiv): Übernahme des bisherigen
  Postfachs als „nur Portal-Mails“; Modus „alle Nachrichten“ für ein
  eigenes Postfach; Adresse als Antwortadresse der Rückfragen.
- inbound_messages: jede Nachricht zuerst gespeichert, einmal je
  Message-ID, mit Testbetriebs-Kennzeichen (M10). Portal-Mails laufen
  unverändert über den Portalpfad (CH-08).
- MessageMatcher (CH-13, ohne Scoring): Antwortbezug; Vorgangsnummer nur
  mit dem Gast als Absender; sonst Absender + Wohnung + beide Termine eines
  einzigen offenen Vorgangs. Ein offener Vorgang des Absenders allein
  genügt nicht (nur Hinweis). Alles andere → Klärung (M04).
- Ausgehende Mails speichern ihre Message-ID; Antwort stoppt Erinnerungen.
- Filament „Posteingang“: lesen, Vorgang zuordnen, als neue Anfrage
  anlegen, ignorieren, erledigt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 13:39:20 +01:00

93 lines
3.2 KiB
PHP
Raw 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\Console\Commands;
use App\Actions\ProcessPortalMail;
use App\Actions\ReceiveInboundMessage;
use App\Channels\ChannelSyncException;
use App\Channels\Mail\MailboxReader;
use App\Enums\TenantServiceKey;
use App\Models\Channel;
use App\Models\Tenant;
use App\Tenancy\TenantFeatures;
use App\Tenancy\TenantRunner;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('channels:fetch-mail')]
#[Description('Read booking notifications of the portals from each landlord\'s mailbox and block recognised bookings immediately')]
class FetchPortalMail extends Command
{
/**
* Execute the console command. Every landlord has their own mailbox, so every landlord gets their own
* run – one landlord's credentials are never used to read another one's mail.
*/
public function handle(MailboxReader $reader, ProcessPortalMail $processPortalMail, TenantRunner $runner): int
{
$runner->each(fn (Tenant $tenant) => $this->fetchForCurrentLandlord($reader, $processPortalMail, $tenant));
return self::SUCCESS;
}
private function fetchForCurrentLandlord(MailboxReader $reader, ProcessPortalMail $processPortalMail, Tenant $tenant): void
{
if (! app(TenantFeatures::class)->enabled(TenantServiceKey::PortalMail, $tenant)) {
return;
}
if (! MailboxReader::isConfigured()) {
$this->line("{$tenant->name}: kein Postfach hinterlegt.");
return;
}
// v0.11: a dedicated mailbox is read completely; portal notifications still go through the portal path.
$mailbox = $tenant->activeMailbox();
if ($mailbox?->readsEverything()) {
try {
$mails = $reader->fetchAll();
} catch (ChannelSyncException $exception) {
$this->error("{$tenant->name}: {$exception->getMessage()}");
return;
}
foreach ($mails as $mail) {
$message = app(ReceiveInboundMessage::class)->handle($mail, $mailbox);
if ($message->wasRecentlyCreated) {
$this->info("{$tenant->name}: {$message->statusLabel()} ({$message->subject})");
}
}
$mailbox->forceFill(['last_fetched_at' => now()])->save();
return;
}
$senders = Channel::query()->active()->get()
->flatMap(fn (Channel $channel): array => $processPortalMail->senders($channel))
->unique()
->values()
->all();
if ($senders === []) {
$this->line("{$tenant->name}: für kein Portal sind Absender hinterlegt.");
return;
}
try {
$mails = $reader->fetch($senders);
} catch (ChannelSyncException $exception) {
$this->error("{$tenant->name}: {$exception->getMessage()}");
return;
}
foreach ($mails as $mail) {
if ($record = $processPortalMail->handle($mail)) {
$this->info("{$tenant->name} – {$record->channel->name}: {$record->type->getLabel()} → {$record->status->getLabel()} ({$record->subject})");
}
}
}
}