diff --git a/src/app/db/schema/recommendations.ts b/src/app/db/schema/recommendations.ts index a771acf5..78a8d92b 100644 --- a/src/app/db/schema/recommendations.ts +++ b/src/app/db/schema/recommendations.ts @@ -104,10 +104,34 @@ export const planRecommendationsRelations = relations( }) ); +// We construct a relationship between a recommendation and recommendationMaterials +// On recommendationMaterial will map to a single recommendation + +// Define the relationships for the recommendation table export const recommendationRelations = relations( - planRecommendations, + recommendation, ({ many }) => ({ - planRecommendations: many(planRecommendations), + recommendationMaterials: many(recommendationMaterials), + }) +); + +// Define the relationships for the material table +export const materialRelations = relations(material, ({ many }) => ({ + recommendationMaterials: many(recommendationMaterials), +})); + +// Define the relationships for the recommendationMaterials table +export const recommendationMaterialsRelations = relations( + recommendationMaterials, + ({ one }) => ({ + recommendation: one(recommendation, { + fields: [recommendationMaterials.recommendationId], + references: [recommendation.id], + }), + material: one(material, { + fields: [recommendationMaterials.materialId], + references: [material.id], + }), }) ); diff --git a/src/app/portfolio/[slug]/(portfolio)/plan/page.tsx b/src/app/portfolio/[slug]/(portfolio)/plan/page.tsx index c01caf20..866d18b2 100644 --- a/src/app/portfolio/[slug]/(portfolio)/plan/page.tsx +++ b/src/app/portfolio/[slug]/(portfolio)/plan/page.tsx @@ -1,7 +1,12 @@ +import { getPortfolioPlan } from "../../utils"; + export default async function PortfolioPlan({ params, }: { - params: { slug: string; propertyId: string; planId: string }; + params: { slug: string }; }) { - return
Portfolio Plan
; + const portfolioId = params.slug; + const portfolioPlan = await getPortfolioPlan(portfolioId); + + return
{String(portfolioPlan)}
; } diff --git a/src/app/portfolio/[slug]/utils.ts b/src/app/portfolio/[slug]/utils.ts index 61915539..f5019c8d 100644 --- a/src/app/portfolio/[slug]/utils.ts +++ b/src/app/portfolio/[slug]/utils.ts @@ -1,9 +1,19 @@ -import { eq } from "drizzle-orm"; +import { recommendation } from "./../../db/schema/recommendations"; +import { material } from "./../../db/schema/materials"; +import { and, eq, inArray } from "drizzle-orm"; import { db } from "@/app/db/db"; import { portfolio } from "@/app/db/schema/portfolio"; import { property } from "@/app/db/schema/property"; import type { Portfolio } from "@/app/db/schema/portfolio"; import type { PropertyWithTarget } from "@/app/db/schema/property"; +import { plan, planRecommendations } from "@/app/db/schema/recommendations"; + +type UnnestedRecommendation = { + quantity: number; + quantityUnit: string; + estimatedCost: number; + materialType: string; +}; export async function getPortfolio(portfolioId: string): Promise { const data = await db @@ -57,8 +67,64 @@ export async function getPortfolioPlan(portfolioId: string) { // 2. For the plans, get the recommendations. This can be done from the planRecommendation table // 3. For the recommendations get the materials, the quantity and the cost. // 4. For the materials, get the type of material - // I need to make the following updates: - // 1) Add quanitity to the recommendationMaterials table - // 2) Add estimatedCost to the recommendationMaterials table. We want the cost here to be the cost associated to - // the material type + + // There was some trouble performing all of the relations in a single query so we split it into + // three requests (unfortunately) + + const plans = await db.query.plan.findMany({ + where: and( + eq(property.portfolioId, BigInt(portfolioId)), + eq(plan.isDefault, true) + ), + }); + + const planIds = plans.map((plan) => plan.id); + + const recommendations = await db.query.planRecommendations.findMany({ + where: inArray(planRecommendations.planId, planIds), + }); + + const recommendationIds = recommendations.map( + (recommendation) => recommendation.id + ); + + const data = await db.query.recommendation.findMany({ + where: and( + inArray(recommendation.id, recommendationIds), + eq(recommendation.default, true) + ), + with: { + recommendationMaterials: { + with: { + material: { + columns: { + type: true, + }, + }, + }, + columns: { + quantity: true, + quantityUnit: true, + estimatedCost: true, + }, + }, + }, + }); + + const unnestedRecommendations: UnnestedRecommendation[] = data.reduce( + (acc: UnnestedRecommendation[], recommendation) => { + const materials = recommendation.recommendationMaterials.map( + (material) => ({ + quantity: material.quantity, + quantityUnit: material.quantityUnit, + estimatedCost: material.estimatedCost, + materialType: material.material.type, + }) + ); + return [...acc, ...materials]; + }, + [] + ); + + return unnestedRecommendations; } diff --git a/tsconfig.json b/tsconfig.json index 7f10c944..64681559 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,