diff --git a/README.md b/README.md index e2f5204..a3aa27d 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,10 @@ enum PotApplicationStatus { } ``` +#### ✅ Get registrations to lists by account: `GET /accounts/{ACCOUNT_ID}/list-registrations` (paginated) + +Can specify status to filter by using `status` query param if desired, e.g. `status=Approved` + ### `List` endpoints #### ✅ Get all lists: `GET /lists` (paginated) diff --git a/accounts/api.py b/accounts/api.py index 0d600fd..907fb33 100644 --- a/accounts/api.py +++ b/accounts/api.py @@ -23,6 +23,8 @@ DonationSerializer, PaginatedDonationsResponseSerializer, ) +from lists.models import ListRegistration, ListRegistrationStatus +from lists.serializers import PAGINATED_LIST_REGISTRATION_EXAMPLE, ListRegistrationSerializer, PaginatedListRegistrationsResponseSerializer from pots.models import Pot, PotApplication, PotApplicationStatus, PotPayout from pots.serializers import ( PAGINATED_PAYOUT_EXAMPLE, @@ -391,3 +393,51 @@ def get(self, request: Request, *args, **kwargs): results = self.paginate_queryset(payouts, request, view=self) serializer = PotPayoutSerializer(results, many=True) return self.get_paginated_response(serializer.data) + + +class AccountListRegistrationsAPI(APIView, CustomSizePageNumberPagination): + + @extend_schema( + parameters=[ + OpenApiParameter("account_id", str, OpenApiParameter.PATH), + *pagination_parameters, + ], + responses={ + 200: OpenApiResponse( + response=PaginatedListRegistrationsResponseSerializer, + description="Returns List registrations for the account provided", + examples=[ + OpenApiExample( + "example-1", + summary="Simple registration example", + description="Example response for list registrations", + value=PAGINATED_LIST_REGISTRATION_EXAMPLE, + response_only=True, + ), + ], + ), + 404: OpenApiResponse(description="Account not found"), + 500: OpenApiResponse(description="Internal server error"), + }, + ) + @method_decorator(cache_page(60 * 5)) + def get(self, request: Request, *args, **kwargs): + account_id = kwargs.get("account_id") + try: + account = Account.objects.get(id=account_id) + except Account.DoesNotExist: + return Response( + {"message": f"Account with ID {account_id} not found."}, status=404 + ) + + registrations = ListRegistration.objects.filter(registrant=account) + status_param = request.query_params.get("status") + if status_param: + if status_param not in ListRegistrationStatus.values: + return Response( + {"message": f"Invalid status value: {status_param}"}, status=400 + ) + registrations = registrations.filter(status=status_param) + results = self.paginate_queryset(registrations, request, view=self) + serializer = ListRegistrationSerializer(results, many=True) + return self.get_paginated_response(serializer.data) diff --git a/api/urls.py b/api/urls.py index 8026c40..fd0c4d8 100644 --- a/api/urls.py +++ b/api/urls.py @@ -10,6 +10,7 @@ AccountDetailAPI, AccountDonationsReceivedAPI, AccountDonationsSentAPI, + AccountListRegistrationsAPI, AccountPayoutsReceivedAPI, AccountPotApplicationsAPI, AccountsListAPI, @@ -76,6 +77,11 @@ AccountPayoutsReceivedAPI.as_view(), name="accounts_api_by_id_payouts_received", ), + path( + "v1/accounts//list-registrations", + AccountListRegistrationsAPI.as_view(), + name="accounts_api_by_id_registrations", + ), # donate contract config path( "v1/donate_contract_config",