diff --git a/commerce_coordinator/apps/commercetools/serializers.py b/commerce_coordinator/apps/commercetools/serializers.py index fafac844e..5a3775817 100644 --- a/commerce_coordinator/apps/commercetools/serializers.py +++ b/commerce_coordinator/apps/commercetools/serializers.py @@ -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): diff --git a/commerce_coordinator/apps/commercetools/sub_messages/tasks.py b/commerce_coordinator/apps/commercetools/sub_messages/tasks.py index e244ddbd8..bc64c6b13 100644 --- a/commerce_coordinator/apps/commercetools/sub_messages/tasks.py +++ b/commerce_coordinator/apps/commercetools/sub_messages/tasks.py @@ -120,7 +120,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 diff --git a/commerce_coordinator/apps/commercetools/utils.py b/commerce_coordinator/apps/commercetools/utils.py index 8f4f6ca60..49a0b239e 100644 --- a/commerce_coordinator/apps/commercetools/utils.py +++ b/commerce_coordinator/apps/commercetools/utils.py @@ -65,6 +65,27 @@ def send_order_confirmation_email( logger.exception(f"Encountered exception sending Order confirmation email. Exception: {exc}") +def send_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_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 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. diff --git a/commerce_coordinator/apps/lms/signal_handlers.py b/commerce_coordinator/apps/lms/signal_handlers.py index e9cccfef1..da72e3533 100644 --- a/commerce_coordinator/apps/lms/signal_handlers.py +++ b/commerce_coordinator/apps/lms/signal_handlers.py @@ -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 diff --git a/commerce_coordinator/apps/lms/tasks.py b/commerce_coordinator/apps/lms/tasks.py index 5e941fd74..999950c3c 100644 --- a/commerce_coordinator/apps/lms/tasks.py +++ b/commerce_coordinator/apps/lms/tasks.py @@ -1,14 +1,16 @@ """ LMS Celery tasks """ +import json -from celery import shared_task +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_fulfillment_error_email from commerce_coordinator.apps.lms.clients import LMSAPIClient # Use the special Celery logger for our tasks @@ -16,7 +18,48 @@ User = get_user_model() -@shared_task(bind=True, autoretry_for=(RequestException,), retry_kwargs={'max_retries': 5, 'countdown': 3}) +class CourseEnrollTaskAfterReturn(Task): + def on_failure(self, exc, task_id, args, kwargs, einfo): + error_message = json.loads(exc.response.text).get('message', '') + 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') + + logger.error( + f"Task {self.name} failed after max retries with error message: {error_message} " + f"for user with User Id: {edx_lms_user_id}, Email: {user_email}, " + f"Order Number: {order_number}, Course Title: {course_title}" + ) + + if ( + self.request.retries >= self.max_retries + and "course mode is expired or otherwise unavailable for course run" in error_message + ): + + logger.info( + f"Sending Fulfillment Error Email for user with " + f"User ID: {edx_lms_user_id}, Email: {user_email}, " + f"Order Number: {order_number}, Course Title: {course_title}" + ) + + canvas_entry_properties = { + 'order_number': order_number, + 'product_type': 'course', + 'product_name': course_title, + 'first_name': user_first_name, + } + # Send failure notification email + send_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, @@ -32,7 +75,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, + user_email, + course_title ): """ Celery task for order placed fulfillment and enrollment via LMS Enrollment API. diff --git a/commerce_coordinator/settings/base.py b/commerce_coordinator/settings/base.py index 491d659bc..a51d87cbe 100644 --- a/commerce_coordinator/settings/base.py +++ b/commerce_coordinator/settings/base.py @@ -468,6 +468,7 @@ def root(*path_fragments): BRAZE_API_KEY = None BRAZE_API_SERVER = None BRAZE_CT_ORDER_CONFIRMATION_CANVAS_ID = '' +BRAZE_CT_FULFILLMENT_ERROR_CANVAS_ID = '' # SEGMENT WRITE KEY SEGMENT_KEY = None diff --git a/commerce_coordinator/settings/local.py b/commerce_coordinator/settings/local.py index 17bd5c978..6d7803d6e 100644 --- a/commerce_coordinator/settings/local.py +++ b/commerce_coordinator/settings/local.py @@ -7,6 +7,7 @@ 'host.docker.internal', 'localhost', '.ngrok-free.app', + '.share.zrok.io', ) INSTALLED_APPS += (