"""Logic layer for Fansifter app notifications.""" import flask from owsresponse import response, status as ows_status from notifications.connectors import ses from notifications.constants.email import ( AUDIENCE_AD_REPORTING_DATA_SYNC_COMPLETED_TEMPLATE, AUDIENCE_AUDIENCE_FILE_EXPORTED_TEMPLATE, AUDIENCE_DELETED_FANS_REPORT_TEMPLATE, AUDIENCE_SHOPIFY_STORE_SYNC_COMPLETED_TEMPLATE, ) from notifications.logic.branding import get_brand_params from notifications.models import ows_users def shopify_store_sync_completed( *, identity_id: str, store_id: str, store_domain: str, is_multiartist_store: bool ) -> response.Response: """Email the user that their Shopify store sync has been completed. Args: identity_id (str): Identity Id of the user to notify. store_id (str): Audience Shopify Store Id. store_domain (str): Shopify Store domain. is_multiartist_store (bool): Whether the Shopify store contains products of multiple Artists. """ identity_response = ows_users.get_identity(identity_id) if not identity_response or not identity_response.message: return identity_response identity = identity_response.message brand_params = get_brand_params(identity) domain_components = store_domain.split('.') store_subdomain = domain_components[0] store_domain = '.' + '.'.join(domain_components[1:]) preview_text = ( 'You can now link your artists to your collections' if is_multiartist_store else 'Store data is now available in the Fansifter app' ) store_url = f'/connections?editStore={store_id}' if is_multiartist_store else '/connections' text_body = flask.render_template( AUDIENCE_SHOPIFY_STORE_SYNC_COMPLETED_TEMPLATE, **{ 'default_brand': brand_params.brand, 'brand_domain': brand_params.domain, 'subdomain': 'fansifter', 'preview_text': preview_text, 'store_subdomain': store_subdomain, 'store_domain': store_domain, 'store_url': store_url, 'is_multiartist_store': is_multiartist_store, 'pref_base_url': brand_params.settings_app_url, }, ) ses.send_html_email( subject='Your Shopify store has been synced', body=text_body, recipient=identity['email'], ) return response.Response(status=ows_status.NO_CONTENT) def ad_reporting_data_sync_completed( *, identity_id: str, ad_reporting_platform: str, ad_accounts: list[str] ) -> response.Response: """Email the user that their reporting data sync has been completed. Args: identity_id (str): Identity Id of the user to notify. ad_reporting_platform (str): Ad Reporting platform. ad_accounts (list): list of ad_accounts that was synced. """ identity_response = ows_users.get_identity(identity_id) if not identity_response or not identity_response.message: return identity_response identity = identity_response.message brand_params = get_brand_params(identity) if ad_reporting_platform == 'META': ad_reporting_platform = 'Meta' ad_reporting_platform_icon = 'https://cdn.theorchard.io/assets/icons/stores/meta.png' elif ad_reporting_platform == 'TIKTOK': ad_reporting_platform = 'TikTok' ad_reporting_platform_icon = 'https://cdn.theorchard.io/assets/icons/stores/tikTok.png' else: ad_reporting_platform = 'Google' ad_reporting_platform_icon = 'https://cdn.theorchard.io/assets/icons/stores/google.png' preview_text = f'{ad_reporting_platform} ad reporting data has been synced successfully' ad_reporting_url = '/ad-reporting' text_body = flask.render_template( AUDIENCE_AD_REPORTING_DATA_SYNC_COMPLETED_TEMPLATE, **{ 'default_brand': brand_params.brand, 'brand_domain': brand_params.domain, 'subdomain': 'fansifter', 'preview_text': preview_text, 'ad_reporting_url': ad_reporting_url, 'ad_reporting_platform': ad_reporting_platform, 'ad_reporting_platform_icon': ad_reporting_platform_icon, 'pref_base_url': brand_params.settings_app_url, 'ad_accounts': ad_accounts, }, ) ses.send_html_email( subject=f'Your {ad_reporting_platform} reporting data has been synced', body=text_body, recipient=identity['email'], ) return response.Response(status=ows_status.NO_CONTENT) def audience_file_exported( *, identity_id: str, audience_name: str, filename: str, password: str ) -> response.Response: """Email the user that their audience file has been exported. Args: identity_id (str): Identity ID of the user to notify. audience_name (str): Audience name. filename (str): Audience file name. password (str): Exported file password to un-archive it. """ identity_response = ows_users.get_identity(identity_id) if not identity_response or not identity_response.message: return identity_response identity = identity_response.message brand_params = get_brand_params(identity) preview_text = 'Please find the password for your custom audience below' text_body = flask.render_template( AUDIENCE_AUDIENCE_FILE_EXPORTED_TEMPLATE, **{ 'full_name': identity['name'], 'default_brand': brand_params.brand, 'brand_domain': brand_params.domain, 'preview_text': preview_text, 'audience_name': audience_name, 'filename': filename, 'password': password, 'pref_base_url': brand_params.settings_app_url, }, ) ses.send_html_email( subject='Your Audience file has been exported', body=text_body, recipient=identity['email'], ) return response.Response(status=ows_status.NO_CONTENT) def deleted_fans_report( *, deleted_fans_count: int, date: str, recipients: list[str], bcc_recipients: list[str] | None = None, ) -> response.Response: """Email the CRM team about deleted fans. Args: deleted_fans_count (int): . date (str): Date when fans was deleted. recipients (List[str]): List of recipients. bcc_recipients (List[str]): List of bcc recipients. """ text_body = flask.render_template( AUDIENCE_DELETED_FANS_REPORT_TEMPLATE, **{ 'deleted_fans_count': deleted_fans_count, 'date': date, }, ) ses.send_html_email_bulk( subject='Sony Music Fan Deletes - Fansifter', body=text_body, recipients=recipients, bcc=bcc_recipients, ) return response.Response(status=ows_status.NO_CONTENT)