Skip to content

Commit

Permalink
fix: add tests around BFF viewset; cleanup and minor fixes (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz authored Nov 22, 2024
1 parent 11a4a02 commit adbd712
Show file tree
Hide file tree
Showing 18 changed files with 1,129 additions and 196 deletions.
41 changes: 41 additions & 0 deletions enterprise_access/apps/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

from rest_framework.exceptions import ParseError

from enterprise_access.apps.bffs.api import (
get_and_cache_enterprise_customer_users,
transform_enterprise_customer_users_data
)
from enterprise_access.apps.content_assignments.api import get_assignment_configuration
from enterprise_access.apps.subsidy_access_policy.api import get_subsidy_access_policy

Expand Down Expand Up @@ -75,3 +79,40 @@ def get_assignment_config_customer_uuid(assignment_configuration_uuid):
if assignment_config:
return str(assignment_config.enterprise_customer_uuid)
return None


def get_or_fetch_enterprise_uuid_for_bff_request(request):
"""
Extracts enterprise_customer_uuid from query params or request data.
"""
enterprise_customer_uuid = (
get_enterprise_uuid_from_query_params(request) or
get_enterprise_uuid_from_request_data(request)
)
if enterprise_customer_uuid:
return enterprise_customer_uuid

enterprise_customer_slug = (
request.query_params.get('enterprise_customer_slug') or
request.data.get('enterprise_customer_slug')
)
if enterprise_customer_slug:
try:
enterprise_customer_users_data = get_and_cache_enterprise_customer_users(
request,
traverse_pagination=True
)
transformed_data = transform_enterprise_customer_users_data(
enterprise_customer_users_data,
request=request,
enterprise_customer_slug=enterprise_customer_slug,
enterprise_customer_uuid=enterprise_customer_uuid,
)
enterprise_customer = transformed_data.get('enterprise_customer') or {}
return enterprise_customer.get('uuid')
except Exception: # pylint: disable=broad-except
logger.exception('Error retrieving linked enterprise customers')
return None

# Could not derive enterprise_customer_uuid for the BFF request.
return None
Loading

0 comments on commit adbd712

Please sign in to comment.