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 10, 2024
1 parent 8c4e665 commit 2e1137e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
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

0 comments on commit 2e1137e

Please sign in to comment.