"""Logic layer for email reporting.""" import flask from flask import g from owsresponse import response, status as ows_status from notifications import config from notifications.connectors import ses from notifications.constants import error from notifications.constants.email import ( CONTENT_REVIEW_ESCALATION_APPROVAL_SUBJECT, CONTENT_REVIEW_ESCALATION_APPROVAL_TEMPLATE, CONTENT_REVIEW_ESCALATION_REJECTION_SUBJECT, CONTENT_REVIEW_ESCALATION_REJECTION_TEMPLATE, CONTENT_REVIEW_FAILURE_NOTIFICATION_SUBJECT, CONTENT_REVIEW_FAILURE_NOTIFICATION_TEMPLATE, CONTENT_REVIEW_QUEUE_MOVE_DE_ESCALATION_SUBJECT, CONTENT_REVIEW_QUEUE_MOVE_DE_ESCALATION_TEMPLATE, CONTENT_REVIEW_QUEUE_MOVE_ESCALATION_SUBJECT, CONTENT_REVIEW_QUEUE_MOVE_ESCALATION_TEMPLATE, CONTENT_REVIEW_SENDER, ) from notifications.models import ows_users def report_product_review_failure( *, identity_id: str, upc: str, product_name: str ) -> response.Response: """Email the user that their review of a product in content review has failed. Kwargs: - identity_id (str): Identity Id of the user to notify. - upc (str): UPC of product. - product_name (str): Name of product. """ identity_response = ows_users.get_identity(identity_id) if not identity_response or not identity_response.message: return identity_response identity = identity_response.message cr_support_email = 'contentreview@sonymusic-pde.com' product_support_email = 'product-support@sonymusic-pde.com' text_body = flask.render_template( CONTENT_REVIEW_FAILURE_NOTIFICATION_TEMPLATE, **{ 'upc': upc, 'product_name': product_name, 'contact': cr_support_email, }, ) cc_recipients = None if config.ENVIRONMENT == config.PROD_ENVIRONMENT: cc_recipients = [cr_support_email, product_support_email] ses.send_email( subject=CONTENT_REVIEW_FAILURE_NOTIFICATION_SUBJECT.format(upc=upc), text_body=text_body, recipient=identity['email'], cc_recipients=cc_recipients, source=CONTENT_REVIEW_SENDER, ) return response.Response(status=ows_status.ACCEPTED) def review_escalation_completed( # noqa: PLR0913 *, identity_id: str, upc: str, resolution: str, notes: str | None = None, escalation_type: str | None = None, escalation_note: str | None = None, ) -> response.Response: """Email the user that an escalated product has been approved or rejected in content review. Kwargs: - identity_id (str): Identity Id of the user to notify. - upc (str): UPC of product. - resolution (str): Resolution of product (approve/ reject). - notes (str | None): Optional decision notes from the final decision maker. - escalation_type (str | None): Optional escalation type from the original escalation. - escalation_note (str | None): Optional note from the original escalation. """ if config.CONTENT_REVIEW_EMAIL_OVERRIDE is None: identity_response = ows_users.get_identity(identity_id) if not identity_response or not identity_response.message: return identity_response email_recipient = identity_response.message['email'] else: email_recipient = config.CONTENT_REVIEW_EMAIL_OVERRIDE if resolution == 'approve': email_template_source = CONTENT_REVIEW_ESCALATION_APPROVAL_TEMPLATE email_subject = CONTENT_REVIEW_ESCALATION_APPROVAL_SUBJECT.format(upc=upc) elif resolution == 'reject': email_template_source = CONTENT_REVIEW_ESCALATION_REJECTION_TEMPLATE email_subject = CONTENT_REVIEW_ESCALATION_REJECTION_SUBJECT.format(upc=upc) else: return response.create_error_response( error.ERROR_CODE_VALIDATION_ERROR, 'Invalid resolution, neither approve nor reject' ) text_body = flask.render_template( email_template_source, **{ 'upc': upc, 'notes': notes, 'escalation_type': escalation_type, 'escalation_note': escalation_note, }, ) ses.send_html_email( subject=email_subject, body=text_body, recipient=email_recipient, source=CONTENT_REVIEW_SENDER, ) g.log.info( f'Notification sent to {email_recipient} for ' f'completed review of escalated product (UPC {upc})' ) return response.Response(status=ows_status.ACCEPTED) def escalate( # noqa: PLR0913 *, moved_to_target_email: str, moved_by_user_name: str, upc: str, product_name: str, label_id: int, label_name: str, review_queue_id: int, target_group: str, move_note: str, escalation_type: str | None, primary_artist: str | None, sale_start_date: str | None, ) -> response.Response: """Email the escalation target user for this queue move. Kwargs: - moved_to_target_email (str): Email to notify. - moved_by_user_name (str): Name of movedBy user. - upc (str): UPC of product. - product_name (str): Name of product. - label_id (int): Label ID. - label_name (str): Name of label. - review_queue_id (int): ID of review queue item. - target_group (str): Escalation target group. - move_note(str): Move note. - escalation_type (str): Escalation type name. - primary_artist (str): Primary Artist. - sale_start_date (str): Sales Start Date. """ if config.CONTENT_REVIEW_EMAIL_OVERRIDE is None: email_recipient = moved_to_target_email else: email_recipient = config.CONTENT_REVIEW_EMAIL_OVERRIDE text_body = flask.render_template( CONTENT_REVIEW_QUEUE_MOVE_ESCALATION_TEMPLATE, **{ 'upc': upc, 'product_name': product_name, 'label_id': label_id, 'label_name': label_name, 'moved_by_user_name': moved_by_user_name, 'review_queue_id': review_queue_id, 'support_type': target_group, 'move_note': move_note, 'escalation_type': escalation_type or 'None', 'primary_artist': primary_artist or 'None', 'sale_start_date': sale_start_date or 'None', }, ) ses.send_html_email( subject=CONTENT_REVIEW_QUEUE_MOVE_ESCALATION_SUBJECT.format(upc=upc), body=text_body, recipient=email_recipient, source=CONTENT_REVIEW_SENDER, ) g.log.info( f'Notification sent to move target {email_recipient} for escalation of product (UPC {upc})' ) return response.Response(status=ows_status.ACCEPTED) def de_escalate( # noqa: PLR0913 *, moved_to_target_email: str, upc: str, review_queue_id: int, product_name: str, label_id: int, label_name: str, move_note: str, escalation_type: str | None, primary_artist: str | None, sale_start_date: str | None, ) -> response.Response: """Email the de-escalation target user for this queue move. Kwargs: - moved_to_target_email (str): Email to notify. - upc (str): UPC of product. - review_queue_id (int): ID of review queue item. - product_name (str): Name of product. - label_id (int): Label ID. - label_name (str): Name of label. - move_note (str): Move note. - escalation_type (str): Escalation type name. - primary_artist (str): Primary Artist. - sale_start_date (str): Sales Start Date. """ if config.CONTENT_REVIEW_EMAIL_OVERRIDE is None: email_recipient = moved_to_target_email else: email_recipient = config.CONTENT_REVIEW_EMAIL_OVERRIDE text_body = flask.render_template( CONTENT_REVIEW_QUEUE_MOVE_DE_ESCALATION_TEMPLATE, **{ 'upc': upc, 'review_queue_id': review_queue_id, 'product_name': product_name, 'label_id': label_id, 'label_name': label_name, 'move_note': move_note, 'escalation_type': escalation_type or 'None', 'primary_artist': primary_artist or 'None', 'sale_start_date': sale_start_date or 'None', }, ) ses.send_html_email( subject=CONTENT_REVIEW_QUEUE_MOVE_DE_ESCALATION_SUBJECT.format(upc=upc), body=text_body, recipient=email_recipient, source=CONTENT_REVIEW_SENDER, ) g.log.info( f'Notification sent to move target {email_recipient} for ' f'de-escalation of product (UPC {upc})' ) return response.Response(status=ows_status.ACCEPTED)