From 2c4d06f746bb3324909bb629715e32d3d85aa500 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Mon, 31 Jul 2023 16:48:26 +0100 Subject: [PATCH] Added database post to plan trigger --- backend/app/db/functions/property_functions.py | 14 ++++++++++---- backend/app/plan/router.py | 9 +++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/backend/app/db/functions/property_functions.py b/backend/app/db/functions/property_functions.py index a5031bd8..499b66b5 100644 --- a/backend/app/db/functions/property_functions.py +++ b/backend/app/db/functions/property_functions.py @@ -8,10 +8,14 @@ from backend.app.db.connection import db_engine from sqlalchemy.orm.exc import NoResultFound -def create_property(portfolio_id: int, address: str, postcode: str): +def create_property(portfolio_id: int, address: str, postcode: str) -> (int, bool): """ This function will create a record for the property in the database if it does not exist. If it does exist, it will just update the updated_at field. + :param portfolio_id: The ID of the portfolio the property belongs to + :param address: The address of the property + :param postcode: The postcode of the property + :return: The ID of the property and a boolean indicating whether it was created or not """ Session = sessionmaker(bind=db_engine) with Session() as session: @@ -31,7 +35,7 @@ def create_property(portfolio_id: int, address: str, postcode: str): session.merge(existing_property) session.commit() - return existing_property.id + return existing_property.id, False except NoResultFound: # Property doesn't exist, create a new one @@ -42,7 +46,9 @@ def create_property(portfolio_id: int, address: str, postcode: str): created_at=now, updated_at=now, creation_status=PropertyCreationStatus.LOADING, - status=PortfolioStatus.ASSESSMENT.value + status=PortfolioStatus.ASSESSMENT.value, + has_pre_condition_report=False, + has_recommendations=False ) # Add the new property to the session @@ -50,4 +56,4 @@ def create_property(portfolio_id: int, address: str, postcode: str): session.commit() - return new_property.id + return new_property.id, True diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index fff83e63..a8dd36e6 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -85,10 +85,13 @@ async def trigger_plan(body: PlanTriggerRequest): # TODO: implment validation # Create a record in db - property_id = create_property( + property_id, is_new = create_property( portfolio_id=body.portfolio_id, address=config['address'], postcode=config['postcode'] ) - return {"message": "success"} + + # if a new record was not created, we don't produduce recommendations + if not is_new: + continue input_properties.append( Property( @@ -98,10 +101,8 @@ async def trigger_plan(body: PlanTriggerRequest): id=property_id ) ) - return {"message": "success"} logger.info("Getting EPC data") - for p in input_properties: p.search_address_epc() p.set_year_built()