Skip to content

Commit

Permalink
feat: calculate total scope 2 emissions
Browse files Browse the repository at this point in the history
  • Loading branch information
evanp committed Jun 14, 2024
1 parent 53743f8 commit 1547b4f
Showing 1 changed file with 66 additions and 42 deletions.
108 changes: 66 additions & 42 deletions app/src/app/api/v0/inventory/[inventory]/cdp/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<bigint> {

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<bigint> {
return 0n;
}

function scope1FromGeneration(inventory: Inventory): number {
return 0.0;
async function scope1FromGeneration(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalScope2(inventory: Inventory): number {
return 0.0;
async function totalScope2(inventory: Inventory): Promise<bigint> {
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<bigint> {
return 0n;
}

function totalStationaryScope1(inventory: Inventory): number {
return 0.0;
async function totalStationaryScope1(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalStationaryScope2(inventory: Inventory): number {
return 0.0;
async function totalStationaryScope2(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalStationaryScope3(inventory: Inventory): number {
return 0.0;
async function totalStationaryScope3(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalTransportationScope1(inventory: Inventory): number {
return 0.0;
async function totalTransportationScope1(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalTransportationScope2(inventory: Inventory): number {
return 0.0;
async function totalTransportationScope2(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalTransportationScope3(inventory: Inventory): number {
return 0.0;
async function totalTransportationScope3(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalWasteWithinScope1(inventory: Inventory): number {
return 0.0;
async function totalWasteWithinScope1(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalWasteWithinScope3(inventory: Inventory): number {
return 0.0;
async function totalWasteWithinScope3(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalWasteOutsideScope1(inventory: Inventory): number {
return 0.0;
async function totalWasteOutsideScope1(inventory: Inventory): Promise<bigint> {
return 0n;
}

function totalBasic(inventory: Inventory): number {
return 0.0;
async function totalBasic(inventory: Inventory): Promise<bigint> {
return 0n;
}

export const POST = apiHandler(async (_req, { session, params }) => {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1547b4f

Please sign in to comment.