diff --git a/app/(protected)/supervisor/page.tsx b/app/(protected)/supervisor/page.tsx index af47a01..92906ca 100644 --- a/app/(protected)/supervisor/page.tsx +++ b/app/(protected)/supervisor/page.tsx @@ -46,9 +46,17 @@ export default async function SupervisorPage() { const { data: siteRow } = await supabase.from('sites').select('name').eq('id', profile.site_id).single() const siteName = (siteRow as unknown as { name: string } | null)?.name ?? 'Your Site' + // Resolve incident IDs once to avoid duplicate queries inside Promise.all + const { data: siteIncidents } = await supabase + .from('incidents') + .select('id') + .eq('site_id', profile.site_id) + const incidentIds = siteIncidents?.map(r => r.id) ?? [] + const [ { data: openIncidents }, - { data: closedIncidents }, + { count: closedCount }, + { count: openCount }, { data: overdueCapas }, { data: allCapas }, ] = await Promise.all([ @@ -61,28 +69,27 @@ export default async function SupervisorPage() { .limit(10), supabase .from('incidents') - .select('id') + .select('*', { count: 'exact', head: true }) .eq('site_id', profile.site_id) .eq('status', 'closed'), + supabase + .from('incidents') + .select('*', { count: 'exact', head: true }) + .eq('site_id', profile.site_id) + .neq('status', 'closed'), supabase .from('capa_actions') .select('id, description, due_date, users (name)') .eq('status', 'overdue') - .in( - 'incident_id', - (await supabase.from('incidents').select('id').eq('site_id', profile.site_id)).data?.map(r => r.id) ?? [] - ), + .in('incident_id', incidentIds), supabase .from('capa_actions') .select('id, status') - .in( - 'incident_id', - (await supabase.from('incidents').select('id').eq('site_id', profile.site_id)).data?.map(r => r.id) ?? [] - ), + .in('incident_id', incidentIds), ]) - const openCount = openIncidents?.length ?? 0 - const closedCount = closedIncidents?.length ?? 0 + const resolvedOpenCount = openCount ?? 0 + const resolvedClosedCount = closedCount ?? 0 const overdueCount = overdueCapas?.length ?? 0 const capaRows = allCapas ?? [] @@ -108,8 +115,8 @@ export default async function SupervisorPage() { {/* Stats */}