diff --git a/app/api/events.py b/app/api/events.py index d954b06..0b4a930 100644 --- a/app/api/events.py +++ b/app/api/events.py @@ -94,17 +94,23 @@ def get_upcoming_events(request: Request, token: AccessTokenPayload = Depends(op @router.get('/past-events/count') -def get_past_events_count(request: Request, token: AccessTokenPayload = Depends(optional_authentication)): +def get_past_events_count(request: Request, + token: AccessTokenPayload = Depends(optional_authentication)): """Get count of past events""" db = get_database(request) # Get current UTC datetime - current_datetime = datetime.now(timezone.utc) + current_datetime = datetime.now() # Apply search filter according to role - search_filter = {"date": {"$lt": current_datetime}} - if token and token.role != Role.admin: - search_filter["public"] = True + if token and token.role == Role.admin: + search_filter = { + 'date' : {"$lt": current_datetime} + } + else : + search_filter = { + "$and": [{'date' : {"$lt": current_datetime}}, {"public" : {"$eq": True}}] + } # Count the number of documents that match the filter count = db.events.count_documents(search_filter) @@ -112,8 +118,7 @@ def get_past_events_count(request: Request, token: AccessTokenPayload = Depends( @router.get('/past-events') def get_past_events(request: Request, token: AccessTokenPayload = Depends(optional_authentication), - skip: int = Query(0, ge=0), - limit: int = Query(10,ge=1,le=50)): + skip: int = Query(0, ge=0)): """ Get last events that have passed """ # TODO: Expand endpoint to accept custom amount? db = get_database(request) @@ -129,12 +134,13 @@ def get_past_events(request: Request, token: AccessTokenPayload = Depends(option if token and token.role == Role.admin: search_filter = {'date': {"$lt": date}} + fixed_limit = 10 # Get the last events pipeline = [ {"$match": search_filter}, {"$sort": {"date": -1}}, {"$skip": skip}, - {"$limit": limit}, + {"$limit": fixed_limit}, ] res = db.events.aggregate(pipeline) diff --git a/tests/test_endpoints/test_event.py b/tests/test_endpoints/test_event.py index e2ebd4e..7fb5b2f 100644 --- a/tests/test_endpoints/test_event.py +++ b/tests/test_endpoints/test_event.py @@ -147,13 +147,13 @@ def test_get_past_events_with_pagination(client): total_past_events = response.json()["count"] # Set pagination parameters - limit = 5 + limit = 10 total_pages = (total_past_events + limit - 1) // limit # Iterate through pages for page in range(total_pages): skip = page * limit - response = client.get(f'/api/event/past-events?skip={skip}&limit={limit}') + response = client.get(f'/api/event/past-events?skip={skip}') assert response.status_code == 200 past_events = response.json()