Skip to content

Commit

Permalink
chore: get customer_id only if not passed as query params
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Noyan Aziz authored and Muhammad Noyan Aziz committed Nov 14, 2024
1 parent 3f583c0 commit 9640ace
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
61 changes: 56 additions & 5 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import decimal
import logging
from typing import Generic, List, Optional, Tuple, TypeVar, Union
from types import SimpleNamespace

import requests
import stripe
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,20 +288,25 @@ 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:
edx_lms_user_id (object):
offset:
limit:
"""
<<<<<<< Updated upstream
start_time = datetime.datetime.now()
logger.info(
"[UserOrdersView] For CT orders get customer id from lms id call started at %s",
start_time
)
=======
<<<<<<< Updated upstream
>>>>>>> Stashed changes
customer = self.get_customer_by_lms_user_id(edx_lms_user_id)
end_time = datetime.datetime.now()
logger.info(
Expand All @@ -315,9 +321,54 @@ def get_orders_for_customer(self, edx_lms_user_id: int, offset=0,
logger.info("[UserOrdersView] Get CT orders call started at %s",
start_time)
orders = self.get_orders(customer, offset, limit)
<<<<<<< Updated upstream
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())
=======
=======
start_time = datetime.datetime.now()
logger.info(
"[UserOrdersView] For CT orders get customer id from lms id call started at %s",
start_time
)
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
logger.info(f"Found customer with 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()
)

start_time = datetime.datetime.now()
logger.info("[UserOrdersView] Get CT orders call started at %s",
start_time)
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())
>>>>>>> Stashed changes
>>>>>>> Stashed changes

return orders, customer

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"],
username=params["username"],
edx_lms_user_id=params["edx_lms_user_id"],
limit=params["page_size"],
offset=params["page"] * params["page_size"]
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 9640ace

Please sign in to comment.