where('type', $type) ->where('is_current', true) ->firstOrFail(); $locale = app()->getLocale(); return view('legal.show', [ 'document' => $document, 'title' => $document->title[$locale] ?? ($document->title['pt_BR'] ?? $document->type), 'body' => $document->body[$locale] ?? ($document->body['pt_BR'] ?? ''), ]); } /** * Show the first-access terms acceptance screen. */ public function acceptForm(Request $request): View|RedirectResponse { $user = $request->user(); $current = LegalDocument::query()->where('is_current', true)->orderBy('type')->get(); if ($current->isEmpty()) { return redirect()->route('dashboard'); } $acceptedIds = $user->consents()->pluck('legal_document_id'); $pending = $current->reject(fn (LegalDocument $doc) => $acceptedIds->contains($doc->id)); if ($pending->isEmpty()) { return redirect()->route('dashboard'); } $locale = app()->getLocale(); $documents = $pending->map(fn (LegalDocument $doc): array => [ 'type' => $doc->type, 'version' => $doc->version, 'title' => $doc->title[$locale] ?? ($doc->title['pt_BR'] ?? $doc->type), 'body' => $doc->body[$locale] ?? ($doc->body['pt_BR'] ?? ''), ])->values(); return view('legal.accept', [ 'documents' => $documents, ]); } /** * Record the acceptance of all current legal documents. */ public function accept(ConsentRequest $request): RedirectResponse { $user = $request->user(); $documents = LegalDocument::query()->where('is_current', true)->get(); DB::transaction(function () use ($user, $documents, $request): void { foreach ($documents as $document) { UserConsent::updateOrCreate( [ 'user_id' => $user->id, 'legal_document_id' => $document->id, ], [ 'accepted_at' => now(), 'ip_address' => $request->ip(), 'user_agent' => (string) $request->userAgent(), 'source' => 'web_form', ], ); } }); return redirect()->route('dashboard')->with('status', __('legal.accepted')); } /** * Display the consent audit history of the authenticated user. */ public function history(Request $request): View { $history = $request->user() ->consents() ->with('document:id,type,version') ->orderByDesc('accepted_at') ->get(); return view('legal.history', [ 'history' => $history, ]); } }