feat: auto-set started_at/completed_at on task status transitions
This commit is contained in:
@@ -240,6 +240,25 @@ async def update_note(user_id: int, note_id: int, **fields: object) -> Note | No
|
||||
elif key == "tags" and isinstance(value, list):
|
||||
value = _normalize_tags(value)
|
||||
setattr(note, key, value)
|
||||
# Auto-set lifecycle timestamps on status transitions
|
||||
if "status" in fields:
|
||||
_now = datetime.now(timezone.utc)
|
||||
if note.status == TaskStatus.in_progress.value:
|
||||
if note.started_at is None:
|
||||
note.started_at = _now
|
||||
elif note.status in (TaskStatus.done.value, TaskStatus.cancelled.value):
|
||||
note.completed_at = _now
|
||||
if note.recurrence_rule:
|
||||
from fabledassistant.services.recurrence import calculate_next_due
|
||||
base = note.due_date or _now.date()
|
||||
next_due = calculate_next_due(note.recurrence_rule, base)
|
||||
note.recurrence_next_spawn_at = datetime(
|
||||
next_due.year, next_due.month, next_due.day, tzinfo=timezone.utc
|
||||
)
|
||||
elif note.status == TaskStatus.todo.value:
|
||||
note.started_at = None
|
||||
note.completed_at = None
|
||||
note.recurrence_next_spawn_at = None
|
||||
note.updated_at = datetime.now(timezone.utc)
|
||||
await session.commit()
|
||||
await session.refresh(note)
|
||||
|
||||
Reference in New Issue
Block a user