diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.factory.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.factory.ts deleted file mode 100644 index c6f1a5597b..0000000000 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.factory.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { PuvsprDatLegacyProject } from './puvspr.dat.legacy-project'; -import { PuvsprDatMarxanProject } from './puvspr.dat.marxan-project'; - -@Injectable() -export class PuvrsprDatFactory { - constructor( - private readonly puvsprDatLegacyProject: PuvsprDatLegacyProject, - private readonly puvsprDatMarxanProject: PuvsprDatMarxanProject, - ) {} - public getPuvsrDat(legacy: boolean) { - return legacy ? this.puvsprDatLegacyProject : this.puvsprDatMarxanProject; - } -} diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.marxan-project.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service.ts similarity index 91% rename from api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.marxan-project.ts rename to api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service.ts index 1d00eb2d82..361c02c3df 100644 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.marxan-project.ts +++ b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service.ts @@ -4,10 +4,15 @@ import { FeatureAmountsPerPlanningUnitRepository } from '@marxan/feature-amounts import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { FeatureAmountPerPlanningUnitId, PuvsprDat } from './puvsrpr.dat'; + +export type FeatureAmountPerPlanningUnitId = { + featureId: string; + amount: number; + puId: number; +}; @Injectable() -export class PuvsprDatMarxanProject implements PuvsprDat { +export class PuvsprDatFeatureAmountsService { constructor( private readonly featureAmountsPerPlanningUnitRepo: FeatureAmountsPerPlanningUnitRepository, @InjectRepository(ProjectsPuEntity, DbConnections.geoprocessingDB) @@ -15,7 +20,6 @@ export class PuvsprDatMarxanProject implements PuvsprDat { ) {} public async getAmountPerPlanningUnitAndFeature( projectId: string, - scenarioId: string, featureIds: string[], ): Promise { const amountPerPlanningUnitOfFeature = diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.legacy-project.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.legacy-project.ts deleted file mode 100644 index 0adb9561d9..0000000000 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.legacy-project.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { GeoFeature } from '@marxan-api/modules/geo-features/geo-feature.api.entity'; -import { FeatureAmountsPerPlanningUnitService } from '@marxan/feature-amounts-per-planning-unit'; -import { Injectable } from '@nestjs/common'; -import { FeatureAmountPerPlanningUnitId, PuvsprDat } from './puvsrpr.dat'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository, In } from 'typeorm'; - -@Injectable() -export class PuvsprDatLegacyProject implements PuvsprDat { - constructor( - private readonly featureAmountsPerPlanningUnit: FeatureAmountsPerPlanningUnitService, - @InjectRepository(GeoFeature) - private readonly featuresRepo: Repository, - ) {} - public async getAmountPerPlanningUnitAndFeature( - projectId: string, - scenarioId: string, - featureIds: string[], - ): Promise { - const { legacyFeatureIds, marxanFeatureIds } = - await this.getLegacyAndMarxanFeatures(featureIds); - - const legacyFeaturesComputed = await this.computeLegacyFeatures( - legacyFeatureIds, - scenarioId, - ); - - const marxanFeaturesComputed = await this.computeMarxanFeatures( - marxanFeatureIds, - scenarioId, - ); - - return legacyFeaturesComputed.concat(marxanFeaturesComputed); - } - - private async getLegacyAndMarxanFeatures(featureIds: string[]) { - if (!featureIds.length) - return { legacyFeatureIds: [], marxanFeatureIds: [] }; - - const features = await this.featuresRepo.find({ - where: { id: In(featureIds) }, - }); - - const legacyFeatureIds = features - .filter(({ isLegacy }) => isLegacy) - .map(({ id }) => id); - const marxanFeatureIds = features - .filter(({ isLegacy }) => !isLegacy) - .map(({ id }) => id); - - return { legacyFeatureIds, marxanFeatureIds }; - } - - private async computeLegacyFeatures( - featureIds: string[], - scenarioId: string, - ): Promise { - const legacyFeaturesComputations = await Promise.all( - featureIds.map((featureId) => - this.featureAmountsPerPlanningUnit.computeLegacyAmountPerPlanningUnit( - featureId, - scenarioId, - ), - ), - ); - return legacyFeaturesComputations.flatMap((legacyFeatureComputed) => { - return legacyFeatureComputed.map(({ amount, featureId, puId }) => { - return { amount, featureId, puId }; - }); - }); - } - - private async computeMarxanFeatures( - featureIds: string[], - scenarioId: string, - ): Promise { - const marxanFeaturesComputations = await Promise.all( - featureIds.map((featureId) => - this.featureAmountsPerPlanningUnit.computeMarxanAmountPerPlanningUnit( - featureId, - scenarioId, - ), - ), - ); - return marxanFeaturesComputations.flatMap((marxanFeatureComputed) => { - return marxanFeatureComputed.map(({ amount, featureId, puId }) => { - return { amount, featureId, puId }; - }); - }); - } -} diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.module.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.module.ts index 01dda88b2b..388d876956 100644 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.module.ts +++ b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.module.ts @@ -10,12 +10,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { Scenario } from '../../scenario.api.entity'; import { GeoFeatureDtoMapper } from '../../specification/geo-feature-dto.mapper'; import { SplitFeatureConfigMapper } from '../../specification/split-feature-config.mapper'; -import { PuvrsprDatFactory } from './puvspr.dat.factory'; -import { PuvsprDatLegacyProject } from './puvspr.dat.legacy-project'; -import { PuvsprDatMarxanProject } from './puvspr.dat.marxan-project'; import { PuvsprDatProcessor } from './puvspr.dat.processor'; import { FeatureHashModule } from '@marxan-api/modules/features-hash/features-hash.module'; import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry'; +import { PuvsprDatFeatureAmountsService } from '@marxan-api/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service'; @Module({ imports: [ @@ -32,9 +30,7 @@ import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry'; providers: [ GeoFeatureDtoMapper, PuvsprDatProcessor, - PuvrsprDatFactory, - PuvsprDatMarxanProject, - PuvsprDatLegacyProject, + PuvsprDatFeatureAmountsService, SplitFeatureConfigMapper, ], exports: [PuvsprDatProcessor], diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.ts index f1fb18ed1e..a29e28139b 100644 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.ts +++ b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.processor.ts @@ -22,7 +22,7 @@ import { isLeft, left, right } from 'fp-ts/lib/Either'; import { EntityManager } from 'typeorm'; import { GeoFeatureDtoMapper } from '../../specification/geo-feature-dto.mapper'; import { SplitFeatureConfigMapper } from '../../specification/split-feature-config.mapper'; -import { PuvrsprDatFactory } from './puvspr.dat.factory'; +import { PuvsprDatFeatureAmountsService } from '@marxan-api/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service'; export type PuvrsprDatRow = { speciesId: number; @@ -41,11 +41,10 @@ export class PuvsprDatProcessor { private readonly featureAmountsPerPlanningUnitService: FeatureAmountsPerPlanningUnitService, private readonly splitConfigHasher: SingleConfigFeatureValueHasher, private readonly splitFeatureConfigMapper: SplitFeatureConfigMapper, - private readonly puvsprDatFactory: PuvrsprDatFactory, + private readonly puvsprFeatureAmounts: PuvsprDatFeatureAmountsService, ) {} async getPuvsprDatRows( - isLegacy: boolean, scenarioId: string, projectId: string, ): Promise { @@ -64,10 +63,8 @@ export class PuvsprDatProcessor { ); const featuresAmountPerPlanningUnit = - await this.getAmountPerPlanningUnitAndFeature( - isLegacy, + await this.puvsprFeatureAmounts.getAmountPerPlanningUnitAndFeature( projectId, - scenarioId, featuresIds, ); @@ -133,21 +130,6 @@ export class PuvsprDatProcessor { return copyFeatureIds.concat(splitFeatureIds); } - private async getAmountPerPlanningUnitAndFeature( - isLegacy: boolean, - projectId: string, - scenarioId: string, - fetaureIds: string[], - ) { - const puvspr = this.puvsprDatFactory.getPuvsrDat(isLegacy); - - return puvspr.getAmountPerPlanningUnitAndFeature( - projectId, - scenarioId, - fetaureIds, - ); - } - private getCopyFeatureIds( featureConfigs: (SpecificationFeatureCopy | SpecificationFeatureSplit)[], ) { diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvsrpr.dat.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvsrpr.dat.ts deleted file mode 100644 index 2c7e82cdbc..0000000000 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.processor/puvsrpr.dat.ts +++ /dev/null @@ -1,13 +0,0 @@ -export type FeatureAmountPerPlanningUnitId = { - featureId: string; - amount: number; - puId: number; -}; - -export abstract class PuvsprDat { - abstract getAmountPerPlanningUnitAndFeature( - projectId: string, - scenarioId: string, - featureIds: string[], - ): Promise; -} diff --git a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.service.ts b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.service.ts index 5d2c7c7249..1ff93e5796 100644 --- a/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.service.ts +++ b/api/apps/api/src/modules/scenarios/input-files/puvspr.dat.service.ts @@ -1,9 +1,7 @@ import { Project } from '@marxan-api/modules/projects/project.api.entity'; -import { ProjectSourcesEnum } from '@marxan/projects'; import { assertDefined } from '@marxan/utils'; import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { isRight } from 'fp-ts/lib/Either'; import { Repository } from 'typeorm'; import { Scenario } from '../scenario.api.entity'; import { @@ -29,10 +27,7 @@ export class PuvsprDatService { const projectId = scenario.projectId; - const isLegacyProject = await this.isLegacyProject(projectId); - const puvsprRows = await this.puvsprDatProcessor.getPuvsprDatRows( - isLegacyProject, scenarioId, projectId, ); @@ -40,14 +35,6 @@ export class PuvsprDatService { return this.getFileContent(puvsprRows); } - private async isLegacyProject(projectId: string) { - const [project] = await this.projectsRepo.find({ - where: { id: projectId }, - }); - assertDefined(project); - return project.sources === ProjectSourcesEnum.legacyImport; - } - private getFileContent(rows: PuvrsprDatRow[]) { return ( 'species\tpu\tamount\n' +