mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-07-12 13:28:55 +00:00
get data from db and render to screen
This commit is contained in:
parent
b156899b71
commit
209729821e
3 changed files with 167 additions and 14 deletions
22
src/app/api/condition/[uprn]/route.ts
Normal file
22
src/app/api/condition/[uprn]/route.ts
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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],
|
||||
}),
|
||||
}));
|
||||
|
|
@ -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 (
|
||||
<>
|
||||
<div>
|
||||
TEST
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
<div style={{ padding: "1rem" }}>
|
||||
<h1>
|
||||
Property Condition for {propertyMeta.address || uprn} (UPRN: {uprn})
|
||||
</h1>
|
||||
|
||||
<table
|
||||
style={{
|
||||
width: "100%",
|
||||
borderCollapse: "collapse",
|
||||
marginTop: "1rem",
|
||||
}}
|
||||
>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Survey ID</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Date</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Source</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Element Type</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Element Instance</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Aspect Type</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Value</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Quantity</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Install Date</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Renewal Year</th>
|
||||
<th style={{ border: "1px solid #ccc", padding: "4px" }}>Comments</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{tableRows.map((row, i) => (
|
||||
<tr key={i}>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.surveyId}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.surveyDate}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.surveySource}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.elementType}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.elementInstance}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.aspectType}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.aspectValue}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.aspectQuantity}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.aspectInstallDate || "-"}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.aspectRenewalYear || "-"}</td>
|
||||
<td style={{ border: "1px solid #ccc", padding: "4px" }}>{row.aspectComments || "-"}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue