diff --git a/pkg/rid/models/api/v2/conversions.go b/pkg/rid/models/api/v2/conversions.go index aa03c203b..b84992b0a 100644 --- a/pkg/rid/models/api/v2/conversions.go +++ b/pkg/rid/models/api/v2/conversions.go @@ -208,6 +208,30 @@ func ToSubscriberToNotify(s *ridmodels.Subscription) *restapi.SubscriberToNotify } } +// 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) + subState := restapi.SubscriptionState{ + SubscriptionId: restapi.SubscriptionUUID(sub.ID), + 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 v2 REST model for API consumption. func ToSubscription(s *ridmodels.Subscription) *restapi.Subscription { diff --git a/pkg/rid/server/v2/isa_handler.go b/pkg/rid/server/v2/isa_handler.go index feb72f78a..e5691edf8 100644 --- a/pkg/rid/server/v2/isa_handler.go +++ b/pkg/rid/server/v2/isa_handler.go @@ -117,10 +117,7 @@ func (s *Server) CreateIdentificationServiceArea(ctx context.Context, req *resta } } - apiSubscribers := make([]restapi.SubscriberToNotify, 0, len(subscribers)) - for _, subscriber := range subscribers { - apiSubscribers = append(apiSubscribers, *apiv2.ToSubscriberToNotify(subscriber)) - } + apiSubscribers := apiv2.MakeSubscribersToNotify(subscribers) return restapi.CreateIdentificationServiceAreaResponseSet{Response200: &restapi.PutIdentificationServiceAreaResponse{ ServiceArea: *apiv2.ToIdentificationServiceArea(insertedISA), @@ -199,10 +196,7 @@ func (s *Server) UpdateIdentificationServiceArea(ctx context.Context, req *resta } } - apiSubscribers := make([]restapi.SubscriberToNotify, 0, len(subscribers)) - for _, subscriber := range subscribers { - apiSubscribers = append(apiSubscribers, *apiv2.ToSubscriberToNotify(subscriber)) - } + apiSubscribers := apiv2.MakeSubscribersToNotify(subscribers) return restapi.UpdateIdentificationServiceAreaResponseSet{Response200: &restapi.PutIdentificationServiceAreaResponse{ ServiceArea: *apiv2.ToIdentificationServiceArea(insertedISA), @@ -253,10 +247,7 @@ func (s *Server) DeleteIdentificationServiceArea(ctx context.Context, req *resta } } - apiSubscribers := make([]restapi.SubscriberToNotify, 0, len(subscribers)) - for _, subscriber := range subscribers { - apiSubscribers = append(apiSubscribers, *apiv2.ToSubscriberToNotify(subscriber)) - } + apiSubscribers := apiv2.MakeSubscribersToNotify(subscribers) return restapi.DeleteIdentificationServiceAreaResponseSet{Response200: &restapi.DeleteIdentificationServiceAreaResponse{ ServiceArea: *apiv2.ToIdentificationServiceArea(isa),