Skip to content

Commit

Permalink
Merge pull request #568 from Open-Earth-Foundation/on-1849
Browse files Browse the repository at this point in the history
Calculations for CDP API
  • Loading branch information
lemilonkh authored Jul 19, 2024
2 parents d795c14 + e363dc8 commit 436bcc1
Showing 1 changed file with 118 additions and 85 deletions.
203 changes: 118 additions & 85 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";
import createHttpError from "http-errors";

const EMISSIONS_SECTION = 3;
Expand All @@ -17,60 +20,118 @@ 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 await getTotalByRefnos(inventory, [
"I.1.1", "I.2.1", "I.3.1", "I.4.1", "I.5.1", "I.6.1",
"I.7.1", "I.8.1",
"II.1.1", "II.2.1", "II.3.1", "II.4.1", "II.5.1",
"III.1.1", "III.2.1", "III.3.1", "III.4.1",
"III.1.3", "III.2.3", "III.3.3", "III.4.3"
]);
}

function scope1FromGeneration(inventory: Inventory): number {
return 0.0;
async function scope1FromGeneration(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"I.4.4"
]);
}

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 await getTotalByRefnos(inventory,
`I.1.3, I.2.3, I.3.3, I.4.3, I.5.3, I.6.3
II.1.3, II.2.3, II.3.3, II.4.3
III.1.2, III.2.2, III.3.2, III.4.2`.split(/\s*,\s*/)
)
}

function totalStationaryScope1(inventory: Inventory): number {
return 0.0;
async function totalStationaryScope1(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"I.1.1", "I.2.1", "I.3.1", "I.4.1", "I.5.1", "I.6.1",
"I.7.1", "I.8.1", "I.4.4"
]);
}

function totalStationaryScope2(inventory: Inventory): number {
return 0.0;
async function totalStationaryScope2(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",
])
}

function totalStationaryScope3(inventory: Inventory): number {
return 0.0;
async function totalStationaryScope3(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"I.1.3", "I.2.3", "I.3.3", "I.4.3", "I.5.3", "I.6.3"
])
}

function totalTransportationScope1(inventory: Inventory): number {
return 0.0;
async function totalTransportationScope1(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"II.1.1", "II.2.1", "II.3.1", "II.4.1", "II.5.1"
]);
}

function totalTransportationScope2(inventory: Inventory): number {
return 0.0;
async function totalTransportationScope2(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"II.1.2", "II.2.2", "II.3.2", "II.4.2", "II.5.2"
]);
}

function totalTransportationScope3(inventory: Inventory): number {
return 0.0;
async function totalTransportationScope3(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"II.1.3", "II.2.3", "II.3.3", "II.4.3"
]);
}

function totalWasteWithinScope1(inventory: Inventory): number {
return 0.0;
async function totalWasteWithinScope1(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"III.1.1", "III.2.1", "III.3.1", "III.4.1"
]);
}

function totalWasteWithinScope3(inventory: Inventory): number {
return 0.0;
async function totalWasteWithinScope3(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"III.1.2", "III.2.2", "III.3.2", "III.4.2"
]);
}

function totalWasteOutsideScope1(inventory: Inventory): number {
return 0.0;
async function totalWasteOutsideScope1(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory, [
"III.1.3", "III.2.3", "III.3.3", "III.4.3"
]);
}

