resructuring api requests

This commit is contained in:
Khalim Conn-Kowlessar 2023-08-02 15:30:23 +01:00
parent eb72860b16
commit 0ab8a34417
6 changed files with 179 additions and 184 deletions

View file

@ -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));
}

View file

@ -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));
}

View file

@ -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<typeof property, "select">;
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;

View file

@ -11,6 +11,8 @@ import {
} from "@heroicons/react/24/solid";
import { getPropertyMeta } from "./utils";
export const revalidate = 1;
export default async function BuildingPassportHome({
params,
}: {

View file

@ -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({
<tr>
<td className="text-gray-100 ">Year built:</td>
<td className="text-gray-100 text-end pr-8 py-1">
{conditionReportData.yearBuilt}
{propertyMeta.yearBuilt}
</td>
</tr>
<tr>
@ -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 (
<div className="leading-loose tracking-wider">
@ -263,11 +266,11 @@ export default async function PreAssessmentReport({
</div>
</div>
<div className="flex py-8 text-lg">General Features</div>
<FeatureTable data={generalFeatures} columns={generalColumns} />
{/* <FeatureTable data={generalFeatures} columns={generalColumns} />
<div className="flex py-8 text-lg">Retrotfit Property Features</div>
<FeatureTable data={retrofitFeatures} columns={retrofitColumns} />
<div className="flex py-8 text-lg">Heating Demand</div>
<FeatureTable data={heatingDemand} columns={generalColumns} />
<FeatureTable data={heatingDemand} columns={generalColumns} /> */}
</div>
);
}

View file

@ -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<PropertyMeta> {
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<PropertyDetailsEpc> {
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;
}