Skip to content

Commit 7df2a4f

Browse files
authored
Merge pull request #871 from NHSDigital/11594-mammogram-workflow-states--sql-bug-fix
Fix appointment status filtering
2 parents 5e54b50 + 9e7a877 commit 7df2a4f

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

manage_breast_screening/participants/models/appointment.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515

1616
class AppointmentQuerySet(models.QuerySet):
1717
def in_status(self, *statuses):
18-
return self.filter(
19-
statuses=Subquery(
20-
AppointmentStatus.objects.filter(
21-
appointment=OuterRef("pk"),
22-
name__in=statuses,
23-
)
24-
.values("pk")
25-
.order_by("-created_at")[:1]
26-
)
18+
# Get the most recent status name for each appointment
19+
most_recent_status = (
20+
AppointmentStatus.objects.filter(appointment=OuterRef("pk"))
21+
.order_by("-created_at")
22+
.values("name")[:1]
23+
)
24+
25+
# Filter appointments where the most recent status name is in the provided list
26+
return self.annotate(current_status_name=Subquery(most_recent_status)).filter(
27+
current_status_name__in=statuses
2728
)
2829

2930
def remaining(self):

manage_breast_screening/participants/tests/test_models.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,50 @@ def test_for_participant(self):
255255
ordered=False,
256256
)
257257

258+
def test_in_status_only_matches_latest_status_not_history(
259+
self, django_assert_num_queries
260+
):
261+
appointment = AppointmentFactory.create()
262+
appointment_with_history = AppointmentFactory.create()
263+
264+
AppointmentStatusFactory.create(
265+
appointment=appointment,
266+
name=models.AppointmentStatus.CHECKED_IN,
267+
created_at=datetime(2025, 1, 1, 9, tzinfo=tz.utc),
268+
)
269+
AppointmentStatusFactory.create(
270+
appointment=appointment_with_history,
271+
name=models.AppointmentStatus.CHECKED_IN,
272+
created_at=datetime(2025, 1, 2, 10, tzinfo=tz.utc),
273+
)
274+
AppointmentStatusFactory.create(
275+
appointment=appointment_with_history,
276+
name=models.AppointmentStatus.SCREENED,
277+
created_at=datetime(2025, 1, 2, 11, tzinfo=tz.utc),
278+
)
279+
280+
with django_assert_num_queries(3):
281+
assertQuerySetEqual(
282+
models.Appointment.objects.in_status(
283+
models.AppointmentStatus.CHECKED_IN
284+
),
285+
[appointment],
286+
ordered=False,
287+
)
288+
assertQuerySetEqual(
289+
models.Appointment.objects.in_status(
290+
models.AppointmentStatus.CHECKED_IN,
291+
models.AppointmentStatus.SCREENED,
292+
),
293+
[appointment, appointment_with_history],
294+
ordered=False,
295+
)
296+
assertQuerySetEqual(
297+
models.Appointment.objects.in_status(models.AppointmentStatus.SCREENED),
298+
[appointment_with_history],
299+
ordered=False,
300+
)
301+
258302
@pytest.mark.django_db
259303
class TestEagerLoadCurrentStatus:
260304
def test_eager_loads_most_recent_status_with_created_by(

0 commit comments

Comments
 (0)