function totalBasic(inventory: Inventory): number {
return 0.0;
async function totalBasic(inventory: Inventory): Promise<bigint> {
return await getTotalByRefnos(inventory,
`I.1.1, I.2.1, I.3.1, I.4.1, I.5.1, I.6.1, I.7.1, I.8.1
II.1.1, II.2.1, II.3.1, II.4.1, II.5.1
III.1.1, III.2.1, III.3.1, III.4.1
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
III.1.2, III.2.2, III.3.2, III.4.2`.split(/\s*,\s/));
}

export const POST = apiHandler(async (_req, { session, params }) => {
Expand Down Expand Up @@ -138,63 +199,35 @@ export const POST = apiHandler(async (_req, { session, params }) => {
});

const rows = [
{
rowId: findRow(matrix.rows, /Total scope 1 emissions.*excluding/),
content: totalScope1ExcludingGeneration(inventory),
},
{
rowId: findRow(matrix.rows, /[Ss]cope 1 emissions.*from generation/),
content: scope1FromGeneration(inventory),
},
{
rowId: findRow(matrix.rows, /Total scope 2 emissions/),
content: totalScope2(inventory),
},
{
rowId: findRow(matrix.rows, /Total scope 3 emissions/),
content: totalScope3(inventory),
},
{
rowId: findRow(matrix.rows, /Stationary Energy.*scope 1/),
content: totalStationaryScope1(inventory),
},
{
rowId: findRow(matrix.rows, /Stationary Energy.*scope 2/),
content: totalStationaryScope2(inventory),
},
{
rowId: findRow(matrix.rows, /Stationary Energy.*scope 3/),
content: totalStationaryScope3(inventory),
},
{
rowId: findRow(matrix.rows, /Transportation.*scope 1/),
content: totalTransportationScope1(inventory),
},
{
rowId: findRow(matrix.rows, /Transportation.*scope 2/),
content: totalTransportationScope2(inventory),
},
{
rowId: findRow(matrix.rows, /Transportation.*scope 3/),
content: totalTransportationScope3(inventory),
},
{
rowId: findRow(matrix.rows, /Waste.*within.*scope 1/),
content: totalWasteWithinScope1(inventory),
},
{
rowId: findRow(matrix.rows, /Waste.*within.*scope 3/),
content: totalWasteWithinScope3(inventory),
},
{
rowId: findRow(matrix.rows, /Waste.*outside.*scope 1/),
content: totalWasteOutsideScope1(inventory),
},
{
rowId: findRow(matrix.rows, /TOTAL BASIC emissions/),
content: totalBasic(inventory),
},
];
{ rowId: findRow(matrix.rows, /Total scope 1 emissions.*excluding/),
content: await totalScope1ExcludingGeneration(inventory) },
{ rowId: findRow(matrix.rows, /[Ss]cope 1 emissions.*from generation/),
content: await scope1FromGeneration(inventory) },
{ rowId: findRow(matrix.rows, /Total scope 2 emissions/),
content: await totalScope2(inventory) },
{ rowId: findRow(matrix.rows, /Total scope 3 emissions/),
content: await totalScope3(inventory) },
{ rowId: findRow(matrix.rows, /Stationary Energy.*scope 1/),
content: await totalStationaryScope1(inventory) },
{ rowId: findRow(matrix.rows, /Stationary Energy.*scope 2/),
content: await totalStationaryScope2(inventory) },
{ rowId: findRow(matrix.rows, /Stationary Energy.*scope 3/),
content: await totalStationaryScope3(inventory) },
{ rowId: findRow(matrix.rows, /Transportation.*scope 1/),
content: await totalTransportationScope1(inventory) },
{ rowId: findRow(matrix.rows, /Transportation.*scope 2/),
content: await totalTransportationScope2(inventory) },
{ rowId: findRow(matrix.rows, /Transportation.*scope 3/),
content: await totalTransportationScope3(inventory) },
{ rowId: findRow(matrix.rows, /Waste.*within.*scope 1/),
content: await totalWasteWithinScope1(inventory) },
{ rowId: findRow(matrix.rows, /Waste.*within.*scope 3/),
content: await totalWasteWithinScope3(inventory) },
{ rowId: findRow(matrix.rows, /Waste.*outside.*scope 1/),
content: await totalWasteOutsideScope1(inventory) },
{ rowId: findRow(matrix.rows, /TOTAL BASIC emissions/),
content: await totalBasic(inventory) },
]

try {
success = await CDPService.submitSingleSelect(
Expand Down

0 comments on commit 436bcc1

Please sign in to comment.