Skip to content

Commit 0a360e4

Browse files
committed
Include bookings from all requested organizations
1 parent b666cf2 commit 0a360e4

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

‎care/emr/api/viewsets/scheduling/booking.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,15 @@ def authorize_booking_list( # noqa PLR0912
361361
raise PermissionDenied(
362362
"You do not have permission to list bookings"
363363
)
364-
users = FacilityOrganizationUser.objects.filter(
365-
organization=organization
366-
).values("user_id")
367-
# TODO : Change to overlap to include children as well
368-
base_query = base_query.filter(
369-
token_slot__resource__in=SchedulableResource.objects.filter(
370-
user__in=users, facility=facility
371-
)
364+
users = FacilityOrganizationUser.objects.filter(
365+
organization__in=organizations
366+
).values("user_id")
367+
# TODO : Change to overlap to include children as well
368+
base_query = base_query.filter(
369+
token_slot__resource__in=SchedulableResource.objects.filter(
370+
user__in=users, facility=facility
372371
)
372+
)
373373
if not resource_ids and not organization_ids and not user.is_superuser:
374374
raise PermissionDenied("You do not have permission to list bookings")
375375
elif resource_type in [

‎care/emr/tests/test_booking_api.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,60 @@ def test_list_booking_filtered_by_organization_without_permissions(self):
204204
)
205205
self.assertEqual(response.status_code, 403)
206206

207+
def test_list_booking_filtered_by_multiple_organizations(self):
208+
"""Bookings from either requested organization are included."""
209+
role = self.create_role_with_permissions(
210+
[SchedulePermissions.can_write_schedule.name]
211+
)
212+
self.attach_role_facility_organization_user(self.organization, self.user, role)
213+
first_booking = self.create_booking()
214+
215+
other_organization = self.create_facility_organization(facility=self.facility)
216+
self.attach_role_facility_organization_user(other_organization, self.user, role)
217+
other_user = self.create_user()
218+
self.attach_role_facility_organization_user(
219+
other_organization, other_user, role
220+
)
221+
other_resource = self.create_resource(user=other_user)
222+
other_slot = self.create_slot(resource=other_resource)
223+
second_booking = self.create_booking(token_slot=other_slot)
224+
225+
response = self.client.get(
226+
self.base_url,
227+
{
228+
"resource_type": SchedulableResourceTypeOptions.practitioner.value,
229+
"organization_ids": (
230+
f"{self.organization.external_id},{other_organization.external_id}"
231+
),
232+
},
233+
)
234+
self.assertEqual(response.status_code, 200)
235+
self.assertCountEqual(
236+
[result["id"] for result in response.data["results"]],
237+
[str(first_booking.external_id), str(second_booking.external_id)],
238+
)
239+
240+
def test_list_booking_filtered_by_multiple_organizations_checks_each_permission(
241+
self,
242+
):
243+
"""A permitted organization cannot grant access to another one."""
244+
role = self.create_role_with_permissions(
245+
[SchedulePermissions.can_write_schedule.name]
246+
)
247+
self.attach_role_facility_organization_user(self.organization, self.user, role)
248+
other_organization = self.create_facility_organization(facility=self.facility)
249+
250+
response = self.client.get(
251+
self.base_url,
252+
{
253+
"resource_type": SchedulableResourceTypeOptions.practitioner.value,
254+
"organization_ids": (
255+
f"{self.organization.external_id},{other_organization.external_id}"
256+
),
257+
},
258+
)
259+
self.assertEqual(response.status_code, 403)
260+
207261
def test_retrieve_booking_with_permissions(self):
208262
"""Users with can_list_user_booking permission can retrieve bookings."""
209263
permissions = [SchedulePermissions.can_list_booking.name]

0 commit comments

Comments
 (0)