"""Logic layer for bulk ingestion emails.""" from datetime import datetime from typing import Any import flask from owsresponse import response, status as ows_status from notifications import config from notifications.connectors import ses from notifications.constants.email import BULK_INGESTION_BCC, QA_BCC from notifications.logic import branding from notifications.logic.branding import BrandParams from notifications.models import ows_users def _get_bulk_ingestion_bcc() -> list[str]: """Get the appropriate BCC list based on the current environment.""" return {config.DEV_ENVIRONMENT: [], config.QA_ENVIRONMENT: [QA_BCC]}.get( config.ENVIRONMENT, [BULK_INGESTION_BCC] ) def _get_template_parameters( *, brand_params: BrandParams, payload: dict[str, Any] ) -> dict[str, str | None]: return { 'default_brand': brand_params.brand, 'brand_domain': brand_params.domain, 'content_app_url': brand_params.content_app_url, 'workstation_sidecar_url': f'https://workstation.{brand_params.domain}/catalog?showBulkSessions=true', **payload, 'completed_on': datetime.fromisoformat(payload['completed_on']).strftime( '%B %-d, %Y at %-I:%M%p %Z' ), } def bulk_ingest_succeeded(payload: dict[str, Any]) -> response.Response: """Send an email notifying that a bulk ingest operation was successful. Args: payload (BulkIngestSuccess): The payload containing the identity ID and bulk session ID. Returns: response.Response: A response indicating the success of the operation. """ identity_response = ows_users.get_identity(payload['identity_id']) if not identity_response or not identity_response.message: return identity_response identity = identity_response.message brand_params = branding.get_brand_params(identity) body = flask.render_template( 'emails/bulk_ingestion/ingestion_success.jinja', **_get_template_parameters(brand_params=brand_params, payload=payload), ) # Override bcc values for dev and qa bcc = _get_bulk_ingestion_bcc() ses.send_html_email( subject=f'Bulk Create Successful - {payload["total_products"]} Products created for {payload["vendor_name"]}', # noqa: E501 body=body, recipient=identity['email'], bcc=bcc, ) return response.Response(message=body, status=ows_status.ACCEPTED) def bulk_ingest_failed(payload: dict[str, Any]) -> response.Response: """Send an email notifying that a bulk ingest operation failed. Args: payload (BulkIngestFailure): The payload containing the identity ID and bulk session ID. Returns: response.Response: A response indicating the success of the operation. """ identity_response = ows_users.get_identity(payload['identity_id']) if not identity_response or not identity_response.message: return identity_response identity = identity_response.message brand_params = branding.get_brand_params(identity) submissions_failed = payload.get('submissions_failed', False) body = flask.render_template( 'emails/bulk_ingestion/ingestion_failure.jinja' if not submissions_failed else 'emails/bulk_ingestion/ingestion_success.jinja', **_get_template_parameters(brand_params=brand_params, payload=payload), ) subject = ( 'Bulk Create Unsuccessful' if not submissions_failed else f'Bulk Create Successful - {payload["total_products"]} Products created for {payload["vendor_name"]}' # noqa: E501 ) # Override bcc values for dev and qa bcc = _get_bulk_ingestion_bcc() ses.send_html_email(subject=subject, body=body, recipient=identity['email'], bcc=bcc) return response.Response(message=body, status=ows_status.ACCEPTED)