mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
added connection to jtk's db
This commit is contained in:
parent
c3c536df16
commit
7ca2fc25c7
5 changed files with 176 additions and 4 deletions
|
|
@ -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"}
|
||||
|
|
|
|||
27
src/app/db/documents_db.ts
Normal file
27
src/app/db/documents_db.ts
Normal 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,
|
||||
});
|
||||
61
src/app/db/documents_schema/documents.ts
Normal file
61
src/app/db/documents_schema/documents.ts
Normal 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">;
|
||||
14
src/app/db/documents_schema/relations.ts
Normal file
14
src/app/db/documents_schema/relations.ts
Normal 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],
|
||||
}),
|
||||
}));
|
||||
|
|
@ -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>;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue