import { prisma } from '@/lib/prisma'; import { isAdminAuthenticated } from '@/lib/adminAuth'; import AdminNav from '@/components/AdminNav'; import Link from 'next/link'; export default async function FinancialAuditPage({ searchParams, }: { searchParams: { type?: string; action?: string; from?: string; to?: string }; }) { const isAdmin = await isAdminAuthenticated(); if (!isAdmin) { return
Unauthorized
; } const entityType = searchParams.type as any; const action = searchParams.action as any; const fromDate = searchParams.from ? new Date(searchParams.from) : null; const toDate = searchParams.to ? new Date(`${searchParams.to}T23:59:59`) : null; const logs = await prisma.financialAuditLog.findMany({ where: { ...(entityType && { entityType }), ...(action && { action }), ...(fromDate || toDate ? { changedAt: { ...(fromDate && { gte: fromDate }), ...(toDate && { lte: toDate }), }, } : {}), }, orderBy: { changedAt: 'desc' }, take: 200, }); // Summary stats const allLogs = await prisma.financialAuditLog.findMany({ orderBy: { changedAt: 'desc' }, }); const last30Days = allLogs.filter( (l) => l.changedAt > new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) ); const deletions = logs.filter((l) => l.action === 'DELETE'); return (

Financial Audit Trail

Back to admin

Complete record of all changes to financial data (orders, sales, purchases, expenses). Use this to track who changed what and when.

{/* Stats */}

Total Changes

{allLogs.length}

Last 30 Days

{last30Days.length}

Deletions

0 ? 'text-splash-pink' : ''}`}> {deletions.length}

Creates/Updates

{allLogs.length - deletions.length}

{/* Filters */}
{(entityType || action || searchParams.from || searchParams.to) && ( Clear filters )}
{/* Logs Table */} {logs.length === 0 ? (

No audit logs found.

) : (
{logs.map((log) => { const changes = JSON.parse(log.changes || '{}'); const changeList = Object.entries(changes) .map(([field, change]: [string, any]) => `${field}: ${change.old} → ${change.new}`) .slice(0, 2) .join('; '); return ( ); })}
Date & Time Action Entity Type Entity ID Changed By (IP) Details
{log.changedAt.toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric', })}{' '} {log.changedAt.toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit', second: '2-digit', })} {log.action} {log.entityType} {log.entityId} {log.changedBy || '—'} {changeList || log.reason || '—'}
)}

Showing last 200 changes. Audit logs help you track financial data integrity and detect unauthorized changes.

); }