Send an event to a subscribed webhook
(ctx context.Context, userID entities.UserID, event cloudevents.Event, phoneNumber string)
| 180 | |
| 181 | // Send an event to a subscribed webhook |
| 182 | func (service *WebhookService) Send(ctx context.Context, userID entities.UserID, event cloudevents.Event, phoneNumber string) error { |
| 183 | ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) |
| 184 | defer span.End() |
| 185 | |
| 186 | webhooks, err := service.repository.LoadByEvent(ctx, userID, event.Type(), phoneNumber) |
| 187 | if err != nil { |
| 188 | msg := fmt.Sprintf("cannot load webhooks for userID [%s] and event [%s]", userID, event.Type()) |
| 189 | return service.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, stacktrace.GetCode(err), msg)) |
| 190 | } |
| 191 | |
| 192 | if len(webhooks) == 0 { |
| 193 | ctxLogger.Info(fmt.Sprintf("user [%s] has no webhook subscription to event [%s]", userID, event.Type())) |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | var wg sync.WaitGroup |
| 198 | for _, webhook := range webhooks { |
| 199 | wg.Add(1) |
| 200 | go func(webhook *entities.Webhook) { |
| 201 | defer wg.Done() |
| 202 | service.sendNotification(ctx, event, phoneNumber, webhook) |
| 203 | }(webhook) |
| 204 | } |
| 205 | wg.Wait() |
| 206 | |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | func (service *WebhookService) sendNotification(ctx context.Context, event cloudevents.Event, owner string, webhook *entities.Webhook) { |
| 211 | ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) |
nothing calls this directly
no test coverage detected