adding missing data to scenario

This commit is contained in:
Khalim Conn-Kowlessar 2024-07-30 22:18:02 +01:00
parent 3015c3062e
commit a9d4d8587f
6 changed files with 2841 additions and 33 deletions

View file

@ -0,0 +1,2 @@
ALTER TABLE "scenario" ADD COLUMN "property_valuation_increase" real;--> statement-breakpoint
ALTER TABLE "scenario" ADD COLUMN "labour_days" real;

File diff suppressed because it is too large Load diff

View file

@ -617,6 +617,13 @@
"when": 1722371993027,
"tag": "0087_nebulous_umar",
"breakpoints": true
},
{
"idx": 88,
"version": "5",
"when": 1722373333782,
"tag": "0088_many_tana_nile",
"breakpoints": true
}
]
}

View file

@ -122,6 +122,8 @@ export const scenario = pgTable("scenario", {
energySavings: real("energy_savings"), // Unit is always kWh so we don't need to store unit
co2EquivalentSavings: real("co2_equivalent_savings"), // Unit is always tonnes so we don't need to store unit
energyCostSavings: real("energy_cost_savings"), // Unit is always £ so we don't need to store unit for the moment
propertyValuationIncrease: real("property_valuation_increase"), // Unit is always £ so we don't need to store unit for the moment
labourDays: real("labour_days"),
// Aggregates added for the summary tab
epcBreakdownPreRetrofit: text("epc_breakdown_pre_retrofit"),
epcBreakdownPostRetrofit: text("epc_breakdown_post_retrofit"),
@ -154,6 +156,7 @@ export type RecommendationMaterial = InferModel<
typeof recommendationMaterials,
"select"
>;
export type ScenarioSelect = InferModel<typeof scenario, "select">;
// We allow recommendation types to be a string however we'll set up a typing for it here to control
// the types we expect

View file

@ -29,7 +29,6 @@ interface SummaryBoxProps {
co2EquivalentSavings: number | null;
totalWorkHours: number | null;
propertyValuationIncrease: number | null;
rentalYieldIncrease: number | null;
energySavings: number | null;
energyCostSavings: number | null;
estimatedDuration: number | null;
@ -42,7 +41,6 @@ function SummaryBox({
co2EquivalentSavings,
totalWorkHours,
propertyValuationIncrease,
rentalYieldIncrease,
energySavings,
energyCostSavings,
estimatedDuration,
@ -90,7 +88,7 @@ function SummaryBox({
const budgetFormatted = formatBudget(budget);
const totalCostFormatted = formatMoney(totalCost);
const totalValueIncreaseFormatted = formatMoney(propertyValuationIncrease);
const totalWorkHoursFormatted = formatHours(totalWorkHours);
// const totalWorkHoursFormatted = formatHours(totalWorkHours);
// const rentalYieldIncreaseFormatted = formatMoney(rentalYieldIncrease);
const energyCostSavingsFormatted = formatMoney(energyCostSavings);
const co2EquivalentSavingsFormatted = formatCo2(co2EquivalentSavings);
@ -203,20 +201,14 @@ export default async function Page({
// This page is served from the server so we can make calls to the database
const portfolioId = params.slug;
const {
budget,
cost: totalCost,
co2EquivalentSavings,
totalWorkHours,
propertyValuationIncrease,
rentalYieldIncrease,
energySavings,
energyCostSavings,
labourDays,
} = await getPortfolio(portfolioId);
const portfolio = await getPortfolio(portfolioId);
const portfolioPerformance = await getPortfolioPerformance(portfolioId);
console.log(portfolioPerformance);
const defaultPerformance = portfolioPerformance.find(
(perf) => perf.isDefault
);
const portfolioData = defaultPerformance || portfolio;
// Default limit to 1000 and offset to 0 for now - will handle pagination later
const properties: PropertyWithRelations[] = await getProperties(
@ -231,16 +223,17 @@ export default async function Page({
<div className="grid grid-cols-11 w-full max-w-8xl h-screen">
<div className="col-span-3 flex-col">
<SummaryBox
budget={budget}
totalCost={totalCost}
budget={portfolioData.budget}
totalCost={portfolioData.cost}
numProperties={properties.length}
co2EquivalentSavings={co2EquivalentSavings}
totalWorkHours={totalWorkHours}
propertyValuationIncrease={propertyValuationIncrease}
rentalYieldIncrease={rentalYieldIncrease}
energySavings={energySavings}
energyCostSavings={energyCostSavings}
estimatedDuration={labourDays}
co2EquivalentSavings={portfolioData.co2EquivalentSavings}
totalWorkHours={portfolioData.totalWorkHours}
propertyValuationIncrease={
portfolioData.propertyValuationIncrease
}
energySavings={portfolioData.energySavings}
energyCostSavings={portfolioData.energyCostSavings}
estimatedDuration={portfolioData.labourDays}
/>
</div>
<div className="col-span-8 bg-white">

View file

@ -1,18 +1,19 @@
import { formatNumber } from "@/app/utils";
import {
recommendation,
UnnestedRecommendation,
PortfolioPlanRecommendation,
scenario,
} from "./../../db/schema/recommendations";
import { and, ConsoleLogWriter, eq, inArray } from "drizzle-orm";
import { db } from "@/app/db/db";
import { portfolio, portfolioUsers } from "@/app/db/schema/portfolio";
import { property } from "@/app/db/schema/property";
import type { Portfolio } from "@/app/db/schema/portfolio";
import type { PropertyWithRelations } from "@/app/db/schema/property";
import { plan, planRecommendations } from "@/app/db/schema/recommendations";
import { a } from "drizzle-orm/column.d-b7dc3bdb";
import {
plan,
planRecommendations,
recommendation,
UnnestedRecommendation,
PortfolioPlanRecommendation,
scenario,
ScenarioSelect,
} from "@/app/db/schema/recommendations";
export async function getPortfolio(portfolioId: string): Promise<Portfolio> {
const data = await db
@ -31,7 +32,9 @@ export async function getPortfolio(portfolioId: string): Promise<Portfolio> {
return data[0];
}
export async function getPortfolioPerformance(portfolioId: string) {
export async function getPortfolioPerformance(
portfolioId: string
): Promise<ScenarioSelect[]> {
// This function will grab portfolio metrics from the database, but it will firstly check for the existence of scenarios and
// use those figures. If they don't exist, we will use the default figures from the portfolio table
const scenarios = await db