Skip to content

Commit 49aa1f4

Browse files
committed
Handle multiple user edge case for medical info review
If a user submits a stale 'mark as reviewed' for a medical info section, and it has already been reviewed by someone else, do nothing in the database and simply display a warning flash message informing the user.
1 parent eed569f commit 49aa1f4

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

manage_breast_screening/mammograms/tests/views/test_appointment_views.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
from django.urls import reverse
33
from pytest_django.asserts import assertContains, assertRedirects
44

5+
from manage_breast_screening.auth.tests.factories import UserFactory
56
from manage_breast_screening.core.models import AuditLog
67
from manage_breast_screening.participants.models import MedicalInformationReview
7-
from manage_breast_screening.participants.tests.factories import AppointmentFactory
8+
from manage_breast_screening.participants.tests.factories import (
9+
AppointmentFactory,
10+
MedicalInformationReviewFactory,
11+
)
812

913

1014
@pytest.mark.django_db
@@ -204,3 +208,31 @@ def test_creates_medical_information_review(self, clinical_user_client):
204208
appointment=appointment, section="SYMPTOMS"
205209
)
206210
assert review.reviewed_by == clinical_user_client.user
211+
212+
def test_does_not_update_reviewed_by_if_already_reviewed(
213+
self, clinical_user_client
214+
):
215+
appointment = AppointmentFactory.create(
216+
clinic_slot__clinic__setting__provider=clinical_user_client.current_provider
217+
)
218+
original_user = UserFactory.create(first_name="Jane", last_name="Doe")
219+
MedicalInformationReviewFactory.create(
220+
appointment=appointment, section="SYMPTOMS", reviewed_by=original_user
221+
)
222+
223+
response = clinical_user_client.http.post(
224+
reverse(
225+
"mammograms:mark_section_reviewed",
226+
kwargs={"pk": appointment.pk, "section": "SYMPTOMS"},
227+
),
228+
follow=True,
229+
)
230+
231+
review = MedicalInformationReview.objects.get(
232+
appointment=appointment, section="SYMPTOMS"
233+
)
234+
assert review.reviewed_by == original_user
235+
assertContains(
236+
response,
237+
"This section has already been reviewed by Jane Doe",
238+
)

manage_breast_screening/mammograms/views/appointment_views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from django.contrib import messages
34
from django.http import Http404
45
from django.shortcuts import redirect, render
56
from django.urls import reverse, reverse_lazy
@@ -296,9 +297,23 @@ def post(self, request, *args, **kwargs):
296297
if section not in valid_sections:
297298
raise Http404("Invalid section")
298299

299-
self.appointment.medical_information_reviews.get_or_create(
300+
existing_review = self.appointment.medical_information_reviews.filter(
301+
section=section
302+
).first()
303+
304+
if existing_review:
305+
messages.add_message(
306+
request,
307+
messages.WARNING,
308+
f"This section has already been reviewed by {existing_review.reviewed_by.get_full_name()}",
309+
)
310+
return redirect(
311+
"mammograms:record_medical_information", pk=self.appointment.pk
312+
)
313+
314+
self.appointment.medical_information_reviews.create(
300315
section=section,
301-
defaults={"reviewed_by": request.user},
316+
reviewed_by=request.user,
302317
)
303318

304319
return redirect("mammograms:record_medical_information", pk=self.appointment.pk)

0 commit comments

Comments
 (0)