diff --git a/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts b/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts index 0ab77e7b..7ab6bece 100644 --- a/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts +++ b/src/app/portfolio/[slug]/building-passport/[propertyId]/utils.ts @@ -1,6 +1,7 @@ import S3 from "aws-sdk/clients/s3"; import { Recommendation, + recommendation, planRecommendations, plan, Plan, @@ -163,23 +164,26 @@ export async function getEnergyAssessmentDocuments( export async function getRecommendations( planId: string ): Promise { - const data = (await db.query.planRecommendations.findMany({ - where: eq(planRecommendations.planId, BigInt(planId)), - columns: {}, - with: { - recommendation: true, - }, - })) as RecommendationList; - - if (!data) { - throw new Error("Network response was not ok"); + // Recommendations link to their plan via recommendation.plan_id (the + // plan_recommendations join table is retired). recommendation.plan_id is NOT + // indexed and the table has tens of millions of rows, so we key off the plan's + // property_id (indexed) and filter plan_id in memory. We drop already-installed + // measures, matching the previous behaviour. + const planRow = await db.query.plan.findFirst({ + columns: { propertyId: true }, + where: eq(plan.id, BigInt(planId)), + }); + if (!planRow?.propertyId) { + return []; } - // unnest the recommendations - // We drop measures that are already installed - const recommendations = data - .map((item) => item.recommendation) - .filter((rec) => !rec.alreadyInstalled); + const recommendations = await db.query.recommendation.findMany({ + where: and( + eq(recommendation.propertyId, planRow.propertyId), + eq(recommendation.planId, BigInt(planId)), + eq(recommendation.alreadyInstalled, false) + ), + }); return recommendations; }