diff --git a/src/app/db/migrations/meta/_journal.json b/src/app/db/migrations/meta/_journal.json index 1754e071..42a4ea7f 100644 --- a/src/app/db/migrations/meta/_journal.json +++ b/src/app/db/migrations/meta/_journal.json @@ -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 } ] } \ No newline at end of file diff --git a/src/app/db/schema/property.ts b/src/app/db/schema/property.ts index b9de840b..38ef26f8 100644 --- a/src/app/db/schema/property.ts +++ b/src/app/db/schema/property.ts @@ -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 }[]; +} diff --git a/src/app/db/schema/relations.ts b/src/app/db/schema/relations.ts index 6d1647b1..dfbd1e13 100644 --- a/src/app/db/schema/relations.ts +++ b/src/app/db/schema/relations.ts @@ -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], + }), + }) +); 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 1f7c118f..6e334390 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 @@ -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({ /> +