Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8c002f36a | ||
|
|
da7d8a9149 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dhv2-api",
|
"name": "dhv2-api",
|
||||||
"version": "0.20.0-2",
|
"version": "0.20.0-3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import { AssetImportsModule } from './asset-imports/asset-imports.module';
|
|||||||
import { InspectionReportsModule } from './inspection-reports/inspection-reports.module';
|
import { InspectionReportsModule } from './inspection-reports/inspection-reports.module';
|
||||||
import { InspectionVerificationsModule } from './inspection-verifications/inspection-verifications.module';
|
import { InspectionVerificationsModule } from './inspection-verifications/inspection-verifications.module';
|
||||||
import { ActAdministrationModule } from './act-administration/act-administration.module';
|
import { ActAdministrationModule } from './act-administration/act-administration.module';
|
||||||
|
import { FieldBriefingModule } from './field-briefing/field-briefing.module';
|
||||||
|
|
||||||
function required(config: ConfigService, key: string): string {
|
function required(config: ConfigService, key: string): string {
|
||||||
const value = config.get<string>(key);
|
const value = config.get<string>(key);
|
||||||
@@ -78,6 +79,7 @@ function required(config: ConfigService, key: string): string {
|
|||||||
InspectionReportsModule,
|
InspectionReportsModule,
|
||||||
InspectionVerificationsModule,
|
InspectionVerificationsModule,
|
||||||
ActAdministrationModule,
|
ActAdministrationModule,
|
||||||
|
FieldBriefingModule,
|
||||||
AssetImportsModule,
|
AssetImportsModule,
|
||||||
],
|
],
|
||||||
controllers: [HealthController],
|
controllers: [HealthController],
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { Controller, Get, Param, ParseUUIDPipe } from '@nestjs/common';
|
||||||
|
import { RequirePermissions } from '../authorization/decorators/require-permissions.decorator';
|
||||||
|
import { FieldBriefingService } from './field-briefing.service';
|
||||||
|
|
||||||
|
@Controller('inspection-visits/:visitId/field-briefing')
|
||||||
|
export class FieldBriefingController {
|
||||||
|
constructor(private readonly briefing: FieldBriefingService) {}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
@RequirePermissions('inspections.read')
|
||||||
|
get(@Param('visitId', new ParseUUIDPipe({ version: '4' })) visitId: string) {
|
||||||
|
return this.briefing.getForVisit(visitId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { FieldBriefingController } from './field-briefing.controller';
|
||||||
|
import { FieldBriefingService } from './field-briefing.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
controllers: [FieldBriefingController],
|
||||||
|
providers: [FieldBriefingService],
|
||||||
|
})
|
||||||
|
export class FieldBriefingModule {}
|
||||||
@@ -0,0 +1,310 @@
|
|||||||
|
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
|
export type FieldBriefingAdminState =
|
||||||
|
| 'NEW'
|
||||||
|
| 'WAITING_RESPONSE'
|
||||||
|
| 'DUE_SOON'
|
||||||
|
| 'OVERDUE'
|
||||||
|
| 'RESPONSE_RECEIVED'
|
||||||
|
| 'VERIFICATION_PENDING'
|
||||||
|
| 'COMMITMENT_OVERDUE';
|
||||||
|
|
||||||
|
export type FieldBriefingReviewState = 'REQUIRED' | 'UPCOMING' | 'CONTEXT';
|
||||||
|
|
||||||
|
interface BriefingVisitRow {
|
||||||
|
id: string;
|
||||||
|
code: string;
|
||||||
|
status: string;
|
||||||
|
plannedStartAt: Date | null;
|
||||||
|
areaId: string | null;
|
||||||
|
areaCode: string | null;
|
||||||
|
areaName: string | null;
|
||||||
|
companyId: string | null;
|
||||||
|
companyCode: string | null;
|
||||||
|
companyName: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BriefingFindingRow {
|
||||||
|
sourceActId: string;
|
||||||
|
sourceActCode: string;
|
||||||
|
sourceActOccurredAt: Date;
|
||||||
|
sourceVisitId: string;
|
||||||
|
sourceVisitCode: string;
|
||||||
|
responseDueOn: string | null;
|
||||||
|
responseReceivedOn: string | null;
|
||||||
|
committedCorrectionOn: string | null;
|
||||||
|
latestResponseId: string | null;
|
||||||
|
responseHasPdf: boolean;
|
||||||
|
adminState: FieldBriefingAdminState;
|
||||||
|
reviewState: FieldBriefingReviewState;
|
||||||
|
reviewReason: string;
|
||||||
|
priority: number;
|
||||||
|
findingId: string;
|
||||||
|
findingCode: string;
|
||||||
|
findingTitle: string;
|
||||||
|
severity: number | null;
|
||||||
|
nextControlOn: string | null;
|
||||||
|
latestVerificationOutcome: string | null;
|
||||||
|
assetId: string;
|
||||||
|
assetCode: string;
|
||||||
|
assetName: string;
|
||||||
|
assetTypeName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class FieldBriefingService {
|
||||||
|
constructor(private readonly dataSource: DataSource) {}
|
||||||
|
|
||||||
|
async getForVisit(visitId: string) {
|
||||||
|
const [visit] = (await this.dataSource.query(`
|
||||||
|
SELECT
|
||||||
|
visit.id,
|
||||||
|
visit.code,
|
||||||
|
visit.status,
|
||||||
|
visit.planned_start_at AS "plannedStartAt",
|
||||||
|
visit.operational_area_id AS "areaId",
|
||||||
|
area.code AS "areaCode",
|
||||||
|
area.name AS "areaName",
|
||||||
|
visit.operator_company_id AS "companyId",
|
||||||
|
company.code AS "companyCode",
|
||||||
|
COALESCE(profile.legal_name, company.name) AS "companyName"
|
||||||
|
FROM inspection_visits visit
|
||||||
|
LEFT JOIN assets area ON area.id = visit.operational_area_id
|
||||||
|
LEFT JOIN assets company ON company.id = visit.operator_company_id
|
||||||
|
LEFT JOIN organization_profiles profile ON profile.asset_id = company.id
|
||||||
|
WHERE visit.id = $1
|
||||||
|
`, [visitId])) as BriefingVisitRow[];
|
||||||
|
|
||||||
|
if (!visit) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
code: 'INSPECTION_VISIT_NOT_FOUND',
|
||||||
|
message: 'Inspección no encontrada',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!visit.areaId || !visit.companyId) {
|
||||||
|
throw new BadRequestException({
|
||||||
|
code: 'FIELD_BRIEFING_CONTEXT_REQUIRED',
|
||||||
|
message: 'La inspección debe tener Área y Operadora antes de preparar el paquete de campo',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const referenceAt = visit.plannedStartAt ?? new Date();
|
||||||
|
const referenceDate = referenceAt.toISOString().slice(0, 10);
|
||||||
|
|
||||||
|
const rows = (await this.dataSource.query(`
|
||||||
|
SELECT
|
||||||
|
act.id AS "sourceActId",
|
||||||
|
act.code AS "sourceActCode",
|
||||||
|
act.occurred_at AS "sourceActOccurredAt",
|
||||||
|
source_visit.id AS "sourceVisitId",
|
||||||
|
source_visit.code AS "sourceVisitCode",
|
||||||
|
deadline.response_due_on AS "responseDueOn",
|
||||||
|
response.received_on AS "responseReceivedOn",
|
||||||
|
response.committed_correction_on AS "committedCorrectionOn",
|
||||||
|
response.id AS "latestResponseId",
|
||||||
|
(response.stored_name IS NOT NULL) AS "responseHasPdf",
|
||||||
|
CASE
|
||||||
|
WHEN response.id IS NOT NULL
|
||||||
|
AND response.committed_correction_on IS NOT NULL
|
||||||
|
AND response.committed_correction_on < $5::date
|
||||||
|
THEN 'COMMITMENT_OVERDUE'
|
||||||
|
WHEN response.id IS NOT NULL AND finding.next_control_on IS NULL
|
||||||
|
THEN 'VERIFICATION_PENDING'
|
||||||
|
WHEN response.id IS NOT NULL THEN 'RESPONSE_RECEIVED'
|
||||||
|
WHEN deadline.id IS NULL THEN 'NEW'
|
||||||
|
WHEN deadline.response_due_on < $5::date THEN 'OVERDUE'
|
||||||
|
WHEN deadline.response_due_on <= ($5::date + 3) THEN 'DUE_SOON'
|
||||||
|
ELSE 'WAITING_RESPONSE'
|
||||||
|
END AS "adminState",
|
||||||
|
CASE
|
||||||
|
WHEN COALESCE(verification.outcome, '') <> 'RESOLVED'
|
||||||
|
AND finding.next_control_on IS NOT NULL
|
||||||
|
AND finding.next_control_on <= $5::date
|
||||||
|
THEN 'REQUIRED'
|
||||||
|
WHEN response.id IS NOT NULL
|
||||||
|
AND response.committed_correction_on IS NOT NULL
|
||||||
|
AND response.committed_correction_on <= $5::date
|
||||||
|
THEN 'REQUIRED'
|
||||||
|
WHEN response.id IS NOT NULL AND finding.next_control_on IS NULL
|
||||||
|
THEN 'REQUIRED'
|
||||||
|
WHEN COALESCE(verification.outcome, '') <> 'RESOLVED'
|
||||||
|
AND finding.next_control_on IS NOT NULL
|
||||||
|
AND finding.next_control_on <= ($5::date + 30)
|
||||||
|
THEN 'UPCOMING'
|
||||||
|
ELSE 'CONTEXT'
|
||||||
|
END AS "reviewState",
|
||||||
|
CASE
|
||||||
|
WHEN COALESCE(verification.outcome, '') <> 'RESOLVED'
|
||||||
|
AND finding.next_control_on IS NOT NULL
|
||||||
|
AND finding.next_control_on <= $5::date
|
||||||
|
THEN 'CONTROL_OVERDUE'
|
||||||
|
WHEN response.id IS NOT NULL
|
||||||
|
AND response.committed_correction_on IS NOT NULL
|
||||||
|
AND response.committed_correction_on <= $5::date
|
||||||
|
THEN 'COMMITMENT_REACHED'
|
||||||
|
WHEN response.id IS NOT NULL AND finding.next_control_on IS NULL
|
||||||
|
THEN 'RESPONSE_WITHOUT_CONTROL'
|
||||||
|
WHEN COALESCE(verification.outcome, '') <> 'RESOLVED'
|
||||||
|
AND finding.next_control_on IS NOT NULL
|
||||||
|
AND finding.next_control_on <= ($5::date + 30)
|
||||||
|
THEN 'CONTROL_UPCOMING'
|
||||||
|
WHEN response.id IS NULL
|
||||||
|
AND deadline.response_due_on IS NOT NULL
|
||||||
|
AND deadline.response_due_on < $5::date
|
||||||
|
THEN 'ADMIN_RESPONSE_OVERDUE'
|
||||||
|
ELSE 'CONTEXT_ONLY'
|
||||||
|
END AS "reviewReason",
|
||||||
|
CASE
|
||||||
|
WHEN COALESCE(verification.outcome, '') <> 'RESOLVED'
|
||||||
|
AND finding.next_control_on IS NOT NULL
|
||||||
|
AND finding.next_control_on <= $5::date THEN 1
|
||||||
|
WHEN response.id IS NOT NULL
|
||||||
|
AND response.committed_correction_on IS NOT NULL
|
||||||
|
AND response.committed_correction_on <= $5::date THEN 2
|
||||||
|
WHEN response.id IS NOT NULL AND finding.next_control_on IS NULL THEN 3
|
||||||
|
WHEN COALESCE(verification.outcome, '') <> 'RESOLVED'
|
||||||
|
AND finding.next_control_on IS NOT NULL
|
||||||
|
AND finding.next_control_on <= ($5::date + 30) THEN 4
|
||||||
|
WHEN response.id IS NULL
|
||||||
|
AND deadline.response_due_on IS NOT NULL
|
||||||
|
AND deadline.response_due_on < $5::date THEN 5
|
||||||
|
ELSE 6
|
||||||
|
END AS priority,
|
||||||
|
finding.id AS "findingId",
|
||||||
|
finding.code AS "findingCode",
|
||||||
|
finding.title AS "findingTitle",
|
||||||
|
finding.severity,
|
||||||
|
finding.next_control_on AS "nextControlOn",
|
||||||
|
verification.outcome AS "latestVerificationOutcome",
|
||||||
|
asset.id AS "assetId",
|
||||||
|
asset.code AS "assetCode",
|
||||||
|
asset.name AS "assetName",
|
||||||
|
asset_type.name AS "assetTypeName"
|
||||||
|
FROM inspection_acts act
|
||||||
|
INNER JOIN inspection_visits source_visit ON source_visit.id = act.visit_id
|
||||||
|
INNER JOIN inspection_findings finding ON finding.act_id = act.id
|
||||||
|
INNER JOIN assets asset ON asset.id = finding.asset_id
|
||||||
|
INNER JOIN asset_types asset_type ON asset_type.id = asset.asset_type_id
|
||||||
|
LEFT JOIN LATERAL (
|
||||||
|
SELECT event.id, event.response_due_on
|
||||||
|
FROM inspection_act_deadline_events event
|
||||||
|
WHERE event.act_id = act.id
|
||||||
|
ORDER BY event.created_at DESC, event.id DESC
|
||||||
|
LIMIT 1
|
||||||
|
) deadline ON true
|
||||||
|
LEFT JOIN LATERAL (
|
||||||
|
SELECT company_response.id, company_response.received_on,
|
||||||
|
company_response.committed_correction_on, company_response.stored_name
|
||||||
|
FROM inspection_act_company_responses company_response
|
||||||
|
WHERE company_response.act_id = act.id
|
||||||
|
ORDER BY company_response.received_on DESC,
|
||||||
|
company_response.created_at DESC,
|
||||||
|
company_response.id DESC
|
||||||
|
LIMIT 1
|
||||||
|
) response ON true
|
||||||
|
LEFT JOIN LATERAL (
|
||||||
|
SELECT verification_visit.outcome
|
||||||
|
FROM inspection_finding_verification_visits verification_visit
|
||||||
|
WHERE verification_visit.finding_id = finding.id
|
||||||
|
AND verification_visit.outcome IS NOT NULL
|
||||||
|
ORDER BY verification_visit.result_recorded_at DESC NULLS LAST,
|
||||||
|
verification_visit.created_at DESC,
|
||||||
|
verification_visit.id DESC
|
||||||
|
LIMIT 1
|
||||||
|
) verification ON true
|
||||||
|
WHERE source_visit.operational_area_id = $2
|
||||||
|
AND source_visit.operator_company_id = $3
|
||||||
|
AND source_visit.id <> $1
|
||||||
|
AND act.status IN ('CLOSED', 'RECTIFIED')
|
||||||
|
AND finding.status = 'OPEN'
|
||||||
|
AND COALESCE(act.closed_at, act.occurred_at) <= $4::timestamptz
|
||||||
|
ORDER BY priority, act.occurred_at, act.act_number, finding.finding_number
|
||||||
|
`, [visit.id, visit.areaId, visit.companyId, referenceAt.toISOString(), referenceDate])) as BriefingFindingRow[];
|
||||||
|
|
||||||
|
const actMap = new Map<string, {
|
||||||
|
actId: string;
|
||||||
|
actCode: string;
|
||||||
|
occurredAt: Date;
|
||||||
|
sourceVisit: { id: string; code: string };
|
||||||
|
adminState: FieldBriefingAdminState;
|
||||||
|
responseDueOn: string | null;
|
||||||
|
latestResponse: null | {
|
||||||
|
id: string;
|
||||||
|
receivedOn: string;
|
||||||
|
committedCorrectionOn: string | null;
|
||||||
|
hasPdf: boolean;
|
||||||
|
};
|
||||||
|
findings: Array<Record<string, unknown>>;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
let act = actMap.get(row.sourceActId);
|
||||||
|
if (!act) {
|
||||||
|
act = {
|
||||||
|
actId: row.sourceActId,
|
||||||
|
actCode: row.sourceActCode,
|
||||||
|
occurredAt: row.sourceActOccurredAt,
|
||||||
|
sourceVisit: { id: row.sourceVisitId, code: row.sourceVisitCode },
|
||||||
|
adminState: row.adminState,
|
||||||
|
responseDueOn: row.responseDueOn,
|
||||||
|
latestResponse: row.latestResponseId && row.responseReceivedOn ? {
|
||||||
|
id: row.latestResponseId,
|
||||||
|
receivedOn: row.responseReceivedOn,
|
||||||
|
committedCorrectionOn: row.committedCorrectionOn,
|
||||||
|
hasPdf: Boolean(row.responseHasPdf),
|
||||||
|
} : null,
|
||||||
|
findings: [],
|
||||||
|
};
|
||||||
|
actMap.set(row.sourceActId, act);
|
||||||
|
}
|
||||||
|
act.findings.push({
|
||||||
|
id: row.findingId,
|
||||||
|
code: row.findingCode,
|
||||||
|
title: row.findingTitle,
|
||||||
|
severity: row.severity,
|
||||||
|
nextControlOn: row.nextControlOn,
|
||||||
|
latestVerificationOutcome: row.latestVerificationOutcome,
|
||||||
|
reviewState: row.reviewState,
|
||||||
|
reviewReason: row.reviewReason,
|
||||||
|
priority: Number(row.priority),
|
||||||
|
asset: {
|
||||||
|
id: row.assetId,
|
||||||
|
code: row.assetCode,
|
||||||
|
name: row.assetName,
|
||||||
|
typeName: row.assetTypeName,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const acts = Array.from(actMap.values());
|
||||||
|
const uniqueAssets = new Set(rows.map((row) => row.assetId));
|
||||||
|
const required = rows.filter((row) => row.reviewState === 'REQUIRED').length;
|
||||||
|
const upcoming = rows.filter((row) => row.reviewState === 'UPCOMING').length;
|
||||||
|
const adminAttention = rows.filter((row) =>
|
||||||
|
['OVERDUE', 'DUE_SOON', 'COMMITMENT_OVERDUE'].includes(row.adminState),
|
||||||
|
).length;
|
||||||
|
|
||||||
|
return {
|
||||||
|
visit: {
|
||||||
|
id: visit.id,
|
||||||
|
code: visit.code,
|
||||||
|
status: visit.status,
|
||||||
|
plannedStartAt: visit.plannedStartAt,
|
||||||
|
operationalArea: { id: visit.areaId, code: visit.areaCode, name: visit.areaName },
|
||||||
|
operatorCompany: { id: visit.companyId, code: visit.companyCode, name: visit.companyName },
|
||||||
|
},
|
||||||
|
referenceDate,
|
||||||
|
generatedAt: new Date().toISOString(),
|
||||||
|
summary: {
|
||||||
|
actCount: acts.length,
|
||||||
|
openFindingCount: rows.length,
|
||||||
|
fieldReviewRequired: required,
|
||||||
|
fieldReviewUpcoming: upcoming,
|
||||||
|
administrativeAttention: adminAttention,
|
||||||
|
assetCount: uniqueAssets.size,
|
||||||
|
},
|
||||||
|
acts,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
export const API_VERSION = '0.20.0-2';
|
export const API_VERSION = '0.20.0-3';
|
||||||
export const API_PHASE = 'F1.2';
|
export const API_PHASE = 'F1.3';
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { readFileSync } from 'node:fs';
|
||||||
|
import { resolve } from 'node:path';
|
||||||
|
import test from 'node:test';
|
||||||
|
|
||||||
|
const source = (path: string) => readFileSync(resolve(process.cwd(), path), 'utf8');
|
||||||
|
const service = source('src/field-briefing/field-briefing.service.ts');
|
||||||
|
const controller = source('src/field-briefing/field-briefing.controller.ts');
|
||||||
|
|
||||||
|
test('F1.3 prepares field briefing from prior Acts in the same operational context', () => {
|
||||||
|
assert.match(service, /source_visit\.operational_area_id = \$2/);
|
||||||
|
assert.match(service, /source_visit\.operator_company_id = \$3/);
|
||||||
|
assert.match(service, /source_visit\.id <> \$1/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F1.3 uses Act-level deadline and company response ledgers', () => {
|
||||||
|
assert.match(service, /inspection_act_deadline_events/);
|
||||||
|
assert.match(service, /inspection_act_company_responses/);
|
||||||
|
assert.doesNotMatch(service, /company_response_received_on/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F1.3 only turns open findings from closed Acts into pending field context', () => {
|
||||||
|
assert.match(service, /act\.status IN \('CLOSED', 'RECTIFIED'\)/);
|
||||||
|
assert.match(service, /finding\.status = 'OPEN'/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F1.3 separates required field review, upcoming review and administrative context', () => {
|
||||||
|
assert.match(service, /'REQUIRED'/);
|
||||||
|
assert.match(service, /'UPCOMING'/);
|
||||||
|
assert.match(service, /'CONTEXT'/);
|
||||||
|
assert.match(service, /ADMIN_RESPONSE_OVERDUE/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F1.3 exposes the briefing through the inspection read contract', () => {
|
||||||
|
assert.match(controller, /field-briefing/);
|
||||||
|
assert.match(controller, /inspections\.read/);
|
||||||
|
});
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dhv2-web",
|
"name": "dhv2-web",
|
||||||
"version": "0.20.0-2",
|
"version": "0.20.0-3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ import { ReportDetailPage } from '../pages/ReportDetailPage';
|
|||||||
import { VerificationPlanningPage } from '../pages/VerificationPlanningPage';
|
import { VerificationPlanningPage } from '../pages/VerificationPlanningPage';
|
||||||
import { ActAdministrationPage } from '../pages/ActAdministrationPage';
|
import { ActAdministrationPage } from '../pages/ActAdministrationPage';
|
||||||
import { ActAdministrationDetailPage } from '../pages/ActAdministrationDetailPage';
|
import { ActAdministrationDetailPage } from '../pages/ActAdministrationDetailPage';
|
||||||
|
import { FieldBriefingsPage } from '../pages/FieldBriefingsPage';
|
||||||
|
|
||||||
const MapPage = lazy(() => import('../pages/MapPage').then((module) => ({ default: module.MapPage })));
|
const MapPage = lazy(() => import('../pages/MapPage').then((module) => ({ default: module.MapPage })));
|
||||||
|
|
||||||
const DocumentDeliveryPage = lazy(() => import('../pages/DocumentDeliveryPage').then((module) => ({ default: module.DocumentDeliveryPage })));
|
const DocumentDeliveryPage = lazy(() => import('../pages/DocumentDeliveryPage').then((module) => ({ default: module.DocumentDeliveryPage })));
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
return <Suspense fallback={<div className="loading-block"><span className="spinner" />Cargando módulo…</div>}><Routes>
|
return <Suspense fallback={<div className="loading-block"><span className="spinner" />Cargando módulo…</div>}><Routes>
|
||||||
<Route path="/login" element={<LoginPage />} />
|
<Route path="/login" element={<LoginPage />} />
|
||||||
@@ -65,6 +66,7 @@ export function App() {
|
|||||||
<Route element={<PermissionRoute permission="inspections.read" />}>
|
<Route element={<PermissionRoute permission="inspections.read" />}>
|
||||||
<Route path="/inspecciones" element={<InspectionVisitsPage />} />
|
<Route path="/inspecciones" element={<InspectionVisitsPage />} />
|
||||||
<Route path="/inspecciones/:id" element={<InspectionVisitEditorPage />} />
|
<Route path="/inspecciones/:id" element={<InspectionVisitEditorPage />} />
|
||||||
|
<Route path="/preparacion-campo" element={<FieldBriefingsPage />} />
|
||||||
</Route>
|
</Route>
|
||||||
<Route element={<PermissionRoute permission="inspections.manage" />}><Route path="/inspecciones/nueva" element={<InspectionVisitEditorPage />} /></Route>
|
<Route element={<PermissionRoute permission="inspections.manage" />}><Route path="/inspecciones/nueva" element={<InspectionVisitEditorPage />} /></Route>
|
||||||
<Route element={<PermissionRoute permission="inspection_acts.read" />}>
|
<Route element={<PermissionRoute permission="inspection_acts.read" />}>
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
export const APP_VERSION = '0.20.0-2';
|
export const APP_VERSION = '0.20.0-3';
|
||||||
export const APP_PHASE = 'Fase F1.2 · Seguimiento administrativo por Acta';
|
export const APP_PHASE = 'Fase F1.3 · Preparación inteligente de campo';
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ interface NavItem {
|
|||||||
const operational: NavItem[] = [
|
const operational: NavItem[] = [
|
||||||
{ to: '/', label: 'Inicio', icon: 'home', permission: 'dashboard.read' },
|
{ to: '/', label: 'Inicio', icon: 'home', permission: 'dashboard.read' },
|
||||||
{ to: '/inspecciones', label: 'Inspecciones', icon: 'clipboard', permission: 'inspections.read' },
|
{ to: '/inspecciones', label: 'Inspecciones', icon: 'clipboard', permission: 'inspections.read' },
|
||||||
|
{ to: '/preparacion-campo', label: 'Preparación de campo', icon: 'clipboard', permission: 'inspections.read' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const followUp: NavItem[] = [
|
const followUp: NavItem[] = [
|
||||||
|
|||||||
@@ -0,0 +1,192 @@
|
|||||||
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
import { Link } from 'react-router';
|
||||||
|
import { SearchableSelect } from '../components/SearchableSelect';
|
||||||
|
import { Alert, EmptyState, LoadingBlock } from '../components/Feedback';
|
||||||
|
import { formatDate, formatDateOnly } from '../lib/format';
|
||||||
|
|
||||||
|
type ReviewState = 'REQUIRED' | 'UPCOMING' | 'CONTEXT';
|
||||||
|
|
||||||
|
interface VisitOption {
|
||||||
|
id: string;
|
||||||
|
code: string;
|
||||||
|
status: string;
|
||||||
|
plannedStartAt: string | null;
|
||||||
|
operationalArea: { name: string } | null;
|
||||||
|
operatorCompany: { name: string } | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BriefingFinding {
|
||||||
|
id: string;
|
||||||
|
code: string;
|
||||||
|
title: string;
|
||||||
|
severity: number | null;
|
||||||
|
nextControlOn: string | null;
|
||||||
|
reviewState: ReviewState;
|
||||||
|
reviewReason: string;
|
||||||
|
asset: { id: string; code: string; name: string; typeName: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BriefingAct {
|
||||||
|
actId: string;
|
||||||
|
actCode: string;
|
||||||
|
occurredAt: string;
|
||||||
|
adminState: string;
|
||||||
|
responseDueOn: string | null;
|
||||||
|
latestResponse: null | { id: string; receivedOn: string; committedCorrectionOn: string | null; hasPdf: boolean };
|
||||||
|
findings: BriefingFinding[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Briefing {
|
||||||
|
visit: {
|
||||||
|
id: string;
|
||||||
|
code: string;
|
||||||
|
status: string;
|
||||||
|
plannedStartAt: string | null;
|
||||||
|
operationalArea: { id: string; code: string | null; name: string | null };
|
||||||
|
operatorCompany: { id: string; code: string | null; name: string | null };
|
||||||
|
};
|
||||||
|
referenceDate: string;
|
||||||
|
generatedAt: string;
|
||||||
|
summary: {
|
||||||
|
actCount: number;
|
||||||
|
openFindingCount: number;
|
||||||
|
fieldReviewRequired: number;
|
||||||
|
fieldReviewUpcoming: number;
|
||||||
|
administrativeAttention: number;
|
||||||
|
assetCount: number;
|
||||||
|
};
|
||||||
|
acts: BriefingAct[];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function json<T>(url: string): Promise<T> {
|
||||||
|
const response = await fetch(url, { credentials: 'same-origin', headers: { Accept: 'application/json' } });
|
||||||
|
const payload = await response.json().catch(() => ({}));
|
||||||
|
if (!response.ok) {
|
||||||
|
const message = typeof payload?.message === 'string'
|
||||||
|
? payload.message
|
||||||
|
: 'No se pudo cargar la preparación de campo.';
|
||||||
|
throw new Error(message);
|
||||||
|
}
|
||||||
|
return payload as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
function reviewLabel(state: ReviewState) {
|
||||||
|
if (state === 'REQUIRED') return 'Revisar en campo';
|
||||||
|
if (state === 'UPCOMING') return 'Próximo control';
|
||||||
|
return 'Antecedente';
|
||||||
|
}
|
||||||
|
|
||||||
|
function reasonLabel(reason: string) {
|
||||||
|
const labels: Record<string, string> = {
|
||||||
|
CONTROL_OVERDUE: 'Control vencido',
|
||||||
|
COMMITMENT_REACHED: 'Compromiso de empresa alcanzado',
|
||||||
|
RESPONSE_WITHOUT_CONTROL: 'Respuesta recibida sin control programado',
|
||||||
|
CONTROL_UPCOMING: 'Control próximo',
|
||||||
|
ADMIN_RESPONSE_OVERDUE: 'Respuesta administrativa vencida',
|
||||||
|
CONTEXT_ONLY: 'Antecedente abierto',
|
||||||
|
};
|
||||||
|
return labels[reason] ?? reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function FieldBriefingsPage() {
|
||||||
|
const [visits, setVisits] = useState<VisitOption[]>([]);
|
||||||
|
const [visitId, setVisitId] = useState('');
|
||||||
|
const [briefing, setBriefing] = useState<Briefing | null>(null);
|
||||||
|
const [loadingVisits, setLoadingVisits] = useState(true);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setLoadingVisits(true);
|
||||||
|
Promise.all([
|
||||||
|
json<{ data: VisitOption[] }>('/api/v3/inspection-visits?status=PLANNED&page=1&pageSize=100'),
|
||||||
|
json<{ data: VisitOption[] }>('/api/v3/inspection-visits?status=DRAFT&page=1&pageSize=100'),
|
||||||
|
]).then(([planned, drafts]) => {
|
||||||
|
const all = [...planned.data, ...drafts.data]
|
||||||
|
.sort((a, b) => String(a.plannedStartAt ?? '').localeCompare(String(b.plannedStartAt ?? '')));
|
||||||
|
setVisits(all);
|
||||||
|
const onlyVisit = all[0];
|
||||||
|
if (all.length === 1 && onlyVisit) setVisitId(onlyVisit.id);
|
||||||
|
}).catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)))
|
||||||
|
.finally(() => setLoadingVisits(false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!visitId) {
|
||||||
|
setBriefing(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
json<Briefing>(`/api/v3/inspection-visits/${visitId}/field-briefing`)
|
||||||
|
.then(setBriefing)
|
||||||
|
.catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)))
|
||||||
|
.finally(() => setLoading(false));
|
||||||
|
}, [visitId]);
|
||||||
|
|
||||||
|
const selected = useMemo(() => visits.find((visit) => visit.id === visitId) ?? null, [visits, visitId]);
|
||||||
|
|
||||||
|
return <section className="field-briefing-page">
|
||||||
|
<div className="page-heading">
|
||||||
|
<div>
|
||||||
|
<span className="eyebrow">ANTES DE SALIR A CAMPO</span>
|
||||||
|
<h1>Preparación de campo</h1>
|
||||||
|
<p>Actas anteriores y hallazgos abiertos del mismo Área/Yacimiento y Operadora, ordenados por lo que el inspector debe revisar.</p>
|
||||||
|
</div>
|
||||||
|
{briefing && <button className="button secondary" type="button" onClick={() => window.print()}>Imprimir preparación</button>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-card no-print">
|
||||||
|
<label>Inspección planificada</label>
|
||||||
|
{loadingVisits ? <LoadingBlock label="Cargando inspecciones…" /> : <SearchableSelect
|
||||||
|
value={visitId}
|
||||||
|
onChange={(event) => setVisitId(event.target.value)}
|
||||||
|
searchPlaceholder="Buscar por código, Área u Operadora…"
|
||||||
|
>
|
||||||
|
<option value="">Seleccionar inspección…</option>
|
||||||
|
{visits.map((visit) => <option key={visit.id} value={visit.id}>
|
||||||
|
{visit.code} · {visit.operationalArea?.name ?? 'Sin Área'} · {visit.operatorCompany?.name ?? 'Sin Operadora'}
|
||||||
|
</option>)}
|
||||||
|
</SearchableSelect>}
|
||||||
|
{selected?.plannedStartAt && <small className="block-muted">Salida prevista: {formatDate(selected.plannedStartAt)}</small>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && <Alert>{error}</Alert>}
|
||||||
|
{loading && <LoadingBlock label="Armando paquete de campo…" />}
|
||||||
|
{!loading && !briefing && !error && <EmptyState title="Seleccioná una inspección" text="El sistema reunirá automáticamente los pendientes de inspecciones anteriores." />}
|
||||||
|
{!loading && briefing && <>
|
||||||
|
<div className="page-heading compact">
|
||||||
|
<div>
|
||||||
|
<span className="eyebrow">{briefing.visit.code}</span>
|
||||||
|
<h2>{briefing.visit.operationalArea.name ?? 'Área'} · {briefing.visit.operatorCompany.name ?? 'Operadora'}</h2>
|
||||||
|
<p>Referencia: {formatDateOnly(briefing.referenceDate)} · generado {formatDate(briefing.generatedAt)}</p>
|
||||||
|
</div>
|
||||||
|
<Link className="button secondary no-print" to={`/inspecciones/${briefing.visit.id}`}>Abrir planificación</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="status-tabs briefing-summary">
|
||||||
|
<span>Actas <strong>{briefing.summary.actCount}</strong></span>
|
||||||
|
<span>Hallazgos abiertos <strong>{briefing.summary.openFindingCount}</strong></span>
|
||||||
|
<span>Revisar en campo <strong>{briefing.summary.fieldReviewRequired}</strong></span>
|
||||||
|
<span>Próximos <strong>{briefing.summary.fieldReviewUpcoming}</strong></span>
|
||||||
|
<span>Inventario involucrado <strong>{briefing.summary.assetCount}</strong></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{briefing.acts.length === 0 ? <EmptyState title="Sin pendientes anteriores" text="No hay hallazgos abiertos de Actas anteriores para este contexto." /> : briefing.acts.map((act) => <article className="table-panel" key={act.actId}>
|
||||||
|
<div className="table-summary">
|
||||||
|
<div><strong>{act.actCode}</strong><small className="block-muted">{formatDate(act.occurredAt)} · {act.adminState}</small></div>
|
||||||
|
<div><small>Plazo empresa</small><strong>{act.responseDueOn ? formatDateOnly(act.responseDueOn) : 'Sin definir'}</strong></div>
|
||||||
|
<div><small>Respuesta</small><strong>{act.latestResponse ? formatDateOnly(act.latestResponse.receivedOn) : 'Pendiente'}</strong></div>
|
||||||
|
</div>
|
||||||
|
<div className="table-scroll"><table><thead><tr><th>Prioridad</th><th>Hallazgo</th><th>Inventario</th><th>Próximo control</th></tr></thead><tbody>
|
||||||
|
{act.findings.map((finding) => <tr key={finding.id}>
|
||||||
|
<td><span className={`status-badge ${finding.reviewState === 'REQUIRED' ? 'danger' : 'pending'}`}>{reviewLabel(finding.reviewState)}</span><small className="block-muted">{reasonLabel(finding.reviewReason)}</small></td>
|
||||||
|
<td><strong>{finding.code}</strong><small className="block-muted">{finding.title}</small></td>
|
||||||
|
<td><strong>{finding.asset.code}</strong><small className="block-muted">{finding.asset.name} · {finding.asset.typeName}</small></td>
|
||||||
|
<td>{finding.nextControlOn ? formatDateOnly(finding.nextControlOn) : 'Sin fecha'}</td>
|
||||||
|
</tr>)}
|
||||||
|
</tbody></table></div>
|
||||||
|
</article>)}
|
||||||
|
</>}
|
||||||
|
</section>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user