Skip to content

Commit

Permalink
Merge branch 'main' into huniafatima/deprecate-edx-sphinx-theme
Browse files Browse the repository at this point in the history
  • Loading branch information
huniafatima-arbi authored Nov 18, 2024
2 parents 3b43934 + f845460 commit a660f61
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
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 @@ 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]:
Expand All @@ -256,15 +257,15 @@ 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()
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 @@ 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:
Expand All @@ -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())
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["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"]
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

0 comments on commit a660f61

Please sign in to comment.