From f84546066bf7a955d894d85bbc5c888f2a1fbbf0 Mon Sep 17 00:00:00 2001 From: NoyanAziz Date: Mon, 18 Nov 2024 07:58:48 +0500 Subject: [PATCH] chore: get customer_id from CT only if not passed in params (#294) --- .../apps/commercetools/clients.py | 40 ++++++++++++++----- .../apps/commercetools/pipeline.py | 3 ++ .../apps/commercetools/tests/_test_cases.py | 2 +- .../apps/commercetools/tests/test_pipeline.py | 3 ++ .../apps/frontend_app_ecommerce/views.py | 2 + 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 5e7047a1..0ceb13e2 100644 --- a/commerce_coordinator/apps/commercetools/clients.py +++ b/commerce_coordinator/apps/commercetools/clients.py @@ -5,6 +5,7 @@ import datetime import decimal import logging +from types import SimpleNamespace from typing import Generic, List, Optional, Tuple, TypeVar, Union import requests @@ -235,7 +236,7 @@ def get_order_by_number(self, order_number: str, expand: ExpandList = DEFAULT_OR logger.info(f"[CommercetoolsAPIClient] - Attempting to find order with number {order_number}") return self.base_client.orders.get_by_order_number(order_number, expand=list(expand)) - def get_orders(self, customer: CTCustomer, offset=0, + def get_orders(self, customer_id: str, offset=0, limit=ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT, expand: ExpandList = DEFAULT_ORDER_EXPANSION, order_state="Complete") -> PaginatedResult[CTOrder]: @@ -256,7 +257,7 @@ def get_orders(self, customer: CTCustomer, offset=0, """ logger.info(f"[CommercetoolsAPIClient] - Attempting to find all completed orders for " - f"customer with ID {customer.id}") + f"customer with ID {customer_id}") order_where_clause = f"orderState=\"{order_state}\"" start_time = datetime.datetime.now() @@ -264,7 +265,7 @@ def get_orders(self, customer: CTCustomer, offset=0, "[UserOrdersView] Get CT orders query call started at %s", start_time) values = self.base_client.orders.query( where=["customerId=:cid", order_where_clause], - predicate_var={'cid': customer.id}, + predicate_var={'cid': customer_id}, sort=["completedAt desc", "lastModifiedAt desc"], limit=limit, offset=offset, @@ -287,8 +288,9 @@ def get_orders(self, customer: CTCustomer, offset=0, return result - def get_orders_for_customer(self, edx_lms_user_id: int, offset=0, - limit=ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT) -> (PaginatedResult[CTOrder], CTCustomer): + def get_orders_for_customer(self, edx_lms_user_id: int, offset=0, limit=ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT, + customer_id=None, email=None, + username=None) -> (PaginatedResult[CTOrder], CTCustomer): """ Args: @@ -301,20 +303,38 @@ def get_orders_for_customer(self, edx_lms_user_id: int, offset=0, "[UserOrdersView] For CT orders get customer id from lms id call started at %s", start_time ) - customer = self.get_customer_by_lms_user_id(edx_lms_user_id) + + if not customer_id: + customer = self.get_customer_by_lms_user_id(edx_lms_user_id) + + if customer is None: # pragma: no cover + raise ValueError(f'Unable to locate customer with ID #{edx_lms_user_id}') + + customer_id = customer.id + else: + if email is None or username is None: # pragma: no cover + raise ValueError("If customer_id is provided, both email and username must be provided") + + customer = SimpleNamespace( + id=customer_id, + email=email, + custom=SimpleNamespace( + fields={ + EdXFieldNames.LMS_USER_NAME: username + } + ) + ) + end_time = datetime.datetime.now() logger.info( "[UserOrdersView] For CT orders get customer id from lms id call finished at %s with total duration: %ss", end_time, (end_time - start_time).total_seconds() ) - if customer is None: # pragma: no cover - raise ValueError(f'Unable to locate customer with ID #{edx_lms_user_id}') - start_time = datetime.datetime.now() logger.info("[UserOrdersView] Get CT orders call started at %s", start_time) - orders = self.get_orders(customer, offset, limit) + orders = self.get_orders(customer_id, offset, limit) end_time = datetime.datetime.now() logger.info("[UserOrdersView] Get CT orders call finished at %s with total duration: %ss", end_time, (end_time - start_time).total_seconds()) diff --git a/commerce_coordinator/apps/commercetools/pipeline.py b/commerce_coordinator/apps/commercetools/pipeline.py index 09c2ea29..acd65572 100644 --- a/commerce_coordinator/apps/commercetools/pipeline.py +++ b/commerce_coordinator/apps/commercetools/pipeline.py @@ -54,6 +54,9 @@ def run_filter(self, request, params, order_data): # pylint: disable=arguments- try: ct_api_client = CommercetoolsAPIClient() ct_orders = ct_api_client.get_orders_for_customer( + customer_id=params["customer_id"], + email=params["email"], + username=params["username"], edx_lms_user_id=params["edx_lms_user_id"], limit=params["page_size"], offset=params["page"] * params["page_size"] diff --git a/commerce_coordinator/apps/commercetools/tests/_test_cases.py b/commerce_coordinator/apps/commercetools/tests/_test_cases.py index b2f33927..7fcaa377 100644 --- a/commerce_coordinator/apps/commercetools/tests/_test_cases.py +++ b/commerce_coordinator/apps/commercetools/tests/_test_cases.py @@ -41,7 +41,7 @@ def get_orders_for_customer(self): # noinspection PyUnusedLocal # pylint: disable=unused-argument # needed for kwargs def _get_orders_for_customer( - _, edx_lms_user_id: int, offset=0, + _, edx_lms_user_id: int, offset=0, customer_id=None, email=None, username=None, limit=ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT ) -> (PaginatedResult[CTOrder], CTCustomer): return ( diff --git a/commerce_coordinator/apps/commercetools/tests/test_pipeline.py b/commerce_coordinator/apps/commercetools/tests/test_pipeline.py index 8dc6133a..1f02f1de 100644 --- a/commerce_coordinator/apps/commercetools/tests/test_pipeline.py +++ b/commerce_coordinator/apps/commercetools/tests/test_pipeline.py @@ -48,6 +48,9 @@ def test_pipeline(self, is_redirect_mock): request, { "edx_lms_user_id": 127, + "customer_id": None, + "email": "test@example.com", + "username": "test", "page_size": ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT, "page": 0, }, diff --git a/commerce_coordinator/apps/frontend_app_ecommerce/views.py b/commerce_coordinator/apps/frontend_app_ecommerce/views.py index dca74a6f..505dd396 100644 --- a/commerce_coordinator/apps/frontend_app_ecommerce/views.py +++ b/commerce_coordinator/apps/frontend_app_ecommerce/views.py @@ -81,7 +81,9 @@ def get(self, request): user.add_lms_user_id("UserOrdersView GET method") # build parameters params = { + 'customer_id': request.query_params.get('customer_id'), 'username': request.user.username, + 'email': request.user.email, "edx_lms_user_id": request.user.lms_user_id, "page": 0, "page_size": ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT