Skip to content

Commit

Permalink
feat: basics of business logic for customer provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Feb 21, 2025
1 parent b26a256 commit 1fd9c49
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
28 changes: 27 additions & 1 deletion enterprise_access/apps/api/v1/views/provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from enterprise_access.apps.api import serializers
from enterprise_access.apps.core import constants
from enterprise_access.apps.provisioning import api as provisioning_api

logger = logging.getLogger(__name__)

Expand All @@ -37,4 +38,29 @@ class ProvisioningCreateView(PermissionRequiredMixin, generics.CreateAPIView):
def create(self, request, *args, **kwargs):
request_serializer = serializers.ProvisioningRequestSerializer(data=request.data)
request_serializer.is_valid(raise_exception=True)
return Response('ack', status=status.HTTP_201_CREATED)

customer_request_data = request_serializer.validated_data['enterprise_customer']
created_customer = provisioning_api.get_or_create_enterprise_customer(
name=customer_request_data['name'],
country=customer_request_data['country'],
slug=customer_request_data['slug'],
)

admin_emails = [
record.get('user_email')
for record in request_serializer.validated_data['pending_admins']
]

customer_admins = provisioning_api.get_or_create_enterprise_admin_users(
enterprise_customer_uuid=created_customer['uuid'],
user_emails=admin_emails,
)

response_serializer = serializers.ProvisioningResponseSerializer({
'enterprise_customer': created_customer,
'pending_admins': customer_admins,
})
return Response(
response_serializer.data,
status=status.HTTP_201_CREATED,
)
27 changes: 27 additions & 0 deletions enterprise_access/apps/api_client/lms_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ def get_enterprise_admin_users(self, enterprise_customer_uuid):

return results

def get_enterprise_pending_admin_users(self, enterprise_customer_uuid):
"""
Gets all pending enterprise admin records for the given customer uuid.
Arguments:
enterprise_customer_uuid (UUID): UUID of the enterprise customer.
Returns:
List of dictionaries of pending admin users.
"""
try:
response = self.client.get(
self.pending_enterprise_admin_endpoint + f'?enterprise_customer={enterprise_customer_uuid}',
timeout=settings.LMS_CLIENT_TIMEOUT,
)
response.raise_for_status()
logger.info(
'Fetched pending admin records for customer %s', enterprise_customer_uuid,
)
payload = response.json()
return payload.get('results', [])
except requests.exceptions.HTTPError:
logger.exception(
'Failed to fetch pending admin record for customer %s',
enterprise_customer_uuid,
)
raise

def create_enterprise_admin_user(self, enterprise_customer_uuid, user_email):
"""
Creates a new enterprise pending admin record.
Expand Down

0 comments on commit 1fd9c49

Please sign in to comment.