-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basics of business logic for customer provisioning
- Loading branch information
1 parent
b26a256
commit ab02184
Showing
4 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Python API for provisioning operations. | ||
""" | ||
import logging | ||
from ..api_client.lms_client import LmsApiClient | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_or_create_enterprise_customer(*, name, slug, country, **kwargs): | ||
""" | ||
Get or creates an enterprise customer with the provided arguments. | ||
""" | ||
client = LmsApiClient() | ||
existing_customer = client.get_enterprise_customer_data(enterprise_customer_slug=slug) | ||
if existing_customer: | ||
logger.info('Provisioning: enterprise_customer slug %s already exists', slug) | ||
return existing_customer | ||
|
||
created_customer = client.create_enterprise_customer( | ||
name=name, slug=slug, country=country, **kwargs, | ||
) | ||
logger.info('Provisioning: created enterprise customer with slug %s', slug) | ||
|
||
|
||
def get_or_create_enterprise_admin_users(enterprise_customer_uuid, user_emails): | ||
""" | ||
Creates pending admin records from the given ``user_email`` for the customer | ||
identified by ``enterprise_customer_uuid``. | ||
""" | ||
client = LmsApiClient() | ||
existing_admins = client.get_enterprise_admin_users(enterprise_customer_uuid) | ||
existing_admin_emails = {record['email'] for record in existing_admins} | ||
logger.info( | ||
'Provisioning: customer %s has existing admin emails %s', | ||
enterprise_customer_uuid, | ||
existing_admin_emails, | ||
) | ||
|
||
existing_pending_admins = client.get_enterprise_pending_admin_users(enterprise_customer_uuid) | ||
existing_pending_admin_emails = {record['user_email'] for record in existing_pending_admins} | ||
logger.info( | ||
'Provisioning: customer %s has existing pending admin emails %s', | ||
enterprise_customer_uuid, | ||
existing_pending_admin_emails, | ||
) | ||
|
||
user_emails_to_create = list( | ||
(set(user_emails) - existing_admin_emails) - existing_pending_admin_emails | ||
) | ||
|
||
created_admins = [] | ||
for user_email in user_emails_to_create: | ||
created_admins.append( | ||
client.create_enterprise_admin_user(enterprise_customer_uuid, user_email) | ||
) | ||
logger.info( | ||
'Provisioning: created admin %s for customer %s', | ||
user_email, | ||
enterprise_customer_uuid, | ||
) | ||
|
||
return created_admins + existing_pending_admins |