"""Logic layer for email reporting.""" from typing import Any import flask from owsresponse import response, status as ows_status from notifications.connectors import ses from notifications.constants.email import ( PARTICIPANT_DATA_ISSUE_RECIPIENT, PARTICIPANT_DATA_ISSUE_SUBJECT, PARTICIPANT_DATA_ISSUE_TEMPLATE, SOUND_RECORDING_DATA_ISSUE_RECIPIENT, SOUND_RECORDING_DATA_ISSUE_SUBJECT, SOUND_RECORDING_DATA_ISSUE_TEMPLATE, ) from notifications.models import identity, ows_users MISSING_IDENTITY_ERROR_MESSAGE = 'User not found for identity id {identity_id}' def _report_data_issue( identity_id: str, subject: str, template: str, recipient: str, context: dict[str, Any] ) -> response.Response: """Send an email containing user information based on identity id. Args: identity_id (str): Identity id of user reporting data issue. subject (str): subject of the email to send template (str): name of the email template to use for rendering text body recipient (str): email address for the recipient of the email context (dict): dictionary of additional variables used to render template """ user_response = ows_users.get_identity(identity_id) if not user_response or not user_response.message: return user_response text_body = flask.render_template( template, **{ 'name': user_response.message['name'], 'email': user_response.message['email'], 'identity_id': identity_id, 'referrer': context['referrer'], 'application': { 'name': context['application']['name'], 'version': context['application']['version'], }, 'category': context['category'], 'message': context['message'], **context, }, ) ses.send_email(subject=subject, text_body=text_body, recipient=recipient) return response.Response(status=ows_status.ACCEPTED) def report_participant_data_issue(identity_id: str, payload: dict[str, Any]) -> response.Response: """Send an email for a user-reported issue with participant data. Args: identity_id (str): Identity id of user reporting data issue. payload (dict): JSON payload from request Returns: Response: 202 accepted, 500 internal server error if problems with SES or ows-users. """ default_brand = identity.get_brand_for_identity(identity_id) if default_brand == 'awal': recipient = PARTICIPANT_DATA_ISSUE_RECIPIENT['AWAL'] else: recipient = PARTICIPANT_DATA_ISSUE_RECIPIENT['ORCHARD'] return _report_data_issue( identity_id=identity_id, subject=PARTICIPANT_DATA_ISSUE_SUBJECT.format(name=payload['participant']['name']), template=PARTICIPANT_DATA_ISSUE_TEMPLATE, recipient=recipient, context={ **payload, 'participant': { 'id': payload['participant']['id'], 'name': payload['participant']['name'], 'chartmetric_id': payload['participant']['chartmetric_id'], }, }, ) def report_sound_recording_data_issue( identity_id: str, payload: dict[str, Any] ) -> response.Response: """Send an email for a user-reported issue with sound recording data. Args: identity_id (str): Identity id of user reporting data issue. payload (dict): "JSON payload from request """ default_brand = identity.get_brand_for_identity(identity_id) if default_brand == 'awal': recipient = SOUND_RECORDING_DATA_ISSUE_RECIPIENT['AWAL'] else: recipient = SOUND_RECORDING_DATA_ISSUE_RECIPIENT['ORCHARD'] return _report_data_issue( identity_id, subject=SOUND_RECORDING_DATA_ISSUE_SUBJECT.format( name=payload['sound_recording']['name'], isrc=payload['sound_recording']['isrc'] ), template=SOUND_RECORDING_DATA_ISSUE_TEMPLATE, recipient=recipient, context={ **payload, 'sound_recording': { 'name': payload['sound_recording']['name'], 'isrc': payload['sound_recording']['isrc'], }, 'artist_name': payload['artist_name'], }, )