From 0ab8a3441714ed95fb75698f3ffb170112d83d7c Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Wed, 2 Aug 2023 15:30:23 +0100 Subject: [PATCH] resructuring api requests --- .../property-meta/[propertyId]/route.ts | 34 -- .../[propertyId]/route.ts | 5 +- src/app/db/schema/property.ts | 4 + .../building-passport/[propertyId]/page.tsx | 2 + .../pre-assessment-report/page.tsx | 293 +++++++++--------- .../building-passport/[propertyId]/utils.ts | 25 +- 6 files changed, 179 insertions(+), 184 deletions(-) delete mode 100644 src/app/api/building-passport/property-meta/[propertyId]/route.ts rename src/app/api/{building-passport/condition-report => property-meta}/[propertyId]/route.ts (83%) diff --git a/src/app/api/building-passport/property-meta/[propertyId]/route.ts b/src/app/api/building-passport/property-meta/[propertyId]/route.ts deleted file mode 100644 index fa3ce80..0000000 --- a/src/app/api/building-passport/property-meta/[propertyId]/route.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { db } from "@/app/db/db"; -import { property } from "@/app/db/schema/property"; -import { serializeBigInt } from "@/app/utils"; -import { eq } from "drizzle-orm"; - -export async function GET( - request: Request, - { params }: { params: { propertyId: string } } -) { - const propertyId = params.propertyId; - - const propertyMeta = await db.query.property.findFirst({ - columns: { - id: true, - address: true, - postcode: true, - hasPreConditionReport: true, - hasRecommendations: true, - createdAt: true, - propertyType: true, - builtForm: true, - localAuthority: true, - constituency: true, - numberOfRooms: true, - yearBuilt: true, - tenure: true, - currentEpcRating: true, - currentSapPoints: true, - }, - where: eq(property.id, BigInt(propertyId)), - }); - - return new Response(JSON.stringify(propertyMeta, serializeBigInt)); -} diff --git a/src/app/api/building-passport/condition-report/[propertyId]/route.ts b/src/app/api/property-meta/[propertyId]/route.ts similarity index 83% rename from src/app/api/building-passport/condition-report/[propertyId]/route.ts rename to src/app/api/property-meta/[propertyId]/route.ts index fa3ce80..7adffe2 100644 --- a/src/app/api/building-passport/condition-report/[propertyId]/route.ts +++ b/src/app/api/property-meta/[propertyId]/route.ts @@ -2,9 +2,10 @@ import { db } from "@/app/db/db"; import { property } from "@/app/db/schema/property"; import { serializeBigInt } from "@/app/utils"; import { eq } from "drizzle-orm"; +import { NextRequest, NextResponse } from "next/server"; export async function GET( - request: Request, + request: NextRequest, { params }: { params: { propertyId: string } } ) { const propertyId = params.propertyId; @@ -30,5 +31,5 @@ export async function GET( where: eq(property.id, BigInt(propertyId)), }); - return new Response(JSON.stringify(propertyMeta, serializeBigInt)); + return new NextResponse(JSON.stringify(propertyMeta, serializeBigInt)); } diff --git a/src/app/db/schema/property.ts b/src/app/db/schema/property.ts index 91fe1a8..d6c3cb8 100644 --- a/src/app/db/schema/property.ts +++ b/src/app/db/schema/property.ts @@ -193,6 +193,10 @@ export const propertyTargetRelations = relations(property, ({ one }) => ({ // TODO: We'll need a property details buildings materials for verisk data? export type Property = InferModel; +export type PropertyDetailsEpc = InferModel< + typeof propertyDetailsEpc, + "select" +>; // This type is used for the getProperties function in src/app/portfolio/[slug]/utils.ts export interface PropertyWithTarget { status: string | null; diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/page.tsx b/src/app/portfolio/[slug]/building-passport/[propertyId]/page.tsx index e8c83d3..2f717d4 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/page.tsx +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/page.tsx @@ -11,6 +11,8 @@ import { } from "@heroicons/react/24/solid"; import { getPropertyMeta } from "./utils"; +export const revalidate = 1; + export default async function BuildingPassportHome({ params, }: { diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/pre-assessment-report/page.tsx b/src/app/portfolio/[slug]/building-passport/[propertyId]/pre-assessment-report/page.tsx index 2e3a92b..8fa3457 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/pre-assessment-report/page.tsx +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/pre-assessment-report/page.tsx @@ -6,7 +6,7 @@ import { generalColumns, retrofitColumns, } from "@/app/components/building-passport/FeatureTableColumns"; -import { getPropertyMeta } from "../utils"; +import { getConditionReport, getPropertyMeta } from "../utils"; function AddressCard({ address }: { address: string }) { // In the future, we might want to use react-wrap-balancer for some of this text @@ -35,7 +35,7 @@ function PropertyDetailsCard({ Year built: - {conditionReportData.yearBuilt} + {propertyMeta.yearBuilt} @@ -91,9 +91,6 @@ function PropertyDetailsCard({ ); } -// This type is used to define the shape of our data. -// You can use a Zod schema here if you want. - export default async function PreAssessmentReport({ params, }: { @@ -101,146 +98,152 @@ export default async function PreAssessmentReport({ }) { // TODO: Add main fuel! const propertyMeta = await getPropertyMeta(params.propertyId); + const conditionReportData = await getConditionReport(params.propertyId); + console.log("DATA!!"); + console.log(conditionReportData); - const conditionReportData: ConditionReportData = { - id: 1, - lastUpdated: "2023-07-12 11:51:31.000 +0100", - fullAddress: "123 Fake Street, Fake Town", - postcode: "AB1 2CD", - currentEpcRating: "C", - inConservationArea: "Yes", - propertyType: "House", - builtForm: "Detached", - totalFloorArea: 60, - tenure: "Rented (social)", - yearBuilt: "1990", - retrofitFeatures: [ - { feature: "Wall", description: "Cavity wall", rating: "Poor" }, - { - feature: "Roof", - description: "Flat, limited insulation (assumed)", - rating: "Very poor", - }, - { feature: "Windows", description: "Double glazing", rating: "Good" }, - { feature: "Heating", description: "Gas boiler", rating: "Good" }, - { - feature: "Heating Control", - description: "Programmer and appliance thermostats", - rating: "Good", - }, - { - feature: "Hot Water", - description: "Electric immersion, standard tariff", - rating: "Good", - }, - { - feature: "Lighting", - description: "Low energy lighting in all fixed outlets", - rating: "Very good", - }, - { - feature: "Floor", - description: "Suspended timber", - rating: "Poor", - }, - { - feature: "Ventilation", - description: "Natural", - rating: "N/A", - }, - { - feature: "Solar Photo Voltaic", - description: "Not present in the property", - rating: "N/A", - }, - { - feature: "Solar Hot Water", - description: "Heating is not solar powered", - rating: "N/A", - }, - { - feature: "Wind Turbines", - description: "No wind turbines present", - rating: "N/A", - }, - ], - generalFeatures: [ - { - feature: "Floor Height", - description: 2.4, - }, - { - feature: "Number of heated rooms", - description: 5, - }, - { - feature: "Heat loss corridor", - description: "No", - }, - { - feature: "Heat loss corridor length", - description: 0, - }, - { - feature: "Number of open fire places", - description: 0, - }, - { - feature: "Number of extensions", - description: 0, - }, - { - feature: "Floor level", - description: "Ground", - }, - { - feature: "Number of storeys", - description: 2, - }, - { - feature: "Mains gas available", - description: "Yes", - }, - { - feature: "Energy tariff", - description: "Standard", - }, - ], - heatingDemand: [ - { - feature: "EPC primary energy consumption", - description: "98 kWh/m2/year", - }, - { - feature: "EPC CO2 emissions", - description: "2.8 tonnes/year", - }, - { - feature: "Elecrticity Supplier", - description: "E.ON Energy", - }, - { - feature: "Gas Supplier", - description: "British Gas", - }, - { - feature: "Meter reading total energy consumption", - description: "108 kWh/m2/year", - }, - { - feature: "Meter reading electicity consumption", - description: "22 kWh/m2/year", - }, - { - feature: "Meter reading gas consumption", - description: "86 kWh/m2/year", - }, - ], - }; + // const conditionReportData: ConditionReportData = { + // id: 1, + // lastUpdated: "2023-07-12 11:51:31.000 +0100", + // fullAddress: "123 Fake Street, Fake Town", + // postcode: "AB1 2CD", + // currentEpcRating: "C", + // inConservationArea: "Yes", + // propertyType: "House", + // builtForm: "Detached", + // totalFloorArea: 60, + // tenure: "Rented (social)", + // yearBuilt: "1990", + // retrofitFeatures: [ + // { feature: "Wall", description: "Cavity wall", rating: "Poor" }, + // { + // feature: "Roof", + // description: "Flat, limited insulation (assumed)", + // rating: "Very poor", + // }, + // { feature: "Windows", description: "Double glazing", rating: "Good" }, + // { feature: "Heating", description: "Gas boiler", rating: "Good" }, + // { + // feature: "Heating Control", + // description: "Programmer and appliance thermostats", + // rating: "Good", + // }, + // { + // feature: "Hot Water", + // description: "Electric immersion, standard tariff", + // rating: "Good", + // }, + // { + // feature: "Lighting", + // description: "Low energy lighting in all fixed outlets", + // rating: "Very good", + // }, + // { + // feature: "Floor", + // description: "Suspended timber", + // rating: "Poor", + // }, + // { + // feature: "Ventilation", + // description: "Natural", + // rating: "N/A", + // }, + // { + // feature: "Solar Photo Voltaic", + // description: "Not present in the property", + // rating: "N/A", + // }, + // { + // feature: "Solar Hot Water", + // description: "Heating is not solar powered", + // rating: "N/A", + // }, + // { + // feature: "Wind Turbines", + // description: "No wind turbines present", + // rating: "N/A", + // }, + // ], + // generalFeatures: [ + // { + // feature: "Floor Height", + // description: 2.4, + // }, + // { + // feature: "Number of heated rooms", + // description: 5, + // }, + // { + // feature: "Heat loss corridor", + // description: "No", + // }, + // { + // feature: "Heat loss corridor length", + // description: 0, + // }, + // { + // feature: "Number of open fire places", + // description: 0, + // }, + // { + // feature: "Number of extensions", + // description: 0, + // }, + // { + // feature: "Floor level", + // description: "Ground", + // }, + // { + // feature: "Number of storeys", + // description: 2, + // }, + // { + // feature: "Mains gas available", + // description: "Yes", + // }, + // { + // feature: "Energy tariff", + // description: "Standard", + // }, + // ], + // heatingDemand: [ + // { + // feature: "EPC primary energy consumption", + // description: "98 kWh/m2/year", + // }, + // { + // feature: "EPC CO2 emissions", + // description: "2.8 tonnes/year", + // }, + // { + // feature: "Elecrticity Supplier", + // description: "E.ON Energy", + // }, + // { + // feature: "Gas Supplier", + // description: "British Gas", + // }, + // { + // feature: "Meter reading total energy consumption", + // description: "108 kWh/m2/year", + // }, + // { + // feature: "Meter reading electicity consumption", + // description: "22 kWh/m2/year", + // }, + // { + // feature: "Meter reading gas consumption", + // description: "86 kWh/m2/year", + // }, + // ], + // }; - const retrofitFeatures = conditionReportData.retrofitFeatures; - const generalFeatures = conditionReportData.generalFeatures; - const heatingDemand = conditionReportData.heatingDemand; + // const retrofitFeatures = conditionReportData.retrofitFeatures; + // const generalFeatures = conditionReportData.generalFeatures; + // const heatingDemand = conditionReportData.heatingDemand; + + console.log("I GOT?"); + console.log(conditionReportData); return (
@@ -263,11 +266,11 @@ export default async function PreAssessmentReport({
General Features
- + {/*
Retrotfit Property Features
Heating Demand
- + */} ); } diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts b/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts index f3d64f8..2c39cc0 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts @@ -1,10 +1,15 @@ -import { PropertyMeta } from "@/app/db/schema/property"; +import { db } from "@/app/db/db"; +import { + PropertyDetailsEpc, + PropertyMeta, + propertyDetailsEpc, +} from "@/app/db/schema/property"; +import { eq } from "drizzle-orm"; export async function getPropertyMeta( propertyId: string ): Promise { - const url = - process.env.URL + `/api/building-passport/property-meta/${propertyId}`; + const url = process.env.URL + `/api/property-meta/${propertyId}`; const response = await fetch(url, { method: "GET", @@ -15,3 +20,17 @@ export async function getPropertyMeta( } return response.json(); } + +export async function getConditionReport( + propertyId: string +): Promise { + const data = await db.query.propertyDetailsEpc.findFirst({ + where: eq(propertyDetailsEpc.propertyId, BigInt(propertyId)), + }); + + if (!data) { + throw new Error("Network response was not ok"); + } + + return data; +}