From bdc6319713cb066dc07f4e545da7fe711414bf65 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 8 Feb 2022 14:30:54 +0100
Subject: [PATCH 2/3] wf: use notification api multi-user mode (#60639)

---
 tests/workflow/test_notification.py | 27 +++++++++++++++++----------
 wcs/wf/notification.py              | 26 +++++++++++++++-----------
 2 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/tests/workflow/test_notification.py b/tests/workflow/test_notification.py
index 9a2cb726..bbb4b923 100644
--- a/tests/workflow/test_notification.py
+++ b/tests/workflow/test_notification.py
@@ -71,13 +71,14 @@ def test_notifications(pub, http_requests):
 
     item.perform(formdata)
     assert http_requests.count() == 1
-    assert http_requests.get_last('url') == 'https://portal/api/notification/add/?NameID=xxx'
+    assert http_requests.get_last('url') == 'https://portal/api/notification/add/'
     assert json.loads(http_requests.get_last('body')) == {
         'body': 'XXX',
         'url': formdata.get_url(),
         'id': 'formdata:%s' % formdata.get_display_id(),
         'origin': '',
         'summary': 'xxx',
+        'name_ids': ['xxx'],
     }
 
     # deleted user
@@ -108,11 +109,9 @@ def test_notifications(pub, http_requests):
 
     item.to = ['_receiver']
     item.perform(formdata)
-    assert http_requests.count() == 2
-    assert {x['url'] for x in http_requests.requests} == {
-        'https://portal/api/notification/add/?NameID=xxy1',
-        'https://portal/api/notification/add/?NameID=xxy2',
-    }
+    assert http_requests.count() == 1
+    assert http_requests.get_last('url') == 'https://portal/api/notification/add/'
+    assert set(json.loads(http_requests.get_last('body'))['name_ids']) == {'xxy1', 'xxy2'}
 
     # test inactive users are ignored
     user2.is_active = False
@@ -120,13 +119,21 @@ def test_notifications(pub, http_requests):
     http_requests.empty()
     item.perform(formdata)
     assert http_requests.count() == 1
-    assert {x['url'] for x in http_requests.requests} == {'https://portal/api/notification/add/?NameID=xxy1'}
+    assert http_requests.get_last('url') == 'https://portal/api/notification/add/'
+    assert set(json.loads(http_requests.get_last('body'))['name_ids']) == {'xxy1'}
+
+    user1.is_active = False
+    user1.store()
+    http_requests.empty()
+    item.perform(formdata)
+    assert http_requests.count() == 0
 
     # check notifications are sent to interco portal if it exists
+    user1.is_active = True
+    user1.store()
     pub.site_options.set('variables', '_interco_portal_url', 'https://interco-portal/')
     http_requests.empty()
     item.perform(formdata)
     assert http_requests.count() == 1
-    assert {x['url'] for x in http_requests.requests} == {
-        'https://interco-portal/api/notification/add/?NameID=xxy1'
-    }
+    assert http_requests.get_last('url') == 'https://interco-portal/api/notification/add/'
+    assert set(json.loads(http_requests.get_last('body'))['name_ids']) == {'xxy1'}
diff --git a/wcs/wf/notification.py b/wcs/wf/notification.py
index b454fe6d..a1367f4a 100644
--- a/wcs/wf/notification.py
+++ b/wcs/wf/notification.py
@@ -137,15 +137,6 @@ class SendNotificationWorkflowStatusItem(WebserviceCallStatusItem):
             )
             return
 
-        self.post_data = {
-            'summary': title,
-            'body': body,
-            'url': formdata.get_url(),
-            'origin': self.origin or '',
-            'id': 'formdata:%s' % formdata.get_display_id(),
-        }
-        self.url = self.get_api_url()
-
         users = []
         for dest in self.to:
             if dest == '_submitter':
@@ -159,12 +150,25 @@ class SendNotificationWorkflowStatusItem(WebserviceCallStatusItem):
                     continue
                 users.extend(get_publisher().user_class.get_users_with_role(role.id))
 
+        name_ids = set()
         for user in users:
             if not user or not user.is_active or user.deleted_timestamp:
                 continue
             for name_id in user.name_identifiers or []:
-                self.qs_data = {'NameID': name_id}
-                super().perform(formdata)
+                name_ids.add(name_id)
+
+        if name_ids:
+            self.post_data = {
+                'summary': title,
+                'body': body,
+                'url': formdata.get_url(),
+                'origin': self.origin or '',
+                'id': 'formdata:%s' % formdata.get_display_id(),
+                'name_ids': list(name_ids),
+            }
+            self.url = self.get_api_url()
+
+            super().perform(formdata)
 
 
 register_item_class(SendNotificationWorkflowStatusItem)
-- 
2.34.1

