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: don't cache if default intentions need enrollment #612

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions enterprise_access/apps/bffs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def get_and_cache_default_enterprise_enrollment_intentions_learner_status(
):
"""
Retrieves and caches default enterprise enrollment intentions for a learner.
This function does not cache this data if it includes any enrollable intentions,
because we don't want to re-attempt enrollment realization on the second of consecutive requests.
"""
cache_key = default_enterprise_enrollment_intentions_learner_status_cache_key(
enterprise_customer_uuid,
Expand All @@ -142,6 +144,13 @@ def get_and_cache_default_enterprise_enrollment_intentions_learner_status(
response_payload = client.get_default_enterprise_enrollment_intentions_learner_status(
enterprise_customer_uuid=enterprise_customer_uuid,
)

# Don't set in the cache if there are enrollable intentions
if statuses := response_payload.get('enrollment_statuses', {}):
needs_enrollment = statuses.get('needs_enrollment', {})
if needs_enrollment.get('enrollable', []):
return response_payload

TieredCache.set_all_tiers(cache_key, response_payload, timeout)
return response_payload

Expand Down
13 changes: 13 additions & 0 deletions enterprise_access/apps/bffs/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ def test_realize_default_enrollments(
}],
)

# a simple validation here that a second consecutive call to
# load the default intentions means the handler doesn't read from the cache,
# because the first request included enrollable intentions.
# We make this assertion using the returned value from the mock call
# to fetch default intention status. In a production-like setting, this
# second call should contain data indicating that default enrollment
# intentions were actually realized.
handler.load_default_enterprise_enrollment_intentions()
self.assertEqual(
handler.context.data['default_enterprise_enrollment_intentions'],
mock_get_intentions.return_value,
)


class TestDashboardHandler(TestHandlerContextMixin):
"""
Expand Down
Loading