"""Logic layer for physical delivery email reporting.""" import flask from owsresponse import response, status as ows_status from notifications.config import ENVIRONMENT, PROD_ENVIRONMENT from notifications.connectors import ses from notifications.constants.email import ( PHYS_DELIVERY_ORDER_COMPLETED_SUBJECT, PHYS_DELIVERY_ORDER_COMPLETED_TEMPLATE, PHYS_DELIVERY_ORDER_FAILED_SUBJECT, PHYS_DELIVERY_ORDER_FAILED_TEMPLATE, ) from notifications.models import graphql_router def order_completed(order_id: str) -> response.Response: """Get order details and email the user who requested this delivery order. Args: order_id (str): Physical Delivery Order Id. """ order_details = graphql_router.phys_delivery_order_by_id(order_id) recipient = order_details['createdBy']['email'] distribution_url = ( 'https://distribution.theorchard.com/' if ENVIRONMENT == PROD_ENVIRONMENT else 'https://distribution.qaorch.com/' ) text_body = flask.render_template( PHYS_DELIVERY_ORDER_COMPLETED_TEMPLATE, **{ 'subject': PHYS_DELIVERY_ORDER_COMPLETED_SUBJECT, 'orderId': order_id, 'orderUrl': f'{distribution_url}monitor/physical/audio?orderId={order_id}', 'iconsBaseUrl': 'https://cdn.theorchard.io/web-icons/', }, ) ses.send_html_email( subject=PHYS_DELIVERY_ORDER_COMPLETED_SUBJECT, body=text_body, recipient=recipient ) return response.Response(status=ows_status.ACCEPTED) def order_failed(order_id: str) -> response.Response: """Get order details and email the user who requested this delivery order. Args: order_id (str): Physical Delivery Order Id. """ order_details = graphql_router.phys_delivery_order_by_id(order_id) recipient = order_details['createdBy']['email'] distribution_url = ( 'https://distribution.theorchard.com/' if ENVIRONMENT == PROD_ENVIRONMENT else 'https://distribution.qaorch.com/' ) text_body = flask.render_template( PHYS_DELIVERY_ORDER_FAILED_TEMPLATE, **{ 'subject': PHYS_DELIVERY_ORDER_FAILED_SUBJECT, 'orderId': order_id, 'orderUrl': f'{distribution_url}monitor/physical/audio?orderId={order_id}', 'iconsBaseUrl': 'https://cdn.theorchard.io/web-icons/', }, ) ses.send_html_email( subject=PHYS_DELIVERY_ORDER_FAILED_SUBJECT, body=text_body, recipient=recipient ) return response.Response(status=ows_status.ACCEPTED)