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
This commit is contained in:
parent
ab29a85903
commit
c90e30806b
|
|
@ -508,40 +508,54 @@ def update_goal(goal_id: str, data: GoalUpdate, session: dict = Depends(require_
|
||||||
params.append(data.description)
|
params.append(data.description)
|
||||||
|
|
||||||
# Handle start_date and start_value
|
# Handle start_date and start_value
|
||||||
if data.start_date is not None:
|
# Determine what start_date and start_value to use
|
||||||
updates.append("start_date = %s")
|
final_start_date = None
|
||||||
params.append(data.start_date)
|
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:
|
if data.start_value is None:
|
||||||
# Get goal_type for historical lookup
|
# Get goal_type for historical lookup
|
||||||
cur.execute("SELECT goal_type FROM goals WHERE id = %s", (goal_id,))
|
cur.execute("SELECT goal_type FROM goals WHERE id = %s", (goal_id,))
|
||||||
goal_row = cur.fetchone()
|
goal_row = cur.fetchone()
|
||||||
if goal_row:
|
if goal_row:
|
||||||
goal_type = goal_row['goal_type']
|
goal_type = goal_row['goal_type']
|
||||||
print(f"[DEBUG] Looking up historical value for {goal_type} on or after {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, data.start_date)
|
historical_data = _get_historical_value_for_goal_type(conn, pid, goal_type, requested_date)
|
||||||
print(f"[DEBUG] Historical data result: {historical_data}")
|
print(f"[DEBUG] Historical data result: {historical_data}")
|
||||||
|
|
||||||
if historical_data is not None:
|
if historical_data is not None:
|
||||||
# Update both start_date and start_value with actual measurement
|
# Use actual measurement date and value
|
||||||
actual_date = historical_data['date']
|
final_start_date = historical_data['date']
|
||||||
actual_value = historical_data['value']
|
final_start_value = historical_data['value']
|
||||||
|
print(f"[INFO] Auto-adjusted to first measurement: {final_start_date} = {final_start_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}")
|
|
||||||
else:
|
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:
|
else:
|
||||||
print(f"[ERROR] Could not find goal with id {goal_id}")
|
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")
|
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)
|
# Handle focus_contributions separately (can be updated even if no other changes)
|
||||||
if data.focus_contributions is not None:
|
if data.focus_contributions is not None:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user