added connection to jtk's db

This commit is contained in:
Khalim Conn-Kowlessar 2025-06-04 15:27:21 +01:00
parent c3c536df16
commit 7ca2fc25c7
5 changed files with 176 additions and 4 deletions

View file

@ -6,6 +6,8 @@ import {
HomeModernIcon,
WrenchScrewdriverIcon,
SunIcon,
CircleStackIcon,
BoltIcon,
} from "@heroicons/react/24/outline";
import {
NavigationMenu,
@ -35,7 +37,7 @@ export function Toolbar({ propertyId, portfolioId }: ToolbarProps) {
href={`/portfolio/${portfolioId}/building-passport/${propertyId}/pre-assessment-report`}
>
<NewspaperIcon className="h-4 w-4 mr-2" />
Pre-assessment Condition Report
Data
</NavigationMenuLink>
);
@ -44,18 +46,28 @@ export function Toolbar({ propertyId, portfolioId }: ToolbarProps) {
className={navigationMenuTriggerStyle() + " ml-3 mr-2"}
href={`/portfolio/${portfolioId}/building-passport/${propertyId}/energy-assessment`}
>
<NewspaperIcon className="h-4 w-4 mr-2" />
<BoltIcon className="h-4 w-4 mr-2" />
Energy Assessment
</NavigationMenuLink>
);
const documentsButton = (
<NavigationMenuLink
className={navigationMenuTriggerStyle() + " ml-3 mr-2"}
href={`/portfolio/${portfolioId}/building-passport/${propertyId}/documents`}
>
<CircleStackIcon className="h-4 w-4 mr-2" />
Documents
</NavigationMenuLink>
);
const solarAnalysisButton = (
<NavigationMenuLink
className={navigationMenuTriggerStyle() + " ml-3 mr-2"}
href={`/portfolio/${portfolioId}/building-passport/${propertyId}/solar-analysis`}
>
<SunIcon className="h-4 w-4 mr-2" />
Solar Analysis
Solar
</NavigationMenuLink>
);
@ -76,13 +88,14 @@ export function Toolbar({ propertyId, portfolioId }: ToolbarProps) {
href={`/portfolio/${portfolioId}/building-passport/${propertyId}`}
>
<HomeModernIcon className="h-4 w-4 mr-2" />
Property Information
Summary
</NavigationMenuLink>
<NavigationMenuList>
{preAssessmentReportButton}
{solarAnalysisButton}
{recommendationsButton}
{documentsButton}
{energyAssessmentsReportButton}
<NavigationMenuItem
className={navigationMenuTriggerStyle() + " ml-3 mr-2"}

View file

@ -0,0 +1,27 @@
// db.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as documentsSchema from "@/app/db/documents_schema/documents";
import * as relations from "@/app/db/documents_schema/relations";
export const pool = new Pool({
host: process.env.DOCUMENTS_DB_HOST,
port: Number(process.env.DOCUMENTS_DB_PORT),
user: process.env.DOCUMENTS_DB_USERNAME,
password: process.env.DOCUMENTS_DB_PASSWORD,
database: process.env.DOCUMENTS_DB_NAME,
max: 20,
idleTimeoutMillis: 30000,
ssl: {
rejectUnauthorized: false, // set to true if you're using a CA-signed cert
},
});
const schema = {
...documentsSchema,
...relations,
};
export const documentsDB = drizzle(pool, {
schema: schema,
});

View file

@ -0,0 +1,61 @@
import { pgTable, uuid, text, timestamp } from "drizzle-orm/pg-core";
import { pgEnum } from "drizzle-orm/pg-core";
import { InferModel } from "drizzle-orm";
export const reportType = pgEnum("report_type", [
"quidos_presite_note",
"charted_surveyor_report",
"energy_performance_report",
"u_value_calculator_report",
"overwriting_u_value_declaration_form",
"osmosis_condition_pas_2035_report",
]);
export const companyInfo = pgTable("companyinfo", {
id: uuid("id").primaryKey().defaultRandom(),
address: text("address").notNull(),
tradingName: text("trading_name").notNull(),
postCode: text("post_code").notNull(),
faxNumber: text("fax_number"),
relatedPartyDisclosure: text("related_party_disclosure"),
});
// --- assessorInfo table ---
export const assessorInfo = pgTable("assessorinfo", {
id: uuid("id").primaryKey().defaultRandom(),
accreditationNumber: text("accreditation_number").notNull(),
name: text("name").notNull(),
phoneNumber: text("phone_number"),
emailAddress: text("email_address"),
companyId: uuid("company_id").references(() => companyInfo.id),
});
// --- buildings table ---
export const buildings = pgTable("buildings", {
id: uuid("id").primaryKey().defaultRandom(),
address: text("address").notNull(),
postcode: text("postcode").notNull(),
uprn: text("UPRN").notNull(),
landlordId: text("landlord_id").notNull(),
domnaId: text("domna_id").notNull(),
});
// --- documents table ---
export const documents = pgTable("documents", {
id: uuid("id").primaryKey().defaultRandom(),
authorId: uuid("assessor_id")
.notNull()
.references(() => assessorInfo.id),
createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
documentType: reportType("document_type").notNull(),
buildingId: uuid("building_id")
.notNull()
.references(() => buildings.id),
targetTable: text("target_table").notNull(),
targetId: uuid("target_id").notNull(),
});
export type Building = InferModel<typeof buildings, "select">;
export type Document = InferModel<typeof documents, "select">;

View file

@ -0,0 +1,14 @@
import { pgTable, serial, text, integer } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
import { buildings, documents } from "@/app/db/documents_schema/documents";
export const buildingsRelations = relations(buildings, ({ many }) => ({
documents: many(documents),
}));
export const postsRelations = relations(documents, ({ one }) => ({
building: one(buildings, {
fields: [documents.buildingId],
references: [buildings.id],
}),
}));

View file

@ -0,0 +1,57 @@
import { documentsDB } from "@/app/db/documents_db";
import {
buildings,
Document,
Building,
} from "@/app/db/documents_schema/documents";
import { getPropertyMeta } from "@/app/portfolio/[slug]/building-passport/[propertyId]/utils";
import { eq } from "drizzle-orm";
export type BuildingWithDocuments = Building & {
documents: Document[];
};
async function getDocuments(
uprn: number
): Promise<BuildingWithDocuments | undefined> {
const result = documentsDB.query.buildings.findFirst({
where: eq(buildings.uprn, String(uprn)),
with: {
documents: true,
},
});
// If we have no buildings, we return an empty object
if (!result) {
return {
id: "",
address: "",
postcode: "",
uprn: String(uprn),
landlordId: "",
domnaId: "",
documents: [],
} as BuildingWithDocuments;
}
return result;
}
export default async function DocumentsPage({
params,
}: {
params: { slug: string; propertyId: string };
}) {
// Get the property UPRN
const propertyId = params.propertyId;
if (!propertyId || propertyId === "0") {
throw Error("Invalid propertyId");
}
const propertyMeta = await getPropertyMeta(propertyId);
console.log("Property Meta:", propertyMeta.uprn);
const documents = await getDocuments(propertyMeta.uprn);
console.log("Documents:", documents);
return <div>Document go here</div>;
}