Skip to content

Commit

Permalink
[dss] ridv2: group subscriptions to notify by their callback URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Shastick committed Sep 9, 2024
1 parent 32f570b commit 375723b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
35 changes: 22 additions & 13 deletions pkg/rid/models/api/v1/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &notifIndex,
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: &notifIdx,
}
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
Expand Down
15 changes: 3 additions & 12 deletions pkg/rid/server/v1/isa_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 375723b

Please sign in to comment.