diff --git a/util/marketplace.py b/util/marketplace.py index 5d3808167f..018988bad6 100644 --- a/util/marketplace.py +++ b/util/marketplace.py @@ -24,15 +24,17 @@ def get_account_number(self, user): email = user.email account_number = entitlements.get_web_customer_id(user.id) if account_number is None: - account_number = self.lookup_customer_id(email) - if account_number: + account_numbers = self.lookup_customer_id(email) + if account_numbers: # store in database for next lookup - entitlements.save_web_customer_id(user, account_number) + for account_number in account_numbers: + entitlements.save_web_customer_id(user, account_number) + account_number = account_numbers[0] return account_number def lookup_customer_id(self, email): """ - Send request to internal api for customer id (ebs acc number) + Send request to internal api for customer id (web customer id) """ request_body_dict = { "by": {"emailStartsWith": email}, @@ -68,14 +70,15 @@ def lookup_customer_id(self, email): if not info: logger.debug("request to %s did not return any data", self.user_endpoint) return None + + customer_ids = [] for account in info: - if account["accountRelationships"][0]["account"]["type"] == "person": - customer_id = account["accountRelationships"][0]["account"].get("id") - # convert str response from api to int value - if customer_id.isdigit(): - customer_id = int(customer_id) - return customer_id - return None + customer_id = account["accountRelationships"][0]["account"].get("id") + # convert str response from api to int value + if customer_id.isdigit(): + customer_id = int(customer_id) + customer_ids.append(customer_id) + return customer_ids class RedHatSubscriptionApi(object): @@ -326,11 +329,11 @@ class FakeUserApi(RedHatUserApi): def lookup_customer_id(self, email): if email == TEST_USER["email"]: - return TEST_USER["account_number"] + return [TEST_USER["account_number"]] if email == FREE_USER["email"]: - return FREE_USER["account_number"] + return [FREE_USER["account_number"]] if email == STRIPE_USER["email"]: - return STRIPE_USER["account_number"] + return [STRIPE_USER["account_number"]] return None diff --git a/util/test/test_marketplace.py b/util/test/test_marketplace.py index aee9b0ba47..ce137f4680 100644 --- a/util/test/test_marketplace.py +++ b/util/test/test_marketplace.py @@ -53,48 +53,6 @@ }, ] -mocked_organization_only_response = [ - { - "id": "12345678", - "accountRelationships": [ - { - "emails": [{"address": "example@example.com", "status": "enabled"}], - "accountId": "fakeid", - "startDate": "2022-09-19T04:18:19.228Z", - "id": "fakeid", - "account": { - "id": "222222222", - "cdhPartyNumber": "11111111", - "ebsAccountNumber": "102030", - "name": "Red Hat", - "displayName": "Red Hat", - "status": "enabled", - "type": "organization", - }, - } - ], - }, - { - "id": "87654321", - "accountRelationships": [ - { - "emails": [{"address": "example@example.com", "status": "enabled"}], - "accountId": "fakeid", - "startDate": "2022-09-20T14:31:09.974Z", - "id": "fakeid", - "account": { - "id": "fakeid", - "cdhPartyNumber": "0000000", - "ebsAccountNumber": "1234567", - "name": "Test Org", - "status": "enabled", - "type": "organization", - }, - } - ], - }, -] - mocked_subscription_response = [ { "id": 1, @@ -219,11 +177,7 @@ def test_user_lookup(self, requests_mock): requests_mock.return_value.content = json.dumps(mocked_user_service_response) customer_id = user_api.lookup_customer_id("example@example.com") - assert customer_id == 000000000 - - requests_mock.return_value.content = json.dumps(mocked_organization_only_response) - customer_id = user_api.lookup_customer_id("example@example.com") - assert customer_id is None + assert customer_id == [222222222, 00000000] @patch("requests.request") def test_subscription_lookup(self, requests_mock): diff --git a/workers/reconciliationworker.py b/workers/reconciliationworker.py index e7fc1a9ec8..da83d14d49 100644 --- a/workers/reconciliationworker.py +++ b/workers/reconciliationworker.py @@ -53,33 +53,34 @@ def _perform_reconciliation(self, user_api, marketplace_api): ) # check against user api - customer_id = user_api.lookup_customer_id(email) - logger.debug("Found %s number for %s", str(customer_id), email) - - if model_customer_id is None and customer_id: - logger.debug("Saving new customer id %s for %s", customer_id, user.username) - entitlements.save_web_customer_id(user, customer_id) - elif model_customer_id != customer_id: - # what is in the database differs from the service - # take the service and store in the database instead - if customer_id: - logger.debug( - "Reconciled differing ids for %s, changing from %s to %s", - user.username, - model_customer_id, - customer_id, - ) - entitlements.update_web_customer_id(user, customer_id) - else: + customer_ids = user_api.lookup_customer_id(email) + if customer_ids is None: + logger.debug("No web customer ids found for %s", email) + if model_customer_id: # user does not have a web customer id from api and should be removed from table logger.debug( "Removing conflicting id %s for %s", model_customer_id, user.username ) entitlements.remove_web_customer_id(user, model_customer_id) - elif customer_id is None: - logger.debug("User %s does not have an account number", user.username) continue + logger.debug("Found %s number for %s", str(customer_ids), email) + + for customer_id in customer_ids: + if model_customer_id is None and customer_id: + logger.debug("Saving new customer id %s for %s", customer_id, user.username) + entitlements.save_web_customer_id(user, customer_id) + elif model_customer_id != customer_id: + # what is in the database differs from the service + # take the service and store in the database instead + logger.debug( + "Reconciled differing ids for %s, changing from %s to %s", + user.username, + model_customer_id, + customer_ids, + ) + entitlements.update_web_customer_id(user, customer_id) + # check if we need to create a subscription for customer in RH marketplace try: stripe_customer = stripe.Customer.retrieve(user.stripe_id) @@ -95,11 +96,12 @@ def _perform_reconciliation(self, user_api, marketplace_api): if plan is None: continue if plan.get("rh_sku") == sku_id: - subscription = marketplace_api.lookup_subscription(customer_id, sku_id) - if subscription is None: - logger.debug("Found %s to create for %s", sku_id, user.username) - marketplace_api.create_entitlement(customer_id, sku_id) - break + for customer_id in customer_ids: + subscription = marketplace_api.lookup_subscription(customer_id, sku_id) + if subscription is None: + logger.debug("Found %s to create for %s", sku_id, user.username) + marketplace_api.create_entitlement(customer_id, sku_id) + break else: logger.debug("User %s does not have a stripe subscription", user.username) diff --git a/workers/test/test_reconciliationworker.py b/workers/test/test_reconciliationworker.py index 6605f11a53..711131a7f1 100644 --- a/workers/test/test_reconciliationworker.py +++ b/workers/test/test_reconciliationworker.py @@ -55,7 +55,7 @@ def test_reconcile_different_ids(initialized_db): new_id = model.entitlements.get_web_customer_id(test_user.id) assert new_id != 12345 - assert new_id == marketplace_users.lookup_customer_id(test_user.email) + assert [new_id] == marketplace_users.lookup_customer_id(test_user.email) # make sure it will remove account numbers from db that do not belong with patch.object(marketplace_users, "lookup_customer_id") as mock: