From c90e30806bfc80c36b40d123dcf456cb341825cf Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 28 Mar 2026 14:28:52 +0100 Subject: [PATCH] fix: save start_date to database in update_goal - Rewrote update logic to determine final_start_date/start_value first - Then append to updates/params arrays (ensures alignment) - Fixes bug where only start_value was saved but not start_date User feedback: start_value correctly calculated but start_date not persisted --- backend/routers/goals.py | 54 +++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/backend/routers/goals.py b/backend/routers/goals.py index 5611d6e..829c5a0 100644 --- a/backend/routers/goals.py +++ b/backend/routers/goals.py @@ -508,40 +508,54 @@ def update_goal(goal_id: str, data: GoalUpdate, session: dict = Depends(require_ params.append(data.description) # Handle start_date and start_value - if data.start_date is not None: - updates.append("start_date = %s") - params.append(data.start_date) + # Determine what start_date and start_value to use + final_start_date = None + final_start_value = None - # If start_value not explicitly provided, recalculate from historical data + if data.start_date is not None: + # User provided a start_date + requested_date = data.start_date + + # If start_value not explicitly provided, try to get historical value if data.start_value is None: # Get goal_type for historical lookup cur.execute("SELECT goal_type FROM goals WHERE id = %s", (goal_id,)) goal_row = cur.fetchone() if goal_row: goal_type = goal_row['goal_type'] - print(f"[DEBUG] Looking up historical value for {goal_type} on or after {data.start_date}") - historical_data = _get_historical_value_for_goal_type(conn, pid, goal_type, data.start_date) + print(f"[DEBUG] Looking up historical value for {goal_type} on or after {requested_date}") + historical_data = _get_historical_value_for_goal_type(conn, pid, goal_type, requested_date) print(f"[DEBUG] Historical data result: {historical_data}") + if historical_data is not None: - # Update both start_date and start_value with actual measurement - actual_date = historical_data['date'] - actual_value = historical_data['value'] - - # Replace the start_date in updates with the actual measurement date - updates[-1] = "start_date = %s" # Update the last added start_date - params[-1] = actual_date - - updates.append("start_value = %s") - params.append(actual_value) - print(f"[INFO] Auto-adjusted to first measurement: {actual_date} = {actual_value}") + # Use actual measurement date and value + final_start_date = historical_data['date'] + final_start_value = historical_data['value'] + print(f"[INFO] Auto-adjusted to first measurement: {final_start_date} = {final_start_value}") else: - print(f"[WARN] No historical data found for {goal_type} on or after {data.start_date}") + # No historical data found, use requested date without value + final_start_date = requested_date + print(f"[WARN] No historical data found for {goal_type} on or after {requested_date}") else: print(f"[ERROR] Could not find goal with id {goal_id}") + final_start_date = requested_date + else: + # User provided both date and value + final_start_date = requested_date + final_start_value = data.start_value - if data.start_value is not None: + elif data.start_value is not None: + # Only start_value provided (no date) + final_start_value = data.start_value + + # Add to updates if we have values + if final_start_date is not None: + updates.append("start_date = %s") + params.append(final_start_date) + + if final_start_value is not None: updates.append("start_value = %s") - params.append(data.start_value) + params.append(final_start_value) # Handle focus_contributions separately (can be updated even if no other changes) if data.focus_contributions is not None: