Added database post to plan trigger

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-31 16:48:26 +01:00
parent 1ceedc2ebb
commit 2c4d06f746
2 changed files with 15 additions and 8 deletions

View file

@ -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

View file

@ -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()