@@ -144,7 +144,7 @@ func (s *WebSubAPIService) Create(orgUUID, createdBy string, req *api.WebSubAPI)
144144 Context : req .Context ,
145145 Channels : mapWebSubChannelsAPIToModel (req .Channels ),
146146 Upstream : * mapUpstreamAPIToModel (req .Upstream ),
147- AllChannels : mapWebSubAllChannelPoliciesAPIToModel (req .AllChannels ),
147+ AllChannels : mapWebSubPoliciesAPIToAllChannels (req .Policies ),
148148 SubscriptionPlans : subscriptionPlans ,
149149 },
150150 }
@@ -259,7 +259,7 @@ func (s *WebSubAPIService) Update(orgUUID, handle string, req *api.WebSubAPI) (*
259259 Context : req .Context ,
260260 Channels : mapWebSubChannelsAPIToModel (req .Channels ),
261261 Upstream : * mapUpstreamAPIToModel (req .Upstream ),
262- AllChannels : mapWebSubAllChannelPoliciesAPIToModel (req .AllChannels ),
262+ AllChannels : mapWebSubPoliciesAPIToAllChannels (req .Policies ),
263263 SubscriptionPlans : subscriptionPlans ,
264264 }
265265
@@ -415,7 +415,7 @@ func mapWebSubAPIModelToAPI(m *model.WebSubAPI, apiUtil *utils.APIUtil) *api.Web
415415 Context : m .Configuration .Context ,
416416 Upstream : mapUpstreamModelToAPI (& m .Configuration .Upstream ),
417417 Channels : mapWebSubChannelsModelToAPI (m .Configuration .Channels ),
418- AllChannels : mapWebSubAllChannelPoliciesModelToAPI (m .Configuration .AllChannels ),
418+ Policies : mapAllChannelsModelToWebSubPolicies (m .Configuration .AllChannels ),
419419 SubscriptionPlans : subscriptionPlans ,
420420 CreatedAt : utils .TimePtr (m .CreatedAt ),
421421 UpdatedAt : utils .TimePtr (m .UpdatedAt ),
@@ -431,11 +431,15 @@ func mapWebSubChannelsAPIToModel(in *map[string]api.WebSubChannel) map[string]mo
431431 }
432432 out := make (map [string ]model.WebSubChannel , len (* in ))
433433 for name , ch := range * in {
434+ var p * api.WebSubChannelPolicies
435+ if ch .Policies != nil {
436+ p = ch .Policies
437+ }
434438 out [name ] = model.WebSubChannel {
435- OnSubscription : mapEventPoliciesAPIToModel ( ch . OnSubscription ),
436- OnUnsubscription : mapEventPoliciesAPIToModel ( ch . OnUnsubscription ),
437- OnMessageReceived : mapEventPoliciesAPIToModel ( ch . OnMessageReceived ),
438- OnMessageDelivery : mapEventPoliciesAPIToModel ( ch . OnMessageDelivery ),
439+ OnSubscription : policySlicePtrToEventPolicies ( policySlicePtrFromChannelPolicies ( p , "on_subscription" ) ),
440+ OnUnsubscription : policySlicePtrToEventPolicies ( policySlicePtrFromChannelPolicies ( p , "on_unsubscription" ) ),
441+ OnMessageReceived : policySlicePtrToEventPolicies ( policySlicePtrFromChannelPolicies ( p , "on_message_received" ) ),
442+ OnMessageDelivery : policySlicePtrToEventPolicies ( policySlicePtrFromChannelPolicies ( p , "on_message_delivery" ) ),
439443 }
440444 }
441445 return out
@@ -457,10 +461,10 @@ func mapWebSubChannelPoliciesAPIToModel(in *api.WebSubChannelPolicies) *model.We
457461 return nil
458462 }
459463 return & model.WebSubChannelPolicies {
460- OnSubscription : mapEventPoliciesAPIToModel (in .OnSubscription ),
461- OnUnsubscription : mapEventPoliciesAPIToModel (in .OnUnsubscription ),
462- OnMessageReceived : mapEventPoliciesAPIToModel (in .OnMessageReceived ),
463- OnMessageDelivery : mapEventPoliciesAPIToModel (in .OnMessageDelivery ),
464+ OnSubscription : policySlicePtrToEventPolicies (in .OnSubscription ),
465+ OnUnsubscription : policySlicePtrToEventPolicies (in .OnUnsubscription ),
466+ OnMessageReceived : policySlicePtrToEventPolicies (in .OnMessageReceived ),
467+ OnMessageDelivery : policySlicePtrToEventPolicies (in .OnMessageDelivery ),
464468 }
465469}
466470
@@ -477,6 +481,80 @@ func mapWebSubAllChannelPoliciesAPIToModel(in *api.WebSubAllChannelPolicies) *mo
477481 }
478482}
479483
484+ // mapWebSubPoliciesAPIToAllChannels converts flat WebSubChannelPolicies (from API) to model.WebSubAllChannelPolicies (for storage).
485+ func mapWebSubPoliciesAPIToAllChannels (in * api.WebSubChannelPolicies ) * model.WebSubAllChannelPolicies {
486+ if in == nil {
487+ return nil
488+ }
489+ return & model.WebSubAllChannelPolicies {
490+ OnSubscription : policySlicePtrToEventPolicies (in .OnSubscription ),
491+ OnUnsubscription : policySlicePtrToEventPolicies (in .OnUnsubscription ),
492+ OnMessageReceived : policySlicePtrToEventPolicies (in .OnMessageReceived ),
493+ OnMessageDelivery : policySlicePtrToEventPolicies (in .OnMessageDelivery ),
494+ }
495+ }
496+
497+ // mapAllChannelsModelToWebSubPolicies converts stored model.WebSubAllChannelPolicies to flat WebSubChannelPolicies (for API response).
498+ func mapAllChannelsModelToWebSubPolicies (in * model.WebSubAllChannelPolicies ) * api.WebSubChannelPolicies {
499+ if in == nil {
500+ return nil
501+ }
502+ return & api.WebSubChannelPolicies {
503+ OnSubscription : eventPoliciesToPolicySlicePtr (in .OnSubscription ),
504+ OnUnsubscription : eventPoliciesToPolicySlicePtr (in .OnUnsubscription ),
505+ OnMessageReceived : eventPoliciesToPolicySlicePtr (in .OnMessageReceived ),
506+ OnMessageDelivery : eventPoliciesToPolicySlicePtr (in .OnMessageDelivery ),
507+ }
508+ }
509+
510+ // policySlicePtrToEventPolicies wraps a flat policy slice pointer into a model.WebSubEventPolicies.
511+ func policySlicePtrToEventPolicies (in * []api.Policy ) * model.WebSubEventPolicies {
512+ if in == nil {
513+ return nil
514+ }
515+ policies := make ([]model.Policy , 0 , len (* in ))
516+ for _ , p := range * in {
517+ policy := model.Policy {
518+ Name : p .Name ,
519+ Version : p .Version ,
520+ }
521+ if p .ExecutionCondition != nil {
522+ policy .ExecutionCondition = p .ExecutionCondition
523+ }
524+ if p .Params != nil {
525+ policy .Params = p .Params
526+ }
527+ policies = append (policies , policy )
528+ }
529+ return & model.WebSubEventPolicies {Policies : policies }
530+ }
531+
532+ // eventPoliciesToPolicySlicePtr converts a model.WebSubEventPolicies to a flat policy slice pointer.
533+ func eventPoliciesToPolicySlicePtr (in * model.WebSubEventPolicies ) * []api.Policy {
534+ if in == nil || len (in .Policies ) == 0 {
535+ return nil
536+ }
537+ return mapModelPolicySliceToAPI (in .Policies )
538+ }
539+
540+ // policySlicePtrFromChannelPolicies extracts the policy slice for a given event type from WebSubChannelPolicies.
541+ func policySlicePtrFromChannelPolicies (p * api.WebSubChannelPolicies , event string ) * []api.Policy {
542+ if p == nil {
543+ return nil
544+ }
545+ switch event {
546+ case "on_subscription" :
547+ return p .OnSubscription
548+ case "on_unsubscription" :
549+ return p .OnUnsubscription
550+ case "on_message_received" :
551+ return p .OnMessageReceived
552+ case "on_message_delivery" :
553+ return p .OnMessageDelivery
554+ }
555+ return nil
556+ }
557+
480558// mapWebSubChannelsModelToAPI converts the model channel map to the API channel map.
481559func mapWebSubChannelsModelToAPI (in map [string ]model.WebSubChannel ) * map [string ]api.WebSubChannel {
482560 if len (in ) == 0 {
@@ -485,10 +563,12 @@ func mapWebSubChannelsModelToAPI(in map[string]model.WebSubChannel) *map[string]
485563 out := make (map [string ]api.WebSubChannel , len (in ))
486564 for name , ch := range in {
487565 out [name ] = api.WebSubChannel {
488- OnSubscription : mapEventPoliciesModelToAPI (ch .OnSubscription ),
489- OnUnsubscription : mapEventPoliciesModelToAPI (ch .OnUnsubscription ),
490- OnMessageReceived : mapEventPoliciesModelToAPI (ch .OnMessageReceived ),
491- OnMessageDelivery : mapEventPoliciesModelToAPI (ch .OnMessageDelivery ),
566+ Policies : & api.WebSubChannelPolicies {
567+ OnSubscription : eventPoliciesToPolicySlicePtr (ch .OnSubscription ),
568+ OnUnsubscription : eventPoliciesToPolicySlicePtr (ch .OnUnsubscription ),
569+ OnMessageReceived : eventPoliciesToPolicySlicePtr (ch .OnMessageReceived ),
570+ OnMessageDelivery : eventPoliciesToPolicySlicePtr (ch .OnMessageDelivery ),
571+ },
492572 }
493573 }
494574 return & out
@@ -510,10 +590,10 @@ func mapWebSubChannelPoliciesModelToAPI(in *model.WebSubChannelPolicies) *api.We
510590 return nil
511591 }
512592 return & api.WebSubChannelPolicies {
513- OnSubscription : mapEventPoliciesModelToAPI (in .OnSubscription ),
514- OnUnsubscription : mapEventPoliciesModelToAPI (in .OnUnsubscription ),
515- OnMessageReceived : mapEventPoliciesModelToAPI (in .OnMessageReceived ),
516- OnMessageDelivery : mapEventPoliciesModelToAPI (in .OnMessageDelivery ),
593+ OnSubscription : eventPoliciesToPolicySlicePtr (in .OnSubscription ),
594+ OnUnsubscription : eventPoliciesToPolicySlicePtr (in .OnUnsubscription ),
595+ OnMessageReceived : eventPoliciesToPolicySlicePtr (in .OnMessageReceived ),
596+ OnMessageDelivery : eventPoliciesToPolicySlicePtr (in .OnMessageDelivery ),
517597 }
518598}
519599
0 commit comments