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