Skip to content

Commit

Permalink
Merge pull request #1080 from Open-Earth-Foundation/nina/bug/ON-3220/…
Browse files Browse the repository at this point in the history
…fixEmptyDataResults

bug: [ON-3320] fix report results when inventory has no data
  • Loading branch information
evanp authored Jan 23, 2025
2 parents a52cea0 + 9496717 commit b2ab56c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions app/src/app/api/v0/inventory/[inventory]/results/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const GET = apiHandler(
return NextResponse.json({
data: {
totalEmissions: {
bySector: totalEmissionsBySector,
total: totalEmissions,
bySector: totalEmissionsBySector || [],
total: totalEmissions || 0,
},
topEmissions: { bySubSector: topEmissionsBySubSector },
topEmissions: { bySubSector: topEmissionsBySubSector || [] },
},
});
},
Expand Down
6 changes: 3 additions & 3 deletions app/src/backend/ResultsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,10 @@ export async function getEmissionResults(inventory: string): Promise<{
const EmissionResults = await getEmissionResultsBatch([inventory]);
const inventoryEmissionResults = EmissionResults[inventory];
return {
totalEmissions: inventoryEmissionResults.totalEmissions.sumOfEmissions,
totalEmissions: inventoryEmissionResults?.totalEmissions?.sumOfEmissions,
totalEmissionsBySector:
inventoryEmissionResults.totalEmissions.totalEmissionsBySector,
topEmissionsBySubSector: inventoryEmissionResults.topEmissionsBySubSector,
inventoryEmissionResults?.totalEmissions?.totalEmissionsBySector,
topEmissionsBySubSector: inventoryEmissionResults?.topEmissionsBySubSector,
};
}

Expand Down
30 changes: 30 additions & 0 deletions app/tests/api/results.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,34 @@ describe("Results API", () => {
},
});
});

it("should return empty arrays when Inventory has no data", async () => {
const emptyInventoryId = randomUUID();
const emptyInventory = await db.models.Inventory.create({
inventoryId: emptyInventoryId,
...baseInventory,
inventoryName: "ReportResultEmptyInventory",
cityId: city.cityId,
inventoryType: InventoryTypeEnum.GPC_BASIC,
globalWarmingPotentialType: GlobalWarmingPotentialTypeEnum.ar6,
year: 2022,
});
const req = mockRequest();
const res = await getResults(req, {
params: { inventory: emptyInventory.inventoryId },
});

await expectStatusCode(res, 200);
expect(await res.json()).toEqual({
data: {
topEmissions: {
bySubSector: [],
},
totalEmissions: {
bySector: [],
total: 0,
},
},
});
});
});

0 comments on commit b2ab56c

Please sign in to comment.