42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\Konfigurasi\KunjunganPengguna;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class CatatKunjungan
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$response = $next($request);
|
|
|
|
$user = $request->user() ?? $request->user('sanctum');
|
|
if (! $user) {
|
|
return $response;
|
|
}
|
|
|
|
$tanda = today()->toDateString() . ':' . $user->id;
|
|
|
|
// Web: tanda di session, tanpa query tambahan
|
|
if ($request->hasSession()) {
|
|
if ($request->session()->get('kunjungan_tercatat') !== $tanda) {
|
|
KunjunganPengguna::catat($user->id);
|
|
$request->session()->put('kunjungan_tercatat', $tanda);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
// API: tanda di cache sampai akhir hari
|
|
if (Cache::add("kunjungan:{$tanda}", true, now()->endOfDay())) {
|
|
KunjunganPengguna::catat($user->id);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|