From cce5c2f644d21e3a2f05c3a68ee272370f7061f2 Mon Sep 17 00:00:00 2001 From: alexeh Date: Mon, 9 Oct 2023 05:04:24 +0300 Subject: [PATCH] update coefficient procedure --- ...95954091382-AddNewImpactStoredProcedure.ts | 46 +++++++++++++++++++ .../services/impact-calculator.service.ts | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts b/api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts index bc4caafa7f..234f320f69 100644 --- a/api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts +++ b/api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts @@ -54,6 +54,52 @@ export class AddNewImpactStoredProcedure1695954091382 $$ LANGUAGE plpgsql; `); + + await queryRunner.query(` + CREATE OR REPLACE FUNCTION get_indicator_coefficient_impact( + nameCode text, + adminRegionId uuid, + material_id uuid) + RETURNS float AS + $$ + DECLARE + indicator_id uuid; + value float; + BEGIN + + --get indicatorId + SELECT "id" INTO indicator_id FROM "indicator" + WHERE "nameCode" = nameCode; + + -- get water footprint value by location, material and indicator + EXECUTE format( + 'SELECT + COALESCE ( + ( + SELECT ic."value" + FROM "indicator_coefficient" ic + WHERE ic."adminRegionId" = $1 + AND ic."materialId" = $2 + AND ic."indicatorId" = $3 + AND ic."value" IS NOT NULL + ), + ( + SELECT ic."value" + FROM "indicator_coefficient" ic + WHERE ic."adminRegionId" IS NULL + AND ic."materialId" = $2 + AND ic."indicatorId" = $3 + AND ic."value" IS NOT NULL + ) + ) AS value;' + ) + USING adminRegionId, material_id, indicator_id + INTO value; + RETURN value; + END; +$$ +LANGUAGE plpgsql; + `); } public async down(queryRunner: QueryRunner): Promise {} diff --git a/api/src/modules/indicator-records/services/impact-calculator.service.ts b/api/src/modules/indicator-records/services/impact-calculator.service.ts index a28f37037d..3f8934de5e 100644 --- a/api/src/modules/indicator-records/services/impact-calculator.service.ts +++ b/api/src/modules/indicator-records/services/impact-calculator.service.ts @@ -319,7 +319,7 @@ export class ImpactCalculator { INDICATOR_NAME_CODES.ENL, nutrientLoad * newIndicatorCoefficients[INDICATOR_NAME_CODES.ENL] * - sourcingData.tonnage, + sourcingData.tonnage || 0, ); return calculatedIndicatorValues;