File: /var/www/api.vaspayment.com/app/Services/ActivityLogger.php
<?php
namespace App\Services;
use App\Models\User;
use Jenssegers\Agent\Agent;
use App\Models\Notification;
use App\Models\UserActivity;
use Illuminate\Support\Facades\Http;
class ActivityLogger
{
public function log($clientIp, $activity, $user_id, $user_role, $device)
{
$location = $this->getLocationFromIp($clientIp);
$userAgent = request($key = null, $default = null)->header('User-Agent');
$agent = new Agent();
$agent->setUserAgent($userAgent);
$deviceDetails = $this->getDeviceDetails($agent);
$deviceJson = json_encode($device ?? $deviceDetails);
$location_data = json_encode($location ?? []);
UserActivity::create([
'user_id' => $user_id,
'role' => $user_role,
'last_login' => now(),
'activity' => $activity,
'ip' => $clientIp,
'location' => $location_data . ' IP: ' . $clientIp,
'device' => $deviceJson,
]);
$photo = User::find($user_id); // Get the user by ID
if ($photo) {
Notification::create([
'user_id' => $user_id,
'message' => $activity,
'title' => 'New Activity Notification',
'image' => $photo->profile_photo_path,
]);
}
}
protected function getLocationFromIp($clientIp)
{
$location = Http::get("https://ipwho.is/{$clientIp}");
$city = $location['city'] ?? 'Unknown City';
$region = $location['region'] ?? 'Unknown Region';
$country = $location['country'] ?? 'Unknown Country';
return "{$city}, {$region}, {$country}";
}
protected function getDeviceDetails(Agent $agent)
{
$device = $agent->device() ?: 'Unknown Device';
$browser = $agent->browser() ?: 'Unknown Browser';
$browserVersion = $agent->version($browser) ?: 'Unknown Version';
$platform = $agent->platform() ?: 'Unknown Platform';
$platformVersion = $agent->version($platform) ?: 'Unknown Version';
return "Device: {$device}, Browser: {$browser} {$browserVersion}, Platform: {$platform} {$platformVersion}";
}
}