fix(f6.1): preserve inventory snapshots when operator changes

This commit is contained in:
2026-09-09 15:07:52 -03:00
parent 44f6002869
commit 33c0c6b8f6
@@ -170,20 +170,9 @@ export class AssetOperationalRelationsService {
RETURNING id RETURNING id
`, [dto.areaId, dto.companyId, dto.relationRole, dto.participationPercent ?? null, dto.legalInstrument ?? null, dto.sourceDocumentId ?? null, dto.reason, principal.userId])) as Array<{ id: string }>; `, [dto.areaId, dto.companyId, dto.relationRole, dto.participationPercent ?? null, dto.legalInstrument ?? null, dto.sourceDocumentId ?? null, dto.reason, principal.userId])) as Array<{ id: string }>;
// Compatibility snapshot only. This does not move or re-parent Inventory. // F5/F6: changing the Area operator is a temporal relation event only.
// area_company_relations remains the temporal source of truth. // Existing Inventory keeps its creation/historical operator snapshot and
if (dto.relationRole === AreaOrganizationRole.OPERATOR) { // physical hierarchy unchanged. Runtime ownership must resolve this row.
await manager.query(`
UPDATE assets asset
SET operator_company_id=$2::uuid,updated_at=CURRENT_TIMESTAMP,updated_by=$3::uuid
FROM asset_types type
WHERE type.id=asset.asset_type_id
AND type.operational_role='GENERIC'
AND asset.operational_area_id=$1::uuid
AND asset.operator_company_id IS DISTINCT FROM $2::uuid
`,[dto.areaId,dto.companyId,principal.userId]);
}
const created = await this.loadRelation(manager, row.id); const created = await this.loadRelation(manager, row.id);
await this.audit.record({ await this.audit.record({
...administrationAuditContext(principal, request), ...administrationAuditContext(principal, request),
@@ -192,7 +181,7 @@ export class AssetOperationalRelationsService {
entityId: row.id, entityId: row.id,
afterData: this.auditView(created), afterData: this.auditView(created),
metadata: dto.relationRole === AreaOrganizationRole.OPERATOR metadata: dto.relationRole === AreaOrganizationRole.OPERATOR
? { inventoryHierarchyChanged:false, operatorSnapshotSynchronized:true } ? { inventoryHierarchyChanged:false, operatorSnapshotPreserved:true }
: undefined, : undefined,
}, manager); }, manager);
return created; return created;
@@ -223,8 +212,8 @@ export class AssetOperationalRelationsService {
}); });
} }
// F5: physical Inventory belongs to Area/Yacimiento hierarchy, not to Company. // F5/F6: physical Inventory belongs to Area/Yacimiento hierarchy, not to Company.
// Ending an operator relation must never be blocked by existing Inventory. // Ending an operator relation never moves, rewrites or blocks existing Inventory.
await manager.query(` await manager.query(`
UPDATE area_company_relations UPDATE area_company_relations
SET valid_until = CURRENT_TIMESTAMP, SET valid_until = CURRENT_TIMESTAMP,