F4: align dashboard counters with technical and INF workflows

This commit is contained in:
2026-09-07 22:56:20 -03:00
parent 44eccaa36c
commit bc7cd48436
+58 -41
View File
@@ -19,12 +19,12 @@ interface DashboardCountsRow {
inactiveUsers: number | string; inactiveUsers: number | string;
activeSessions: number | string; activeSessions: number | string;
openFindings: number | string; openFindings: number | string;
awaitingCompanyResponse: number | string; findingsWithoutControlDate: number | string;
overdueCompanyResponses: number | string;
companyResponsesDueNext7Days: number | string;
awaitingVerificationSchedule: number | string;
overdueControls: number | string; overdueControls: number | string;
controlsNext30Days: number | string; controlsNext30Days: number | string;
reportsWorking: number | string;
reportsOfficialized: number | string;
sealedActsWithoutReport: number | string;
} }
export interface DashboardUpcomingControl { export interface DashboardUpcomingControl {
@@ -65,43 +65,54 @@ export class DashboardService {
SELECT COUNT(*) FROM inspection_findings WHERE status = 'OPEN' SELECT COUNT(*) FROM inspection_findings WHERE status = 'OPEN'
) AS "openFindings", ) AS "openFindings",
( (
SELECT COUNT(*) FROM inspection_findings SELECT COUNT(*)
WHERE status = 'OPEN' AND company_response_received_on IS NULL FROM inspection_findings finding
) AS "awaitingCompanyResponse", 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 SELECT COUNT(*)
WHERE status = 'OPEN' FROM inspection_findings finding
AND company_response_received_on IS NULL WHERE finding.status = 'OPEN'
AND correction_due_on IS NOT NULL AND finding.next_control_on < (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date
AND correction_due_on < (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date AND COALESCE((
) AS "overdueCompanyResponses", SELECT verification.outcome
( FROM inspection_finding_verification_visits verification
SELECT COUNT(*) FROM inspection_findings WHERE verification.finding_id=finding.id AND verification.outcome IS NOT NULL
WHERE status = 'OPEN' ORDER BY verification.result_recorded_at DESC NULLS LAST, verification.created_at DESC, verification.id DESC
AND company_response_received_on IS NULL LIMIT 1
AND correction_due_on BETWEEN ), '') <> 'RESOLVED'
(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
) AS "overdueControls", ) AS "overdueControls",
( (
SELECT COUNT(*) FROM inspection_findings SELECT COUNT(*)
WHERE status = 'OPEN' FROM inspection_findings finding
AND next_control_on BETWEEN WHERE finding.status = 'OPEN'
AND finding.next_control_on BETWEEN
(CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date
AND (CURRENT_TIMESTAMP AT TIME ZONE 'America/Argentina/Mendoza')::date + 30 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[]; `)) as DashboardCountsRow[];
const recentAudit = (await manager.query(` 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 inspection_visits visit ON visit.id = act.visit_id
INNER JOIN assets asset ON asset.id = finding.asset_id INNER JOIN assets asset ON asset.id = finding.asset_id
WHERE finding.status = 'OPEN' WHERE finding.status = 'OPEN'
AND finding.company_response_received_on IS NOT NULL
AND finding.next_control_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 ORDER BY finding.next_control_on, finding.code
LIMIT 8 LIMIT 8
`)) as DashboardUpcomingControl[]; `)) as DashboardUpcomingControl[];
@@ -151,12 +168,12 @@ export class DashboardService {
inactiveUsers: Number(counts?.inactiveUsers ?? 0), inactiveUsers: Number(counts?.inactiveUsers ?? 0),
activeSessions: Number(counts?.activeSessions ?? 0), activeSessions: Number(counts?.activeSessions ?? 0),
openFindings: Number(counts?.openFindings ?? 0), openFindings: Number(counts?.openFindings ?? 0),
awaitingCompanyResponse: Number(counts?.awaitingCompanyResponse ?? 0), findingsWithoutControlDate: Number(counts?.findingsWithoutControlDate ?? 0),
overdueCompanyResponses: Number(counts?.overdueCompanyResponses ?? 0),
companyResponsesDueNext7Days: Number(counts?.companyResponsesDueNext7Days ?? 0),
awaitingVerificationSchedule: Number(counts?.awaitingVerificationSchedule ?? 0),
overdueControls: Number(counts?.overdueControls ?? 0), overdueControls: Number(counts?.overdueControls ?? 0),
controlsNext30Days: Number(counts?.controlsNext30Days ?? 0), controlsNext30Days: Number(counts?.controlsNext30Days ?? 0),
reportsWorking: Number(counts?.reportsWorking ?? 0),
reportsOfficialized: Number(counts?.reportsOfficialized ?? 0),
sealedActsWithoutReport: Number(counts?.sealedActsWithoutReport ?? 0),
}, },
recentAudit, recentAudit,
upcomingControls, upcomingControls,