import { BadRequestException } from '@nestjs/common'; import type { EntityManager } from 'typeorm'; /** * F6.1 keeps territorial levels explicit. operationalAreaId freezes the Area * context while scopeAssetId identifies the Yacimiento inspected inside it. * Legacy/mobile callers may still use the Area itself as scope until Android * supplies a Yacimiento explicitly. */ export async function validateInspectionPlanningScope( manager: EntityManager, operationalAreaId: string, scopeAssetId: string, ): Promise { if (scopeAssetId === operationalAreaId) return; const [scope] = await manager.query(` SELECT 1 FROM assets yacimiento INNER JOIN asset_types type ON type.id=yacimiento.asset_type_id WHERE yacimiento.id=$1::uuid AND yacimiento.parent_id=$2::uuid AND lower(type.code)='yacimiento' AND type.is_active=true AND yacimiento.information_status<>'INACTIVE' LIMIT 1 `, [scopeAssetId, operationalAreaId]) as Array<{ '?column?': number }>; if (!scope) { throw new BadRequestException({ code: 'INSPECTION_YACIMIENTO_OUTSIDE_AREA', message: 'El Yacimiento seleccionado no pertenece al Área indicada', }); } }