Skip to content

Commit

Permalink
feat: unlink learners from enterprise if their licenses are expired
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Nov 1, 2024
1 parent 69aabec commit e8396ff
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
from datetime import datetime, timedelta

from django.conf import settings
from django.core.management.base import BaseCommand

from license_manager.apps.api.tasks import license_expiration_task
from license_manager.apps.api_client.enterprise import EnterpriseApiClient
from license_manager.apps.subscriptions.constants import (
ACTIVATED,
ASSIGNED,
Expand Down Expand Up @@ -70,6 +72,41 @@ def add_arguments(self, parser):
default=False,
)

def _unlink_expired_licenses_users(self, expired_subscription_plan, expired_licenses):
"""
Unlinks the users from the enterprise customer for the expired licenses.
"""
user_emails = []
license_uuids = []

enterprise_customer_uuid = expired_subscription_plan.customer_agreement.enterprise_customer_uuid
# only unlink users if the enterprise is in the list of customers with unlinking enabled
if enterprise_customer_uuid not in settings.CUSTOMERS_WITH_EXPIRED_LICENSES_UNLINKING_ENABLED:
return

# only unlink if 90 days has passed since the license was expired
days_since_expiration = (localized_utcnow() - expired_subscription_plan.expiration_date).days
if days_since_expiration < 90:
return

for license in expired_licenses:
user_emails.append(license.user_email)
license_uuids.append(license.uuid)

EnterpriseApiClient().bulk_unlink_enterprise_users(
enterprise_customer_uuid,
{
'user_emails': user_emails,
'is_relinkable': False
},
)

logger.info(
"[EXPIRE_SUBSCRIPTIONS] Learners unlinked. Enterprise: [%s], LicenseUUIDs: [%s].",
enterprise_customer_uuid,
license_uuids
)

def _expire_subscription_plan(self, expired_subscription_plan):
"""
Expires a single subscription plan.
Expand Down Expand Up @@ -97,6 +134,7 @@ def _expire_subscription_plan(self, expired_subscription_plan):
license_chunk_uuids,
ignore_enrollments_modified_after=ignore_enrollments_modified_after
)
self._unlink_expired_licenses_users(expired_subscription_plan, license_chunk)
except Exception: # pylint: disable=broad-except
any_failures = True
msg = 'Failed to terminate course enrollments for learners in subscription: {}'.format(
Expand Down

0 comments on commit e8396ff

Please sign in to comment.