From 2e1137e3b6e3fabed7e06b829f0240475530c7b4 Mon Sep 17 00:00:00 2001 From: andrea rota Date: Sun, 10 Mar 2024 15:59:54 +0000 Subject: [PATCH] export stable id of cost surface and map this to unique id of copy of each cost surface on import [MRXN23-606] --- .../scenario-metadata.piece-exporter.ts | 12 ++++++++- .../scenario-metadata.piece-importer.ts | 27 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/api/apps/geoprocessing/src/export/pieces-exporters/scenario-metadata.piece-exporter.ts b/api/apps/geoprocessing/src/export/pieces-exporters/scenario-metadata.piece-exporter.ts index d544b57346..7ca31c5359 100644 --- a/api/apps/geoprocessing/src/export/pieces-exporters/scenario-metadata.piece-exporter.ts +++ b/api/apps/geoprocessing/src/export/pieces-exporters/scenario-metadata.piece-exporter.ts @@ -71,6 +71,16 @@ export class ScenarioMetadataPieceExporter implements ExportPieceProcessor { .where('s.id = :scenarioId', { scenarioId }) .execute(); + const scenarioCostSurfaceStableId: string = await this.entityManager + .createQueryBuilder() + .select(['stable_id']) + .from('cost_surfaces', 'cs') + .where('cs.id = :costSurfaceId', { + costSurfaceId: scenario.cost_surface_id, + }) + .execute() + .then((result: { stable_id: string }[]) => result[0]?.stable_id); + if (!scenario) { const errorMessage = `${ScenarioMetadataPieceExporter.name} - Scenario ${scenarioId} does not exist.`; this.logger.error(errorMessage); @@ -101,7 +111,7 @@ export class ScenarioMetadataPieceExporter implements ExportPieceProcessor { solutionsAreLocked: scenario.solutions_are_locked, type: scenario.type, status: scenario.status ?? undefined, - cost_surface_id: scenario.cost_surface_id, + cost_surface_id: scenarioCostSurfaceStableId, }; const relativePath = ClonePieceRelativePathResolver.resolveFor( diff --git a/api/apps/geoprocessing/src/import/pieces-importers/scenario-metadata.piece-importer.ts b/api/apps/geoprocessing/src/import/pieces-importers/scenario-metadata.piece-importer.ts index f8eae1e844..b6b4e862c8 100644 --- a/api/apps/geoprocessing/src/import/pieces-importers/scenario-metadata.piece-importer.ts +++ b/api/apps/geoprocessing/src/import/pieces-importers/scenario-metadata.piece-importer.ts @@ -84,6 +84,21 @@ export class ScenarioMetadataPieceImporter implements ImportPieceProcessor { .execute(); } + private async mapCostSurfaceStableIdToIdOfClonedCostSurface( + em: EntityManager, + costSurfaceId: string, + projectId: string, + ): Promise { + return await em + .createQueryBuilder() + .select('id') + .from('cost_surfaces', 'cs') + .where('cs.stable_id = :costSurfaceId', { costSurfaceId }) + .andWhere('cs.project_id = :projectId', { projectId }) + .execute() + .then((result) => result[0]?.id); + } + async run(input: ImportJobInput): Promise { const { pieceResourceId: scenarioId, @@ -121,6 +136,13 @@ export class ScenarioMetadataPieceImporter implements ImportPieceProcessor { const scenarioCloning = resourceKind === ResourceKind.Scenario; await this.entityManager.transaction(async (em) => { + const idOfClonedCostSurfaceLinkedToScenario = + await this.mapCostSurfaceStableIdToIdOfClonedCostSurface( + em, + metadata.cost_surface_id, + projectId, + ); + if (scenarioCloning) { await this.updateScenario(em, scenarioId, metadata, input.ownerId); } else { @@ -135,7 +157,10 @@ export class ScenarioMetadataPieceImporter implements ImportPieceProcessor { em, scenarioId, projectId, - metadata, + { + ...metadata, + cost_surface_id: idOfClonedCostSurfaceLinkedToScenario, + }, input.ownerId, ); }