![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/-3ecb40d5/ |
import { NextApiRequest, NextApiResponse } from 'next';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
// Get all public cases - simple query
const cases = await prisma.legal_cases.findMany({
where: {
isPublic: true
},
select: {
id: true,
title: true,
status: true,
urgencyLevel: true,
estimatedValue: true,
category: true,
jurisdiction: true,
createdAt: true,
updatedAt: true
}
});
// Get lawyers count
const lawyersCount = await prisma.users.count({
where: {
role: 'LAWYER'
}
});
// Get clients count
const clientsCount = await prisma.users.count({
where: {
role: 'CLIENT'
}
});
// Calculate stats
const totalCases = cases.length;
const activeCases = cases.filter(c => c.status === 'ACTIVE' || c.status === 'active').length;
const urgentCases = cases.filter(c => c.urgencyLevel === 'URGENT').length;
const totalValue = cases.reduce((sum, c) => sum + (c.estimatedValue || 0), 0);
// Calculate category distribution
const categoryCounts = cases.reduce((acc, c) => {
if (c.category) {
acc[c.category] = (acc[c.category] || 0) + 1;
}
return acc;
}, {} as Record<string, number>);
const topCategories = Object.entries(categoryCounts)
.map(([name, count]) => ({
name,
count,
percentage: Math.round((count / totalCases) * 100)
}))
.sort((a, b) => b.count - a.count)
.slice(0, 10);
// Calculate jurisdiction distribution
const jurisdictionCounts = cases.reduce((acc, c) => {
if (c.jurisdiction) {
acc[c.jurisdiction] = (acc[c.jurisdiction] || 0) + 1;
}
return acc;
}, {} as Record<string, number>);
const topJurisdictions = Object.entries(jurisdictionCounts)
.map(([name, count]) => ({
name,
count,
percentage: Math.round((count / totalCases) * 100)
}))
.sort((a, b) => b.count - a.count)
.slice(0, 10);
// Mock recent activity
const recentActivity = [
{
type: 'NEW_CASE',
description: 'Nouveau cas urgent en litige civil',
timestamp: '2 heures'
},
{
type: 'LAWYER_JOIN',
description: '5 nouveaux avocats ont rejoint la plateforme',
timestamp: '4 heures'
},
{
type: 'CASE_COMPLETED',
description: 'Dossier de droit familial résolu avec succès',
timestamp: '1 jour'
}
];
const stats = {
totalCases,
activeCases,
urgentCases,
totalValue: Math.round(totalValue),
averageCaseValue: totalCases > 0 ? Math.round(totalValue / totalCases) : 0,
totalLawyers: lawyersCount,
totalClients: clientsCount,
successRate: 85, // Mock data
averageResponseTime: 24, // Mock data - 24 hours
topCategories,
topJurisdictions,
recentActivity
};
res.status(200).json(stats);
} catch (error) {
console.error('Error fetching live cases stats:', error);
res.status(500).json({ error: 'Failed to fetch statistics' });
} finally {
await prisma.$disconnect();
}
}