diff --git a/api/apps/api/src/modules/geo-features/geo-feature.api.entity.ts b/api/apps/api/src/modules/geo-features/geo-feature.api.entity.ts index 37e5578a22..a8b1049571 100644 --- a/api/apps/api/src/modules/geo-features/geo-feature.api.entity.ts +++ b/api/apps/api/src/modules/geo-features/geo-feature.api.entity.ts @@ -27,6 +27,11 @@ export interface GeoFeatureProperty { distinctValues: Array; } +export type FeatureAmountRange = { + min: number | undefined; + max: number | undefined; +}; + @Entity('features') export class GeoFeature extends BaseEntity { @ApiProperty() @@ -95,6 +100,12 @@ export class GeoFeature extends BaseEntity { @Column('boolean', { name: 'is_custom' }) isCustom?: boolean; + @Column('float8', { name: 'amount_min' }) + amountMin?: number; + + @Column('float8', { name: 'amount_max' }) + amountMax?: number; + @Column('boolean', { name: 'is_legacy' }) isLegacy!: boolean; @@ -109,6 +120,9 @@ export class GeoFeature extends BaseEntity { @ApiPropertyOptional() scenarioUsageCount?: number; + + @ApiPropertyOptional() + amountRange?: FeatureAmountRange; } export class JSONAPIGeoFeaturesData { diff --git a/api/apps/api/src/modules/geo-features/geo-features.controller.ts b/api/apps/api/src/modules/geo-features/geo-features.controller.ts index e02eaf91a2..ad3f896d34 100644 --- a/api/apps/api/src/modules/geo-features/geo-features.controller.ts +++ b/api/apps/api/src/modules/geo-features/geo-features.controller.ts @@ -56,7 +56,6 @@ import { mapAclDomainToHttpError } from '@marxan-api/utils/acl.utils'; ) export class GeoFeaturesController { constructor( - public readonly service: GeoFeaturesService, private readonly geoFeatureService: GeoFeaturesService, public readonly geoFeaturesTagService: GeoFeatureTagsService, private readonly proxyService: ProxyService, @@ -75,8 +74,10 @@ export class GeoFeaturesController { async findAll( @ProcessFetchSpecification() fetchSpecification: FetchSpecification, ): Promise { - const results = await this.service.findAllPaginated(fetchSpecification); - return this.service.serialize(results.data, results.metadata); + const results = await this.geoFeatureService.findAllPaginated( + fetchSpecification, + ); + return this.geoFeatureService.serialize(results.data, results.metadata); } @ImplementsAcl() diff --git a/api/apps/api/src/modules/geo-features/geo-features.service.ts b/api/apps/api/src/modules/geo-features/geo-features.service.ts index 91efdb9dbb..a43a805220 100644 --- a/api/apps/api/src/modules/geo-features/geo-features.service.ts +++ b/api/apps/api/src/modules/geo-features/geo-features.service.ts @@ -41,7 +41,6 @@ import { ScenarioFeaturesService } from '@marxan-api/modules/scenarios-features' import { GeoFeatureTag } from '@marxan-api/modules/geo-feature-tags/geo-feature-tag.api.entity'; import { GeoFeatureTagsService } from '@marxan-api/modules/geo-feature-tags/geo-feature-tags.service'; import { FeatureAmountUploadService } from '@marxan-api/modules/geo-features/import/features-amounts-upload.service'; -import { isLeft } from 'fp-ts/Either'; import { isNil } from 'lodash'; const geoFeatureFilterKeyNames = [ @@ -138,6 +137,7 @@ export class GeoFeaturesService extends AppBaseService< 'isCustom', 'tag', 'scenarioUsageCount', + 'amountRange', ], keyForAttribute: 'camelCase', }; @@ -832,4 +832,24 @@ export class GeoFeaturesService extends AppBaseService< scenarioUsageCount: usage ? Number(usage.count) : 0, } as GeoFeature; } + + async saveAmountRangeForFeatures(featureIds: string[]) { + for (const featureId of featureIds) { + const minAndMaxAmount = await this.geoEntityManager + .createQueryBuilder() + .select('MIN(amount)', 'min') + .addSelect('MAX(amount)', 'max') + .from('puvspr_calculations', 'puvspr') + .where('puvspr.feature_id = :featureId', { featureId }) + .getOneOrFail(); + + await this.geoFeaturesRepository.update( + { id: featureId }, + { + amountMin: minAndMaxAmount.min, + amountMax: minAndMaxAmount.max, + }, + ); + } + } }