Files
canina/frontend/admin-panel/src/pages/AuditHistoryPage.tsx
T
devops d13ce5cbc2
Deploy Canina / deploy (push) Failing after 2m14s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 8s
feat(admin): implement live audit history and department tasks management system
2026-10-14 13:42:26 +00:00

324 lines
15 KiB
TypeScript

import React, { useState, useEffect } from 'react';
import {
History,
Search,
Filter,
RefreshCw,
Clock,
User,
Database,
ArrowRight,
Shield,
Layers,
ChevronDown,
Calendar,
AlertCircle,
Eye,
CheckCircle2
} from 'lucide-react';
import api from '../services/api';
interface AuditLogItem {
id: string;
entity: string;
entityId: string;
action: 'CREATE' | 'UPDATE' | 'DELETE' | 'EXECUTE' | 'BULK_UPDATE';
department: string;
actor: string;
summary: string;
oldValues?: any;
newValues?: any;
metadata?: any;
createdAt: string;
}
export default function AuditHistoryPage() {
const [logs, setLogs] = useState<AuditLogItem[]>([]);
const [loading, setLoading] = useState(true);
const [searchTerm, setSearchTerm] = useState('');
const [selectedEntity, setSelectedEntity] = useState('ALL');
const [selectedDept, setSelectedDept] = useState('ALL');
const [selectedAction, setSelectedAction] = useState('ALL');
const [activeLogModal, setActiveLogModal] = useState<AuditLogItem | null>(null);
const fetchLogs = async () => {
setLoading(true);
try {
const res = await api.get('/admin/audit-logs?limit=100');
if (res.data?.success) {
setLogs(res.data.data);
}
} catch (err) {
console.error('Error fetching audit logs:', err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchLogs();
}, []);
const filteredLogs = logs.filter((log) => {
const matchesSearch =
!searchTerm ||
log.summary.toLowerCase().includes(searchTerm.toLowerCase()) ||
log.actor.toLowerCase().includes(searchTerm.toLowerCase()) ||
log.entity.toLowerCase().includes(searchTerm.toLowerCase()) ||
log.entityId.toLowerCase().includes(searchTerm.toLowerCase());
const matchesEntity = selectedEntity === 'ALL' || log.entity === selectedEntity;
const matchesDept = selectedDept === 'ALL' || log.department === selectedDept;
const matchesAction = selectedAction === 'ALL' || log.action === selectedAction;
return matchesSearch && matchesEntity && matchesDept && matchesAction;
});
const getActionBadge = (action: string) => {
switch (action) {
case 'CREATE':
return <span className="px-2 py-0.5 rounded-full text-[10px] font-black bg-emerald-100 text-emerald-800 border border-emerald-200">افزودن (Create)</span>;
case 'UPDATE':
return <span className="px-2 py-0.5 rounded-full text-[10px] font-black bg-blue-100 text-blue-800 border border-blue-200">ویرایش (Update)</span>;
case 'DELETE':
return <span className="px-2 py-0.5 rounded-full text-[10px] font-black bg-rose-100 text-rose-800 border border-rose-200">حذف (Delete)</span>;
case 'EXECUTE':
return <span className="px-2 py-0.5 rounded-full text-[10px] font-black bg-purple-100 text-purple-800 border border-purple-200">اجرا (Execute)</span>;
case 'BULK_UPDATE':
return <span className="px-2 py-0.5 rounded-full text-[10px] font-black bg-amber-100 text-amber-800 border border-amber-200">دسته‌جمعی (Bulk)</span>;
default:
return <span className="px-2 py-0.5 rounded-full text-[10px] font-black bg-gray-100 text-gray-800">{action}</span>;
}
};
const getDeptBadge = (dept: string) => {
const map: Record<string, string> = {
COMMERCE: 'بازرگانی و مالی',
PRODUCT: 'محصولات',
CONTENT: 'محتوا و وبلاگ',
SEO: 'سئو و مارکتینگ',
DEV: 'فنی و زیرساخت',
ORDER: 'سفارشات',
APPROVAL: 'تاییدات مدیریت',
};
return (
<span className="text-[11px] font-bold text-gray-600 bg-gray-100 px-2 py-0.5 rounded-md border border-gray-200">
{map[dept] || dept}
</span>
);
};
return (
<div className="space-y-6 font-vazir pb-12" dir="rtl">
{/* Top Header */}
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 bg-white p-6 rounded-2xl border border-gray-100 shadow-sm">
<div>
<div className="flex items-center gap-3 mb-1">
<div className="w-10 h-10 rounded-xl bg-purple-100 flex items-center justify-center text-purple-700">
<History className="w-5 h-5" />
</div>
<div>
<h1 className="text-xl font-black text-gray-900">تاریخچه تغییرات زنده دیتابیس (Audit Logs)</h1>
<p className="text-xs text-gray-500 font-medium">ردیابی تمامی عملیات‌های اصلاح قیمت، استوک، پست‌ها و کاتالوگ</p>
</div>
</div>
</div>
<button
onClick={fetchLogs}
disabled={loading}
className="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl bg-purple-50 text-purple-700 text-xs font-bold hover:bg-purple-100 transition-colors border border-purple-200 cursor-pointer disabled:opacity-50"
>
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
<span>به‌روزرسانی لحظه‌ای</span>
</button>
</div>
{/* Filter and Search Bar */}
<div className="bg-white p-4 rounded-2xl border border-gray-100 shadow-sm space-y-4">
<div className="grid grid-cols-1 md:grid-cols-4 gap-3">
<div className="relative">
<Search className="w-4 h-4 absolute right-3 top-3 text-gray-400" />
<input
type="text"
placeholder="جستجو در خلاصه تغییرات، شناسه، اپراتور..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full pr-9 pl-3 py-2 text-xs bg-gray-50 border border-gray-200 rounded-xl focus:bg-white focus:outline-none focus:border-purple-500 font-medium"
/>
</div>
<div>
<select
value={selectedEntity}
onChange={(e) => setSelectedEntity(e.target.value)}
className="w-full px-3 py-2 text-xs bg-gray-50 border border-gray-200 rounded-xl focus:bg-white focus:outline-none focus:border-purple-500 font-bold text-gray-700"
>
<option value="ALL">همه موجودیت‌ها (Entities)</option>
<option value="product">محصولات (Product)</option>
<option value="blog">وبلاگ و مقالات (Blog)</option>
<option value="category">دسته‌بندی‌ها (Category)</option>
<option value="order">سفارشات (Order)</option>
<option value="approval">تاییدیه مدیریت (Approval)</option>
<option value="task">تسک‌ها (Task)</option>
</select>
</div>
<div>
<select
value={selectedDept}
onChange={(e) => setSelectedDept(e.target.value)}
className="w-full px-3 py-2 text-xs bg-gray-50 border border-gray-200 rounded-xl focus:bg-white focus:outline-none focus:border-purple-500 font-bold text-gray-700"
>
<option value="ALL">همه دپارتمان‌ها</option>
<option value="COMMERCE">بازرگانی و مالی</option>
<option value="PRODUCT">کاتالوگ و انبار</option>
<option value="CONTENT">محتوا و وبلاگ</option>
<option value="SEO">سئو و مارکتینگ</option>
<option value="DEV">فنی و زیرساخت</option>
<option value="APPROVAL">تاییدات هیئت مدیره</option>
</select>
</div>
<div>
<select
value={selectedAction}
onChange={(e) => setSelectedAction(e.target.value)}
className="w-full px-3 py-2 text-xs bg-gray-50 border border-gray-200 rounded-xl focus:bg-white focus:outline-none focus:border-purple-500 font-bold text-gray-700"
>
<option value="ALL">همه نوع عملیات</option>
<option value="CREATE">افزودن (Create)</option>
<option value="UPDATE">ویرایش (Update)</option>
<option value="DELETE">حذف (Delete)</option>
<option value="EXECUTE">اجرای تغییر (Execute)</option>
<option value="BULK_UPDATE">به‌روزرسانی انبوه (Bulk)</option>
</select>
</div>
</div>
</div>
{/* Audit Logs Table */}
<div className="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
{loading ? (
<div className="p-12 text-center text-gray-500 font-bold text-sm">
<RefreshCw className="w-6 h-6 animate-spin mx-auto mb-2 text-purple-600" />
در حال بارگذاری لاگ‌های تغییرات دیتابیس...
</div>
) : filteredLogs.length === 0 ? (
<div className="p-12 text-center text-gray-400 font-bold text-sm">
هیچ رکورد تغییری با فیلترهای انتخابی یافت نشد.
</div>
) : (
<div className="overflow-x-auto">
<table className="w-full text-right text-xs">
<thead className="bg-gray-50/75 border-b border-gray-100 text-gray-500 font-black">
<tr>
<th className="py-3 px-4">زمان ثبت</th>
<th className="py-3 px-4">موجودیت / شناسه</th>
<th className="py-3 px-4">نوع اقدام</th>
<th className="py-3 px-4">دپارتمان</th>
<th className="py-3 px-4">اقدام‌کننده</th>
<th className="py-3 px-4">شرح تغییرات</th>
<th className="py-3 px-4 text-center">جزییات فنی</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-100 font-medium text-gray-700">
{filteredLogs.map((log) => (
<tr key={log.id} className="hover:bg-purple-50/20 transition-colors">
<td className="py-3.5 px-4 whitespace-nowrap text-gray-500">
<div className="flex items-center gap-1.5 font-sans font-bold text-[11px]">
<Clock className="w-3.5 h-3.5 text-gray-400" />
<span>{new Date(log.createdAt).toLocaleString('fa-IR')}</span>
</div>
</td>
<td className="py-3.5 px-4 whitespace-nowrap">
<div className="font-bold text-gray-900">{log.entity}</div>
<div className="text-[10px] font-mono text-gray-400 truncate max-w-[120px]">{log.entityId}</div>
</td>
<td className="py-3.5 px-4 whitespace-nowrap">
{getActionBadge(log.action)}
</td>
<td className="py-3.5 px-4 whitespace-nowrap">
{getDeptBadge(log.department)}
</td>
<td className="py-3.5 px-4 whitespace-nowrap font-bold text-purple-900">
<div className="flex items-center gap-1">
<User className="w-3.5 h-3.5 text-purple-600" />
<span>{log.actor}</span>
</div>
</td>
<td className="py-3.5 px-4">
<div className="font-bold text-gray-800 line-clamp-2 max-w-md">{log.summary}</div>
</td>
<td className="py-3.5 px-4 text-center whitespace-nowrap">
<button
onClick={() => setActiveLogModal(log)}
className="inline-flex items-center gap-1 px-2.5 py-1 rounded-lg bg-gray-100 hover:bg-purple-100 hover:text-purple-700 text-gray-600 font-bold text-[11px] transition-colors cursor-pointer"
>
<Eye className="w-3.5 h-3.5" />
<span>مشاهده Diff</span>
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
{/* Modal View Diff */}
{activeLogModal && (
<div className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4 backdrop-blur-sm animate-in fade-in duration-150">
<div className="bg-white rounded-2xl max-w-2xl w-full p-6 space-y-4 shadow-2xl border border-gray-100 max-h-[85vh] overflow-y-auto">
<div className="flex items-center justify-between border-b border-gray-100 pb-3">
<div className="flex items-center gap-2">
<Database className="w-5 h-5 text-purple-600" />
<h3 className="font-black text-sm text-gray-900">جزییات تغییر داده‌ها ({activeLogModal.entity})</h3>
</div>
<button
onClick={() => setActiveLogModal(null)}
className="text-gray-400 hover:text-gray-600 font-bold text-sm cursor-pointer"
>
✕
</button>
</div>
<div className="text-xs space-y-2">
<div className="p-3 bg-gray-50 rounded-xl">
<span className="font-bold text-gray-500">خلاصه عملیات: </span>
<span className="font-black text-gray-800">{activeLogModal.summary}</span>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<h4 className="font-bold text-rose-700 mb-1">مقادیر قبلی (Old Values):</h4>
<pre className="bg-rose-50 text-rose-900 p-3 rounded-xl text-[10px] font-mono overflow-x-auto max-h-56">
{JSON.stringify(activeLogModal.oldValues || { info: 'مقداری ثبت نشده بود' }, null, 2)}
</pre>
</div>
<div>
<h4 className="font-bold text-emerald-700 mb-1">مقادیر جدید (New Values):</h4>
<pre className="bg-emerald-50 text-emerald-900 p-3 rounded-xl text-[10px] font-mono overflow-x-auto max-h-56">
{JSON.stringify(activeLogModal.newValues || { info: 'تغییری ثبت نشده' }, null, 2)}
</pre>
</div>
</div>
</div>
<div className="pt-2 text-left">
<button
onClick={() => setActiveLogModal(null)}
className="px-4 py-2 bg-purple-600 text-white rounded-xl text-xs font-bold hover:bg-purple-700 transition-colors"
>
بستن
</button>
</div>
</div>
</div>
)}
</div>
);
}