66from zgw_consumers .api_models .base import factory
77
88from notifications .api .serializers import NotificatieSerializer
9+ from notifications .models import NotificationRecord
910from open_inwoner .configurations .models import SiteConfiguration
1011from open_inwoner .openzaak .api_models import Notification
1112from open_inwoner .openzaak .auth import get_valid_subscription_from_request
@@ -40,6 +41,14 @@ def post(self, request):
4041 status = status .HTTP_401_UNAUTHORIZED ,
4142 )
4243
44+ # Create initial log, so
45+ notification_record = NotificationRecord .objects .create (
46+ subscription = subscription ,
47+ payload = request .content ,
48+ is_processed = False ,
49+ is_valid = False ,
50+ )
51+
4352 # deserialize
4453 serializer = NotificatieSerializer (data = request .data )
4554 if not serializer .is_valid ():
@@ -48,19 +57,33 @@ def post(self, request):
4857
4958 notification = factory (Notification , serializer .validated_data )
5059
51- # verify channel
60+ # Update record with basic metadata
61+ notification_record .is_valid = True
62+ notification_record .kanaal = notification .kanaal
63+ notification_record .save (
64+ update_fields = (
65+ "is_valid" ,
66+ "kanaal" ,
67+ )
68+ )
69+
70+ # Handle test webhooks, and leave a record so it's visible for validation purposes
5271 if notification .kanaal == "test" :
5372 self .log_webhook_test_channel (notification )
54- return Response (status = status .HTTP_204_NO_CONTENT )
5573
74+ # Test webhooks don't have to be processed
75+ notification_record .is_processed = True
76+ notification_record .processing_output = "test channel, no further processing done"
77+ notification_record .save (update_fields = ("is_processed" , "processing_output" , ))
78+ return Response (status = status .HTTP_204_NO_CONTENT )
79+
5680 if notification .kanaal not in subscription .channels :
5781 self .log_webhook_channel_not_subscribed (notification )
58- return Response (
59- {
60- "detail" : f"notification channel '{ notification .kanaal } ' not subscribed to"
61- },
62- status = status .HTTP_400_BAD_REQUEST ,
63- )
82+
83+ notification_record .is_processed = True
84+ notification_record .processing_output = "channel not subscribed"
85+ notification_record .save (update_fields = ("is_processed" , "processing_output" , ))
86+ return Response ({"detail" : "channel not subscribed" }, status = status .HTTP_400_BAD_REQUEST )
6487
6588 if self .accept_channels and notification .kanaal not in self .accept_channels :
6689 self .log_webhook_channel_not_acceptable (notification )
@@ -97,5 +120,7 @@ def handle_notification(self, notification: Notification):
97120 if not config .notifications_cases_enabled :
98121 return
99122
123+ # If process eager, do now
124+
100125 notification_data = dataclasses .asdict (notification )
101126 process_zaken_notification .delay (notification_data )
0 commit comments