Add retry-safe scan completion notifications

This commit is contained in:
Joren de Graaf
2026-10-26 01:57:24 +01:00
parent 0e4077d8e3
commit 31266b1533
5 changed files with 68 additions and 11 deletions
+22 -1
View File
@@ -12,9 +12,11 @@ from food.models import (FoodProduct, InventoryLocation, InventoryScanBatch,
KitchenInventoryItem, OpenFoodFactsRequest)
from food.services.inventory_scanner import (ScanWorkflowError,
adjust_confirmed_count, approve_scan_item, approve_scan_items, confirm_scan_item, create_active_scan, get_or_create_active_run,
process_next_scan_item, save_review_item, submit_batch, update_review_preference)
dispatch_pending_scan_notifications, process_next_scan_item, save_review_item,
submit_batch, update_review_preference)
from food.services.off_rate_limit import OpenFoodFactsRateLimited, off_http_request
from food.services.openfoodfacts import OpenFoodFactsTransientError
from planner.services.notifications import NotificationError
class InventoryScanQueueTests(TestCase):
@@ -184,6 +186,25 @@ class InventoryReviewServiceTests(TestCase):
stock = KitchenInventoryItem.objects.get(product=approved.product)
self.assertEqual(stock.quantity, 2)
def test_completion_notification_retries_with_stable_ntfy_sequence_id(self):
run = InventoryScanRun.objects.create(user=self.user, selected_location=self.fridge,
status=InventoryScanRun.Status.COMPLETE_WITH_REVIEW, completed_at=timezone.now())
with patch('planner.services.notifications.send_notification', side_effect=NotificationError('offline')) as send:
dispatch_pending_scan_notifications()
run.refresh_from_db()
self.assertIsNotNone(run.notification_attempted_at)
self.assertIsNone(run.notification_sent_at)
first_sequence_id = send.call_args.kwargs['sequence_id']
InventoryScanRun.objects.filter(pk=run.pk).update(
notification_attempted_at=timezone.now() - timedelta(seconds=61))
with patch('planner.services.notifications.send_notification', return_value=True) as send:
dispatch_pending_scan_notifications()
self.assertEqual(send.call_args.kwargs['sequence_id'], first_sequence_id)
dispatch_pending_scan_notifications()
send.assert_called_once()
run.refresh_from_db()
self.assertIsNotNone(run.notification_sent_at)
def test_bulk_approval_applies_shared_fields_to_selected_items_atomically(self):
run = get_or_create_active_run(self.user, self.fridge.pk)
items = []