diff --git a/api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts b/api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts index bc4caafa7..234f320f6 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 a28f37037..3f8934de5 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;