@@ -93,30 +93,48 @@ func (n *Notifier) NotifyPsychologist(ctx context.Context, event *CrisisEvent) e
9393 RequiresAction : event .ResponseActions ["require_acknowledgment" ],
9494 }
9595
96+ var errors []string
97+
9698 // 1. Send webhook (real-time)
9799 if n .webhookURL != "" {
98- err := n .sendWebhook (ctx , payload )
99- if err != nil {
100+ if err := n .sendWebhook (ctx , payload ); err != nil {
100101 log .Error ().Err (err ).Msg ("Failed to send webhook" )
102+ errors = append (errors , "webhook: " + err .Error ())
101103 }
102104 }
103105
104106 // 2. Send email (backup)
105- if n .emailAPI != "" {
106- err := n .sendEmail (ctx , payload )
107- if err != nil {
107+ if n .emailAPI != "" || n .emailSvc != nil {
108+ if err := n .sendEmail (ctx , payload ); err != nil {
108109 log .Error ().Err (err ).Msg ("Failed to send email" )
110+ errors = append (errors , "email: " + err .Error ())
109111 }
110112 }
111113
112114 // 3. Send SMS (critical only)
113- if event .Severity == "CRITICAL" && n .smsAPI != "" {
114- err := n .sendSMS (ctx , payload )
115- if err != nil {
115+ if event .Severity == "CRITICAL" && (n .smsAPI != "" || n .smsSvc != nil ) {
116+ if err := n .sendSMS (ctx , payload ); err != nil {
116117 log .Error ().Err (err ).Msg ("Failed to send SMS" )
118+ errors = append (errors , "sms: " + err .Error ())
117119 }
118120 }
119121
122+ // Se NENHUM canal conseguiu enviar, retorna erro
123+ channelsAttempted := 0
124+ if n .webhookURL != "" {
125+ channelsAttempted ++
126+ }
127+ if n .emailAPI != "" || n .emailSvc != nil {
128+ channelsAttempted ++
129+ }
130+ if event .Severity == "CRITICAL" && (n .smsAPI != "" || n .smsSvc != nil ) {
131+ channelsAttempted ++
132+ }
133+
134+ if channelsAttempted > 0 && len (errors ) == channelsAttempted {
135+ return fmt .Errorf ("CRITICO: todos os %d canais de notificacao falharam para evento %d: %v" , channelsAttempted , event .ID , errors )
136+ }
137+
120138 return nil
121139}
122140
@@ -179,7 +197,10 @@ func (n *Notifier) sendEmail(ctx context.Context, payload NotificationPayload) e
179197 "subject" : subject ,
180198 "payload" : payload ,
181199 }
182- jsonData , _ := json .Marshal (emailPayload )
200+ jsonData , err := json .Marshal (emailPayload )
201+ if err != nil {
202+ return fmt .Errorf ("marshal email payload: %w" , err )
203+ }
183204 req , err := http .NewRequestWithContext (ctx , "POST" , n .emailAPI , bytes .NewBuffer (jsonData ))
184205 if err != nil {
185206 return err
@@ -222,7 +243,10 @@ func (n *Notifier) sendSMS(ctx context.Context, payload NotificationPayload) err
222243 "message" : reason ,
223244 "payload" : payload ,
224245 }
225- jsonData , _ := json .Marshal (smsPayload )
246+ jsonData , err := json .Marshal (smsPayload )
247+ if err != nil {
248+ return fmt .Errorf ("marshal sms payload: %w" , err )
249+ }
226250 req , err := http .NewRequestWithContext (ctx , "POST" , n .smsAPI , bytes .NewBuffer (jsonData ))
227251 if err != nil {
228252 return err
0 commit comments