Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
(PC-10215): Allow user admin to see every bookings
Browse files Browse the repository at this point in the history
  • Loading branch information
asagot-ansa committed Aug 11, 2021
1 parent cbf9ba7 commit 82c6d5b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/pcapi/core/bookings/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def find_by_pro_user_id(
venue_id: Optional[int] = None,
page: int = 1,
per_page_limit: int = 1000,
is_user_admin: bool = False,
) -> BookingsRecapPaginated:
if page == 1:
total_bookings_recap_count_query = _filter_bookings_recap_query(
Expand All @@ -84,7 +85,9 @@ def find_by_pro_user_id(
else:
total_bookings_recap = 0

bookings_recap_query = _filter_bookings_recap_query(Booking.query, user_id, booking_period, event_date, venue_id)
bookings_recap_query = _filter_bookings_recap_query(
Booking.query, user_id, booking_period, event_date, venue_id, is_user_admin
)
bookings_recap_query = _build_bookings_recap_query(bookings_recap_query)
bookings_recap_query_with_duplicates = _duplicate_booking_when_quantity_is_two(bookings_recap_query)
paginated_bookings = (
Expand Down Expand Up @@ -310,6 +313,7 @@ def _filter_bookings_recap_query(
booking_period: tuple[date, date],
event_date: Optional[date],
venue_id: Optional[int],
is_user_admin: bool = False,
) -> Query:
booking_date = cast(func.timezone(Venue.timezone, func.timezone("UTC", Booking.dateCreated)), Date)
query = (
Expand All @@ -321,9 +325,13 @@ def _filter_bookings_recap_query(
.join(Venue)
.join(Offerer)
.join(UserOfferer)
.filter(UserOfferer.userId == user_id)
.filter(UserOfferer.validationToken.is_(None))
.filter(booking_date.between(*booking_period, symmetric=True))
)

if not is_user_admin:
query = query.filter(UserOfferer.userId == user_id)

query = query.filter(UserOfferer.validationToken.is_(None)).filter(
booking_date.between(*booking_period, symmetric=True)
)

if venue_id:
Expand Down
1 change: 1 addition & 0 deletions src/pcapi/routes/pro/bookings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def get_all_bookings(query: ListBookingsQueryModel) -> ListBookingsResponseModel
event_date=event_date,
venue_id=venue_id,
page=int(page),
is_user_admin=current_user.isAdmin,
)

return ListBookingsResponseModel(
Expand Down
9 changes: 9 additions & 0 deletions tests/routes/pro/get_all_bookings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_call_repository_with_user_and_page(self, find_by_pro_user_id, app):
event_date=None,
venue_id=None,
page=3,
is_user_admin=False,
)

@pytest.mark.usefixtures("db_session")
Expand All @@ -50,6 +51,7 @@ def test_call_repository_with_page_1(self, find_by_pro_user_id, app):
event_date=None,
venue_id=None,
page=1,
is_user_admin=False,
)

@pytest.mark.usefixtures("db_session")
Expand All @@ -71,18 +73,25 @@ def test_call_repository_with_venue_id(self, find_by_pro_user_id, app):
event_date=None,
venue_id=venue.id,
page=1,
is_user_admin=False,
)


@pytest.mark.usefixtures("db_session")
class Returns200Test:
def when_user_is_admin(self, app):
admin = users_factories.AdminFactory()
user_offerer = offers_factories.UserOffererFactory()
bookings_factories.BookingFactory(
dateCreated=datetime(2020, 8, 11, 12, 0, 0),
stock__offer__venue__managingOfferer=user_offerer.offerer,
)

client = TestClient(app.test_client()).with_auth(admin.email)
response = client.get(f"/bookings/pro?{BOOKING_PERIOD_PARAMS}")

assert response.status_code == 200
assert len(response.json["bookings_recap"]) == 1

def when_user_is_linked_to_a_valid_offerer(self, app):
booking = bookings_factories.BookingFactory(
Expand Down

0 comments on commit 82c6d5b

Please sign in to comment.