Skip to content

Commit

Permalink
fix: don't cache if default intentions need enrollment
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Dec 12, 2024
1 parent 4ca1895 commit c0208fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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('enrollment', []):
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

0 comments on commit c0208fe

Please sign in to comment.