Skip to content

Commit

Permalink
Add field filter for event reservation units preferred order
Browse files Browse the repository at this point in the history
  • Loading branch information
MrThearMan committed Dec 14, 2023
1 parent 00c801b commit 1fc5a66
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
12 changes: 10 additions & 2 deletions api/graphql/types/application_event/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

import graphene
from django.db import models
from graphene_permissions.permissions import AllowAuthenticated
Expand All @@ -9,7 +11,7 @@
from api.graphql.types.application_event_schedule.types import ApplicationEventScheduleNode
from applications.choices import ApplicationEventStatusChoice
from applications.models import ApplicationEvent, EventReservationUnit
from common.typing import AnyUser
from common.typing import AnyUser, GQLInfo
from permissions.helpers import get_service_sectors_where_can_view_applications, get_units_where_can_view_applications


Expand All @@ -31,7 +33,7 @@ class ApplicationEventNode(DjangoAuthNode):
max_duration = Duration()

application_event_schedules = ApplicationEventScheduleNode.ListField()
event_reservation_units = EventReservationUnitNode.ListField()
event_reservation_units = EventReservationUnitNode.ListField(preferred_order=graphene.Int())

class Meta:
model = ApplicationEvent
Expand Down Expand Up @@ -68,3 +70,9 @@ def filter_queryset(cls, queryset: models.QuerySet, user: AnyUser) -> models.Que
| models.Q(event_reservation_units__reservation_unit__unit__in=units)
| models.Q(application__user=user)
).distinct()

def resolve_event_reservation_units(root: ApplicationEvent, info: GQLInfo, **kwargs: Any) -> models.QuerySet:
preferred_order: int | None = kwargs.get("preferred_order")
if preferred_order is not None:
return root.event_reservation_units.filter(preferred_order=preferred_order)
return root.event_reservation_units.all()
2 changes: 1 addition & 1 deletion tests/gql_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _build_field_filter_params(field_filter_params: dict[str, Any]) -> dict[str,
plain_key, lookup_field = _split_lookups(key)
parts = plain_key.split(LOOKUP_SEP)
last_index = len(parts) - 1
params[to_camel_case(parts[0])] = current_params = FieldFilterParams()
current_params = params.setdefault(to_camel_case(parts[0]), FieldFilterParams())

for i, part in enumerate(parts[1:], start=1):
if i != last_index:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_graphql_api/test_application_event/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,31 @@ def test_application_event__filter__by_text_search__not_found(graphql):
# - The response contains no application events
assert response.has_errors is False, response
assert len(response.edges) == 0, response


def test_application_event__filter__event_reservation_units__preferred_order(graphql):
# given:
# - There is draft application in an open application round with two application events
# - The owner of the application is using the system
application = ApplicationFactory.create_in_status_draft()
event_1 = ApplicationEventFactory.create_in_status_unallocated(
application=application,
event_reservation_units__preferred_order=0,
)
event_2 = ApplicationEventFactory.create_in_status_unallocated(
application=application,
event_reservation_units__preferred_order=1,
)
graphql.force_login(application.user)

# when:
# - User tries to filter only event reservation units with preferred order of 0
fields = "pk eventReservationUnits { preferredOrder }"
query = events_query(fields=fields, event_reservation_units__preferred_order=0)
response = graphql(query)

# then:
# - The response contains only the event reservation units with the given preferred order
assert len(response.edges) == 2, response
assert response.node(0) == {"pk": event_1.pk, "eventReservationUnits": [{"preferredOrder": 0}]}
assert response.node(1) == {"pk": event_2.pk, "eventReservationUnits": []}

0 comments on commit 1fc5a66

Please sign in to comment.