From 92158bb94d42784693ae481510384e1935cce97f Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 30 Jul 2024 20:47:06 +0100 Subject: [PATCH] Added scenario name to plan cards --- src/app/db/schema/relations.ts | 1 + .../[propertyId]/plans/[planId]/page.tsx | 2 - .../[propertyId]/plans/page.tsx | 11 ++- .../components/propertyTableColumns.tsx | 1 + src/app/portfolio/[slug]/utils.ts | 68 ++++++++++++++++++- tsconfig.json | 4 +- 6 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/app/db/schema/relations.ts b/src/app/db/schema/relations.ts index 7ff9328..dfc44bd 100644 --- a/src/app/db/schema/relations.ts +++ b/src/app/db/schema/relations.ts @@ -27,6 +27,7 @@ export const recommendationsRelations = relations( references: [property.id], }), recommendationMaterials: many(recommendationMaterials), + planRecommendations: many(planRecommendations), }) ); diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/[planId]/page.tsx b/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/[planId]/page.tsx index c5f4a0f..a0ee436 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/[planId]/page.tsx +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/[planId]/page.tsx @@ -9,8 +9,6 @@ export default async function Recommendations({ const propertyMeta = await getPropertyMeta(params.propertyId); const recommendations = await getRecommendations(params.planId); - console.log(recommendations); - return (
Recommendations
diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/page.tsx b/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/page.tsx index 5c40507..b9a5a5e 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/page.tsx +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/plans/page.tsx @@ -9,6 +9,7 @@ function PlanCard({ createdAt, totalEstimatedCost, totalSapPoints, + planName, planId, isDefault, }: { @@ -16,6 +17,7 @@ function PlanCard({ createdAt: Date; totalEstimatedCost: number; totalSapPoints: number; + planName: string | null; planId: string; isDefault: boolean; }) { @@ -29,7 +31,13 @@ function PlanCard({ />
- + + {planName && ( +
+ {planName} +
+ )} +
Total cost: @@ -103,6 +111,7 @@ export default async function RecommendationPlans({ createdAt={plan.createdAt} totalEstimatedCost={totalEstimatedCost} totalSapPoints={totalSapPoints} + planName={plan.name} planId={String(plan.id)} isDefault={plan.isDefault} /> diff --git a/src/app/portfolio/[slug]/components/propertyTableColumns.tsx b/src/app/portfolio/[slug]/components/propertyTableColumns.tsx index 7788dd1..67ed29f 100644 --- a/src/app/portfolio/[slug]/components/propertyTableColumns.tsx +++ b/src/app/portfolio/[slug]/components/propertyTableColumns.tsx @@ -153,6 +153,7 @@ export const columns: ColumnDef[] = [ const recommendations = row.original.recommendations; const currentSapPoints = row.original.currentSapPoints; + const expectedapPoints = recommendations.reduce( (acc: number, rec: PropertyToRecommendation) => { if (rec.sapPoints === null || rec.sapPoints === undefined) { diff --git a/src/app/portfolio/[slug]/utils.ts b/src/app/portfolio/[slug]/utils.ts index 6e5fee6..5d1f9a4 100644 --- a/src/app/portfolio/[slug]/utils.ts +++ b/src/app/portfolio/[slug]/utils.ts @@ -4,7 +4,7 @@ import { UnnestedRecommendation, PortfolioPlanRecommendation, } from "./../../db/schema/recommendations"; -import { and, eq, inArray } from "drizzle-orm"; +import { and, ConsoleLogWriter, eq, inArray } from "drizzle-orm"; import { db } from "@/app/db/db"; import { portfolio, portfolioUsers } from "@/app/db/schema/portfolio"; import { property } from "@/app/db/schema/property"; @@ -264,6 +264,57 @@ export async function getProperties( limit: number = 1000, offset: number = 0 ): Promise { + // When pulling in the associated recommendations, we need to pull in the associated plan and take recommendations that are associated to the + // default plans + + // const data: PropertyWithRelations[] = await db.query.property.findMany({ + // limit: limit, + // offset: offset, + // columns: { + // id: true, + // portfolioId: true, + // address: true, + // postcode: true, + // status: true, + // creationStatus: true, + // currentEpcRating: true, + // currentSapPoints: true, + // }, + // where: eq(property.portfolioId, BigInt(portfolioId)), + // with: { + // target: { + // columns: { + // epc: true, + // }, + // }, + // recommendations: { + // columns: { + // id: true, + // estimatedCost: true, + // sapPoints: true, + // }, + // where: eq(recommendation.default, true), + // with: { + // planRecommendations: { + // columns: { + // planId: true, + // }, + // with: { + // plan: { + // columns: { + // id: true, + // isDefault: true, + // }, + // where: eq(plan.isDefault, true), + // }, + // }, + // }, + // }, + // }, + // }, + // }); + + // We need to perform the query like this because the nested query is not supported in the ORM right now const data: PropertyWithRelations[] = await db.query.property.findMany({ limit: limit, offset: offset, @@ -286,10 +337,23 @@ export async function getProperties( }, recommendations: { columns: { + id: true, estimatedCost: true, sapPoints: true, }, - where: eq(recommendation.default, true), + where: and( + eq(recommendation.default, true), + inArray( + recommendation.id, + db + .select({ + recommendationId: planRecommendations.recommendationId, + }) + .from(planRecommendations) + .innerJoin(plan, eq(plan.id, planRecommendations.planId)) + .where(eq(plan.isDefault, true)) + ) + ), }, }, }); diff --git a/tsconfig.json b/tsconfig.json index 7f10c94..6468155 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { "allowSyntheticDefaultImports": true, - // "target": "es5", - "target": "ESNext", + "target": "es5", + // "target": "ESNext", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true,