<?php
 namespace App\Http\Middleware\Store; use App\Services\Contract\ContractAcceptanceService; use Closure; use Illuminate\Http\Request; class RequiresContractAcceptance { public function __construct(private readonly ContractAcceptanceService $service) {} private const SKIP_ROUTE_NAMES = [ 'store.auth.contracts.pending', 'store.auth.contracts.pending.accept', 'store.auth.contracts.pending.skip', 'store.auth.contracts.accept', 'store.auth.contracts.detail', 'store.auth.logout', 'legal.contract.show', ]; public function handle(Request $request, Closure $next): \Symfony\Component\HttpFoundation\Response { $client = $request->user('client'); if (! $client) { return $next($request); } if (session('active_client_id') && (int) session('active_client_id') !== (int) $client->id) { return $next($request); } $routeName = $request->route()?->getName(); if ($routeName && in_array($routeName, self::SKIP_ROUTE_NAMES, true)) { return $next($request); } if ($this->service->hasPending($client)) { if ($request->expectsJson() || $request->ajax()) { return response()->json([ 'error' => 'contract_acceptance_required', 'redirect' => route('store.auth.contracts.pending'), ], 423); } return redirect()->route('store.auth.contracts.pending'); } return $next($request); } } 