commiting due considerations, fixing bug with updating of sap and cost on recommendations

This commit is contained in:
Khalim Conn-Kowlessar 2023-09-28 09:28:49 +01:00
parent 662ae1cfb0
commit e35f70b3c8
7 changed files with 92 additions and 16 deletions

View file

@ -0,0 +1,57 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
const BodySchema = z.object({
folderKey: z.string(),
userId: z.string(),
scheme: z.string(),
});
export async function POST(request: NextRequest) {
const body = await request.json();
let validatedBody;
try {
validatedBody = BodySchema.parse(body);
} catch (error) {
console.error("Invalid input: ", error);
return new NextResponse(JSON.stringify({ msg: "Invalid input" }), {
status: 400,
});
}
try {
// We'll trigger the plan build in our fastapi backend and then the user will just have to
// wait for the plan to be ready
const headers = {
"Content-Type": "application/json",
};
console.log("validatedBody", validatedBody);
const response = await fetch(
`${process.env.DUE_CONSIDERATIONS_API_URL}/due-considerations`,
{
method: "POST",
headers: headers,
body: JSON.stringify(validatedBody),
}
);
if (!response.ok) {
throw new Error("API request failed");
}
const responseData = await response.json();
return new NextResponse(JSON.stringify(responseData), {
status: 200,
});
} catch (error) {
console.error(error);
return new NextResponse(JSON.stringify({ msg: "Internal server error" }), {
status: 500,
});
}
}

View file

@ -48,14 +48,14 @@ export default function RecommendationContainer({
// ) || { estimatedCost: 0, sapPoints: 0 };
const [costMap, setCostMap] = useState<RecommendationMetricMap>({
Walls: defaultWallsRecommendations?.estimatedCost || 0,
Floor: defaultFloorRecommendations?.estimatedCost || 0,
wall_insulation: defaultWallsRecommendations?.estimatedCost || 0,
floor_insulation: defaultFloorRecommendations?.estimatedCost || 0,
// Ventilation: defaultVentiliationRecommendations?.estimatedCost || 0,
});
const [sapMap, setSapMap] = useState<RecommendationMetricMap>({
Walls: defaultWallsRecommendations?.sapPoints || 0,
Floor: defaultFloorRecommendations.sapPoints || 0,
wall_insulation: defaultWallsRecommendations?.sapPoints || 0,
floor_insulation: defaultFloorRecommendations.sapPoints || 0,
// Ventilation: defaultVentiliationRecommendations.sapPoints,
});

View file

@ -1,6 +1,5 @@
"use client";
import { formatNumber } from "@/app/utils";
import { useState } from "react";
export default function RecommendationCostSummaryCard({
totalEstimatedCost,

View file

@ -77,6 +77,8 @@ export default function RecommendationModal({
// update the cost sum
setTotalEstimatedCost(sumRecommendationMetricMap(newCostMap));
console.log("B4", sapMap);
// Update the sap map
const newSapMap = {
...sapMap,
@ -84,12 +86,15 @@ export default function RecommendationModal({
};
setSapMap(newSapMap);
console.log("AFTER", newSapMap);
// update the sap sum
const newSapImporvement = sumRecommendationMetricMap(newSapMap);
setTotalSapPoints(newSapImporvement);
const newSapImprovement = sumRecommendationMetricMap(newSapMap);
console.log("newSapImprovement", newSapImprovement);
setTotalSapPoints(newSapImprovement);
// TODO: While we have placeholder SAP points, constrain to 100
const newSapPoints = Math.min(currentSapPoints + newSapImporvement, 100);
const newSapPoints = Math.min(currentSapPoints + newSapImprovement, 100);
// update the expected EPC rating
setExpectedEpcRating(sapToEpc(newSapPoints));

View file

@ -0,0 +1,15 @@
import { cn } from "@/lib/utils"
function Skeleton({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn("animate-pulse rounded-md bg-muted", className)}
{...props}
/>
)
}
export { Skeleton }

View file

@ -30,17 +30,17 @@ export function sapToEpc(sapPoints: number): string {
throw new Error("SAP points should be between 1 and 100.");
}
if (sapPoints > 91) {
if (sapPoints >= 92) {
return "A";
} else if (sapPoints > 80) {
} else if (sapPoints >= 81) {
return "B";
} else if (sapPoints > 69) {
} else if (sapPoints >= 69) {
return "C";
} else if (sapPoints > 55) {
} else if (sapPoints >= 55) {
return "D";
} else if (sapPoints > 39) {
} else if (sapPoints >= 39) {
return "E";
} else if (sapPoints > 21) {
} else if (sapPoints >= 21) {
return "F";
} else {
return "G";

View file

@ -1,6 +1,6 @@
export interface RecommendationMetricMap {
Walls: number;
Floor: number;
wall_insulation: number;
floor_insulation: number;
// TODO: Implement ventilation
// Ventilation: number;
}