From b3946a8e1bc4741f368f0d31d9a6771fd7f502ff Mon Sep 17 00:00:00 2001 From: katrinan029 Date: Thu, 29 Feb 2024 06:34:58 +0000 Subject: [PATCH] feat: update contact email logic in can_redeem feat: update contact email logic in can_redeem fix: refactored fix: refactored --- .../tests/test_subsidy_access_policy_views.py | 34 +++++++++++++++---- .../api/v1/views/subsidy_access_policy.py | 18 ++++++++-- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/enterprise_access/apps/api/v1/tests/test_subsidy_access_policy_views.py b/enterprise_access/apps/api/v1/tests/test_subsidy_access_policy_views.py index 88d7c48b..a4259608 100644 --- a/enterprise_access/apps/api/v1/tests/test_subsidy_access_policy_views.py +++ b/enterprise_access/apps/api/v1/tests/test_subsidy_access_policy_views.py @@ -1711,21 +1711,34 @@ def mock_get_subsidy_content_data(*args): @mock.patch('enterprise_access.apps.subsidy_access_policy.subsidy_api.get_and_cache_transactions_for_learner') @mock.patch('enterprise_access.apps.api.v1.views.subsidy_access_policy.LmsApiClient', return_value=mock.MagicMock()) @ddt.data( - {"has_admin_users": True}, - {"has_admin_users": False}, + {"has_admin_users": True, + "contact_email": 'edx@example.org', + "admin_users": [{ + "email": 'frodo@example.org', + "lms_user_id": 12 + }]}, + {"has_admin_users": True, + "contact_email": None, + "admin_users": [{ + "email": 'frodo@example.org', + "lms_user_id": 12, + }]}, + {"has_admin_users": False, + "contact_email": None, + "admin_users": None} ) @ddt.unpack def test_can_redeem_policy_none_redeemable( - self, mock_lms_client, mock_transactions_cache_for_learner, has_admin_users + self, mock_lms_client, mock_transactions_cache_for_learner, has_admin_users, contact_email, admin_users ): """ Test that the can_redeem endpoint returns reasons for why each non-redeemable policy failed. """ slug = 'sluggy' - admin_email = 'edx@example.org' mock_lms_client().get_enterprise_customer_data.return_value = { 'slug': slug, - 'admin_users': [{'email': admin_email}] if has_admin_users else [], + 'admin_users': admin_users if has_admin_users else [], + 'contact_email': contact_email } mock_transactions_cache_for_learner.return_value = { @@ -1791,10 +1804,17 @@ def mock_get_subsidy_content_data(*args, **kwargs): expected_user_message = ( MissingSubsidyAccessReasonUserMessages.ORGANIZATION_NO_FUNDS - if has_admin_users + if contact_email is not None or has_admin_users else MissingSubsidyAccessReasonUserMessages.ORGANIZATION_NO_FUNDS_NO_ADMINS ) - expected_enterprise_admins = [{'email': admin_email}] if has_admin_users else [] + expected_enterprise_admins = [] + if contact_email is not None: + expected_enterprise_admins = [{ + "email": contact_email, + "lms_user_id": None, + }] + elif has_admin_users: + expected_enterprise_admins = admin_users assert response_list[0]["reasons"] == [ { diff --git a/enterprise_access/apps/api/v1/views/subsidy_access_policy.py b/enterprise_access/apps/api/v1/views/subsidy_access_policy.py index 31b5b4ef..ae002461 100644 --- a/enterprise_access/apps/api/v1/views/subsidy_access_policy.py +++ b/enterprise_access/apps/api/v1/views/subsidy_access_policy.py @@ -95,14 +95,14 @@ def _get_reasons_for_no_redeemable_policies(enterprise_customer_uuid, non_redeem reasons = [] lms_client = LmsApiClient() enterprise_customer_data = lms_client.get_enterprise_customer_data(enterprise_customer_uuid) - enterprise_admin_users = enterprise_customer_data.get('admin_users') + admin_contact = _get_admin_contact_email(enterprise_customer_data) for reason, policies in non_redeemable_policies_by_reason.items(): reasons.append({ "reason": reason, - "user_message": _get_user_message_for_reason(reason, enterprise_admin_users), + "user_message": _get_user_message_for_reason(reason, admin_contact), "metadata": { - "enterprise_administrators": enterprise_admin_users, + "enterprise_administrators": admin_contact, }, "policy_uuids": [policy.uuid for policy in policies], }) @@ -110,6 +110,18 @@ def _get_reasons_for_no_redeemable_policies(enterprise_customer_uuid, non_redeem return reasons +def _get_admin_contact_email(enterprise_customer_data): + """ + Return the point of contact email for an enterprise customer. + """ + if admin_contact_email := enterprise_customer_data.get('contact_email'): + return [{ + "email": admin_contact_email, + "lms_user_id": None, + }] + return enterprise_customer_data.get('admin_users', []) + + def _get_user_message_for_reason(reason_slug, enterprise_admin_users): """ Return the user-facing message for a given reason slug.