Skip to content

Commit

Permalink
export stable id of cost surface and map this to unique id of copy of…
Browse files Browse the repository at this point in the history
… each cost surface on import [MRXN23-606]
  • Loading branch information
hotzevzl committed Mar 11, 2024
1 parent b9e02f2 commit d876ac1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ export class ScenarioMetadataPieceExporter implements ExportPieceProcessor {
return piece === ClonePiece.ScenarioMetadata;
}

private async getStableIdForScenarioCostSurface(
costSurfaceId: string,
): Promise<string> {
return await this.entityManager
.createQueryBuilder()
.select(['stable_id'])
.from('cost_surfaces', 'cs')
.where('cs.id = :costSurfaceId', {
costSurfaceId: costSurfaceId,
})
.execute()
.then((result: { stable_id: string }[]) => result[0]?.stable_id);
}

async run(input: ExportJobInput): Promise<ExportJobOutput> {
const scenarioId = input.resourceId;

Expand Down Expand Up @@ -77,6 +91,9 @@ export class ScenarioMetadataPieceExporter implements ExportPieceProcessor {
throw new Error(errorMessage);
}

const scenarioCostSurfaceStableId: string =
await this.getStableIdForScenarioCostSurface(scenario.cost_surface_id);

const [blmRange]: [SelectScenarioBlmResult] = await this.entityManager
.createQueryBuilder()
.select(['values', 'defaults', 'range'])
Expand All @@ -101,7 +118,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class ProjectCostSurfacesPieceImporter implements ImportPieceProcessor {
.createQueryBuilder()
.insert()
.into('cost_surfaces')
.values(omit(costSurfaceData, ['origin_id']))
.values(costSurfaceData)
.execute(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ export class ScenarioMetadataPieceImporter implements ImportPieceProcessor {
.execute();
}

private async mapCostSurfaceStableIdToIdOfClonedCostSurface(
em: EntityManager,
costSurfaceId: string,
projectId: string,
): Promise<string> {
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<ImportJobOutput> {
const {
pieceResourceId: scenarioId,
Expand Down Expand Up @@ -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 {
Expand All @@ -135,7 +157,10 @@ export class ScenarioMetadataPieceImporter implements ImportPieceProcessor {
em,
scenarioId,
projectId,
metadata,
{
...metadata,
cost_surface_id: idOfClonedCostSurfaceLinkedToScenario,
},
input.ownerId,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { ClonePiece, ExportJobInput } from '@marxan/cloning';
import { ResourceKind } from '@marxan/cloning/domain';
import { ProjectCustomFeaturesContent } from '@marxan/cloning/infrastructure/clone-piece-data/project-custom-features';
import { CloningFilesRepository } from '@marxan/cloning-files-repository';
import { GeoFeatureGeometry } from '@marxan/geofeatures';
import { FixtureType } from '@marxan/utils/tests/fixture-type';
import { Test } from '@nestjs/testing';
import { getEntityManagerToken, TypeOrmModule } from '@nestjs/typeorm';
import { isLeft, Right } from 'fp-ts/lib/Either';
import { Readable } from 'stream';
import { EntityManager, In } from 'typeorm';
import { EntityManager } from 'typeorm';
import { v4 } from 'uuid';
import {
DeleteProjectAndOrganization,
GivenCostSurfaceData,
GivenCostSurfaces,
GivenFeatures,
GivenFeaturesData,
GivenProjectExists,
readSavedFile,
} from '../fixtures';
Expand Down

0 comments on commit d876ac1

Please sign in to comment.