Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: scenarios features data piece exporter #1070

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ComponentLocation } from '@marxan/cloning/domain';
import { ClonePieceRelativePathResolver } from '@marxan/cloning/infrastructure/clone-piece-data';
import {
FeatureDataElement,
OutputFeatureDataElement,
ScenarioFeaturesDataContent,
} from '@marxan/cloning/infrastructure/clone-piece-data/scenario-features-data';
import { ScenarioFeaturesData } from '@marxan/features';
Expand All @@ -13,7 +14,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
import { isLeft } from 'fp-ts/Either';
import { Readable } from 'stream';
import { EntityManager, In } from 'typeorm';
import { EntityManager } from 'typeorm';
import {
ExportPieceProcessor,
PieceExportProvider,
Expand All @@ -25,6 +26,10 @@ type FeaturesSelectResult = {
is_custom: boolean;
};

type OutputScenarioFeaturesDataSelectResult = OutputFeatureDataElement & {
featureScenarioId: string;
};

type FeatureDataElementWithFeatureId = FeatureDataElement & {
sfdId: string;
apiFeatureId: string;
Expand Down Expand Up @@ -108,7 +113,7 @@ export class ScenarioFeaturesDataPieceExporter implements ExportPieceProcessor {

private getFileContent(
scenarioFeaturesDataWithIsCustom: FeatureDataElementWithIsCustom[],
outputScenariosFeaturesData: OutputScenariosFeaturesDataGeoEntity[],
outputScenariosFeaturesData: OutputScenarioFeaturesDataSelectResult[],
): ScenarioFeaturesDataContent {
const customFeaturesData: FeatureDataElement[] = [];
const platformFeaturesData: FeatureDataElement[] = [];
Expand All @@ -123,7 +128,7 @@ export class ScenarioFeaturesDataPieceExporter implements ExportPieceProcessor {
array.push({
...sfd,
outputFeaturesData: outputData.map(
({ id, featureScenarioId, ...rest }) => ({
({ featureScenarioId, ...rest }) => ({
...rest,
}),
),
Expand All @@ -144,13 +149,27 @@ export class ScenarioFeaturesDataPieceExporter implements ExportPieceProcessor {
where: { scenarioId: input.resourceId },
relations: ['featureData'],
});
const outputScenariosFeaturesData = await this.geoprocessingEntityManager
.getRepository(OutputScenariosFeaturesDataGeoEntity)
.find({
where: {
featureScenarioId: In(scenarioFeaturesData.map((el) => el.id)),
},
});

const outputScenariosFeaturesData: OutputScenarioFeaturesDataSelectResult[] = await this.geoprocessingEntityManager
.createQueryBuilder()
.select('osfd.run_id', 'runId')
.addSelect('osfd.amount', 'amount')
.addSelect('osfd.occurrences', 'occurrences')
.addSelect('osfd.separation', 'separation')
.addSelect('osfd.target', 'target')
.addSelect('osfd.mpm', 'mpm')
.addSelect('osfd.total_area', 'totalArea')
.addSelect('osfd.feature_scenario_id', 'featureScenarioId')
.from(ScenarioFeaturesData, 'sfd')
.innerJoin(
OutputScenariosFeaturesDataGeoEntity,
'osfd',
'sfd.id = osfd.feature_scenario_id',
)
.where('sfd.scenario_id = :scenarioId', {
scenarioId: input.resourceId,
})
.execute();

const scenarioFeaturesDataWithFeatureId = this.parseScenarioFeaturesDataToFeatureDataElementWithFeatureId(
scenarioFeaturesData,
Expand Down