- 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>
93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?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})");
|
||
}
|
||
}
|
||
}
|
||
}
|