from typing import Optional from external_api.base.clients.notifications_client import SendEmailSchema, EmailTemplateSchema, EmailContextSchema from notifications.strategies.email_strategy import EmailStrategy from notifications.notifications_center import NotificationsCenter from models.user import User from models.artists import Artist from models.projects import Project from models.labels import Label from config import ( DECIBEL_UI_DOMAIN, REQUEST_APPROVAL_NOTIFICATION_TEMPLATE_ID, APPROVE_REVIEW_NOTIFICATION_TEMPLATE_ID, SOME_CAMPAIGNS_APPROVED_NOTIFICATION_TEMPLATE_ID, CAMPAIGNS_DISAPPROVED_NOTIFICATION_TEMPLATE_ID ) class EmailService: def send_request_approval_email(self, requester: User, reviewer: User, artist: Artist, project: Project, media_plan_id: int, label: Label, message: Optional[str] = None): template = EmailTemplateSchema( id=REQUEST_APPROVAL_NOTIFICATION_TEMPLATE_ID, context=EmailContextSchema( reviewer_fullname=reviewer.name, requester_fullname=requester.name, requester_email=requester.email, artist_image_link=self._get_artist_image_link(artist), artist_name=artist.name, media_plan_link=self._get_media_plan_link(project.id, media_plan_id), review_link=self._get_review_media_plan_link(project.id, media_plan_id), label_name=label.name, message=message, project_name=project.name, ) ) notifications_center = NotificationsCenter(EmailStrategy()) notification = SendEmailSchema(template=template, to=[reviewer.email], track_clicks=True) result = notifications_center.notify([notification]) return result[0][reviewer.email] def send_submit_approval_emails(self, users_with_campaigns, project: Project, media_plan_id: int, artist: Artist): notifications = [ SendEmailSchema( template=EmailTemplateSchema( id=self.__get_template_id(user), context=EmailContextSchema( artist_image_link=self._get_artist_image_link(artist), artist_name=artist.name, project_name=project.name, media_plan_link=self._get_media_plan_link(project.id, media_plan_id), reviewer_name=user.reviewer_name, requester_name=user.requester_name, approval_count=user.not_completed_count + user.approved_count + user.disapproved_count, approved_campaigns_count=user.approved_count, disapproved_campaigns_count=user.disapproved_count, ) ), to=user.requester_email, track_clicks=True ) for user in users_with_campaigns ] notifications_center = NotificationsCenter(EmailStrategy()) return notifications_center.notify(notifications) def __get_template_id(self, user_with_campaign): all_approval_count = (user_with_campaign.approved_count + user_with_campaign.disapproved_count + user_with_campaign.not_completed_count) if user_with_campaign.approved_count == all_approval_count: return APPROVE_REVIEW_NOTIFICATION_TEMPLATE_ID elif user_with_campaign.approved_count < all_approval_count and not user_with_campaign.disapproved_count: return SOME_CAMPAIGNS_APPROVED_NOTIFICATION_TEMPLATE_ID else: return CAMPAIGNS_DISAPPROVED_NOTIFICATION_TEMPLATE_ID def _get_artist_image_link(self, artist): return artist.images[0].url # todo get default image def _get_review_media_plan_link(self, project_id: int, media_plan_id: int): return f"{DECIBEL_UI_DOMAIN}/projects/{project_id}/media-plans/{media_plan_id}?mode=approval&referrer=email" def _get_media_plan_link(self, project_id: int, media_plan_id: int): return f"{DECIBEL_UI_DOMAIN}/projects/{project_id}/media-plans/{media_plan_id}"