Chore: Remove bus from notification service (#46813)

* Chore: Remove bus from notification service

* fix signature

* fix function signature in tests
This commit is contained in:
Serge Zaitsev 2022-03-22 09:04:30 +01:00 committed by GitHub
parent 5a25ada3d0
commit 4ce7978cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 9 deletions

View File

@ -233,6 +233,7 @@ var wireSet = wire.NewSet(
sqlstore.ProvideService, sqlstore.ProvideService,
wire.Bind(new(alerting.AlertStore), new(*sqlstore.SQLStore)), wire.Bind(new(alerting.AlertStore), new(*sqlstore.SQLStore)),
ngmetrics.ProvideService, ngmetrics.ProvideService,
wire.Bind(new(notifications.TempUserStore), new(*sqlstore.SQLStore)),
wire.Bind(new(notifications.Service), new(*notifications.NotificationService)), wire.Bind(new(notifications.Service), new(*notifications.NotificationService)),
wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationService)), wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationService)),
wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationService)), wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationService)),
@ -247,6 +248,7 @@ var wireTestSet = wire.NewSet(
wire.Bind(new(alerting.AlertStore), new(*sqlstore.SQLStore)), wire.Bind(new(alerting.AlertStore), new(*sqlstore.SQLStore)),
notifications.MockNotificationService, notifications.MockNotificationService,
wire.Bind(new(notifications.TempUserStore), new(*mockstore.SQLStoreMock)),
wire.Bind(new(notifications.Service), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.Service), new(*notifications.NotificationServiceMock)),
wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationServiceMock)),
wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationServiceMock)),

View File

@ -265,7 +265,7 @@ func createCoreEmailService(t *testing.T) *notifications.NotificationService {
cfg.Smtp.Host = "localhost:1234" cfg.Smtp.Host = "localhost:1234"
mailer := notifications.NewFakeMailer() mailer := notifications.NewFakeMailer()
ns, err := notifications.ProvideService(bus, cfg, mailer) ns, err := notifications.ProvideService(bus, cfg, mailer, nil)
require.NoError(t, err) require.NoError(t, err)
return ns return ns

View File

@ -29,16 +29,12 @@ type Service interface {
EmailSender EmailSender
} }
type Store interface {
GetUserByLogin(context.Context, *models.GetUserByLoginQuery) error
}
var mailTemplates *template.Template var mailTemplates *template.Template
var tmplResetPassword = "reset_password" var tmplResetPassword = "reset_password"
var tmplSignUpStarted = "signup_started" var tmplSignUpStarted = "signup_started"
var tmplWelcomeOnSignUp = "welcome_on_signup" var tmplWelcomeOnSignUp = "welcome_on_signup"
func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer) (*NotificationService, error) { func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer, store TempUserStore) (*NotificationService, error) {
ns := &NotificationService{ ns := &NotificationService{
Bus: bus, Bus: bus,
Cfg: cfg, Cfg: cfg,
@ -46,6 +42,7 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer) (*Notification
mailQueue: make(chan *Message, 10), mailQueue: make(chan *Message, 10),
webhookQueue: make(chan *Webhook, 10), webhookQueue: make(chan *Webhook, 10),
mailer: mailer, mailer: mailer,
store: store,
} }
ns.Bus.AddHandler(ns.SendResetPasswordEmail) ns.Bus.AddHandler(ns.SendResetPasswordEmail)
@ -81,6 +78,10 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer) (*Notification
return ns, nil return ns, nil
} }
type TempUserStore interface {
UpdateTempUserWithEmailSent(ctx context.Context, cmd *models.UpdateTempUserWithEmailSentCommand) error
}
type NotificationService struct { type NotificationService struct {
Bus bus.Bus Bus bus.Bus
Cfg *setting.Cfg Cfg *setting.Cfg
@ -89,6 +90,7 @@ type NotificationService struct {
webhookQueue chan *Webhook webhookQueue chan *Webhook
mailer Mailer mailer Mailer
log log.Logger log log.Logger
store TempUserStore
} }
func (ns *NotificationService) Run(ctx context.Context) error { func (ns *NotificationService) Run(ctx context.Context) error {
@ -237,7 +239,7 @@ func (ns *NotificationService) signUpStartedHandler(ctx context.Context, evt *ev
} }
emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: evt.Code} emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: evt.Code}
return bus.Dispatch(ctx, &emailSentCmd) return ns.store.UpdateTempUserWithEmailSent(ctx, &emailSentCmd)
} }
func (ns *NotificationService) signUpCompletedHandler(ctx context.Context, evt *events.SignUpCompleted) error { func (ns *NotificationService) signUpCompletedHandler(ctx context.Context, evt *events.SignUpCompleted) error {

View File

@ -252,7 +252,7 @@ func createSut(t *testing.T, bus bus.Bus) (*NotificationService, *FakeMailer) {
func createSutWithConfig(t *testing.T, bus bus.Bus, cfg *setting.Cfg) (*NotificationService, *FakeMailer, error) { func createSutWithConfig(t *testing.T, bus bus.Bus, cfg *setting.Cfg) (*NotificationService, *FakeMailer, error) {
smtp := NewFakeMailer() smtp := NewFakeMailer()
ns, err := ProvideService(bus, cfg, smtp) ns, err := ProvideService(bus, cfg, smtp, nil)
return ns, smtp, err return ns, smtp, err
} }
@ -261,7 +261,7 @@ func createDisconnectedSut(t *testing.T, bus bus.Bus) *NotificationService {
cfg := createSmtpConfig() cfg := createSmtpConfig()
smtp := NewFakeDisconnectedMailer() smtp := NewFakeDisconnectedMailer()
ns, err := ProvideService(bus, cfg, smtp) ns, err := ProvideService(bus, cfg, smtp, nil)
require.NoError(t, err) require.NoError(t, err)
return ns return ns
} }