Files
fewo-stommer/app/Actions/ReceiveInboundMessage.php
LarsandClaude Opus 5.5 ad63db78f3
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m6s
Posteingang: Antwortbezug ohne Klammern, Testadresse im Testbetrieb, sofortige Auswertung
- Antwortbezug (In-Reply-To/References) wird mit und ohne spitze Klammern
  verglichen – Strato liefert ihn ohne, gespeichert war er mit; Antworten
  blieben deshalb im Posteingang hängen.
- Testbetrieb: Die Testadresse steht zusammen mit der genannten
  Vorgangsnummer für den Gast dieses Vorgangs (alle Antworten kommen vom
  Testpostfach). Im Live-Betrieb gilt das nicht.
- Eine zugeordnete Nachricht wird sofort ausgewertet (automatisch und nach
  Zuordnung von Hand), nicht erst im Fünf-Minuten-Lauf; die Meldung nennt,
  was übernommen wurde. Neu: „Erneut auswerten“.

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

113 lines
5.0 KiB
PHP
Raw Permalink 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\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;
use Throwable;
/**
* 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();
// CH-09/12: if the notification belongs to a case, the guest details it carries are evaluated for that case.
$bookingId = $record?->blockedPeriod()->value('linked_booking_id');
$message->update(['status' => InboundMessage::PORTAL, 'portal_email_id' => $record?->id, 'booking_id' => $bookingId, 'match_reason' => $bookingId ? 'portal_reference' : null, '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.']);
// Evaluate at once instead of waiting for the next five-minute run (the run remains the safety net).
$this->evaluate($message);
return $message->refresh();
}
/**
* Runs the extraction for a freshly assigned message; a problem never undoes the assignment.
*/
public function evaluate(InboundMessage $message): void
{
try {
app(ExtractMessageFacts::class)->handle($message->refresh());
} catch (Throwable $exception) {
report($exception);
}
}
/**
* 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);
}
}