diff --git a/modules/localgov_workflows_notifications/src/WorkflowNotification.php b/modules/localgov_workflows_notifications/src/WorkflowNotification.php index 45ec023..f3f9624 100644 --- a/modules/localgov_workflows_notifications/src/WorkflowNotification.php +++ b/modules/localgov_workflows_notifications/src/WorkflowNotification.php @@ -42,11 +42,11 @@ public function enqueue(ContentEntityInterface $entity, string $type): void { continue; } - // Ensure the queue contains only one item for per service contact. + // Aggregate notifications by service contact and type. $found = FALSE; $claimed_items = []; while ($queue_item = $queue->claimItem(1)) { - if ($queue_item->data->service_contact == $contact->id()) { + if ($queue_item->data->service_contact == $contact->id() && $queue_item->data->type === $type) { // Delete old item and create new one with additional entity. $queue->deleteItem($queue_item); diff --git a/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php b/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php index d79da9b..0277150 100644 --- a/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php +++ b/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php @@ -166,7 +166,7 @@ public function testEnqueueNodes() { $this->notifier->enqueue($node5, 'published'); $this->assertEquals(2, $this->queue->numberOfItems()); - // Enqueue a notification with different type. + // Ensure notifications are not duplicated. $node6 = $this->createNode([ 'type' => 'page', 'title' => $this->randomMachineName(), @@ -175,6 +175,17 @@ public function testEnqueueNodes() { ], ]); $this->notifier->enqueue($node6, 'published'); + $this->assertEquals(2, $this->queue->numberOfItems()); + + // Enqueue a notification with different type. + $node7 = $this->createNode([ + 'type' => 'page', + 'title' => $this->randomMachineName(), + 'localgov_service_contacts' => [ + ['target_id' => $this->serviceContacts['enabled']->id()], + ], + ]); + $this->notifier->enqueue($node7, 'unpublished'); $this->assertEquals(3, $this->queue->numberOfItems()); }