Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e89d815868 | ||
|
|
ff0b6d83db | ||
|
|
d894391d97 | ||
|
|
c79537def9 | ||
|
|
83c9e2ca3e | ||
|
|
9254583154 | ||
|
|
0b21e6bc50 | ||
|
|
350e26566f | ||
|
|
ba79fbf9ae |
@@ -0,0 +1,407 @@
|
|||||||
|
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
|
type DossierRecord = Record<string, any>;
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ActivityDossierService {
|
||||||
|
private readonly logger = new Logger(ActivityDossierService.name);
|
||||||
|
|
||||||
|
constructor(private readonly dataSource: DataSource) {}
|
||||||
|
|
||||||
|
async dossier(id: string): Promise<Record<string, unknown>> {
|
||||||
|
const assetRows = (await this.dataSource.query(
|
||||||
|
`SELECT id, code, name, common_name AS "commonName", created_at AS "createdAt"
|
||||||
|
FROM assets
|
||||||
|
WHERE id = $1`,
|
||||||
|
[id],
|
||||||
|
)) as Array<{ id: string; code: string; name: string; commonName: string | null; createdAt: Date }>;
|
||||||
|
const asset = assetRows[0];
|
||||||
|
if (!asset) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
code: 'ASSET_NOT_FOUND',
|
||||||
|
message: 'Activo no encontrado',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const failedFacets: string[] = [];
|
||||||
|
const safeQuery = async (facet: string, sql: string): Promise<DossierRecord[]> => {
|
||||||
|
try {
|
||||||
|
return (await this.dataSource.query(sql, [id])) as DossierRecord[];
|
||||||
|
} catch (error) {
|
||||||
|
failedFacets.push(facet);
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
this.logger.error(`No se pudo cargar la faceta ${facet} del expediente ${id}: ${message}`);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const [
|
||||||
|
visits,
|
||||||
|
acts,
|
||||||
|
findings,
|
||||||
|
evidence,
|
||||||
|
communications,
|
||||||
|
verificationResults,
|
||||||
|
documents,
|
||||||
|
inspectionReports,
|
||||||
|
media,
|
||||||
|
versions,
|
||||||
|
] = await Promise.all([
|
||||||
|
safeQuery('visits', `
|
||||||
|
SELECT visit.id, visit.code, visit.status,
|
||||||
|
visit.planned_start_at AS "plannedStartAt",
|
||||||
|
visit.actual_started_at AS "actualStartedAt",
|
||||||
|
visit.actual_closed_at AS "actualClosedAt",
|
||||||
|
visit.created_at AS "createdAt"
|
||||||
|
FROM inspection_visits visit
|
||||||
|
WHERE visit.scope_asset_id = $1
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_visit_assets visit_asset
|
||||||
|
WHERE visit_asset.visit_id = visit.id
|
||||||
|
AND visit_asset.asset_id = $1
|
||||||
|
AND visit_asset.included = true
|
||||||
|
)
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_acts act
|
||||||
|
JOIN inspection_act_assets act_asset
|
||||||
|
ON act_asset.act_id = act.id AND act_asset.included = true
|
||||||
|
WHERE act.visit_id = visit.id
|
||||||
|
AND act_asset.asset_id = $1
|
||||||
|
)
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_acts act
|
||||||
|
JOIN inspection_findings finding ON finding.act_id = act.id
|
||||||
|
WHERE act.visit_id = visit.id
|
||||||
|
AND finding.asset_id = $1
|
||||||
|
)
|
||||||
|
ORDER BY COALESCE(visit.actual_started_at, visit.planned_start_at, visit.created_at) DESC
|
||||||
|
LIMIT 200
|
||||||
|
`),
|
||||||
|
safeQuery('acts', `
|
||||||
|
SELECT act.id, act.visit_id AS "visitId", act.code, act.status,
|
||||||
|
act.occurred_at AS "occurredAt", act.title, act.summary,
|
||||||
|
act.closed_at AS "closedAt", act.current_version AS "currentVersion",
|
||||||
|
visit.code AS "visitCode"
|
||||||
|
FROM inspection_acts act
|
||||||
|
JOIN inspection_visits visit ON visit.id = act.visit_id
|
||||||
|
WHERE EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_act_assets act_asset
|
||||||
|
WHERE act_asset.act_id = act.id
|
||||||
|
AND act_asset.asset_id = $1
|
||||||
|
AND act_asset.included = true
|
||||||
|
)
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_findings finding
|
||||||
|
WHERE finding.act_id = act.id
|
||||||
|
AND finding.asset_id = $1
|
||||||
|
)
|
||||||
|
ORDER BY act.occurred_at DESC
|
||||||
|
LIMIT 200
|
||||||
|
`),
|
||||||
|
safeQuery('findings', `
|
||||||
|
SELECT finding.id, finding.act_id AS "actId", finding.code, finding.status,
|
||||||
|
finding.title, finding.description,
|
||||||
|
finding.correction_due_on AS "correctionDueOn",
|
||||||
|
finding.company_response_received_on AS "companyResponseReceivedOn",
|
||||||
|
finding.next_control_on AS "nextControlOn",
|
||||||
|
finding.closed_at AS "closedAt", finding.closure_notes AS "closureNotes",
|
||||||
|
finding.created_at AS "createdAt", finding.updated_at AS "updatedAt",
|
||||||
|
act.code AS "actCode", act.occurred_at AS "actOccurredAt",
|
||||||
|
visit.id AS "visitId", visit.code AS "visitCode"
|
||||||
|
FROM inspection_findings finding
|
||||||
|
JOIN inspection_acts act ON act.id = finding.act_id
|
||||||
|
JOIN inspection_visits visit ON visit.id = act.visit_id
|
||||||
|
WHERE finding.asset_id = $1
|
||||||
|
ORDER BY finding.created_at DESC
|
||||||
|
LIMIT 500
|
||||||
|
`),
|
||||||
|
safeQuery('evidence', `
|
||||||
|
SELECT evidence.id, evidence.finding_id AS "findingId", evidence.communication_id AS "communicationId",
|
||||||
|
evidence.kind, evidence.purpose, evidence.original_name AS "originalName",
|
||||||
|
evidence.title, evidence.description, evidence.captured_at AS "capturedAt",
|
||||||
|
evidence.created_at AS "createdAt", finding.code AS "findingCode",
|
||||||
|
finding.title AS "findingTitle"
|
||||||
|
FROM inspection_finding_evidence evidence
|
||||||
|
JOIN inspection_findings finding ON finding.id = evidence.finding_id
|
||||||
|
WHERE finding.asset_id = $1
|
||||||
|
ORDER BY COALESCE(evidence.captured_at, evidence.created_at) DESC
|
||||||
|
LIMIT 500
|
||||||
|
`),
|
||||||
|
safeQuery('communications', `
|
||||||
|
SELECT communication.id, communication.finding_id AS "findingId",
|
||||||
|
communication.direction, communication.channel, communication.type,
|
||||||
|
communication.occurred_at AS "occurredAt", communication.subject,
|
||||||
|
communication.details, communication.contact_name AS "contactName",
|
||||||
|
communication.created_at AS "createdAt", finding.code AS "findingCode",
|
||||||
|
finding.title AS "findingTitle"
|
||||||
|
FROM inspection_finding_communications communication
|
||||||
|
JOIN inspection_findings finding ON finding.id = communication.finding_id
|
||||||
|
WHERE finding.asset_id = $1
|
||||||
|
ORDER BY communication.occurred_at DESC
|
||||||
|
LIMIT 500
|
||||||
|
`),
|
||||||
|
safeQuery('verificationResults', `
|
||||||
|
SELECT verification_link.id,
|
||||||
|
verification_link.finding_id AS "findingId",
|
||||||
|
verification_link.visit_id AS "visitId",
|
||||||
|
verification_link.target_control_on AS "targetControlOn",
|
||||||
|
verification_link.outcome,
|
||||||
|
verification_link.result_notes AS "resultNotes",
|
||||||
|
verification_link.verified_at AS "verifiedAt",
|
||||||
|
verification_link.result_recorded_at AS "resultRecordedAt",
|
||||||
|
verification_link.rescheduled_control_on AS "rescheduledControlOn",
|
||||||
|
finding.code AS "findingCode", finding.title AS "findingTitle",
|
||||||
|
visit.code AS "visitCode", visit.status AS "visitStatus",
|
||||||
|
(SELECT COUNT(*)::integer
|
||||||
|
FROM inspection_finding_evidence verification_evidence
|
||||||
|
WHERE verification_evidence.finding_id = finding.id
|
||||||
|
AND verification_evidence.verification_visit_id = visit.id
|
||||||
|
AND verification_evidence.purpose = 'VERIFICATION') AS "evidenceCount"
|
||||||
|
FROM inspection_finding_verification_visits verification_link
|
||||||
|
JOIN inspection_findings finding ON finding.id = verification_link.finding_id
|
||||||
|
JOIN inspection_visits visit ON visit.id = verification_link.visit_id
|
||||||
|
WHERE finding.asset_id = $1 AND verification_link.outcome IS NOT NULL
|
||||||
|
ORDER BY verification_link.verified_at DESC NULLS LAST, verification_link.result_recorded_at DESC
|
||||||
|
LIMIT 500
|
||||||
|
`),
|
||||||
|
safeQuery('documents', `
|
||||||
|
SELECT document.id, document.document_type AS "documentType",
|
||||||
|
document.document_number AS "documentNumber", document.title,
|
||||||
|
document.issuer, document.document_date AS "documentDate",
|
||||||
|
document.external_reference AS "externalReference",
|
||||||
|
link.relation_type AS "relationType", link.notes,
|
||||||
|
link.created_at AS "linkedAt"
|
||||||
|
FROM asset_source_documents link
|
||||||
|
JOIN source_documents document ON document.id = link.document_id
|
||||||
|
WHERE link.asset_id = $1
|
||||||
|
ORDER BY COALESCE(document.document_date::timestamptz, link.created_at) DESC
|
||||||
|
LIMIT 300
|
||||||
|
`),
|
||||||
|
safeQuery('inspectionReports', `
|
||||||
|
SELECT report.id, report.code, report.status,
|
||||||
|
report.pdf_status AS "pdfStatus", report.title,
|
||||||
|
report.generated_at AS "generatedAt", report.frozen_sha256 AS "frozenSha256",
|
||||||
|
act.id AS "actId", act.code AS "actCode",
|
||||||
|
visit.id AS "visitId", visit.code AS "visitCode"
|
||||||
|
FROM inspection_reports report
|
||||||
|
JOIN inspection_acts act ON act.id = report.act_id
|
||||||
|
JOIN inspection_visits visit ON visit.id = report.visit_id
|
||||||
|
WHERE visit.scope_asset_id = $1
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_act_assets act_asset
|
||||||
|
WHERE act_asset.act_id = act.id
|
||||||
|
AND act_asset.asset_id = $1
|
||||||
|
AND act_asset.included = true
|
||||||
|
)
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM inspection_findings finding
|
||||||
|
WHERE finding.act_id = act.id
|
||||||
|
AND finding.asset_id = $1
|
||||||
|
)
|
||||||
|
ORDER BY report.generated_at DESC
|
||||||
|
LIMIT 200
|
||||||
|
`),
|
||||||
|
safeQuery('media', `
|
||||||
|
SELECT media.id, media.kind, media.original_name AS "originalName",
|
||||||
|
media.title, media.description, media.captured_at AS "capturedAt",
|
||||||
|
media.created_at AS "createdAt", media.source
|
||||||
|
FROM asset_media media
|
||||||
|
WHERE media.asset_id = $1 AND media.deleted_at IS NULL
|
||||||
|
ORDER BY COALESCE(media.captured_at, media.created_at) DESC
|
||||||
|
LIMIT 500
|
||||||
|
`),
|
||||||
|
safeQuery('versions', `
|
||||||
|
SELECT version.id, version.version_number AS "versionNumber",
|
||||||
|
version.change_type AS "changeType", version.changed_fields AS "changedFields",
|
||||||
|
version.occurred_at AS "occurredAt", version.actor_username AS "actorUsername",
|
||||||
|
version.source
|
||||||
|
FROM asset_versions version
|
||||||
|
WHERE version.asset_id = $1
|
||||||
|
ORDER BY version.occurred_at DESC
|
||||||
|
LIMIT 500
|
||||||
|
`),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const timeline: DossierRecord[] = [];
|
||||||
|
const push = (event: DossierRecord) => timeline.push(event);
|
||||||
|
|
||||||
|
versions.forEach((version) => push({
|
||||||
|
id: `version:${String(version.id)}`,
|
||||||
|
kind: 'INVENTORY_CHANGE',
|
||||||
|
occurredAt: version.occurredAt,
|
||||||
|
title: version.changeType === 'CREATED' || version.changeType === 'BASELINE'
|
||||||
|
? 'Registro incorporado al inventario'
|
||||||
|
: 'Inventario actualizado',
|
||||||
|
description: Array.isArray(version.changedFields) && version.changedFields.length > 0
|
||||||
|
? `Campos: ${(version.changedFields as string[]).join(', ')}`
|
||||||
|
: null,
|
||||||
|
meta: {
|
||||||
|
versionNumber: version.versionNumber,
|
||||||
|
changeType: version.changeType,
|
||||||
|
actorUsername: version.actorUsername,
|
||||||
|
source: version.source,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
visits.forEach((visit) => push({
|
||||||
|
id: `visit:${String(visit.id)}`,
|
||||||
|
kind: 'INSPECTION',
|
||||||
|
occurredAt: visit.actualStartedAt ?? visit.plannedStartAt ?? visit.createdAt,
|
||||||
|
title: `Inspección ${String(visit.code)}`,
|
||||||
|
description: null,
|
||||||
|
href: `/inspecciones/${String(visit.id)}`,
|
||||||
|
meta: { status: visit.status },
|
||||||
|
}));
|
||||||
|
acts.forEach((act) => push({
|
||||||
|
id: `act:${String(act.id)}`,
|
||||||
|
kind: 'ACT',
|
||||||
|
occurredAt: act.occurredAt,
|
||||||
|
title: `Acta ${String(act.code)}`,
|
||||||
|
description: act.title,
|
||||||
|
href: `/inspecciones/actas/${String(act.id)}`,
|
||||||
|
meta: { status: act.status, visitCode: act.visitCode },
|
||||||
|
}));
|
||||||
|
findings.forEach((finding) => {
|
||||||
|
push({
|
||||||
|
id: `finding:${String(finding.id)}`,
|
||||||
|
kind: 'FINDING',
|
||||||
|
occurredAt: finding.createdAt,
|
||||||
|
title: `Hallazgo ${String(finding.code)}`,
|
||||||
|
description: finding.title,
|
||||||
|
href: `/hallazgos/${String(finding.id)}`,
|
||||||
|
meta: { status: finding.status, actCode: finding.actCode },
|
||||||
|
});
|
||||||
|
if (finding.closedAt) {
|
||||||
|
push({
|
||||||
|
id: `finding-close:${String(finding.id)}`,
|
||||||
|
kind: 'FINDING_CLOSED',
|
||||||
|
occurredAt: finding.closedAt,
|
||||||
|
title: `Hallazgo ${String(finding.code)} cerrado`,
|
||||||
|
description: finding.closureNotes,
|
||||||
|
href: `/hallazgos/${String(finding.id)}`,
|
||||||
|
meta: { status: finding.status },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
verificationResults.forEach((verification) => push({
|
||||||
|
id: `verification:${String(verification.id)}`,
|
||||||
|
kind: 'VERIFICATION',
|
||||||
|
occurredAt: verification.verifiedAt ?? verification.resultRecordedAt,
|
||||||
|
title: verification.outcome === 'RESOLVED'
|
||||||
|
? `Verificación conforme · ${String(verification.findingCode)}`
|
||||||
|
: verification.outcome === 'NOT_RESOLVED'
|
||||||
|
? `Verificación no conforme · ${String(verification.findingCode)}`
|
||||||
|
: `Verificación reprogramada · ${String(verification.findingCode)}`,
|
||||||
|
description: verification.resultNotes,
|
||||||
|
href: `/hallazgos/${String(verification.findingId)}`,
|
||||||
|
meta: {
|
||||||
|
outcome: verification.outcome,
|
||||||
|
visitCode: verification.visitCode,
|
||||||
|
targetControlOn: verification.targetControlOn,
|
||||||
|
rescheduledControlOn: verification.rescheduledControlOn,
|
||||||
|
evidenceCount: verification.evidenceCount,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
communications.forEach((communication) => push({
|
||||||
|
id: `communication:${String(communication.id)}`,
|
||||||
|
kind: 'COMMUNICATION',
|
||||||
|
occurredAt: communication.occurredAt,
|
||||||
|
title: communication.type === 'COMPANY_RESPONSE'
|
||||||
|
? 'Respuesta de la empresa'
|
||||||
|
: String(communication.subject),
|
||||||
|
description: communication.details,
|
||||||
|
href: `/hallazgos/${String(communication.findingId)}`,
|
||||||
|
meta: {
|
||||||
|
findingCode: communication.findingCode,
|
||||||
|
direction: communication.direction,
|
||||||
|
channel: communication.channel,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
evidence.forEach((item) => push({
|
||||||
|
id: `evidence:${String(item.id)}`,
|
||||||
|
kind: item.kind === 'PHOTO' ? 'PHOTO' : 'DOCUMENT',
|
||||||
|
occurredAt: item.capturedAt ?? item.createdAt,
|
||||||
|
title: item.kind === 'PHOTO' ? 'Fotografía / evidencia' : 'Documento incorporado',
|
||||||
|
description: item.title ?? item.originalName,
|
||||||
|
href: `/hallazgos/${String(item.findingId)}`,
|
||||||
|
meta: { findingCode: item.findingCode, purpose: item.purpose },
|
||||||
|
}));
|
||||||
|
inspectionReports.forEach((report) => push({
|
||||||
|
id: `inspection-report:${String(report.id)}`,
|
||||||
|
kind: 'REPORT',
|
||||||
|
occurredAt: report.generatedAt,
|
||||||
|
title: `Informe ${String(report.code)}`,
|
||||||
|
description: report.title,
|
||||||
|
href: `/informes/${String(report.id)}`,
|
||||||
|
meta: { status: report.status, pdfStatus: report.pdfStatus, actCode: report.actCode },
|
||||||
|
}));
|
||||||
|
documents.forEach((document) => push({
|
||||||
|
id: `source-document:${String(document.id)}`,
|
||||||
|
kind: document.documentType === 'TECHNICAL_REPORT' ? 'REPORT' : 'SOURCE_DOCUMENT',
|
||||||
|
occurredAt: document.documentDate ?? document.linkedAt,
|
||||||
|
title: String(document.title),
|
||||||
|
description: document.documentNumber
|
||||||
|
? `Documento ${String(document.documentNumber)}`
|
||||||
|
: 'Documento vinculado al inventario',
|
||||||
|
meta: { documentType: document.documentType, relationType: document.relationType },
|
||||||
|
}));
|
||||||
|
media.forEach((item) => push({
|
||||||
|
id: `asset-media:${String(item.id)}`,
|
||||||
|
kind: item.kind === 'PHOTO' ? 'PHOTO' : 'DOCUMENT',
|
||||||
|
occurredAt: item.capturedAt ?? item.createdAt,
|
||||||
|
title: item.kind === 'PHOTO' ? 'Fotografía del inventario' : 'Archivo del inventario',
|
||||||
|
description: item.title ?? item.originalName,
|
||||||
|
meta: { source: item.source },
|
||||||
|
}));
|
||||||
|
|
||||||
|
timeline.sort(
|
||||||
|
(a, b) => new Date(String(b.occurredAt ?? 0)).getTime() - new Date(String(a.occurredAt ?? 0)).getTime(),
|
||||||
|
);
|
||||||
|
|
||||||
|
const openFindings = findings.filter((finding) => finding.status === 'OPEN').length;
|
||||||
|
const closedFindings = findings.filter((finding) => finding.status === 'CLOSED').length;
|
||||||
|
const reports = documents.filter((document) => document.documentType === 'TECHNICAL_REPORT');
|
||||||
|
|
||||||
|
return {
|
||||||
|
asset: {
|
||||||
|
id: asset.id,
|
||||||
|
code: asset.code,
|
||||||
|
name: asset.name,
|
||||||
|
commonName: asset.commonName,
|
||||||
|
},
|
||||||
|
counters: {
|
||||||
|
inspections: visits.length,
|
||||||
|
acts: acts.length,
|
||||||
|
findings: findings.length,
|
||||||
|
verifications: verificationResults.length,
|
||||||
|
openFindings,
|
||||||
|
closedFindings,
|
||||||
|
evidence: evidence.length,
|
||||||
|
documents: documents.length + media.filter((item) => item.kind === 'DOCUMENT').length,
|
||||||
|
photos: evidence.filter((item) => item.kind === 'PHOTO').length
|
||||||
|
+ media.filter((item) => item.kind === 'PHOTO').length,
|
||||||
|
reports: reports.length + inspectionReports.length,
|
||||||
|
},
|
||||||
|
visits,
|
||||||
|
acts,
|
||||||
|
findings,
|
||||||
|
evidence,
|
||||||
|
communications,
|
||||||
|
verificationResults,
|
||||||
|
documents,
|
||||||
|
inspectionReports,
|
||||||
|
reports,
|
||||||
|
media,
|
||||||
|
versions,
|
||||||
|
timeline: timeline.slice(0, 500),
|
||||||
|
warnings: failedFacets,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,7 @@ import { InventoryTechnicalValuesService } from './inventory-technical-values.se
|
|||||||
import { InventoryFunctionService } from './inventory-function.service';
|
import { InventoryFunctionService } from './inventory-function.service';
|
||||||
import { FieldInventoryMergeController, InventoryMergeController } from './inventory-merge.controller';
|
import { FieldInventoryMergeController, InventoryMergeController } from './inventory-merge.controller';
|
||||||
import { InventoryMergeService } from './inventory-merge.service';
|
import { InventoryMergeService } from './inventory-merge.service';
|
||||||
|
import { ActivityDossierService } from './activity-dossier.service';
|
||||||
import { MergedInventoryDossierService } from './merged-inventory-dossier.service';
|
import { MergedInventoryDossierService } from './merged-inventory-dossier.service';
|
||||||
import { InventoryBrowserController } from './inventory-browser.controller';
|
import { InventoryBrowserController } from './inventory-browser.controller';
|
||||||
import { InventoryBrowserService } from './inventory-browser.service';
|
import { InventoryBrowserService } from './inventory-browser.service';
|
||||||
@@ -66,6 +67,7 @@ import { InventoryBrowserService } from './inventory-browser.service';
|
|||||||
InventoryFunctionService,
|
InventoryFunctionService,
|
||||||
InventoryBrowserService,
|
InventoryBrowserService,
|
||||||
InventoryMergeService,
|
InventoryMergeService,
|
||||||
|
ActivityDossierService,
|
||||||
MergedInventoryDossierService,
|
MergedInventoryDossierService,
|
||||||
AssetGeometriesService,
|
AssetGeometriesService,
|
||||||
AssetHistoryService,
|
AssetHistoryService,
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { ActivityDossierService } from './activity-dossier.service';
|
||||||
import { AssetsService } from './assets.service';
|
import { AssetsService } from './assets.service';
|
||||||
import { InventoryFunctionService } from './inventory-function.service';
|
import { InventoryFunctionService } from './inventory-function.service';
|
||||||
import { InventoryMergeService } from './inventory-merge.service';
|
import { InventoryMergeService } from './inventory-merge.service';
|
||||||
|
|
||||||
type LooseRecord = Record<string, any>;
|
type LooseRecord = Record<string, any>;
|
||||||
|
|
||||||
|
const MERGEABLE_DOSSIER_TYPES = new Set(['instalacion', 'subinstalacion']);
|
||||||
|
|
||||||
function dedupeById<T extends LooseRecord>(items: T[]): T[] {
|
function dedupeById<T extends LooseRecord>(items: T[]): T[] {
|
||||||
const seen = new Set<string>();
|
const seen = new Set<string>();
|
||||||
const result: T[] = [];
|
const result: T[] = [];
|
||||||
@@ -29,11 +32,22 @@ function sortDesc(items: LooseRecord[], fieldCandidates: string[]): LooseRecord[
|
|||||||
export class MergedInventoryDossierService {
|
export class MergedInventoryDossierService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly assets: AssetsService,
|
private readonly assets: AssetsService,
|
||||||
|
private readonly activity: ActivityDossierService,
|
||||||
private readonly merges: InventoryMergeService,
|
private readonly merges: InventoryMergeService,
|
||||||
private readonly functions: InventoryFunctionService,
|
private readonly functions: InventoryFunctionService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async dossier(requestedAssetId: string): Promise<Record<string, unknown>> {
|
async dossier(requestedAssetId: string): Promise<Record<string, unknown>> {
|
||||||
|
const requestedAsset = await this.assets.getById(requestedAssetId);
|
||||||
|
const requestedTypeCode = requestedAsset.type.code.trim().toLowerCase();
|
||||||
|
|
||||||
|
// La conciliación/fusión existe sólo para Instalaciones y Subinstalaciones.
|
||||||
|
// Los registros territoriales (Departamento, Área, Yacimiento) deben poder
|
||||||
|
// abrir su Actividad sin depender del subsistema de merge.
|
||||||
|
if (!MERGEABLE_DOSSIER_TYPES.has(requestedTypeCode)) {
|
||||||
|
return await this.activity.dossier(requestedAssetId);
|
||||||
|
}
|
||||||
|
|
||||||
const mergeStatus = await this.merges.status(requestedAssetId) as LooseRecord;
|
const mergeStatus = await this.merges.status(requestedAssetId) as LooseRecord;
|
||||||
const canonical = mergeStatus.canonical as LooseRecord;
|
const canonical = mergeStatus.canonical as LooseRecord;
|
||||||
const requested = mergeStatus.requested as LooseRecord;
|
const requested = mergeStatus.requested as LooseRecord;
|
||||||
@@ -44,7 +58,7 @@ export class MergedInventoryDossierService {
|
|||||||
.filter((value, index, all) => all.indexOf(value) === index);
|
.filter((value, index, all) => all.indexOf(value) === index);
|
||||||
|
|
||||||
const dossiers = await Promise.all(inventoryIds.map(async (assetId) => {
|
const dossiers = await Promise.all(inventoryIds.map(async (assetId) => {
|
||||||
const dossier = await this.assets.dossier(assetId) as LooseRecord;
|
const dossier = await this.activity.dossier(assetId) as LooseRecord;
|
||||||
const identity = dossier.asset as LooseRecord;
|
const identity = dossier.asset as LooseRecord;
|
||||||
const functionDossier = await this.functions.getForAsset(assetId).catch(() => null) as LooseRecord | null;
|
const functionDossier = await this.functions.getForAsset(assetId).catch(() => null) as LooseRecord | null;
|
||||||
return { assetId, identity, dossier, functionDossier };
|
return { assetId, identity, dossier, functionDossier };
|
||||||
@@ -145,6 +159,9 @@ export class MergedInventoryDossierService {
|
|||||||
const reports = documents.filter((document) => document.documentType === 'TECHNICAL_REPORT');
|
const reports = documents.filter((document) => document.documentType === 'TECHNICAL_REPORT');
|
||||||
const openFindings = findings.filter((finding) => finding.status === 'OPEN').length;
|
const openFindings = findings.filter((finding) => finding.status === 'OPEN').length;
|
||||||
const closedFindings = findings.filter((finding) => finding.status === 'CLOSED').length;
|
const closedFindings = findings.filter((finding) => finding.status === 'CLOSED').length;
|
||||||
|
const warnings = dossiers.flatMap(({ assetId, dossier }) =>
|
||||||
|
((dossier.warnings ?? []) as string[]).map((facet) => `${assetId}:${facet}`),
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
asset: {
|
asset: {
|
||||||
@@ -194,6 +211,7 @@ export class MergedInventoryDossierService {
|
|||||||
media,
|
media,
|
||||||
versions,
|
versions,
|
||||||
timeline: timeline.slice(0, 1000),
|
timeline: timeline.slice(0, 1000),
|
||||||
|
warnings,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import test from 'node:test';
|
||||||
|
import { readFileSync } from 'node:fs';
|
||||||
|
import { ActivityDossierService } from '../../src/asset-master/activity-dossier.service';
|
||||||
|
|
||||||
|
test('F7 Actividad no usa DISTINCT con ORDER BY COALESCE en visitas', () => {
|
||||||
|
const source = readFileSync('src/asset-master/activity-dossier.service.ts', 'utf8');
|
||||||
|
assert.doesNotMatch(source, /SELECT\s+DISTINCT\s+visit\.id/i);
|
||||||
|
assert.match(source, /ORDER BY COALESCE\(visit\.actual_started_at, visit\.planned_start_at, visit\.created_at\) DESC/);
|
||||||
|
assert.match(source, /EXISTS \(\s*SELECT 1\s*FROM inspection_visit_assets/s);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F7 una faceta auxiliar defectuosa no derriba todo el expediente', async () => {
|
||||||
|
const dataSource = {
|
||||||
|
query: async (sql: string) => {
|
||||||
|
if (sql.includes('FROM assets\n WHERE id = $1')) {
|
||||||
|
return [{
|
||||||
|
id: 'bcba740f-cc84-48b3-8d06-88a9c439e25c',
|
||||||
|
code: 'YAC-0005',
|
||||||
|
name: 'Agua Botada',
|
||||||
|
commonName: null,
|
||||||
|
createdAt: new Date('2026-01-01T00:00:00Z'),
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
if (sql.includes('inspection_finding_communications')) {
|
||||||
|
throw new Error('simulated optional facet failure');
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const service = new ActivityDossierService(dataSource as never);
|
||||||
|
const dossier = await service.dossier('bcba740f-cc84-48b3-8d06-88a9c439e25c') as {
|
||||||
|
asset: { code: string; name: string };
|
||||||
|
counters: { inspections: number; findings: number };
|
||||||
|
warnings: string[];
|
||||||
|
timeline: unknown[];
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.equal(dossier.asset.code, 'YAC-0005');
|
||||||
|
assert.equal(dossier.asset.name, 'Agua Botada');
|
||||||
|
assert.equal(dossier.counters.inspections, 0);
|
||||||
|
assert.equal(dossier.counters.findings, 0);
|
||||||
|
assert.deepEqual(dossier.warnings, ['communications']);
|
||||||
|
assert.deepEqual(dossier.timeline, []);
|
||||||
|
});
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import test from 'node:test';
|
||||||
|
import { readFileSync } from 'node:fs';
|
||||||
|
|
||||||
|
const mergedDossier = readFileSync('src/asset-master/merged-inventory-dossier.service.ts', 'utf8');
|
||||||
|
const dossierPanel = readFileSync('../web-v2/src/features/assets/AssetDossierPanel.tsx', 'utf8');
|
||||||
|
const simpleDetail = readFileSync('../web-v2/src/pages/SimpleInventoryDetailPage.tsx', 'utf8');
|
||||||
|
|
||||||
|
test('F7 mantiene Actividad disponible para Yacimiento', () => {
|
||||||
|
assert.match(simpleDetail, /kind === 'YACIMIENTO'/);
|
||||||
|
assert.match(simpleDetail, /setTab\('activity'\)/);
|
||||||
|
assert.match(simpleDetail, /AssetDossierPanel assetId=\{asset\.id\}/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F7 no hace depender el dossier territorial del subsistema de fusión', () => {
|
||||||
|
assert.match(mergedDossier, /MERGEABLE_DOSSIER_TYPES = new Set\(\['instalacion', 'subinstalacion'\]\)/);
|
||||||
|
assert.match(mergedDossier, /if \(!MERGEABLE_DOSSIER_TYPES\.has\(requestedTypeCode\)\)/);
|
||||||
|
assert.match(mergedDossier, /private readonly activity: ActivityDossierService/);
|
||||||
|
assert.match(mergedDossier, /return await this\.activity\.dossier\(requestedAssetId\)/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('F7 la consulta auxiliar de merge nunca bloquea la carga de Actividad', () => {
|
||||||
|
assert.match(dossierPanel, /getInventoryMergeStatus\(assetId\)\.catch\(\(\) => null\)/);
|
||||||
|
assert.match(dossierPanel, /getAssetDossier\(assetId\)/);
|
||||||
|
});
|
||||||
@@ -81,10 +81,12 @@ export function AssetDossierPanel({ assetId }: { assetId: string }) {
|
|||||||
const load = () => {
|
const load = () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError('');
|
setError('');
|
||||||
|
setMergeStatus(null);
|
||||||
Promise.all([
|
Promise.all([
|
||||||
getAssetDossier(assetId),
|
getAssetDossier(assetId),
|
||||||
getAsset(assetId),
|
getAsset(assetId),
|
||||||
getInventoryMergeStatus(assetId),
|
// La conciliación es auxiliar: nunca debe bloquear la Actividad del Inventario.
|
||||||
|
getInventoryMergeStatus(assetId).catch(() => null),
|
||||||
])
|
])
|
||||||
.then(([loadedDossier, loadedAsset, loadedMerge]) => {
|
.then(([loadedDossier, loadedAsset, loadedMerge]) => {
|
||||||
setDossier(loadedDossier as ExtendedDossier);
|
setDossier(loadedDossier as ExtendedDossier);
|
||||||
|
|||||||
Reference in New Issue
Block a user