made meta data in non instrusive survey mandatory and added relations

This commit is contained in:
Khalim Conn-Kowlessar 2024-04-13 18:09:46 +01:00
parent fb3ec0c04c
commit b212d198a4
6 changed files with 84 additions and 9 deletions

View file

@ -449,6 +449,13 @@
"when": 1713021324021,
"tag": "0063_elite_black_tarantula",
"breakpoints": true
},
{
"idx": 64,
"version": "5",
"when": 1713028155621,
"tag": "0064_icy_blue_shield",
"breakpoints": true
}
]
}

View file

@ -206,19 +206,19 @@ export const propertyTargets = pgTable("property_targets", {
// This is the model for the results of non-invasive surveys, associated with a property.
// We store the data against a uprn. Each row will contain a metadata about the survey itself
export const NonInstrusiveSurvey = pgTable("non_intrusive_survey", {
export const nonInstrusiveSurvey = pgTable("non_intrusive_survey", {
id: bigserial("id", { mode: "bigint" }).primaryKey(),
uprn: bigint("uprn", { mode: "bigint" }),
surveyDate: timestamp("survey_date"),
surveyor: text("surveyor"),
uprn: bigint("uprn", { mode: "bigint" }).notNull(),
surveyDate: timestamp("survey_date").notNull(),
surveyor: text("surveyor").notNull(),
});
// This model contains the actual notes that were taken down during the non-invasive survey
export const NonIntrusiveSurveyNotes = pgTable("non_intrusive_survey_notes", {
export const nonIntrusiveSurveyNotes = pgTable("non_intrusive_survey_notes", {
id: bigserial("id", { mode: "bigint" }).primaryKey(),
surveyId: bigint("survey_id", { mode: "bigint" })
.notNull()
.references(() => NonInstrusiveSurvey.id),
.references(() => nonInstrusiveSurvey.id),
title: text("title"),
note: text("note"),
});
@ -252,3 +252,18 @@ export interface PropertyWithRelations {
currentEpcRating: string | null;
currentSapPoints: number | null;
}
export type NonIntrusiveSurveyNotes = InferModel<
typeof nonIntrusiveSurveyNotes,
"select"
>;
export type NonInstrusiveSurvey = InferModel<
typeof nonInstrusiveSurvey,
"select"
>;
export interface NonIntrusiveSurveyData {
uprn: bigint;
surveyDate: Date;
surveyor: string;
notes: { title: string; note: string }[];
}

View file

@ -1,7 +1,13 @@
// This script contains ALL relations for the database, used by drizzle-orm
import { relations } from "drizzle-orm";
import { property, propertyDetailsEpc, propertyTargets } from "./property";
import {
nonInstrusiveSurvey,
nonIntrusiveSurveyNotes,
property,
propertyDetailsEpc,
propertyTargets,
} from "./property";
import {
plan,
planRecommendations,
@ -104,3 +110,22 @@ export const portfolioUsersToPortfolioRelations = relations(
portfolio: many(portfolio),
})
);
// Define a relation from non-intrusive survey to non-intrusive survey notes
// One survey can have many notes
export const nonInstrusiveSurveyRelations = relations(
nonInstrusiveSurvey,
({ many }) => ({
notes: many(nonIntrusiveSurveyNotes),
})
);
export const nonIntrusiveSurveyNotesRelations = relations(
nonIntrusiveSurveyNotes,
({ one }) => ({
survey: one(nonInstrusiveSurvey, {
fields: [nonIntrusiveSurveyNotes.surveyId],
references: [nonInstrusiveSurvey.id],
}),
})
);

View file

@ -18,6 +18,7 @@ import {
getConditionReport,
getPropertyMeta,
getSpatialData,
getNonIntrusiveSurvey,
} from "../utils";
function AddressCard({ address }: { address: string | null }) {
@ -129,6 +130,9 @@ export default async function PreAssessmentReport({
propertyMeta.propertyType
);
const nonIntrusiveSurvey = await getNonIntrusiveSurvey(propertyMeta.uprn);
console.log(nonIntrusiveSurvey);
const retrofitFeatures = formatRetrofitFeatures(conditionReportData);
const heatingDemand = formatHeatDemandFeatures(conditionReportData);
@ -153,6 +157,7 @@ export default async function PreAssessmentReport({
/>
</div>
</div>
<div className="flex py-8 text-lg">General Features</div>
<FeatureTable data={generalFeatures} columns={generalColumns} />
<div className="flex py-8 text-lg">Existing Property Features</div>

View file

@ -12,6 +12,8 @@ import {
PropertyMeta,
propertyDetailsEpc,
propertyDetailsSpatial,
NonIntrusiveSurveyData,
nonInstrusiveSurvey,
} from "@/app/db/schema/property";
import { plan, Plan } from "@/app/db/schema/recommendations";
import { getRating } from "@/app/utils";
@ -124,6 +126,27 @@ export async function getSpatialData(
return data;
}
export async function getNonIntrusiveSurvey(
uprn: number
): Promise<NonIntrusiveSurveyData> {
// Query to find the newest survey for a given UPRN
const data = await db.query.nonInstrusiveSurvey.findMany({
where: eq(nonInstrusiveSurvey.uprn, BigInt(uprn)),
orderBy: desc(nonInstrusiveSurvey.surveyDate),
columns: { surveyDate: true, surveyor: true, uprn: true },
limit: 1,
with: {
notes: {
columns: {
title: true,
note: true,
},
},
},
});
return data[0];
}
export function formatGeneralFeatures(
conditionReportData: PropertyDetailsEpc,
propertyType: string

View file

@ -1,8 +1,8 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"target": "es5",
// "target": "ESNext",
// "target": "es5",
"target": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,