"""Logic layer for 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 ( NR_OWNERSHIP_DELIVERY_ORDER_COMPLETED_SUBJECT, NR_OWNERSHIP_DELIVERY_ORDER_COMPLETED_TEMPLATE, NR_OWNERSHIP_DELIVERY_ORDER_FAILED_SUBJECT, NR_OWNERSHIP_DELIVERY_ORDER_FAILED_TEMPLATE, ) from notifications.models import graphql_router def order_completed(order_id: str) -> response.Response: """Get ownership delivery order details and email the user who requested this order. Args: order_id (str): Nr Ownership Delivery Order Id. """ order_details = graphql_router.nr_ownership_delivery_order_by_id(order_id) recipient = order_details['createdBy']['email'] output_file_count = order_details['summary']['outputFileCount'] distribution_url = ( 'https://distribution.theorchard.com/' if ENVIRONMENT == PROD_ENVIRONMENT else 'https://distribution.qaorch.com/' ) text_body = flask.render_template( NR_OWNERSHIP_DELIVERY_ORDER_COMPLETED_TEMPLATE, **{ 'subject': NR_OWNERSHIP_DELIVERY_ORDER_COMPLETED_SUBJECT, 'orderId': order_id, 'orderUrl': f'{distribution_url}monitor/sound-recording/ownership?orderId={order_id}', 'iconsBaseUrl': 'https://cdn.theorchard.io/web-icons/', 'outputFileCount': output_file_count, }, ) ses.send_html_email( subject=NR_OWNERSHIP_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 ownership delivery order details and email the user who requested this order. Args: order_id (str): Nr Ownership Delivery Order Id. """ order_details = graphql_router.nr_ownership_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( NR_OWNERSHIP_DELIVERY_ORDER_FAILED_TEMPLATE, **{ 'subject': NR_OWNERSHIP_DELIVERY_ORDER_FAILED_SUBJECT, 'orderId': order_id, 'orderUrl': f'{distribution_url}monitor/sound-recording/ownership?orderId={order_id}', 'iconsBaseUrl': 'https://cdn.theorchard.io/web-icons/', }, ) ses.send_html_email( subject=NR_OWNERSHIP_DELIVERY_ORDER_FAILED_SUBJECT, body=text_body, recipient=recipient ) return response.Response(status=ows_status.ACCEPTED)