From bc7cd484367f58276a125a1ede701538eae62046 Mon Sep 17 00:00:00 2001 From: enlineawork Date: Mon, 7 Sep 2026 22:56:20 -0300 Subject: [PATCH] F4: align dashboard counters with technical and INF workflows --- api-v3/src/dashboard/dashboard.service.ts | 99 +++++++++++++---------- 1 file changed, 58 insertions(+), 41 deletions(-) diff --git a/api-v3/src/dashboard/dashboard.service.ts b/api-v3/src/dashboard/dashboard.service.ts index 4db8e0a..56e5daa 100644 --- a/api-v3/src/dashboard/dashboard.service.ts +++ b/api-v3/src/dashboard/dashboard.service.ts @@ -19,12 +19,12 @@ interface DashboardCountsRow { inactiveUsers: number | string; activeSessions: number | string; openFindings: number | string; - awaitingCompanyResponse: number | string; - overdueCompanyResponses: number | string; - companyResponsesDueNext7Days: number | string; - awaitingVerificationSchedule: number | string; + findingsWithoutControlDate: number | string; overdueControls: number | string; controlsNext30Days: number | string; + reportsWorking: number | string; + reportsOfficialized: number | string; + sealedActsWithoutReport: number | string; } export interface DashboardUpcomingControl { @@ -65,43 +65,54 @@ export class DashboardService { SELECT COUNT(*) FROM inspection_findings WHERE status = 'OPEN' ) AS "openFindings", ( - SELECT COUNT(*) FROM inspection_findings - WHERE status = 'OPEN' AND company_response_received_on IS NULL - ) AS "awaitingCompanyResponse", + SELECT COUNT(*) + FROM inspection_findings finding + WHERE finding.status = 'OPEN' + AND finding.next_control_on IS NULL + AND COALESCE(( + SELECT verification.outcome + FROM inspection_finding_verification_visits verification + WHERE verification.finding_id=finding.id AND verification.outcome IS NOT NULL + ORDER BY verification.result_recorded_at DESC NULLS LAST, verification.created_at DESC, verification.id DESC + LIMIT 1 + ), '') <> 'RESOLVED' + ) AS "findingsWithoutControlDate", ( - SELECT COUNT(*) FROM inspection_findings - WHERE status = 'OPEN' - AND company_response_received_on IS NULL - AND correction_due_on IS NOT NULL - AND correction_due_on < (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date - ) AS "overdueCompanyResponses", - ( - SELECT COUNT(*) FROM inspection_findings - WHERE status = 'OPEN' - AND company_response_received_on IS NULL - AND correction_due_on BETWEEN - (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date - AND (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date + 7 - ) AS "companyResponsesDueNext7Days", - ( - SELECT COUNT(*) FROM inspection_findings - WHERE status = 'OPEN' - AND company_response_received_on IS NOT NULL - AND next_control_on IS NULL - ) AS "awaitingVerificationSchedule", - ( - SELECT COUNT(*) FROM inspection_findings - WHERE status = 'OPEN' - AND company_response_received_on IS NOT NULL - AND next_control_on < (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date + SELECT COUNT(*) + FROM inspection_findings finding + WHERE finding.status = 'OPEN' + AND finding.next_control_on < (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date + AND COALESCE(( + SELECT verification.outcome + FROM inspection_finding_verification_visits verification + WHERE verification.finding_id=finding.id AND verification.outcome IS NOT NULL + ORDER BY verification.result_recorded_at DESC NULLS LAST, verification.created_at DESC, verification.id DESC + LIMIT 1 + ), '') <> 'RESOLVED' ) AS "overdueControls", ( - SELECT COUNT(*) FROM inspection_findings - WHERE status = 'OPEN' - AND next_control_on BETWEEN + SELECT COUNT(*) + FROM inspection_findings finding + WHERE finding.status = 'OPEN' + AND finding.next_control_on BETWEEN (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date AND (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date + 30 - ) AS "controlsNext30Days" + AND COALESCE(( + SELECT verification.outcome + FROM inspection_finding_verification_visits verification + WHERE verification.finding_id=finding.id AND verification.outcome IS NOT NULL + ORDER BY verification.result_recorded_at DESC NULLS LAST, verification.created_at DESC, verification.id DESC + LIMIT 1 + ), '') <> 'RESOLVED' + ) AS "controlsNext30Days", + (SELECT COUNT(*) FROM inspection_reports WHERE status='WORKING') AS "reportsWorking", + (SELECT COUNT(*) FROM inspection_reports WHERE status='OFFICIALIZED') AS "reportsOfficialized", + ( + SELECT COUNT(*) + FROM inspection_acts act + WHERE act.status='SEALED' + AND NOT EXISTS (SELECT 1 FROM inspection_reports report WHERE report.act_id=act.id) + ) AS "sealedActsWithoutReport" `)) as DashboardCountsRow[]; const recentAudit = (await manager.query(` @@ -135,8 +146,14 @@ export class DashboardService { INNER JOIN inspection_visits visit ON visit.id = act.visit_id INNER JOIN assets asset ON asset.id = finding.asset_id WHERE finding.status = 'OPEN' - AND finding.company_response_received_on IS NOT NULL AND finding.next_control_on IS NOT NULL + AND COALESCE(( + SELECT verification.outcome + FROM inspection_finding_verification_visits verification + WHERE verification.finding_id=finding.id AND verification.outcome IS NOT NULL + ORDER BY verification.result_recorded_at DESC NULLS LAST, verification.created_at DESC, verification.id DESC + LIMIT 1 + ), '') <> 'RESOLVED' ORDER BY finding.next_control_on, finding.code LIMIT 8 `)) as DashboardUpcomingControl[]; @@ -151,12 +168,12 @@ export class DashboardService { inactiveUsers: Number(counts?.inactiveUsers ?? 0), activeSessions: Number(counts?.activeSessions ?? 0), openFindings: Number(counts?.openFindings ?? 0), - awaitingCompanyResponse: Number(counts?.awaitingCompanyResponse ?? 0), - overdueCompanyResponses: Number(counts?.overdueCompanyResponses ?? 0), - companyResponsesDueNext7Days: Number(counts?.companyResponsesDueNext7Days ?? 0), - awaitingVerificationSchedule: Number(counts?.awaitingVerificationSchedule ?? 0), + findingsWithoutControlDate: Number(counts?.findingsWithoutControlDate ?? 0), overdueControls: Number(counts?.overdueControls ?? 0), controlsNext30Days: Number(counts?.controlsNext30Days ?? 0), + reportsWorking: Number(counts?.reportsWorking ?? 0), + reportsOfficialized: Number(counts?.reportsOfficialized ?? 0), + sealedActsWithoutReport: Number(counts?.sealedActsWithoutReport ?? 0), }, recentAudit, upcomingControls,