Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONIC-740: get customer_id only if not passed in params #294

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -235,7 +236,7 @@
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]:
Expand All @@ -256,15 +257,15 @@

"""
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()
logger.info(
"[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,
Expand All @@ -287,8 +288,9 @@

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:
Expand All @@ -301,20 +303,38 @@
"[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(

Check failure on line 318 in commerce_coordinator/apps/commercetools/clients.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04, 3.8, django42)

Missing coverage

Missing coverage on line 318
abdullahwaheed marked this conversation as resolved.
Show resolved Hide resolved
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())
Expand Down
3 changes: 3 additions & 0 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.get("customer_id"),
email=params["email"],
NoyanAziz marked this conversation as resolved.
Show resolved Hide resolved
username=params["username"],
edx_lms_user_id=params["edx_lms_user_id"],
limit=params["page_size"],
offset=params["page"] * params["page_size"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def test_pipeline(self, is_redirect_mock):
request,
{
"edx_lms_user_id": 127,
"customer_id": None,
"email": "[email protected]",
"username": "test",
"page_size": ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT,
"page": 0,
},
Expand Down
2 changes: 2 additions & 0 deletions commerce_coordinator/apps/frontend_app_ecommerce/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading