fix: supervisor dashboard — dedup incident ID queries, accurate counts, due_date null guard
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V5pD9CZzUjknw5nxzX5abr
This commit is contained in:
@@ -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 { 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'
|
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 [
|
const [
|
||||||
{ data: openIncidents },
|
{ data: openIncidents },
|
||||||
{ data: closedIncidents },
|
{ count: closedCount },
|
||||||
|
{ count: openCount },
|
||||||
{ data: overdueCapas },
|
{ data: overdueCapas },
|
||||||
{ data: allCapas },
|
{ data: allCapas },
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
@@ -61,28 +69,27 @@ export default async function SupervisorPage() {
|
|||||||
.limit(10),
|
.limit(10),
|
||||||
supabase
|
supabase
|
||||||
.from('incidents')
|
.from('incidents')
|
||||||
.select('id')
|
.select('*', { count: 'exact', head: true })
|
||||||
.eq('site_id', profile.site_id)
|
.eq('site_id', profile.site_id)
|
||||||
.eq('status', 'closed'),
|
.eq('status', 'closed'),
|
||||||
|
supabase
|
||||||
|
.from('incidents')
|
||||||
|
.select('*', { count: 'exact', head: true })
|
||||||
|
.eq('site_id', profile.site_id)
|
||||||
|
.neq('status', 'closed'),
|
||||||
supabase
|
supabase
|
||||||
.from('capa_actions')
|
.from('capa_actions')
|
||||||
.select('id, description, due_date, users (name)')
|
.select('id, description, due_date, users (name)')
|
||||||
.eq('status', 'overdue')
|
.eq('status', 'overdue')
|
||||||
.in(
|
.in('incident_id', incidentIds),
|
||||||
'incident_id',
|
|
||||||
(await supabase.from('incidents').select('id').eq('site_id', profile.site_id)).data?.map(r => r.id) ?? []
|
|
||||||
),
|
|
||||||
supabase
|
supabase
|
||||||
.from('capa_actions')
|
.from('capa_actions')
|
||||||
.select('id, status')
|
.select('id, status')
|
||||||
.in(
|
.in('incident_id', incidentIds),
|
||||||
'incident_id',
|
|
||||||
(await supabase.from('incidents').select('id').eq('site_id', profile.site_id)).data?.map(r => r.id) ?? []
|
|
||||||
),
|
|
||||||
])
|
])
|
||||||
|
|
||||||
const openCount = openIncidents?.length ?? 0
|
const resolvedOpenCount = openCount ?? 0
|
||||||
const closedCount = closedIncidents?.length ?? 0
|
const resolvedClosedCount = closedCount ?? 0
|
||||||
const overdueCount = overdueCapas?.length ?? 0
|
const overdueCount = overdueCapas?.length ?? 0
|
||||||
|
|
||||||
const capaRows = allCapas ?? []
|
const capaRows = allCapas ?? []
|
||||||
@@ -108,8 +115,8 @@ export default async function SupervisorPage() {
|
|||||||
|
|
||||||
{/* Stats */}
|
{/* Stats */}
|
||||||
<div className="grid grid-cols-2 gap-4 sm:grid-cols-4 mb-6">
|
<div className="grid grid-cols-2 gap-4 sm:grid-cols-4 mb-6">
|
||||||
<StatCard label="Open Incidents" value={openCount} accent={openCount > 0 ? 'yellow' : 'green'} />
|
<StatCard label="Open Incidents" value={resolvedOpenCount} accent={resolvedOpenCount > 0 ? 'yellow' : 'green'} />
|
||||||
<StatCard label="Closed Incidents" value={closedCount} accent="green" />
|
<StatCard label="Closed Incidents" value={resolvedClosedCount} accent="green" />
|
||||||
<StatCard label="Open CAPAs" value={capaOpenCount} accent={capaOpenCount > 0 ? 'yellow' : 'green'} />
|
<StatCard label="Open CAPAs" value={capaOpenCount} accent={capaOpenCount > 0 ? 'yellow' : 'green'} />
|
||||||
<StatCard label="CAPA Overdue" value={overdueCount} accent={overdueCount > 0 ? 'red' : 'green'} />
|
<StatCard label="CAPA Overdue" value={overdueCount} accent={overdueCount > 0 ? 'red' : 'green'} />
|
||||||
</div>
|
</div>
|
||||||
@@ -154,7 +161,7 @@ export default async function SupervisorPage() {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<span className="text-xs text-red-600 font-semibold shrink-0">
|
<span className="text-xs text-red-600 font-semibold shrink-0">
|
||||||
Due {new Date(capa.due_date as string).toLocaleDateString('en-MY')}
|
Due {capa.due_date ? new Date(capa.due_date as string).toLocaleDateString('en-MY') : '—'}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user