Skip to content

fix(sessions): same-day sessions no longer crash log/invoice lookup - #537

Merged
dholbach merged 1 commit into
mainfrom
fix/same-day-session-lookup
Oct 23, 2026
Merged

dholbach merged 1 commit into
mainfrom
fix/same-day-session-lookup

Conversation

@dholbach

Copy link
Copy Markdown
Owner

Problem

Posting a new session log raised MultipleObjectsReturned: get() returned more than one Session -- it returned 2! for a client who had been rescheduled: the original event was cancelled and a new one booked at the same date and time, leaving two Session rows (one cancelled) on that date. The calendar import keys sessions on date + time, so several sessions per date are legitimate, but two sites looked them up by date only via get_or_create:

  • session_log_create (views/clinical_views.py)
  • InvoiceFormsetMixin.attach_sessions_to_formset (views/crud_mixins.py), the same crash when saving an invoice item dated on such a day

Fix

Both sites now:

  • choose from the day's live (non-cancelled) sessions when there are any, falling back to all of the day's sessions;
  • prefer one not yet logged (session log) or not yet billed (invoice item), ordered by time;
  • create a Session only when none exists for that date.

The log view still redirects to editing the existing log when every candidate already has one. An invoice item already linked to a session on the same date keeps its link on edit.

Tests

Six new tests in test_clinical.py and test_views_invoice_billing.py:

  • two sessions on the same day
  • every session that day already logged
  • the cancelled leftover is skipped, for both logs and invoices
  • a logged live session that has a cancelled leftover beside it

./dev.py test --fast passes.

🤖 Generated with Claude Code

A client can have several Sessions on one date: the calendar import keys
on date + time, and a reschedule (cancel + rebook at the same slot) leaves
a cancelled twin behind. session_log_create and
InvoiceFormsetMixin.attach_sessions_to_formset both used
get_or_create(client, session_date), which raised MultipleObjectsReturned.

Both now pick from the day's live (non-cancelled) sessions when any exist,
preferring one not yet logged / not yet billed, and only create a Session
when none exists. An invoice item that already links a session on the
same date keeps it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dholbach
dholbach merged commit 55fe73d into main Oct 23, 2026
7 checks passed
@dholbach
dholbach deleted the fix/same-day-session-lookup branch October 23, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant