diff --git a/app/src/app/api/v0/inventory/[inventory]/cdp/route.ts b/app/src/app/api/v0/inventory/[inventory]/cdp/route.ts index 96f866339..f8d61b699 100644 --- a/app/src/app/api/v0/inventory/[inventory]/cdp/route.ts +++ b/app/src/app/api/v0/inventory/[inventory]/cdp/route.ts @@ -5,6 +5,9 @@ import { logger } from "@/services/logger"; import { apiHandler } from "@/util/api"; import { NextResponse } from "next/server"; import { Inventory } from "@/models/Inventory"; +import { InventoryValue } from "@/models/InventoryValue"; +import { db } from "@/models"; +import { Op } from "sequelize"; const EMISSIONS_SECTION = 3; const EMISSIONS_INVENTORY_QUESTION = 0; @@ -16,60 +19,81 @@ function findRow(rows: any[], regex: RegExp): string|null { return row ? row.id : null; } -function totalScope1ExcludingGeneration(inventory: Inventory): number { - return 0.0; +async function getTotalByRefnos(inventory: Inventory, refNos: string[]): Promise { + + let total:bigint = 0n; + + const values = await db.models.InventoryValue.findAll({ + where: + { + inventoryId: inventory.inventoryId, + gpcReferenceNumber: { [Op.in]: refNos } + } + }); + + for (const value of values) { + total += (value.co2eq ?? 0n); + } + + return total; +} + +async function totalScope1ExcludingGeneration(inventory: Inventory): Promise { + return 0n; } -function scope1FromGeneration(inventory: Inventory): number { - return 0.0; +async function scope1FromGeneration(inventory: Inventory): Promise { + return 0n; } -function totalScope2(inventory: Inventory): number { - return 0.0; +async function totalScope2(inventory: Inventory): Promise { + return await getTotalByRefnos(inventory, [ + "I.1.2", "I.2.2", "I.3.2", "I.4.2", "I.5.2", "I.6.2", + "II.1.2", "II.2.2", "II.3.2", "II.4.2", "II.5.2"]) } -function totalScope3(inventory: Inventory): number { - return 0.0; +async function totalScope3(inventory: Inventory): Promise { + return 0n; } -function totalStationaryScope1(inventory: Inventory): number { - return 0.0; +async function totalStationaryScope1(inventory: Inventory): Promise { + return 0n; } -function totalStationaryScope2(inventory: Inventory): number { - return 0.0; +async function totalStationaryScope2(inventory: Inventory): Promise { + return 0n; } -function totalStationaryScope3(inventory: Inventory): number { - return 0.0; +async function totalStationaryScope3(inventory: Inventory): Promise { + return 0n; } -function totalTransportationScope1(inventory: Inventory): number { - return 0.0; +async function totalTransportationScope1(inventory: Inventory): Promise { + return 0n; } -function totalTransportationScope2(inventory: Inventory): number { - return 0.0; +async function totalTransportationScope2(inventory: Inventory): Promise { + return 0n; } -function totalTransportationScope3(inventory: Inventory): number { - return 0.0; +async function totalTransportationScope3(inventory: Inventory): Promise { + return 0n; } -function totalWasteWithinScope1(inventory: Inventory): number { - return 0.0; +async function totalWasteWithinScope1(inventory: Inventory): Promise { + return 0n; } -function totalWasteWithinScope3(inventory: Inventory): number { - return 0.0; +async function totalWasteWithinScope3(inventory: Inventory): Promise { + return 0n; } -function totalWasteOutsideScope1(inventory: Inventory): number { - return 0.0; +async function totalWasteOutsideScope1(inventory: Inventory): Promise { + return 0n; } -function totalBasic(inventory: Inventory): number { - return 0.0; +async function totalBasic(inventory: Inventory): Promise { + return 0n; } export const POST = apiHandler(async (_req, { session, params }) => { @@ -134,33 +158,33 @@ export const POST = apiHandler(async (_req, { session, params }) => { const rows = [ { rowId: findRow(matrix.rows, /Total scope 1 emissions.*excluding/), - content: totalScope1ExcludingGeneration(inventory) }, + content: await totalScope1ExcludingGeneration(inventory) }, { rowId: findRow(matrix.rows, /[Ss]cope 1 emissions.*from generation/), - content: scope1FromGeneration(inventory) }, + content: await scope1FromGeneration(inventory) }, { rowId: findRow(matrix.rows, /Total scope 2 emissions/), - content: totalScope2(inventory) }, + content: await totalScope2(inventory) }, { rowId: findRow(matrix.rows, /Total scope 3 emissions/), - content: totalScope3(inventory) }, + content: await totalScope3(inventory) }, { rowId: findRow(matrix.rows, /Stationary Energy.*scope 1/), - content: totalStationaryScope1(inventory) }, + content: await totalStationaryScope1(inventory) }, { rowId: findRow(matrix.rows, /Stationary Energy.*scope 2/), - content: totalStationaryScope2(inventory) }, + content: await totalStationaryScope2(inventory) }, { rowId: findRow(matrix.rows, /Stationary Energy.*scope 3/), - content: totalStationaryScope3(inventory) }, + content: await totalStationaryScope3(inventory) }, { rowId: findRow(matrix.rows, /Transportation.*scope 1/), - content: totalTransportationScope1(inventory) }, + content: await totalTransportationScope1(inventory) }, { rowId: findRow(matrix.rows, /Transportation.*scope 2/), - content: totalTransportationScope2(inventory) }, + content: await totalTransportationScope2(inventory) }, { rowId: findRow(matrix.rows, /Transportation.*scope 3/), - content: totalTransportationScope3(inventory) }, + content: await totalTransportationScope3(inventory) }, { rowId: findRow(matrix.rows, /Waste.*within.*scope 1/), - content: totalWasteWithinScope1(inventory) }, + content: await totalWasteWithinScope1(inventory) }, { rowId: findRow(matrix.rows, /Waste.*within.*scope 3/), - content: totalWasteWithinScope3(inventory) }, + content: await totalWasteWithinScope3(inventory) }, { rowId: findRow(matrix.rows, /Waste.*outside.*scope 1/), - content: totalWasteOutsideScope1(inventory) }, + content: await totalWasteOutsideScope1(inventory) }, { rowId: findRow(matrix.rows, /TOTAL BASIC emissions/), - content: totalBasic(inventory) }, + content: await totalBasic(inventory) }, ] try {