From 20c49e8093be13aba9072bc27dd12d59b4652dcd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= <tde@odoo.com>
Date: Thu, 10 Oct 2019 10:47:25 +0000
Subject: [PATCH] [FIX][IMP] mail: update all notification status before
 sending bus notifications

Otherwise status of notifications is not correctly updated when sending them
on the bus.

Task ID 2068986
---
 addons/mail/models/mail_mail.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/addons/mail/models/mail_mail.py b/addons/mail/models/mail_mail.py
index d846041c9f54..029caf22b018 100644
--- a/addons/mail/models/mail_mail.py
+++ b/addons/mail/models/mail_mail.py
@@ -157,18 +157,20 @@ class MailMail(models.Model):
                 failed = self.env['mail.notification']
                 if failure_type:
                     failed = notifications.filtered(lambda notif: notif.res_partner_id not in success_pids)
+                (notifications - failed).sudo().write({
+                    'notification_status': 'sent',
+                    'failure_type': '',
+                    'failure_reason': '',
+                })
+                if failed:
                     failed.sudo().write({
                         'notification_status': 'exception',
                         'failure_type': failure_type,
                         'failure_reason': failure_reason,
                     })
                     messages = notifications.mapped('mail_message_id').filtered(lambda m: m.is_thread_message())
+                    # TDE TODO: could be great to notify message-based, not notifications-based, to lessen number of notifs
                     messages._notify_mail_failure_update()  # notify user that we have a failure
-                (notifications - failed).sudo().write({
-                    'notification_status': 'sent',
-                    'failure_type': '',
-                    'failure_reason': '',
-                })
         if not failure_type or failure_type == 'RECIPIENT':  # if we have another error, we want to keep the mail.
             mail_to_delete_ids = [mail.id for mail in self if mail.auto_delete]
             self.browse(mail_to_delete_ids).sudo().unlink()
-- 
GitLab