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 08816b293e..1a43e0f43e 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 @@ -53,7 +53,7 @@ import { FeatureAmountsPerPlanningUnitService, } from '@marxan/feature-amounts-per-planning-unit'; import { ComputeFeatureAmountPerPlanningUnit } from '@marxan/feature-amounts-per-planning-unit/feature-amounts-per-planning-units.service'; -import { CHUNK_SIZE_FOR_BATCH_GEODB_OPERATIONS } from '@marxan-geoprocessing/utils/chunk-size-for-batch-geodb-operations'; +import { CHUNK_SIZE_FOR_BATCH_APIDB_OPERATIONS } from '@marxan-api/utils/chunk-size-for-batch-apidb-operations'; const geoFeatureFilterKeyNames = [ 'featureClassName', @@ -431,10 +431,14 @@ export class GeoFeaturesService extends AppBaseService< computedFeatureAmounts, ); + await this.saveAmountRangeForFeatures( + apiQueryRunner.manager, + geoQueryRunner.manager, + [geoFeature.id], + ); + await apiQueryRunner.commitTransaction(); await geoQueryRunner.commitTransaction(); - - await this.saveAmountRangeForFeatures([geoFeature.id]); } catch (err) { await apiQueryRunner.rollbackTransaction(); await geoQueryRunner.rollbackTransaction(); @@ -468,7 +472,7 @@ export class GeoFeaturesService extends AppBaseService< amount, projectPuId, })), - { chunk: CHUNK_SIZE_FOR_BATCH_GEODB_OPERATIONS }, + { chunk: CHUNK_SIZE_FOR_BATCH_APIDB_OPERATIONS }, ); } @@ -882,10 +886,14 @@ export class GeoFeaturesService extends AppBaseService< } as GeoFeature; } - async saveAmountRangeForFeatures(featureIds: string[]) { + async saveAmountRangeForFeatures( + apiEntityManager: EntityManager, + geoEntityManager: EntityManager, + featureIds: string[], + ) { this.logger.log(`Saving min and max amounts for new features...`); - const minAndMaxAmountsForFeatures = await this.geoEntityManager + const minAndMaxAmountsForFeatures = await geoEntityManager .createQueryBuilder() .select('feature_id', 'id') .addSelect('MIN(amount)', 'amountMin') @@ -896,7 +904,7 @@ export class GeoFeaturesService extends AppBaseService< .getRawMany(); if (minAndMaxAmountsForFeatures.length === 0) { - return; + throw new Error('Error saving Min/Max amounts for given features '); } const minMaxSqlValueStringForFeatures = minAndMaxAmountsForFeatures @@ -915,7 +923,7 @@ export class GeoFeaturesService extends AppBaseService< ${minMaxSqlValueStringForFeatures} ) as minmax(feature_id, min, max) where features.id = minmax.feature_id;`; - await this.geoFeaturesRepository.query(query); + await apiEntityManager.query(query); } async checkProjectFeatureVisibility( diff --git a/api/apps/api/src/modules/geo-features/import/features-amounts-upload.service.ts b/api/apps/api/src/modules/geo-features/import/features-amounts-upload.service.ts index e188b19b9f..a5610f9fca 100644 --- a/api/apps/api/src/modules/geo-features/import/features-amounts-upload.service.ts +++ b/api/apps/api/src/modules/geo-features/import/features-amounts-upload.service.ts @@ -28,6 +28,7 @@ import { UploadedFeatureAmount } from '@marxan-api/modules/geo-features/import/f @Injectable() export class FeatureAmountUploadService { private readonly logger = new Logger(this.constructor.name); + constructor( @InjectDataSource(DbConnections.default) private readonly apiDataSource: DataSource, @@ -107,15 +108,17 @@ export class FeatureAmountUploadService { `Upload temporary data removed from apiDB uploads tables`, ); - this.logger.log(`Csv file upload process finished successfully`); - // Committing transaction - await apiQueryRunner.commitTransaction(); - await geoQueryRunner.commitTransaction(); - this.logger.log(`Saving min and max amounts for new features...`); await this.geoFeaturesService.saveAmountRangeForFeatures( + apiQueryRunner.manager, + geoQueryRunner.manager, newFeaturesFromCsvUpload.map((feature) => feature.id), ); + + this.logger.log(`Csv file upload process finished successfully`); + // Committing transaction + await apiQueryRunner.commitTransaction(); + await geoQueryRunner.commitTransaction(); } catch (err) { await this.events.failEvent(err); await apiQueryRunner.rollbackTransaction(); @@ -134,6 +137,7 @@ export class FeatureAmountUploadService { } return right(newFeaturesFromCsvUpload); } + async saveCsvToRegistry( data: { fileBuffer: Buffer; diff --git a/api/libs/feature-amounts-per-planning-unit/src/feature-amounts-per-planning-units.service.ts b/api/libs/feature-amounts-per-planning-unit/src/feature-amounts-per-planning-units.service.ts index 8a9e7d712a..a26dd18c94 100644 --- a/api/libs/feature-amounts-per-planning-unit/src/feature-amounts-per-planning-units.service.ts +++ b/api/libs/feature-amounts-per-planning-unit/src/feature-amounts-per-planning-units.service.ts @@ -42,7 +42,7 @@ export class FeatureAmountsPerPlanningUnitService { public async computeMarxanAmountPerPlanningUnit( featureId: string, projectId: string, - geoEntityManager?: EntityManager, + geoEntityManager: EntityManager = this.geoEntityManager, ): Promise { /** * @TODO further performance savings: limiting scans to planning_units_geom @@ -50,10 +50,6 @@ export class FeatureAmountsPerPlanningUnitService { * && operator instead of st_intersects() for bbox-based calculation of * intersections. */ - geoEntityManager = geoEntityManager - ? geoEntityManager - : this.geoEntityManager; - const rows: { featureid: string; puid: number;