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 */ 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 $fields * @return array */ 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); } }