Files
fewo-stommer/app/Http/Controllers/GuestDataController.php
LarsandClaude Opus 5.5 e4a3cb7f7b v0.11 U4: Rückfrage, Folgefrage, Erinnerung und Ergänzungslink
- data_requests: ein Beschaffungsvorgang je Vorgang mit Modus
  (automatisch / Freigabe / manuell), getrennten Zählern für Folgefragen
  und Erinnerungen ohne Rücksetzen, Antwortfrist ≠ Reservierungsfrist.
- RequestCaseData: fragt nach, was für das nächste Angebot, die
  Wunschbestätigung oder (bei verbindlichen Buchungen) administrativ fehlt;
  ohne E-Mail keine Rückfrage. Teilantwort → gezielte Folgefrage, Schweigen
  → Erinnerungen, an der Grenze Aufgabe für die Vermieterin. Nie ein Hold.
- Mail DataRequested mit Texten nach tatsächlichem Sperrstatus (M11);
  Dokumentart „Rückfrage“ – im Live-Betrieb ohne Freigabe nur nach Klick.
- Signierter, versionierter Ergänzungslink (/angebot/angaben): Lesen
  schreibt nicht, Absenden über den Fakteneingang, idempotent.
- Vorgang: „Rückfrage an den Gast senden“ / „Rückfrage beenden“;
  Eingangsbestätigung nennt die Klärungsreservierung korrekt.
- Zeitplan: cases:data-requests alle fünf Minuten.

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

