Skip to content

Commit

Permalink
Merge branch 'main' into bilalqamar95/dockerfile-setup-removal
Browse files Browse the repository at this point in the history
  • Loading branch information
UsamaSadiq authored Dec 5, 2024
2 parents 14601b2 + 0522131 commit d0e086f
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 9 deletions.
10 changes: 10 additions & 0 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,14 @@ def get_customer_by_lms_user_id(self, lms_user_id: int) -> Optional[CTCustomer]:

edx_lms_user_id_key = EdXFieldNames.LMS_USER_ID

start_time = datetime.datetime.now()
results = self.base_client.customers.query(
where=f'custom(fields({edx_lms_user_id_key}=:id))',
limit=2,
predicate_var={'id': f"{lms_user_id}"}
)
duration = (datetime.datetime.now() - start_time).total_seconds()
logger.info(f"[Performance Check] - customerId query took {duration} seconds")

if results.count > 1:
# We are unable due to CT Limitations to enforce unique LMS ID values on Customers on the catalog side, so
Expand Down Expand Up @@ -260,6 +263,7 @@ def get_orders(self, customer_id: str, offset=0,
f"customer with ID {customer_id}")
order_where_clause = f"orderState=\"{order_state}\""

start_time = datetime.datetime.now()
values = self.base_client.orders.query(
where=["customerId=:cid", order_where_clause],
predicate_var={'cid': customer_id},
Expand All @@ -268,6 +272,8 @@ def get_orders(self, customer_id: str, offset=0,
offset=offset,
expand=list(expand)
)
duration = (datetime.datetime.now() - start_time).total_seconds()
logger.info(f"[Performance Check] get_orders call took {duration} seconds")

return PaginatedResult(values.results, values.total, values.offset)

Expand Down Expand Up @@ -327,7 +333,11 @@ def get_product_variant_by_course_run(self, cr_id: str) -> Optional[CTProductVar
Args:
cr_id: variant course run key
"""
start_time = datetime.datetime.now()
results = self.base_client.product_projections.search(False, filter=f"variants.sku:\"{cr_id}\"").results
duration = (datetime.datetime.now() - start_time).total_seconds()
logger.info(
f"[Performance Check] get_product_variant_by_course_run took {duration} seconds")

if len(results) < 1: # pragma no cover
return None
Expand Down
7 changes: 7 additions & 0 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Commercetools filter pipelines
"""
import decimal
from datetime import datetime
from logging import getLogger

import attrs
Expand Down Expand Up @@ -102,7 +103,10 @@ def run_filter(self, active_order_management_system, order_number, **kwargs): #

try:
ct_api_client = CommercetoolsAPIClient()
start_time = datetime.now()
ct_order = ct_api_client.get_order_by_number(order_number=order_number)
duration = (datetime.now() - start_time).total_seconds()
log.info(f"[Performance Check] get_order_by_number call took {duration} seconds")

ret_val = {
"order_data": ct_order,
Expand Down Expand Up @@ -155,7 +159,10 @@ def run_filter(self, active_order_management_system, order_id, **kwargs): # pyl

try:
ct_api_client = CommercetoolsAPIClient()
start_time = datetime.now()
ct_order = ct_api_client.get_order_by_id(order_id=order_id)
duration = (datetime.now() - start_time).total_seconds()
log.info(f"[Performance Check] get_order_by_id call took {duration} seconds")

ret_val = {
"order_data": ct_order,
Expand Down
3 changes: 3 additions & 0 deletions commerce_coordinator/apps/commercetools/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class OrderFulfillViewInputSerializer(CoordinatorSerializer):
line_item_state_id = serializers.CharField(allow_null=False)
edx_lms_user_id = serializers.IntegerField(allow_null=False)
message_id = serializers.CharField(allow_null=False)
course_title = serializers.CharField(allow_null=False)
user_first_name = serializers.CharField(allow_null=False)
user_email = serializers.EmailField(allow_null=False)


class OrderReturnedViewMessageLineItemReturnItemSerializer(CoordinatorSerializer):
Expand Down
10 changes: 9 additions & 1 deletion commerce_coordinator/apps/commercetools/sub_messages/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Commercetools Subscription Message tasks (Celery)
"""
from datetime import datetime

from celery import shared_task
from celery.utils.log import get_task_logger
from commercetools import CommercetoolsError
Expand Down Expand Up @@ -59,7 +61,10 @@ def fulfill_order_placed_message_signal_task(
client = CommercetoolsAPIClient()

try:
start_time = datetime.now()
order = client.get_order_by_id(order_id)
duration = (datetime.now() - start_time).total_seconds()
logger.info(f"[Performance Check] get_order_by_id call took {duration} seconds")
except CommercetoolsError as err: # pragma no cover
logger.error(f'[CT-{tag}] Order not found: {order_id} with CT error {err}, {err.errors},'
f'message id: {message_id}')
Expand Down Expand Up @@ -120,7 +125,10 @@ def fulfill_order_placed_message_signal_task(
'course_mode': get_course_mode_from_ct_order(item),
'item_quantity': item.quantity,
'line_item_state_id': line_item_state_id,
'message_id': message_id
'message_id': message_id,
'user_first_name': customer.first_name,
'user_email': customer.email,
'course_title': item.name.get('en-US', '')
})

# the following throws and thus doesn't need to be a conditional
Expand Down
1 change: 1 addition & 0 deletions commerce_coordinator/apps/commercetools/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def gen_example_customer() -> CTCustomer:

def gen_customer(email: str, un: str):
return CTCustomer(
first_name='John',
email=email,
custom=CTCustomFields(
type=CTTypeReference(
Expand Down
55 changes: 55 additions & 0 deletions commerce_coordinator/apps/commercetools/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
has_full_refund_transaction,
has_refund_transaction,
send_order_confirmation_email,
send_unsupported_mode_fulfillment_error_email,
translate_stripe_refund_status_to_transaction_status
)

Expand Down Expand Up @@ -126,6 +127,60 @@ def test_send_order_confirmation_email_failure(self, mock_logger, mock_get_braze
mock_logger.assert_called_once_with('Encountered exception sending Order confirmation email. '
'Exception: Error sending Braze email')

@override_settings(
BRAZE_API_KEY="braze_api_key",
BRAZE_API_SERVER="braze_api_server",
BRAZE_CT_FULFILLMENT_UNSUPPORTED_MODE_ERROR_CANVAS_ID="dummy_canvas"
)
@patch('commerce_coordinator.apps.commercetools.utils.get_braze_client')
def test_send_unsupported_mode_fulfillment_error_email_success(self, mock_get_braze_client):
mock_braze_client = Mock()
mock_get_braze_client.return_value = mock_braze_client

canvas_entry_properties = {}
lms_user_id = 'user123'
lms_user_email = '[email protected]'

with patch.object(mock_braze_client, 'send_canvas_message') as mock_send_canvas_message:
send_unsupported_mode_fulfillment_error_email(
lms_user_id, lms_user_email, canvas_entry_properties
)

mock_send_canvas_message.assert_called_once_with(
canvas_id='dummy_canvas',
recipients=[{"external_user_id": lms_user_id, "attributes": {"email": lms_user_email}}],
canvas_entry_properties=canvas_entry_properties,
)

@override_settings(
BRAZE_API_KEY="braze_api_key",
BRAZE_API_SERVER="braze_api_server",
BRAZE_CT_FULFILLMENT_UNSUPPORTED_MODE_ERROR_CANVAS_ID="dummy_canvas"
)
@patch('commerce_coordinator.apps.commercetools.utils.get_braze_client')
@patch('commerce_coordinator.apps.commercetools.utils.logger.exception')
def test_send_unsupported_mode_fulfillment_error_email_failure(self, mock_logger, mock_get_braze_client):
mock_braze_client = Mock()
mock_get_braze_client.return_value = mock_braze_client

canvas_entry_properties = {}
lms_user_id = 'user123'
lms_user_email = '[email protected]'

with patch.object(mock_braze_client, 'send_canvas_message') as mock_send_canvas_message:
mock_send_canvas_message.side_effect = Exception('Error sending Braze email')
send_unsupported_mode_fulfillment_error_email(
lms_user_id, lms_user_email, canvas_entry_properties
)

mock_send_canvas_message.assert_called_once_with(
canvas_id='dummy_canvas',
recipients=[{"external_user_id": lms_user_id, "attributes": {"email": lms_user_email}}],
canvas_entry_properties=canvas_entry_properties,
)
mock_logger.assert_called_once_with('Encountered exception sending Fulfillment unsupported mode error '
'email. Exception: Error sending Braze email')

def test_extract_ct_product_information_for_braze_canvas(self):
order = gen_order(EXAMPLE_FULFILLMENT_SIGNAL_PAYLOAD['order_number'])
line_item = order.line_items[0]
Expand Down
21 changes: 21 additions & 0 deletions commerce_coordinator/apps/commercetools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ def send_order_confirmation_email(
logger.exception(f"Encountered exception sending Order confirmation email. Exception: {exc}")


def send_unsupported_mode_fulfillment_error_email(
lms_user_id, lms_user_email, canvas_entry_properties
):
""" Sends fulfillment error email via Braze. """
recipients = [{"external_user_id": lms_user_id, "attributes": {
"email": lms_user_email,
}}]
canvas_id = settings.BRAZE_CT_FULFILLMENT_UNSUPPORTED_MODE_ERROR_CANVAS_ID

try:
braze_client = get_braze_client()
if braze_client:
braze_client.send_canvas_message(
canvas_id=canvas_id,
recipients=recipients,
canvas_entry_properties=canvas_entry_properties,
)
except Exception as exc: # pylint: disable=broad-exception-caught
logger.exception(f"Encountered exception sending Fulfillment unsupported mode error email. Exception: {exc}")


def format_amount_for_braze_canvas(centAmount):
"""
Utility to convert amount to dollar with 2 decimals percision. Also adds the Dollar signs to resulting value.
Expand Down
5 changes: 4 additions & 1 deletion commerce_coordinator/apps/lms/signal_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def fulfill_order_placed_send_enroll_in_course(**kwargs):
line_item_id=kwargs['line_item_id'],
item_quantity=kwargs['item_quantity'],
line_item_state_id=kwargs['line_item_state_id'],
message_id=kwargs['message_id']
message_id=kwargs['message_id'],
user_first_name=kwargs['user_first_name'],
user_email=kwargs['user_email'],
course_title=kwargs['course_title']
)
return async_result.id
67 changes: 64 additions & 3 deletions commerce_coordinator/apps/lms/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,76 @@
LMS Celery tasks
"""

from celery import shared_task
import json
from datetime import datetime

from celery import Task, shared_task
from celery.utils.log import get_task_logger
from django.contrib.auth import get_user_model
from requests import RequestException

from commerce_coordinator.apps.commercetools.catalog_info.constants import TwoUKeys
from commerce_coordinator.apps.commercetools.clients import CommercetoolsAPIClient
from commerce_coordinator.apps.commercetools.utils import send_unsupported_mode_fulfillment_error_email
from commerce_coordinator.apps.lms.clients import LMSAPIClient

# Use the special Celery logger for our tasks
logger = get_task_logger(__name__)
User = get_user_model()


@shared_task(bind=True, autoretry_for=(RequestException,), retry_kwargs={'max_retries': 5, 'countdown': 3})
class CourseEnrollTaskAfterReturn(Task): # pylint: disable=abstract-method
"""
Base class for fulfill_order_placed_send_enroll_in_course_task
"""

def on_failure(self, exc, task_id, args, kwargs, einfo):
edx_lms_user_id = kwargs.get('edx_lms_user_id')
user_email = kwargs.get('user_email')
order_number = kwargs.get('order_number')
user_first_name = kwargs.get('user_first_name')
course_title = kwargs.get('course_title')

error_message = (
json.loads(exc.response.text).get('message', '')
if isinstance(exc, RequestException) and exc.response is not None
else str(exc)
)

logger.error(
f"Post-purchase fulfillment task {self.name} failed after max "
f"retries with the error message: {error_message} "
f"for user with user Id: {edx_lms_user_id}, email: {user_email}, "
f"order number: {order_number}, and course title: {course_title}"
)

# This error is returned from LMS if the course mode is unsupported
# https://github.com/openedx/edx-platform/blob/master/openedx/core/djangoapps/enrollments/views.py#L870
course_mode_expired_error = "course mode is expired or otherwise unavailable for course run"

if course_mode_expired_error in error_message:
logger.info(
f"Sending unsupported course mode fulfillment error email "
f"for the user with user ID: {edx_lms_user_id}, email: {user_email}, "
f"order number: {order_number}, and course title: {course_title}"
)

canvas_entry_properties = {
'order_number': order_number,
'product_type': 'course', # TODO: Fetch product type from commercetools product object
'product_name': course_title,
'first_name': user_first_name,
}
# Send failure notification email
send_unsupported_mode_fulfillment_error_email(edx_lms_user_id, user_email, canvas_entry_properties)


@shared_task(
bind=True,
autoretry_for=(RequestException,),
retry_kwargs={'max_retries': 5, 'countdown': 3},
base=CourseEnrollTaskAfterReturn,
)
def fulfill_order_placed_send_enroll_in_course_task(
self,
course_id,
Expand All @@ -32,7 +87,10 @@ def fulfill_order_placed_send_enroll_in_course_task(
line_item_id,
item_quantity,
line_item_state_id,
message_id
message_id,
user_first_name, # pylint: disable=unused-argument
user_email, # pylint: disable=unused-argument
course_title # pylint: disable=unused-argument
):
"""
Celery task for order placed fulfillment and enrollment via LMS Enrollment API.
Expand Down Expand Up @@ -92,7 +150,10 @@ def fulfill_order_placed_send_enroll_in_course_task(
client = CommercetoolsAPIClient()
# A retry means the current line item state on the order would be a failure state
line_item_state_id = client.get_state_by_key(TwoUKeys.FAILURE_FULFILMENT_STATE).id
start_time = datetime.now()
order_version = client.get_order_by_id(order_id).version
duration = (datetime.now() - start_time).total_seconds()
logger.info(f"[Performance Check] get_order_by_id call took {duration} seconds")

line_item_state_payload = {
'order_id': order_id,
Expand Down
5 changes: 4 additions & 1 deletion commerce_coordinator/apps/lms/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
'line_item_id': '822d77c4-00a6-4fb9-909b-094ef0b8c4b9',
'item_quantity': 1,
'line_item_state_id': '8f2e888e-9777-4557-9a7f-c649153770c2',
'message_id': '1063f19c-08f3-41a4-a952-a8577374373c'
'message_id': '1063f19c-08f3-41a4-a952-a8577374373c',
'user_first_name': 'test',
'user_email': '[email protected]',
'course_title': 'Demonstration Course',
}

EXAMPLE_FULFILLMENT_REQUEST_PAYLOAD = {
Expand Down
5 changes: 4 additions & 1 deletion commerce_coordinator/apps/lms/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class FulfillOrderPlacedSendEnrollInCourseTest(CoordinatorSignalReceiverTestCase
'line_item_id': 11,
'item_quantity': 1,
'line_item_state_id': 12,
'message_id': 13
'message_id': 13,
'user_first_name': 14,
'user_email': 15,
'course_title': 16,
}

def test_correct_arguments_passed(self, mock_task):
Expand Down
Loading

0 comments on commit d0e086f

Please sign in to comment.