From a313aa75fc3746aeca226efae3c880137ed2c102 Mon Sep 17 00:00:00 2001 From: Tobias Waslowski Date: Tue, 2 Apr 2024 19:09:34 +0200 Subject: [PATCH] Send user message after creating baseline record --- src/handlers/record_handlers.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/handlers/record_handlers.py b/src/handlers/record_handlers.py index 94c36bd..d11afb8 100644 --- a/src/handlers/record_handlers.py +++ b/src/handlers/record_handlers.py @@ -237,20 +237,34 @@ async def baseline_handler( user_repository, record_repository = get_user_repository(), get_record_repository() user = user_repository.find_user(update.effective_user.id) if user.has_baselines_defined(): - logging.info(f"Creating baseline record for user {user.user_id}") + record = {metric.name: metric.baseline for metric in user.metrics} + logging.info(f"Creating baseline record for user {user.user_id}: {record}") record_repository.create_record( user.user_id, - { - # todo refactor this - metric.name: metric.baseline - for metric in user.metrics - }, + record, datetime.datetime.now().isoformat(), ) + logging.info(f"Baseline record successfully created for user {user.user_id}") + await send(update, text=create_baseline_success_message(record)) else: + logging.error( + f"User {user.user_id} has not defined baselines for all metrics yet." + ) await send(update, text="You have not defined baselines for all metrics yet.") +def create_baseline_success_message(record: dict) -> str: + """ + Creates a success message for the baseline record creation. + :param record: The record that was created. + :return: The success message. + """ + bullet_point_list = ", ".join( + [f"{name.capitalize()} = {value}" for name, value in record.items()] + ) + return f"Baseline record successfully created: {bullet_point_list}." + + @autowire("user_repository") def get_user_repository(user_repository: UserRepository): return user_repository