151 lines
7.1 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\Http\Controllers;
use App\Actions\ContributeCaseFacts;
use App\Actions\RequestCaseData;
use App\Enums\BookingStatus;
use App\Enums\FactSource;
use App\Models\Booking;
use App\Models\CaseFact;
use App\Models\DataRequest;
use App\Process\CaseRequirements;
use App\Support\Countries;
use DateTimeInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\View\View;
/**
* The guest's page to complete missing details (v0.11, CH-18, signed link from the data request): no account, the link
* belongs to one case, one request and its current version. Opening it changes nothing; it shows only the details that
* are asked for and what the guest gave for this case himself. Sending goes through the common fact intake – the same
* form twice is harmless, a differing value never overwrites a stored one, and nothing binding happens here.
*/
class GuestDataController extends Controller
{
public function show(Request $request, Booking $booking): View
{
$booking->loadMissing(['guest', 'property']);
[$dataRequest, $problem] = $this->current($request, $booking);
$fields = $problem === null ? $this->fields($dataRequest, $booking) : [];
return view('guest-data', [
'booking' => $booking,
'problem' => $problem,
'fields' => $fields,
'values' => $this->ownValues($booking, $fields),
'clarification' => $booking->clarificationState(),
'purpose' => $dataRequest?->purpose,
'submitUrl' => $problem === null ? $this->signed($booking, 'guest-data.submit', now()->addHour(), $dataRequest) : null,
]);
}
public function submit(Request $request, Booking $booking, ContributeCaseFacts $facts): RedirectResponse
{
$booking->loadMissing('guest');
[$dataRequest, $problem] = $this->current($request, $booking);
$back = $dataRequest !== null ? $this->signed($booking, 'guest-data.show', now()->addDays(2), $dataRequest) : url()->previous();
if ($problem !== null) {
return redirect()->to($back)->with('guest-data.error', $problem);
}
$fields = $this->fields($dataRequest, $booking);
$rules = [];
foreach ($fields as $field) {
$rules[$this->input($field)] = match ($field) {
'guest.country' => ['nullable', 'in:'.implode(',', array_keys(Countries::options()))],
'guest.email' => ['nullable', 'email', 'max:255'],
'booking.guests' => ['nullable', 'integer', 'min:1', 'max:'.$booking->property->max_guests],
default => ['nullable', 'string', 'max:255', 'not_regex:/https?:|www\.|<|>/i'],
};
}
$input = $request->validate($rules, [], collect($fields)->mapWithKeys(fn (string $field): array => [$this->input($field) => CaseRequirements::label($field)])->all());
$given = collect($fields)->mapWithKeys(fn (string $field): array => [$field => $input[$this->input($field)] ?? null])->filter(fn (mixed $value): bool => filled($value))->all();
if ($given === []) {
return redirect()->to($back)->with('guest-data.error', 'Bitte füllen Sie mindestens eines der Felder aus.');
}
$result = $facts->handle($booking, $given, FactSource::GuestLink, [
'reference' => 'link:r'.$dataRequest->id.':v'.$dataRequest->link_version.':'.hash('sha256', json_encode($given)),
'actor' => 'Gast über den Ergänzungslink',
]);
$booking->refresh();
$stillMissing = RequestCaseData::needs($booking)['fields'] ?? [];
return redirect()->to($back)->with('guest-data.success', match (true) {
$result->isEmpty() => 'Diese Angaben liegen uns bereits vor – vielen Dank.',
$result->needsReview() => 'Vielen Dank! Einige Angaben weichen von unseren Unterlagen ab; wir prüfen sie und melden uns bei Bedarf.',
$stillMissing !== [] => 'Vielen Dank! Es fehlen noch: '.implode(', ', CaseRequirements::labels($stillMissing)).'.',
default => 'Vielen Dank – Ihre Angaben sind vollständig. Wir melden uns in Kürze per E-Mail.',
});
}
/**
* The request this link belongs to and why the link cannot be used (or null).
*
* @return array{0: ?DataRequest, 1: ?string}
*/
private function current(Request $request, Booking $booking): array
{
$dataRequest = $booking->dataRequests()->whereKey((int) $request->query('r'))->first();
if ($dataRequest === null) {
return [null, 'Dieser Link gehört nicht zu dieser Anfrage.'];
}
if ((int) $request->query('v') !== $dataRequest->link_version) {
return [$dataRequest, 'Dieser Link ist nicht mehr aktuell – wir haben Ihnen inzwischen eine neuere Nachricht gesendet. Bitte verwenden Sie den Link aus der neuesten E-Mail.'];
}
if (RequestCaseData::needs($booking) === null) {
return [$dataRequest, in_array($booking->status, [BookingStatus::Cancelled, BookingStatus::Declined, BookingStatus::Expired], true)
? 'Diese Anfrage ist abgeschlossen. Gern können Sie über unsere Webseite neu anfragen.'
: 'Vielen Dank – uns liegen alle Angaben vor. Sie müssen nichts weiter tun.'];
}
return [$dataRequest, null];
}
/**
* The details asked for now, plus the phone number as an optional extra.
*
* @return list<string>
*/
private function fields(?DataRequest $dataRequest, Booking $booking): array
{
$needed = RequestCaseData::needs($booking)['fields'] ?? (array) $dataRequest?->fields;
return array_values(array_unique([...$needed, ...(blank($booking->guest?->phone) ? ['guest.phone'] : [])]));
}
/**
* Pre-filled values: only what this guest gave for this case himself (minimal insight, CH-18), and Germany as a
* changeable default of the country (B4).
*
* @param list<string> $fields
* @return array<string, string>
*/
private function ownValues(Booking $booking, array $fields): array
{
$own = CaseFact::query()->where('booking_id', $booking->id)->whereIn('field', $fields)
->whereIn('source', [FactSource::GuestLink, FactSource::WebsiteForm])->whereIn('outcome', [CaseFact::APPLIED, CaseFact::CONFIRMED])
->orderBy('id')->get()->mapWithKeys(fn (CaseFact $fact): array => [$fact->field => (string) $fact->value])->all();
return in_array('guest.country', $fields, true) && ! isset($own['guest.country']) ? [...$own, 'guest.country' => Countries::DEFAULT] : $own;
}
private function input(string $field): string
{
return str_replace('.', '_', $field);
}
private function signed(Booking $booking, string $name, DateTimeInterface $expiration, DataRequest $dataRequest): string
{
$parameters = ['booking' => $booking, 'r' => $dataRequest->id, 'v' => $dataRequest->link_version];
return $booking->website()?->temporarySignedRoute($name, $expiration, $parameters)
?? URL::temporarySignedRoute($name, $expiration, $parameters);
}
}