Skip to content

Commit

Permalink
port over stable_id of cost surfaces when cloning them
Browse files Browse the repository at this point in the history
  • Loading branch information
hotzevzl committed Mar 11, 2024
1 parent e52b32d commit b9e02f2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class CostSurface {
@PrimaryGeneratedColumn('uuid')
id!: string;

@Column({ name: 'stable_id' })
stableId!: string;

@ApiProperty({ type: () => Project })
@ManyToOne((_type) => Project, (project) => project.costSurfaces)
@JoinColumn({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { ClonePiece, ExportJobInput, ExportJobOutput } from '@marxan/cloning';
import { ComponentLocation, ResourceKind } from '@marxan/cloning/domain';
import { ClonePieceRelativePathResolver } from '@marxan/cloning/infrastructure/clone-piece-data';
import {
ProjectCustomFeature,
ProjectCustomFeaturesContent,
} from '@marxan/cloning/infrastructure/clone-piece-data/project-custom-features';
import { ProjectCustomFeature } from '@marxan/cloning/infrastructure/clone-piece-data/project-custom-features';
import { CloningFilesRepository } from '@marxan/cloning-files-repository';
import { GeometrySource } from '@marxan/geofeatures';
import { Injectable, Logger } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
import { isLeft } from 'fp-ts/lib/Either';
Expand All @@ -24,6 +20,7 @@ type CreationStatus = ProjectCustomFeature['creation_status'];

type ProjectCostSurfacesSelectResult = {
id: string;
stable_id: string;
name: string;
min: number;
max: number;
Expand All @@ -32,6 +29,7 @@ type ProjectCostSurfacesSelectResult = {

type CostSurfaceDataSelectResult = {
cost_surface_id: string;
stable_id: string;
cost: number;
projects_pu_id: number;
};
Expand Down Expand Up @@ -61,7 +59,14 @@ export class ProjectCostSurfacesPieceExporter implements ExportPieceProcessor {
const costSurfaces: ProjectCostSurfacesSelectResult[] =
await this.apiEntityManager
.createQueryBuilder()
.select(['cs.id', 'cs.name', 'cs.min', 'cs.max', 'cs.is_default'])
.select([
'cs.id',
'cs.stable_id',
'cs.name',
'cs.min',
'cs.max',
'cs.is_default',
])
.from('cost_surfaces', 'cs')
.where('cs.project_id = :projectId', { projectId: input.resourceId })
.execute();
Expand Down Expand Up @@ -94,16 +99,10 @@ export class ProjectCostSurfacesPieceExporter implements ExportPieceProcessor {
(data: CostSurfaceDataSelectResult) =>
data.cost_surface_id === costSurface.id,
)
.map(
({
cost_surface_id: _cost_surface_id,
projects_pu_id,
...data
}) => {
const puid = projectPusMap[projects_pu_id];
return { puid, ...data };
},
),
.map(({ projects_pu_id, ...data }) => {
const puid = projectPusMap[projects_pu_id];
return { puid, ...data };
}),
})),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { ClonePiece, ImportJobInput, ImportJobOutput } 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 { readableToBuffer } from '@marxan/utils';
import { Injectable, Logger } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
Expand All @@ -14,11 +12,12 @@ import {
ImportPieceProcessor,
PieceImportProvider,
} from '../pieces/import-piece-processor';
import { chunk } from 'lodash';
import { chunk, omit } from 'lodash';
import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry';
import { CHUNK_SIZE_FOR_BATCH_GEODB_OPERATIONS } from '@marxan-geoprocessing/utils/chunk-size-for-batch-geodb-operations';
import {
CostSurfaceData,
ProjectCostSurface,
ProjectCostSurfacesContent,
} from '@marxan/cloning/infrastructure/clone-piece-data/project-cost-surfaces';
import { CostSurfacePuDataEntity } from '@marxan/cost-surfaces';
Expand Down Expand Up @@ -85,15 +84,19 @@ export class ProjectCostSurfacesPieceImporter implements ImportPieceProcessor {
const projectPusMap = await this.getProjectPusMap(projectId);

await this.apiEntityManager.transaction(async (apiEm) => {
const costSurfacesInsertValues: any[] = [];
let costSurfacesDataInsertValues: any[] = [];
const costSurfacesInsertValues: (Omit<ProjectCostSurface, 'data'> & {
project_id: string;
stable_id: string;
})[] = [];
let costSurfacesDataInsertValues: Record<string, unknown>[] = [];
costSurfaces.forEach(({ data, ...costSurface }) => {
const costSurfaceId = v4();

costSurfacesInsertValues.push({
...costSurface,
project_id: projectId,
id: costSurfaceId,
stable_id: costSurface.stable_id,
});

const costSurfaceData = data.map((data: CostSurfaceData) => ({
Expand All @@ -115,12 +118,12 @@ export class ProjectCostSurfacesPieceImporter implements ImportPieceProcessor {
});

await Promise.all(
costSurfacesInsertValues.map((values) =>
costSurfacesInsertValues.map((costSurfaceData) =>
apiEm
.createQueryBuilder()
.insert()
.into('cost_surfaces')
.values(values)
.values(omit(costSurfaceData, ['origin_id']))
.execute(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export type CostSurfaceData = {
};

export type ProjectCostSurface = {
/**
* The stable_id of the cost surface in the original project is stored as part
* of the export, so that it can be used to port over as part of cloned
* projects any links between scenarios and cost surfaces (since the former
* are imported with a cost_surface_id matching the stable_id stored here, but
* the corresponding cost surfaces are created with a new unique id in the
* cloned project).
*/
id: string;
stable_id: string;
name: string;
min: number;
max: number;
Expand Down

0 comments on commit b9e02f2

Please sign in to comment.