- 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>
96 lines
4.2 KiB
PHP
96 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Channels\Mail\PortalMail;
|
||
use App\Communication\MessageMatcher;
|
||
use App\Enums\BookingTaskKind;
|
||
use App\Models\BookingTask;
|
||
use App\Models\InboundMessage;
|
||
use App\Models\Mailbox;
|
||
use App\Process\CaseLog;
|
||
use App\Support\Settings;
|
||
use Illuminate\Database\UniqueConstraintViolationException;
|
||
|
||
/**
|
||
* The inbox of v0.11 (S2/S3, CH-01, CH-06, CH-08): every message of the landlord's dedicated mailbox is stored first,
|
||
* once per Message-ID. Booking notifications of the portals go unchanged through the existing portal path; every other
|
||
* message is assigned to a case by the deterministic rules or waits in the inbox for the landlord. A message never
|
||
* creates a case by itself. An assigned reply stops the reminders of the case's data request; its details are taken
|
||
* over only through the extraction and the common fact intake.
|
||
*/
|
||
class ReceiveInboundMessage
|
||
{
|
||
public function __construct(private ProcessPortalMail $portalMail, private MessageMatcher $matcher, private RequestCaseData $requests) {}
|
||
|
||
public function handle(PortalMail $mail, ?Mailbox $mailbox = null): InboundMessage
|
||
{
|
||
$existing = InboundMessage::query()->where('message_id', $mail->messageId)->first();
|
||
if ($existing !== null) {
|
||
return $existing;
|
||
}
|
||
|
||
try {
|
||
$message = InboundMessage::query()->create([
|
||
'mailbox_id' => $mailbox?->id,
|
||
'message_id' => $mail->messageId,
|
||
'in_reply_to' => $mail->inReplyTo,
|
||
'references' => $mail->references,
|
||
'from_address' => mb_strtolower($mail->from),
|
||
'from_name' => $mail->fromName,
|
||
'to_addresses' => $mail->to,
|
||
'subject' => mb_substr($mail->subject, 0, 500),
|
||
'body' => $mail->text,
|
||
'received_at' => $mail->receivedAt,
|
||
// CH §5 / M10: what arrives in test operation is marked as such.
|
||
'test_mode' => ! Settings::isLive(),
|
||
'status' => InboundMessage::NEW,
|
||
]);
|
||
} catch (UniqueConstraintViolationException) {
|
||
return InboundMessage::query()->where('message_id', $mail->messageId)->firstOrFail();
|
||
}
|
||
|
||
if ($this->portalMail->channelFor($mail->from) !== null) {
|
||
// A portal's notification: the existing portal path decides, unchanged (CH-08).
|
||
$record = $this->portalMail->handle($mail);
|
||
$record?->forceFill(['inbound_message_id' => $message->id])->save();
|
||
$message->update(['status' => InboundMessage::PORTAL, 'portal_email_id' => $record?->id, 'processed_at' => now()]);
|
||
|
||
return $message;
|
||
}
|
||
|
||
if ($this->isOwn($mail, $mailbox)) {
|
||
$message->update(['status' => InboundMessage::IGNORED, 'match_reason' => 'own', 'processed_at' => now()]);
|
||
|
||
return $message;
|
||
}
|
||
|
||
$match = $this->matcher->match($message);
|
||
if ($match['booking'] === null) {
|
||
$message->update(['status' => InboundMessage::NEEDS_REVIEW, 'hints' => $match['hints']]);
|
||
|
||
return $message;
|
||
}
|
||
|
||
$booking = $match['booking'];
|
||
$message->update(['status' => InboundMessage::ASSIGNED, 'booking_id' => $booking->id, 'match_reason' => $match['reason']]);
|
||
CaseLog::record($booking, 'mail_in', 'E-Mail des Gastes eingegangen'.($message->test_mode ? ' (im Testbetrieb)' : '').': '.$message->subject, actor: CaseLog::GUEST, at: $message->received_at);
|
||
$this->requests->replyReceived($booking);
|
||
BookingTask::openOnce($booking, BookingTaskKind::InboundReview, 'm'.$message->id, ['due_on' => today(), 'note' => '„'.mb_substr((string) $message->subject, 0, 120).'“ – im Posteingang ansehen; erkannte Angaben werden dort zur Übernahme vorgeschlagen.']);
|
||
|
||
return $message;
|
||
}
|
||
|
||
/**
|
||
* Our own mails (copies, loops) are not guest messages.
|
||
*/
|
||
private function isOwn(PortalMail $mail, ?Mailbox $mailbox): bool
|
||
{
|
||
$own = array_filter(array_map(fn (?string $address): string => mb_strtolower(trim((string) $address)), [
|
||
$mailbox?->address, config('mail.from.address'), Settings::tenant()?->sender_email,
|
||
]));
|
||
|
||
return in_array(mb_strtolower($mail->from), $own, true);
|
||
}
|
||
}
|