diff --git a/server/vbv_lernwelt/notify/services.py b/server/vbv_lernwelt/notify/services.py index c4b5c8bb..34143871 100644 --- a/server/vbv_lernwelt/notify/services.py +++ b/server/vbv_lernwelt/notify/services.py @@ -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,25 +137,35 @@ class NotificationService: if emailed: notification_identifier += "_emailed" + if notification: + notification.emailed = True + notification.save() else: log.debug("Should not send email") - response = notify.send( - sender=sender, - recipient=recipient, - verb=verb, - emailed=emailed, - # The metadata is saved in the 'data' member of the AbstractNotification model - 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 - sent_notification.course = course - sent_notification.actor_avatar_url = actor_avatar_url - sent_notification.save() - log.info("Notification sent successfully", emailed=emailed) + 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, + verb=verb, + emailed=emailed, + # The metadata is saved in the 'data' member of the AbstractNotification model + 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 + sent_notification.course = course + sent_notification.actor_avatar_url = actor_avatar_url + sent_notification.save() + log.info("Notification sent successfully", emailed=emailed) return f"{notification_identifier}_success" except Exception as e: log.error( @@ -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()