Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for active, current license #596

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions enterprise_access/apps/bffs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,19 @@ def transform_subscriptions_result(self, subscriptions_result):
'subscription_licenses_by_status': subscription_licenses_by_status,
}

def check_has_activated_license(self):
@property
def current_active_license(self):
Comment on lines +232 to +233
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call to move this to a @property :) Been thinking it might make sense to do that.

"""
Check if the user has an activated license.

Returns:
bool: True if the user has an activated license, False otherwise.
Returns an activated license for the user iff the related subscription plan is current,
otherwise returns None.
"""
return bool(self.subscription_licenses_by_status.get('activated'))
current_active_licenses = [
_license for _license in self.subscription_licenses_by_status.get('activated', [])
if _license['subscription_plan']['is_current']
]
if current_active_licenses:
return current_active_licenses[0]
return None

def process_subscription_licenses(self):
"""
Expand All @@ -248,7 +253,7 @@ def process_subscription_licenses(self):
This method is called after `load_subscription_licenses` to handle further actions based
on the loaded data.
"""
if not (self.subscriptions or self.check_has_activated_license()):
if not self.subscriptions or self.current_active_license:
# Skip processing if:
# - there is no subscriptions data
# - user already has an activated license
Expand Down Expand Up @@ -345,7 +350,7 @@ def check_and_auto_apply_license(self):
subscription_licenses_by_status = self.subscription_licenses_by_status
assigned_licenses = subscription_licenses_by_status.get('assigned', [])

if assigned_licenses or self.check_has_activated_license():
if assigned_licenses or self.current_active_license:
# Skip auto-apply if user already has assigned license(s) or an already-activated license
return

Expand Down