From 4917aa33aa001d62ab88bfbe9f07e16fcc2439fd Mon Sep 17 00:00:00 2001 From: Samuel Plumppu <6125097+Greenheart@users.noreply.github.com> Date: Thu, 12 Dec 2024 05:45:32 +0100 Subject: [PATCH] Account for `scope3.statedTotalEmissions` and `scope1And2.total` when preparing `calculatedTotalEmissions` (#458) fix #456 --- src/routes/readCompanies.ts | 68 ++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/src/routes/readCompanies.ts b/src/routes/readCompanies.ts index 9ef4fc0b..1b0cf1de 100644 --- a/src/routes/readCompanies.ts +++ b/src/routes/readCompanies.ts @@ -235,13 +235,18 @@ router.get( (reportingPeriod.emissions?.scope3 && { ...reportingPeriod.emissions.scope3, calculatedTotalEmissions: - reportingPeriod.emissions.scope3.categories.reduce( - (total, category) => - isNumber(category.total) - ? category.total + total - : total, - 0 - ), + reportingPeriod.emissions.scope3.categories.some((c) => + Boolean(c.metadata.verifiedBy) + ) + ? reportingPeriod.emissions.scope3.categories.reduce( + (total, category) => + isNumber(category.total) + ? category.total + total + : total, + 0 + ) + : reportingPeriod.emissions.scope3 + .statedTotalEmissions?.total ?? 0, }) || undefined, }, @@ -271,9 +276,18 @@ router.get( emissions: { ...reportingPeriod.emissions, calculatedTotalEmissions: - (reportingPeriod.emissions?.scope1?.total || 0) + - (reportingPeriod.emissions?.scope2 - ?.calculatedTotalEmissions || 0) + + // If either scope 1 and scope 2 have verification, then we use them for the total. + // Otherwise, we use the combined scope1And2 if it exists + (Boolean( + reportingPeriod.emissions?.scope1?.metadata?.verifiedBy + ) || + Boolean( + reportingPeriod.emissions?.scope2?.metadata?.verifiedBy + ) + ? (reportingPeriod.emissions?.scope1?.total || 0) + + (reportingPeriod.emissions?.scope2 + ?.calculatedTotalEmissions || 0) + : reportingPeriod.emissions?.scope1And2?.total || 0) + (reportingPeriod.emissions?.scope3 ?.calculatedTotalEmissions || 0), }, @@ -473,13 +487,18 @@ router.get( (reportingPeriod.emissions?.scope3 && { ...reportingPeriod.emissions.scope3, calculatedTotalEmissions: - reportingPeriod.emissions.scope3.categories.reduce( - (total, category) => - isNumber(category.total) - ? category.total + total - : total, - 0 - ), + reportingPeriod.emissions.scope3.categories.some((c) => + Boolean(c.metadata.verifiedBy) + ) + ? reportingPeriod.emissions.scope3.categories.reduce( + (total, category) => + isNumber(category.total) + ? category.total + total + : total, + 0 + ) + : reportingPeriod.emissions.scope3 + .statedTotalEmissions?.total ?? 0, }) || undefined, }, @@ -509,9 +528,18 @@ router.get( emissions: { ...reportingPeriod.emissions, calculatedTotalEmissions: - (reportingPeriod.emissions?.scope1?.total || 0) + - (reportingPeriod.emissions?.scope2 - ?.calculatedTotalEmissions || 0) + + // if either scope 1 and scope 2 have verification, then we use them for the total. + // Otherwise, we use the combined scope1And2 if it exists + (Boolean( + reportingPeriod.emissions?.scope1?.metadata?.verifiedBy + ) || + Boolean( + reportingPeriod.emissions?.scope2?.metadata?.verifiedBy + ) + ? (reportingPeriod.emissions?.scope1?.total || 0) + + (reportingPeriod.emissions?.scope2 + ?.calculatedTotalEmissions || 0) + : reportingPeriod.emissions?.scope1And2?.total || 0) + (reportingPeriod.emissions?.scope3 ?.calculatedTotalEmissions || 0), },