From 209729821ee101cfed12cc0a5d97590130edd761 Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Fri, 30 Jan 2026 18:05:34 +0000 Subject: [PATCH] get data from db and render to screen --- src/app/api/condition/[uprn]/route.ts | 22 +++ src/app/db/schema/relations.ts | 23 +++ .../[propertyId]/condition/page.tsx | 136 ++++++++++++++++-- 3 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 src/app/api/condition/[uprn]/route.ts diff --git a/src/app/api/condition/[uprn]/route.ts b/src/app/api/condition/[uprn]/route.ts new file mode 100644 index 00000000..459cf801 --- /dev/null +++ b/src/app/api/condition/[uprn]/route.ts @@ -0,0 +1,22 @@ +import { db } from "@/app/db/db"; +import { NextRequest } from "next/server"; + +export async function GET( + request: NextRequest, + { params }: { params: { uprn: string } } +) { + const uprn = Number(params.uprn); + + const surveys = await db.query.propertyConditionSurvey.findMany({ + where: (survey, { eq }) => eq(survey.uprn, uprn), + with: { + elements: { + with: { + aspects: true, + }, + }, + }, + }); + + return Response.json(surveys); +} \ No newline at end of file diff --git a/src/app/db/schema/relations.ts b/src/app/db/schema/relations.ts index 8614ec66..1c1c49c7 100644 --- a/src/app/db/schema/relations.ts +++ b/src/app/db/schema/relations.ts @@ -22,6 +22,7 @@ import { material } from "./materials"; import { portfolio, portfolioUsers } from "./portfolio"; import { user } from "./users"; import { fundingPackage, fundingPackageMeasures } from "./funding"; +import { aspectCondition, element, propertyConditionSurvey } from "./condition"; // Define the other side of the one to many relation between a property and its recomendations export const recommendationsRelations = relations( @@ -192,3 +193,25 @@ export const fundingPackageMeasuresRelations = relations( }), }) ); + +export const propertyConditionSurveyRelations = relations( + propertyConditionSurvey, + ({ many }) => ({ + elements: many(element), + }) +); + +export const elementRelations = relations(element, ({ one, many }) => ({ + survey: one(propertyConditionSurvey, { + fields: [element.surveyId], + references: [propertyConditionSurvey.id], + }), + aspects: many(aspectCondition), +})); + +export const aspectConditionRelations = relations(aspectCondition, ({ one }) => ({ + element: one(element, { + fields: [aspectCondition.elementId], + references: [element.id], + }), +})); \ No newline at end of file diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/condition/page.tsx b/src/app/portfolio/[slug]/building-passport/[propertyId]/condition/page.tsx index cfb90a4d..01d6900f 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/condition/page.tsx +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/condition/page.tsx @@ -1,19 +1,127 @@ import { getPropertyMeta } from "../utils"; +import { db } from "@/app/db/db"; -export default async function Condition(props: { - params: Promise<{ slug: string; propertyId: string }>; -}) { - const params = await props.params; - const propertyMeta = await getPropertyMeta(params.propertyId); - const urpn = propertyMeta.uprn; +type ConditionPageProps = { + params: { + slug: string; + propertyId: string; + }; +}; - console.log(propertyMeta); +// --- Helper to serialize BigInts --- +function serializeBigInt(obj: any): any { + return JSON.parse( + JSON.stringify(obj, (_key, value) => + typeof value === "bigint" ? value.toString() : value + ) + ); +} + +async function getConditionsByUprn(uprn: number) { + const surveys = await db.query.propertyConditionSurvey.findMany({ + where: (survey, { eq }) => eq(survey.uprn, uprn), + with: { + elements: { + with: { + aspects: true, + }, + }, + }, + }); + + return surveys; +} + +export default async function Condition({ params }: ConditionPageProps) { + const { slug, propertyId } = params; + + const propertyMeta = await getPropertyMeta(propertyId); + const uprn = propertyMeta.uprn; + + const conditions = await getConditionsByUprn(Number(uprn)); + const safeConditions = serializeBigInt(conditions); + + // Flatten elements + aspects for table rendering + const tableRows: { + surveyId: string; + surveyDate: string; + surveySource: string; + elementType: string; + elementInstance: number; + aspectType: string; + aspectValue: string | null; + aspectQuantity: number | null; + aspectInstallDate: string | null; + aspectRenewalYear: number | null; + aspectComments: string | null; + }[] = []; + + safeConditions.forEach((survey: any) => { + survey.elements.forEach((el: any) => { + el.aspects.forEach((asp: any) => { + tableRows.push({ + surveyId: survey.id, + surveyDate: survey.date, + surveySource: survey.source, + elementType: el.elementType, + elementInstance: el.elementInstance, + aspectType: asp.aspectType, + aspectValue: asp.value, + aspectQuantity: asp.quantity, + aspectInstallDate: asp.installDate, + aspectRenewalYear: asp.renewalYear, + aspectComments: asp.comments, + }); + }); + }); + }); return ( - <> -
- TEST -
- - ) -} \ No newline at end of file +
+

+ Property Condition for {propertyMeta.address || uprn} (UPRN: {uprn}) +

+ + + + + + + + + + + + + + + + + + + {tableRows.map((row, i) => ( + + + + + + + + + + + + + + ))} + +
Survey IDDateSourceElement TypeElement InstanceAspect TypeValueQuantityInstall DateRenewal YearComments
{row.surveyId}{row.surveyDate}{row.surveySource}{row.elementType}{row.elementInstance}{row.aspectType}{row.aspectValue}{row.aspectQuantity}{row.aspectInstallDate || "-"}{row.aspectRenewalYear || "-"}{row.aspectComments || "-"}
+
+ ); +}