Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
102 lines
3.6 KiB
Go
102 lines
3.6 KiB
Go
package notificationimp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/tech/sendico/notification/interface/api"
|
|
mmail "github.com/tech/sendico/notification/internal/server/notificationimp/mail"
|
|
"github.com/tech/sendico/notification/internal/server/notificationimp/telegram"
|
|
"github.com/tech/sendico/pkg/domainprovider"
|
|
"github.com/tech/sendico/pkg/merrors"
|
|
na "github.com/tech/sendico/pkg/messaging/notifications/account"
|
|
ni "github.com/tech/sendico/pkg/messaging/notifications/invitation"
|
|
snotifications "github.com/tech/sendico/pkg/messaging/notifications/site"
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
"github.com/tech/sendico/pkg/model"
|
|
"github.com/tech/sendico/pkg/mservice"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type NotificationAPI struct {
|
|
logger mlogger.Logger
|
|
client mmail.Client
|
|
dp domainprovider.DomainProvider
|
|
tg telegram.Client
|
|
}
|
|
|
|
func (a *NotificationAPI) Name() mservice.Type {
|
|
return mservice.Notifications
|
|
}
|
|
|
|
func (a *NotificationAPI) Finish(_ context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
func CreateAPI(a api.API) (*NotificationAPI, error) {
|
|
p := &NotificationAPI{
|
|
dp: a.DomainProvider(),
|
|
}
|
|
p.logger = a.Logger().Named(p.Name())
|
|
|
|
if a.Config().Notification == nil {
|
|
return nil, merrors.InvalidArgument("notification configuration is missing", "config.notification")
|
|
}
|
|
if a.Config().Notification.Telegram == nil {
|
|
return nil, merrors.InvalidArgument("telegram configuration is missing", "config.notification.telegram")
|
|
}
|
|
|
|
var err error
|
|
if p.client, err = mmail.CreateMailClient(p.logger.Named("mailer"), p.Name(), a.Register().Producer(), a.Localizer(), a.DomainProvider(), a.Config().Notification); err != nil {
|
|
p.logger.Error("Failed to create mail connection", zap.Error(err), zap.String("driver", a.Config().Notification.Driver))
|
|
return nil, err
|
|
}
|
|
if p.tg, err = telegram.NewClient(p.logger.Named("telegram"), a.Config().Notification.Telegram); err != nil {
|
|
p.logger.Error("Failed to create telegram client", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
db, err := a.DBFactory().NewAccountDB()
|
|
if err != nil {
|
|
p.logger.Error("Failed to create account db connection", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
if err := a.Register().Consumer(na.NewAccountCreatedMessageProcessor(p.logger, db, p.onAccount)); err != nil {
|
|
p.logger.Error("Failed to create account creation handler", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
if err := a.Register().Consumer(na.NewPasswordResetRequestedMessageProcessor(p.logger, db, p.onPasswordReset)); err != nil {
|
|
p.logger.Error("Failed to create password reset handler", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
idb, err := a.DBFactory().NewInvitationsDB()
|
|
if err != nil {
|
|
p.logger.Error("Failed to create invitation db connection", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
if err := a.Register().Consumer(ni.NewInvitationCreatedProcessor(p.logger, p.onInvitation, idb, db)); err != nil {
|
|
p.logger.Error("Failed to create invitation creation handler", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
if err := a.Register().Consumer(snotifications.NewDemoRequestProcessor(p.logger, p.onDemoRequest)); err != nil {
|
|
p.logger.Error("Failed to register demo request handler", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
func (a *NotificationAPI) onDemoRequest(ctx context.Context, request *model.DemoRequest) error {
|
|
if a.tg == nil {
|
|
return merrors.Internal("telegram client is not configured")
|
|
}
|
|
if err := a.tg.SendDemoRequest(ctx, request); err != nil {
|
|
a.logger.Warn("Failed to send demo request via telegram", zap.Error(err))
|
|
return err
|
|
}
|
|
a.logger.Info("Demo request sent via Telegram", zap.String("name", request.Name), zap.String("organization", request.OrganizationName))
|
|
return nil
|
|
}
|