Skip to content

Commit

Permalink
Add service to find and save min and max values from puvspr_calculati…
Browse files Browse the repository at this point in the history
…ons for a features
  • Loading branch information
yulia-bel committed Oct 16, 2023
1 parent 9d2f321 commit 539ae99
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
14 changes: 14 additions & 0 deletions api/apps/api/src/modules/geo-features/geo-feature.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface GeoFeatureProperty {
distinctValues: Array<string | number>;
}

export type FeatureAmountRange = {
min: number | undefined;
max: number | undefined;
};

@Entity('features')
export class GeoFeature extends BaseEntity {
@ApiProperty()
Expand Down Expand Up @@ -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;

Expand All @@ -109,6 +120,9 @@ export class GeoFeature extends BaseEntity {

@ApiPropertyOptional()
scenarioUsageCount?: number;

@ApiPropertyOptional()
amountRange?: FeatureAmountRange;
}

export class JSONAPIGeoFeaturesData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -75,8 +74,10 @@ export class GeoFeaturesController {
async findAll(
@ProcessFetchSpecification() fetchSpecification: FetchSpecification,
): Promise<GeoFeatureResult> {
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()
Expand Down
22 changes: 21 additions & 1 deletion api/apps/api/src/modules/geo-features/geo-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -138,6 +137,7 @@ export class GeoFeaturesService extends AppBaseService<
'isCustom',
'tag',
'scenarioUsageCount',
'amountRange',
],
keyForAttribute: 'camelCase',
};
Expand Down Expand Up @@ -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,
},
);
}
}
}

0 comments on commit 539ae99

Please sign in to comment.