@@ -75,18 +75,30 @@ func (s *Service) Replace(userID string, webhook smsgateway.Webhook) error {
7575 webhook .ID = s .idgen ()
7676 }
7777
78+ // Check device ownership if deviceID is provided
79+ if webhook .DeviceID != nil {
80+ ok , err := s .devicesSvc .Exists (userID , devices .WithID (* webhook .DeviceID ))
81+ if err != nil {
82+ return fmt .Errorf ("failed to select devices: %w" , err )
83+ }
84+ if ! ok {
85+ return newValidationError ("device_id" , * webhook .DeviceID , devices .ErrNotFound )
86+ }
87+ }
88+
7889 model := Webhook {
79- ExtID : webhook .ID ,
80- UserID : userID ,
81- URL : webhook .URL ,
82- Event : webhook .Event ,
90+ ExtID : webhook .ID ,
91+ UserID : userID ,
92+ DeviceID : webhook .DeviceID ,
93+ URL : webhook .URL ,
94+ Event : webhook .Event ,
8395 }
8496
8597 if err := s .webhooks .Replace (& model ); err != nil {
8698 return fmt .Errorf ("can't replace webhook: %w" , err )
8799 }
88100
89- go s .notifyDevices (userID )
101+ go s .notifyDevices (userID , webhook . DeviceID )
90102
91103 return nil
92104}
@@ -99,30 +111,48 @@ func (s *Service) Delete(userID string, filters ...SelectFilter) error {
99111 return fmt .Errorf ("can't delete webhooks: %w" , err )
100112 }
101113
102- go s .notifyDevices (userID )
114+ go s .notifyDevices (userID , nil )
103115
104116 return nil
105117}
106118
107119// notifyDevices sends a push notification to all devices associated with the given user.
108- func (s * Service ) notifyDevices (userID string ) {
109- s .logger .Info ("Notifying devices" , zap .String ("user_id" , userID ))
120+ func (s * Service ) notifyDevices (userID string , deviceID * string ) {
121+ logFields := []zap.Field {
122+ zap .String ("user_id" , userID ),
123+ }
124+ if deviceID != nil {
125+ logFields = append (logFields , zap .String ("device_id" , * deviceID ))
126+ }
110127
111- devices , err := s .devicesSvc .Select (userID )
128+ s .logger .Info ("Notifying devices" , logFields ... )
129+
130+ var filters []devices.SelectFilter
131+ if deviceID != nil {
132+ filters = []devices.SelectFilter {devices .WithID (* deviceID )}
133+ }
134+
135+ devices , err := s .devicesSvc .Select (userID , filters ... )
112136 if err != nil {
113- s .logger .Error ("Failed to select devices" , zap .String ("user_id" , userID ), zap .Error (err ))
137+ s .logger .Error ("Failed to select devices" , append (logFields , zap .Error (err ))... )
138+ return
139+ }
140+
141+ if len (devices ) == 0 {
142+ s .logger .Info ("No devices found" , logFields ... )
114143 return
115144 }
116145
117146 for _ , device := range devices {
118147 if device .PushToken == nil {
148+ s .logger .Info ("Device has no push token" , zap .String ("user_id" , userID ), zap .String ("device_id" , device .ID ))
119149 continue
120150 }
121151
122152 if err := s .pushSvc .Enqueue (* device .PushToken , push .NewWebhooksUpdatedEvent ()); err != nil {
123- s .logger .Error ("Failed to send push notification" , zap .String ("user_id" , userID ), zap .Error (err ))
153+ s .logger .Error ("Failed to send push notification" , zap .String ("user_id" , userID ), zap .String ( "device_id" , device . ID ), zap . Error (err ))
124154 }
125155 }
126156
127- s .logger .Info ("Notified devices" , zap . String ( "user_id" , userID ), zap .Int ("count" , len (devices )))
157+ s .logger .Info ("Notified devices" , append ( logFields , zap .Int ("count" , len (devices ))) ... )
128158}
0 commit comments