From 8e0720c3f058d7532b8db7109a22d7de98568b12 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 1 Dec 2023 15:01:58 +0000 Subject: [PATCH] added retrival of spatial data from database to app --- .../api/property-meta/[propertyId]/route.ts | 1 + src/app/db/migrations/meta/_journal.json | 7 +++++ src/app/db/schema/property.ts | 20 ++++++++++++-- .../pre-assessment-report/page.tsx | 27 +++++++++++-------- .../building-passport/[propertyId]/utils.ts | 16 +++++++++++ 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/app/api/property-meta/[propertyId]/route.ts b/src/app/api/property-meta/[propertyId]/route.ts index 0bf718cf..d4c54037 100644 --- a/src/app/api/property-meta/[propertyId]/route.ts +++ b/src/app/api/property-meta/[propertyId]/route.ts @@ -13,6 +13,7 @@ export async function GET( const propertyMeta = await db.query.property.findFirst({ columns: { id: true, + uprn: true, address: true, postcode: true, hasPreConditionReport: true, diff --git a/src/app/db/migrations/meta/_journal.json b/src/app/db/migrations/meta/_journal.json index 16f2acad..d21aff11 100644 --- a/src/app/db/migrations/meta/_journal.json +++ b/src/app/db/migrations/meta/_journal.json @@ -379,6 +379,13 @@ "when": 1701217628773, "tag": "0053_reflective_virginia_dare", "breakpoints": true + }, + { + "idx": 54, + "version": "5", + "when": 1701440165529, + "tag": "0054_sharp_mojo", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/app/db/schema/property.ts b/src/app/db/schema/property.ts index c4ef120d..bdf4ce1c 100644 --- a/src/app/db/schema/property.ts +++ b/src/app/db/schema/property.ts @@ -16,6 +16,7 @@ import { InferModel } from "drizzle-orm"; // This is a placeholder for the property schema export interface PropertyMeta { id: number; + uprn: number; address: string; postcode: string; hasPreConditionReport: boolean; @@ -165,6 +166,18 @@ export const propertyDetailsEpc = pgTable("property_details_epc", { adjustedEnergyConsumption: real("adjusted_energy_consumption"), }); +export const propertyDetailsSpatial = pgTable("property_details_spatial", { + id: bigserial("id", { mode: "bigint" }).primaryKey(), + uprn: bigint("uprn", { mode: "bigint" }), + xCoordinate: real("x_coordinate"), + yCoordinate: real("y_coordinate"), + latitude: real("latitude"), + longitude: real("longitude"), + conservationStatus: boolean("conservation_status"), + isListedBuilding: boolean("is_listed_building"), + isHeritageBuilding: boolean("is_heritage_building"), +}); + export const propertyDetailsMeter = pgTable("property_details_meter", { id: bigserial("id", { mode: "bigint" }).primaryKey(), uprn: bigint("uprn", { mode: "bigint" }), @@ -188,13 +201,16 @@ export const propertyTargets = pgTable("property_targets", { heatDemand: text("heat_demand"), }); -// TODO: We'll need a property details buildings materials for verisk data? - export type Property = InferModel; export type PropertyDetailsEpc = InferModel< typeof propertyDetailsEpc, "select" >; +export type PropertyDetailsSpatial = InferModel< + typeof propertyDetailsSpatial, + "select" +>; + // This type is used for the getProperties function in src/app/portfolio/[slug]/utils.ts export interface PropertyToRecommendation { estimatedCost?: number | null; 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 5302c13e..235abb5b 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 @@ -3,6 +3,7 @@ import FeatureTable from "@/app/components/building-passport/FeatureTable"; import { ConditionReportData, PropertyDetailsEpc, + PropertyDetailsSpatial, PropertyMeta, } from "@/app/db/schema/property"; import { formatDateTime } from "@/app/utils"; @@ -16,6 +17,7 @@ import { formatRetrofitFeatures, getConditionReport, getPropertyMeta, + getSpatialData, } from "../utils"; function AddressCard({ address }: { address: string | null }) { @@ -30,10 +32,7 @@ function AddressCard({ address }: { address: string | null }) { interface PropertyDetailsCardProps { conditionReportData: PropertyDetailsEpc; propertyMeta: PropertyMeta; - propertyDetailsSpatial: { - inConservationArea: string; - isListedorHeritage: string; - }; + propertyDetailsSpatial: PropertyDetailsSpatial; } const rowTitleStyle = "text-brandblue align-top pb-3"; @@ -48,6 +47,9 @@ function PropertyDetailsCard({ .filter(Boolean) .join(" "); + console.log("KJHEKJLWFWEL"); + console.log(propertyDetailsSpatial); + return (
@@ -72,13 +74,19 @@ function PropertyDetailsCard({ In conservation area: - {propertyDetailsSpatial.inConservationArea} + {propertyDetailsSpatial.conservationStatus ? "Yes" : "No"} - Is listed or heritage: + Is listed: - {propertyDetailsSpatial.isListedorHeritage} + {propertyDetailsSpatial.isListedBuilding ? "Yes" : "No"} + + + + Is heritage: + + {propertyDetailsSpatial.isHeritageBuilding ? "Yes" : "No"} @@ -118,10 +126,7 @@ export default async function PreAssessmentReport({ }) { const propertyMeta = await getPropertyMeta(params.propertyId); const conditionReportData = await getConditionReport(params.propertyId); - const propertyDetailsSpatial = { - inConservationArea: "No", - isListedorHeritage: "No", - }; + const propertyDetailsSpatial = await getSpatialData(propertyMeta.uprn); const generalFeatures = formatGeneralFeatures( conditionReportData, propertyMeta.propertyType diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts b/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts index 99a20398..e328401d 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts @@ -8,8 +8,10 @@ import { Feature, GeneralFeature, PropertyDetailsEpc, + PropertyDetailsSpatial, PropertyMeta, propertyDetailsEpc, + propertyDetailsSpatial, } from "@/app/db/schema/property"; import { plan, Plan } from "@/app/db/schema/recommendations"; import { getRating } from "@/app/utils"; @@ -108,6 +110,20 @@ export async function getConditionReport( return data; } +export async function getSpatialData( + uprn: number +): Promise { + const data = await db.query.propertyDetailsSpatial.findFirst({ + where: eq(propertyDetailsSpatial.uprn, BigInt(uprn)), + }); + + if (!data) { + throw new Error("Network response was not ok"); + } + + return data; +} + export function formatGeneralFeatures( conditionReportData: PropertyDetailsEpc, propertyType: string