mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
formatting the return from the portfolio plan
This commit is contained in:
parent
0bcf1e0a3c
commit
011d98fe48
4 changed files with 106 additions and 11 deletions
|
|
@ -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],
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <div>Portfolio Plan</div>;
|
||||
const portfolioId = params.slug;
|
||||
const portfolioPlan = await getPortfolioPlan(portfolioId);
|
||||
|
||||
return <section>{String(portfolioPlan)}</section>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Portfolio> {
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"allowSyntheticDefaultImports": true,
|
||||
// "target": "es5",
|
||||
"target": "ESNext",
|
||||
"target": "es5",
|
||||
// "target": "ESNext",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue