43 lines
975 B
PHP
43 lines
975 B
PHP
<?php
|
|
/**
|
|
* Dashboard Recent Activity Endpoint
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Core\Database;
|
|
use App\Middleware\AuthMiddleware;
|
|
|
|
$decoded = AuthMiddleware::check();
|
|
$db = Database::getInstance();
|
|
|
|
$tenantId = $decoded['tenant_id'] ?? null;
|
|
$role = $decoded['role'];
|
|
|
|
try {
|
|
if ($role === 'super_admin') {
|
|
$where = "WHERE 1=1";
|
|
$params = [];
|
|
} else {
|
|
$where = "WHERE tenant_id = ?";
|
|
$params = [$tenantId];
|
|
}
|
|
|
|
// Join with users table to get the name of the person who did the action
|
|
$stmt = $db->prepare("
|
|
SELECT a.id, a.action, a.entity_type, a.created_at, u.name as user_name
|
|
FROM audit_logs a
|
|
LEFT JOIN users u ON a.user_id = u.id
|
|
$where
|
|
ORDER BY a.created_at DESC
|
|
LIMIT 20
|
|
");
|
|
$stmt->execute($params);
|
|
$activities = $stmt->fetchAll();
|
|
|
|
json_success($activities);
|
|
|
|
} catch (\Exception $e) {
|
|
json_error('Failed to fetch recent activity', 500);
|
|
}
|