diff --git a/enterprise_access/apps/bffs/api.py b/enterprise_access/apps/bffs/api.py index 94f21eb1..69263403 100644 --- a/enterprise_access/apps/bffs/api.py +++ b/enterprise_access/apps/bffs/api.py @@ -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, @@ -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 diff --git a/enterprise_access/apps/bffs/tests/test_handlers.py b/enterprise_access/apps/bffs/tests/test_handlers.py index 59fb3f51..9ca78619 100644 --- a/enterprise_access/apps/bffs/tests/test_handlers.py +++ b/enterprise_access/apps/bffs/tests/test_handlers.py @@ -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): """