diff --git a/backend/app/db/functions/recommendations_functions.py b/backend/app/db/functions/recommendations_functions.py index b544b43b..e4f22fd2 100644 --- a/backend/app/db/functions/recommendations_functions.py +++ b/backend/app/db/functions/recommendations_functions.py @@ -1,3 +1,4 @@ +from sqlalchemy import text from sqlalchemy.orm import sessionmaker from backend.app.db.connection import db_engine from backend.app.db.models.recommendations import Plan, Recommendation, RecommendationMaterials @@ -60,12 +61,13 @@ def create_plan_recommendations(plan_id, recommendation_ids): :param plan_id: ID of the plan :param recommendation_ids: list of recommendation IDs """ - Session = sessionmaker(bind=db_engine) with Session() as session: for recommendation_id in recommendation_ids: session.execute( - 'INSERT INTO plan_recommendations (plan_id, recommendation_id) VALUES (:plan_id, :recommendation_id)', + text( + 'INSERT INTO plan_recommendations (plan_id, recommendation_id) VALUES (:plan_id, ' + ':recommendation_id)'), {'plan_id': plan_id, 'recommendation_id': recommendation_id} ) session.commit() diff --git a/backend/app/db/models/recommendations.py b/backend/app/db/models/recommendations.py index 66d1a063..9c11ab83 100644 --- a/backend/app/db/models/recommendations.py +++ b/backend/app/db/models/recommendations.py @@ -36,6 +36,7 @@ class RecommendationMaterials(Base): recommendation_id = Column(BigInteger, ForeignKey('recommendation.id'), nullable=False) material_id = Column(BigInteger, ForeignKey(Material.id), nullable=False) created_at = Column(TIMESTAMP, nullable=False, server_default=func.now()) + depth = Column(Float, nullable=False) class Plan(Base): diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index 9b401a3c..96f591ba 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -272,14 +272,13 @@ async def trigger_plan(body: PlanTriggerRequest): # upload recommendations uploaded_recommendation_ids = [] for rec in recommendations_to_upload: - # TODO: implement costs (at least a placeholder) - estimated_cost = sum([x["cost"] if x["cost"] else 0 for x in rec["parts"]]) recommendation_id = create_recommendation( { + "property_id": p.id, "type": rec["type"], "description": rec["description"], - "estimated_cost": estimated_cost, + "estimated_cost": rec["cost"], "default": True, "starting_u_value": rec.get("starting_u_value"), "new_u_value": rec.get("new_u_value"), diff --git a/recommendations/FloorRecommendations.py b/recommendations/FloorRecommendations.py index bf18cfe2..681d267d 100644 --- a/recommendations/FloorRecommendations.py +++ b/recommendations/FloorRecommendations.py @@ -308,7 +308,7 @@ class FloorRecommendations(BaseUtility): self.recommendations.append( { "parts": [ - get_recommended_part(part, depth), + get_recommended_part(part, depth, cost_per_unit), ], "type": "floor_insulation", "description": self._make_floor_description(part, depth), diff --git a/recommendations/WallRecommendations.py b/recommendations/WallRecommendations.py index fa625d53..fc8f3c7b 100644 --- a/recommendations/WallRecommendations.py +++ b/recommendations/WallRecommendations.py @@ -334,7 +334,7 @@ class WallRecommendations(BaseUtility): recommendations.append( { - "parts": [get_recommended_part(part, depth)], + "parts": [get_recommended_part(part, depth, cost_per_unit)], "type": "wall_insulation", "description": "Install " + self._make_description(part, depth), "starting_u_value": u_value, @@ -396,8 +396,8 @@ class WallRecommendations(BaseUtility): # For now, I'm adding them as separate items in the list recommendation = { "parts": [ - get_recommended_part(ewi_part, ewi_depth), - get_recommended_part(iwi_part, iwi_depth) + get_recommended_part(ewi_part, ewi_depth, ewi_cost_per_unit), + get_recommended_part(iwi_part, iwi_depth, iwi_cost_per_unit) ], "type": "wall_insulation", "description": ( diff --git a/recommendations/recommendation_utils.py b/recommendations/recommendation_utils.py index 4ab7f6b6..5bbcbe9f 100644 --- a/recommendations/recommendation_utils.py +++ b/recommendations/recommendation_utils.py @@ -110,15 +110,17 @@ def update_lowest_selected_u_value(lowest_selected_u_value, new_u_value): return lowest_selected_u_value -def get_recommended_part(part, selected_depth): +def get_recommended_part(part, selected_depth, selected_cost): """ Utility function to return a recommended part with the selected depth. - :param part: - :param selected_depth: + :param part: part to be recommended + :param selected_depth: depth of the selected part + :param selected_cost: cost of the selected depth :return: """ recommended_part = deepcopy(part) recommended_part["depths"] = [selected_depth] + recommended_part["cost"] = [selected_cost] return recommended_part