Skip to content

Commit

Permalink
update coefficient procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 9, 2023
1 parent 7c9741a commit 2eb16f1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions api/src/migrations/1695954091382-AddNewImpactStoredProcedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export class ImpactCalculator {
INDICATOR_NAME_CODES.ENL,
nutrientLoad *
newIndicatorCoefficients[INDICATOR_NAME_CODES.ENL] *
sourcingData.tonnage,
sourcingData.tonnage || 0,
);

return calculatedIndicatorValues;
Expand Down

0 comments on commit 2eb16f1

Please sign in to comment.