letsbe-hub/src/app/api/v1/admin/enterprise-clients/error-summary/route.ts

53 lines
1.9 KiB
TypeScript
Raw Normal View History

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/lib/auth'
import { errorDashboardService } from '@/lib/services/error-dashboard-service'
// GET /api/v1/admin/enterprise-clients/error-summary
// Get error summary for ALL clients (for main enterprise page widget)
export async function GET(request: NextRequest) {
const session = await auth()
if (!session || session.user.userType !== 'staff') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
try {
const summaries = await errorDashboardService.getAllClientsErrorSummary()
// Calculate totals
const totals = summaries.reduce(
(acc, client) => ({
criticalErrors24h: acc.criticalErrors24h + client.criticalErrors24h,
totalErrors24h: acc.totalErrors24h + client.totalErrors24h,
crashes24h: acc.crashes24h + client.crashes24h,
clientsWithIssues: acc.clientsWithIssues + (client.criticalErrors24h > 0 || client.crashes24h > 0 ? 1 : 0),
}),
{ criticalErrors24h: 0, totalErrors24h: 0, crashes24h: 0, clientsWithIssues: 0 }
)
// Calculate overall trend
const increasingCount = summaries.filter(s => s.errorTrend === 'increasing').length
const decreasingCount = summaries.filter(s => s.errorTrend === 'decreasing').length
let overallTrend: 'increasing' | 'decreasing' | 'stable' = 'stable'
if (increasingCount > decreasingCount) {
overallTrend = 'increasing'
} else if (decreasingCount > increasingCount) {
overallTrend = 'decreasing'
}
return NextResponse.json({
clients: summaries,
totals: {
...totals,
overallTrend,
totalClients: summaries.length,
},
})
} catch (error) {
console.error('Failed to get error summary:', error)
return NextResponse.json(
{ error: 'Failed to get error summary' },
{ status: 500 }
)
}
}