File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33namespace App \Http \Controllers ;
44
55use App \Models \ReferralClick ;
6+ use Illuminate \Http \JsonResponse ;
67use Illuminate \Http \Request ;
7- use Illuminate \View \View ;
88
99class ReferralAnalyticsController extends Controller
1010{
11- public function index (): View
11+ public function index (): JsonResponse
1212 {
13+ // Ensure user is authenticated
14+ if (!auth ()->check ()) {
15+ return response ()->json (['error ' => 'Unauthenticated. ' ], 401 );
16+ }
17+
18+ // Ensure user is the admin
19+ $ adminUserId = (int ) env ('ADMIN_USER_ID ' , 0 );
20+ if (auth ()->id () !== $ adminUserId ) {
21+ return response ()->json (['error ' => 'Forbidden. ' ], 403 );
22+ }
23+
1324 $ clicks = ReferralClick::selectRaw ('referrer, COUNT(*) as total ' )
1425 ->groupBy ('referrer ' )
1526 ->orderByDesc ('total ' )
1627 ->get ();
1728
18- return view ( ' referral.analytics ' , compact ( ' clicks ' ) );
29+ return response ()-> json ( $ clicks );
1930 }
2031}
You can’t perform that action at this time.
0 commit comments