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 22b7bef340..aaa2eddfae 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 @@ -850,8 +850,22 @@ export class GeoFeaturesService extends AppBaseService< .groupBy('puvspr.feature_id') .getRawMany(); - await this.geoFeaturesRepository.upsert(minAndMaxAmountsForFeatures, [ - 'id', - ]); + const minMaxSqlValueStringForFeatures = minAndMaxAmountsForFeatures + .map( + (feature) => + `(uuid('${feature.id}'), ${feature.amountMin}, ${feature.amountMax})`, + ) + .join(', '); + + const query = ` + update features set + amount_min = minmax.min, + amount_max = minmax.max + from ( + values + ${minMaxSqlValueStringForFeatures} + ) as minmax(feature_id, min, max) + where features.id = minmax.feature_id;`; + await this.geoFeaturesRepository.query(query); } } 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 3b4a0e5b13..75f31629c9 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 @@ -119,15 +119,15 @@ export class FeatureAmountUploadService { this.logger.log(`Saving min and max amounts for new features...`); - await this.geoFeaturesService.saveAmountRangeForFeatures( - newFeaturesFromCsvUpload.map((feature) => feature.id), - ); - this.logger.log(`Csv file upload process finished successfully`); // Committing transaction await apiQueryRunner.commitTransaction(); await geoQueryRunner.commitTransaction(); + + await this.geoFeaturesService.saveAmountRangeForFeatures( + newFeaturesFromCsvUpload.map((feature) => feature.id), + ); } catch (err) { await this.events.failEvent(err); await apiQueryRunner.rollbackTransaction(); @@ -334,6 +334,10 @@ export class FeatureAmountUploadService { `, parameters, ); + await geoQueryRunner.manager.query( + ` INSERT INTO puvspr_calculations (project_id, feature_id, amount, project_pu_id) select $1, $2, amount, project_pu_id from features_data where feature_id = $2`, + [projectId, newFeature.id], + ); this.logger.log( `Chunk with index ${amountIndex} saved to (geoDB).features_data`, ); diff --git a/api/apps/api/test/upload-feature/import-files/feature_amount_upload.csv b/api/apps/api/test/upload-feature/import-files/feature_amount_upload.csv index 5b8aed641d..dc2d861d5c 100644 --- a/api/apps/api/test/upload-feature/import-files/feature_amount_upload.csv +++ b/api/apps/api/test/upload-feature/import-files/feature_amount_upload.csv @@ -1,4 +1,4 @@ puid,feat_1d666bd,feat_28135ef 1,4.245387225,0 2,4.245387225,0 -3,4.245387225,0 \ No newline at end of file +3,3.245387225,0 diff --git a/api/apps/api/test/upload-feature/upload-feature.fixtures.ts b/api/apps/api/test/upload-feature/upload-feature.fixtures.ts index c108a67a9d..00fbb2cb28 100644 --- a/api/apps/api/test/upload-feature/upload-feature.fixtures.ts +++ b/api/apps/api/test/upload-feature/upload-feature.fixtures.ts @@ -307,6 +307,8 @@ export const getFixtures = async () => { expect(newFeaturesAdded[0].projectId).toBe(projectId); expect(newFeaturesAdded[0].isLegacy).toBe(true); expect(newFeaturesAdded[1].isLegacy).toBe(true); + expect(newFeaturesAdded[0].amountMin).toEqual(3.245387225); + expect(newFeaturesAdded[0].amountMax).toEqual(4.245387225); }, ThenNewFeaturesAmountsAreCreated: async () => { @@ -322,6 +324,9 @@ export const getFixtures = async () => { }); const newFeature1Amounts = await featuresAmounsGeoDbRepository.find({ where: { featureId: newFeatures1?.id }, + order: { + amount: 'DESC', + }, }); const newFeature2Amounts = await featuresAmounsGeoDbRepository.find({ where: { featureId: newFeatures2?.id }, @@ -331,7 +336,7 @@ export const getFixtures = async () => { expect(newFeature2Amounts).toHaveLength(3); expect(newFeature1Amounts[0].amount).toBe(4.245387225); expect(newFeature1Amounts[1].amount).toBe(4.245387225); - expect(newFeature1Amounts[2].amount).toBe(4.245387225); + expect(newFeature1Amounts[2].amount).toBe(3.245387225); expect(newFeature2Amounts[0].amount).toBe(0); expect(newFeature2Amounts[1].amount).toBe(0);