Add ability to send email, when it was not sent before

This commit is contained in:
Daniel Egger 2023-08-29 15:08:12 +02:00
parent ae9d7cf471
commit 88e7e0edcc
1 changed files with 35 additions and 24 deletions

View File

@ -105,18 +105,19 @@ class NotificationService:
)
emailed = False
try:
if NotificationService._is_duplicate_notification(
notification = NotificationService._find_duplicate_notification(
recipient=recipient,
verb=verb,
notification_type=notification_type,
target_url=target_url,
template_name=email_template.name,
template_data=template_data,
):
log.info("A duplicate notification was omitted from being sent")
return f"{notification_identifier}_duplicate"
)
emailed = False
if notification and notification.emailed:
emailed = True
if cls._should_send_email(notification_type, recipient):
if cls._should_send_email(notification_type, recipient) and not emailed:
log.debug("Try to send email")
try:
emailed = cls._send_email(
@ -136,9 +137,18 @@ class NotificationService:
if emailed:
notification_identifier += "_emailed"
if notification:
notification.emailed = True
notification.save()
else:
log.debug("Should not send email")
if notification:
log.info("Duplicate notification was omitted from being sent")
notification_identifier += "_duplicate"
return notification_identifier
else:
response = notify.send(
sender=sender,
recipient=recipient,
@ -148,6 +158,7 @@ class NotificationService:
email_template=email_template.name,
template_data=template_data,
)
sent_notification: Notification = response[0][1][0] # 🫨
sent_notification.target_url = target_url
sent_notification.notification_type = notification_type
@ -192,14 +203,14 @@ class NotificationService:
)
@staticmethod
def _is_duplicate_notification(
def _find_duplicate_notification(
recipient: User,
verb: str,
notification_type: NotificationType,
template_name: str,
template_data: dict,
target_url: str | None,
) -> bool:
) -> Notification | None:
"""Check if a notification with the same parameters has already been sent to the recipient.
This is to prevent duplicate notifications from being sent and to protect against potential programming errors.
"""
@ -212,4 +223,4 @@ class NotificationService:
"email_template": template_name,
"template_data": template_data,
},
).exists()
).first()