fix: renumber sets after delete and use last-logged values for prefill

Two bugs fixed:
- Deleting a set left gaps in set numbering (1, 3, 3). Now renumbers
  remaining sets sequentially after deletion.
- Logging set 1 caused the prefill to recalculate via the progression
  engine, shifting suggested reps mid-session. Now prefills from the
  last logged set's actual values; progression suggestion is only used
  for the first set of a session.
This commit is contained in:
2026-02-24 15:55:27 -06:00
parent 7b535bef6e
commit 2208f0492b
2 changed files with 62 additions and 21 deletions

View File

@@ -149,7 +149,8 @@ class LogService:
def delete_log(self, log_id: int) -> None:
"""Delete a log entry.
Removes the log and cleans up the parent session if no logs remain.
Removes the log, renumbers remaining sets, and cleans up the
parent session if no logs remain.
Args:
log_id: The log entry ID.
@@ -162,15 +163,32 @@ class LogService:
raise ValueError(f"WorkoutLog with id {log_id} not found")
session_id = log.session_id
exercise_id = log.exercise_id
self._session.delete(log)
self._session.commit()
logger.info("log_deleted", log_id=log_id)
# Clean up orphaned session if no logs remain
# Renumber remaining sets so they stay sequential (1, 2, 3...)
remaining = self._session.exec(
select(WorkoutLog)
.where(
WorkoutLog.session_id == session_id,
WorkoutLog.exercise_id == exercise_id,
)
.order_by(WorkoutLog.set_number)
).all()
for i, remaining_log in enumerate(remaining, start=1):
if remaining_log.set_number != i:
remaining_log.set_number = i
self._session.add(remaining_log)
if remaining:
self._session.commit()
# Clean up orphaned session if no logs remain for ANY exercise
any_remaining = self._session.exec(
select(WorkoutLog).where(WorkoutLog.session_id == session_id)
).first()
if remaining is None:
if any_remaining is None:
ws = self._session.get(WorkoutSession, session_id)
if ws:
self._session.delete(ws)