dana-medical-site/internal/api/notify.go
2026-10-14 13:33:13 +00:00

39 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"context"
"log/slog"
"time"
"dana/internal/notify"
)
// WithNotifier подключает уведомления о заявках. Без него заявки просто сохраняются.
func (a *API) WithNotifier(n notify.Notifier) *API {
a.notifier = n
return a
}
// notifyLead отправляет уведомление в фоне: посетитель не ждёт почтовый сервер,
// а сбой отправки не отменяет уже сохранённую заявку — она видна в админке.
func (a *API) notifyLead(id int64) {
if a.notifier == nil {
return
}
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
notice, err := a.store.LeadNotice(ctx, id)
if err != nil {
slog.Error("lead notice", "lead_id", id, "err", err)
return
}
if err := a.notifier.LeadCreated(ctx, notice); err != nil {
slog.Error("notify lead", "lead_id", id, "err", err)
return
}
slog.Info("lead notification sent", "lead_id", id)
}()
}