From 97bbbe5065bf1a0bab1bf0baf90b7b98d44120d7 Mon Sep 17 00:00:00 2001 From: Julien Perrochet Date: Tue, 10 Sep 2024 02:01:33 +0200 Subject: [PATCH] [dss/RIDv1]: group subscriptions to notify by their callback URL (#1106) --- pkg/rid/models/api/v1/conversions.go | 35 +++++++++++++++++----------- pkg/rid/server/v1/isa_handler.go | 15 +++--------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pkg/rid/models/api/v1/conversions.go b/pkg/rid/models/api/v1/conversions.go index a5bda3906..c3fdd6711 100644 --- a/pkg/rid/models/api/v1/conversions.go +++ b/pkg/rid/models/api/v1/conversions.go @@ -163,20 +163,29 @@ func ToIdentificationServiceArea(i *ridmodels.IdentificationServiceArea) *restap return result } -// ToSubscriberToNotify converts a Subscription to a SubscriberToNotify RID v1 -// REST model for API consumption. -func ToSubscriberToNotify(s *ridmodels.Subscription) *restapi.SubscriberToNotify { - notifIndex := restapi.SubscriptionNotificationIndex(s.NotificationIndex) - subID := restapi.SubscriptionUUID(s.ID.String()) - return &restapi.SubscriberToNotify{ - Url: restapi.URL(s.URL), - Subscriptions: []restapi.SubscriptionState{ - { - NotificationIndex: ¬ifIndex, - SubscriptionId: &subID, - }, - }, +// MakeSubscribersToNotify groups the passed subscriptions by their callback URL, +// returning a collection of subscribers to notify that contains one entry per distinct callback URL. +func MakeSubscribersToNotify(subscriptions []*ridmodels.Subscription) []restapi.SubscriberToNotify { + subscriptionsByURL := map[string][]restapi.SubscriptionState{} + for _, sub := range subscriptions { + notifIdx := restapi.SubscriptionNotificationIndex(sub.NotificationIndex) + subID := restapi.SubscriptionUUID(sub.ID) + subState := restapi.SubscriptionState{ + SubscriptionId: &subID, + NotificationIndex: ¬ifIdx, + } + subscriptionsByURL[sub.URL] = append(subscriptionsByURL[sub.URL], subState) + } + + result := []restapi.SubscriberToNotify{} + for url, states := range subscriptionsByURL { + result = append(result, restapi.SubscriberToNotify{ + Url: restapi.URL(url), + Subscriptions: states, + }) } + + return result } // ToSubscription converts a subscription business object to a Subscription RID diff --git a/pkg/rid/server/v1/isa_handler.go b/pkg/rid/server/v1/isa_handler.go index 5bf890c88..e60b61a35 100644 --- a/pkg/rid/server/v1/isa_handler.go +++ b/pkg/rid/server/v1/isa_handler.go @@ -123,10 +123,7 @@ func (s *Server) CreateIdentificationServiceArea(ctx context.Context, req *resta } } - apiSubscribers := make([]restapi.SubscriberToNotify, 0, len(subscribers)) - for _, subscriber := range subscribers { - apiSubscribers = append(apiSubscribers, *apiv1.ToSubscriberToNotify(subscriber)) - } + apiSubscribers := apiv1.MakeSubscribersToNotify(subscribers) return restapi.CreateIdentificationServiceAreaResponseSet{Response200: &restapi.PutIdentificationServiceAreaResponse{ ServiceArea: *apiv1.ToIdentificationServiceArea(insertedISA), @@ -210,10 +207,7 @@ func (s *Server) UpdateIdentificationServiceArea(ctx context.Context, req *resta } } - apiSubscribers := make([]restapi.SubscriberToNotify, 0, len(subscribers)) - for _, subscriber := range subscribers { - apiSubscribers = append(apiSubscribers, *apiv1.ToSubscriberToNotify(subscriber)) - } + apiSubscribers := apiv1.MakeSubscribersToNotify(subscribers) return restapi.UpdateIdentificationServiceAreaResponseSet{Response200: &restapi.PutIdentificationServiceAreaResponse{ ServiceArea: *apiv1.ToIdentificationServiceArea(insertedISA), @@ -264,10 +258,7 @@ func (s *Server) DeleteIdentificationServiceArea(ctx context.Context, req *resta } } - apiSubscribers := make([]restapi.SubscriberToNotify, 0, len(subscribers)) - for _, subscriber := range subscribers { - apiSubscribers = append(apiSubscribers, *apiv1.ToSubscriberToNotify(subscriber)) - } + apiSubscribers := apiv1.MakeSubscribersToNotify(subscribers) return restapi.DeleteIdentificationServiceAreaResponseSet{Response200: &restapi.DeleteIdentificationServiceAreaResponse{ ServiceArea: *apiv1.ToIdentificationServiceArea(isa),