Files
fewo-stommer/app/Support/Inbox.php
T
LarsandClaude Opus 5.5 1c757f7429 Erinnerungsabstand und taegliche/woechentliche Zusammenfassung einstellbar
System > Betrieb > Benachrichtigungen: offene Meldungen (Doppelbuchungen im
Postfach, Portal-Aufgaben) nur einmal oder alle 1/2/3/7 Tage erneut senden;
Tages- und/oder Wochenuebersicht mit waehlbarer Uhrzeit, Wochentag und Inhalt
(Postfach, Anfragen, Neues, An-/Abreisen, Zahlungen, Portale, Bewertungen).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 10:11:15 +01:00

62 lines
2.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\Support;
use App\Enums\InboxMessageKind;
use App\Models\Channel;
use App\Models\InboxMessage;
use Illuminate\Database\UniqueConstraintViolationException;
/**
* Posts findings to the admin inbox ("Postfach"). Each finding has a fingerprint and arrives only once
* per landlord, so a check that runs every few minutes does not report a lasting problem again and again.
* While the finding is still detected and the message is unread, it is repeated after the landlord's
* reminder interval (Settings::reminderIntervalDays(), 0 = never).
*/
class Inbox
{
/**
* @return InboxMessage|null the message to e-mail now – new (wasRecentlyCreated) or due for a reminder – else null
*/
public function post(InboxMessageKind $kind, string $fingerprint, string $subject, string $body, ?Channel $channel = null, ?string $actionUrl = null): ?InboxMessage
{
$fingerprint = $kind->value.':'.$fingerprint;
$existing = InboxMessage::query()->where('fingerprint', $fingerprint)->first();
if ($existing) {
$due = $this->reminderDue($existing);
$existing->forceFill(['last_seen_at' => now(), ...($due ? ['notified_at' => now()] : [])])->save();
return $due ? $existing : null;
}
try {
return InboxMessage::create([
'kind' => $kind,
'fingerprint' => $fingerprint,
'subject' => mb_substr($subject, 0, 255),
'body' => $body,
'channel_id' => $channel?->id,
'property_id' => $channel?->property_id,
'action_url' => $actionUrl,
'notified_at' => now(),
'last_seen_at' => now(),
]);
} catch (UniqueConstraintViolationException) {
return null;
}
}
/**
* Unread messages are repeated once the reminder interval has passed; reading one ends its reminders.
*/
public function reminderDue(InboxMessage $message): bool
{
$days = Settings::reminderIntervalDays($message->tenant);
return $days > 0
&& ! $message->isRead()
&& ($message->notified_at === null || $message->notified_at->lessThanOrEqualTo(now()->subDays($days)));
}
}