Remove With(ctx) pattern from notifications service
This commit is contained in:
@@ -8,19 +8,25 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/internal/mail"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
gomail "gopkg.in/mail.v2"
|
||||
)
|
||||
|
||||
var _ = errors.Wrap
|
||||
|
||||
type (
|
||||
Notification struct {
|
||||
notification service.NotificationService
|
||||
notification notificationService
|
||||
}
|
||||
|
||||
contentPayload struct {
|
||||
Plain string `json:"plain"`
|
||||
Html string `json:"html"`
|
||||
}
|
||||
|
||||
notificationService interface {
|
||||
SendEmail(*gomail.Message) error
|
||||
AttachEmailRecipients(*gomail.Message, string, ...string) error
|
||||
}
|
||||
)
|
||||
|
||||
func (Notification) New() *Notification {
|
||||
@@ -31,7 +37,7 @@ func (Notification) New() *Notification {
|
||||
|
||||
// EmailSend assembles Email Message and pushes message to notification service
|
||||
func (ctrl *Notification) EmailSend(ctx context.Context, r *request.NotificationEmailSend) (interface{}, error) {
|
||||
ntf := ctrl.notification.With(ctx)
|
||||
ntf := ctrl.notification
|
||||
|
||||
msg := mail.New()
|
||||
if err := ntf.AttachEmailRecipients(msg, "To", r.To...); err != nil {
|
||||
@@ -58,7 +64,7 @@ func (ctrl *Notification) EmailSend(ctx context.Context, r *request.Notification
|
||||
|
||||
msg.SetHeader("Subject", r.Subject)
|
||||
|
||||
if err := ctrl.notification.With(ctx).SendEmail(msg); err != nil {
|
||||
if err := ntf.SendEmail(msg); err != nil {
|
||||
return false, err
|
||||
} else {
|
||||
return true, nil
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -13,28 +12,13 @@ import (
|
||||
|
||||
type (
|
||||
notification struct {
|
||||
ctx context.Context
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
NotificationService interface {
|
||||
With(ctx context.Context) NotificationService
|
||||
|
||||
SendEmail(message *gomail.Message) error
|
||||
AttachEmailRecipients(message *gomail.Message, field string, recipients ...string) error
|
||||
}
|
||||
)
|
||||
|
||||
func Notification() NotificationService {
|
||||
return (¬ification{
|
||||
logger: DefaultLogger.Named("notification"),
|
||||
}).With(context.Background())
|
||||
}
|
||||
|
||||
func (svc notification) With(ctx context.Context) NotificationService {
|
||||
func Notification() *notification {
|
||||
return ¬ification{
|
||||
ctx: ctx,
|
||||
logger: svc.logger,
|
||||
logger: DefaultLogger.Named("notification"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +31,7 @@ func (svc notification) SendEmail(message *gomail.Message) error {
|
||||
return mail.Send(message)
|
||||
}
|
||||
|
||||
// AttachEmailRecipients validates, resolves, formats andd attaches set of recipients to message
|
||||
// AttachEmailRecipients validates, resolves, formats and attaches set of recipients to message
|
||||
//
|
||||
// Supports 3 input formats:
|
||||
// - <valid email>
|
||||
|
||||
@@ -65,7 +65,7 @@ var (
|
||||
DefaultChart ChartService
|
||||
DefaultPage PageService
|
||||
DefaultAttachment AttachmentService
|
||||
DefaultNotification NotificationService
|
||||
DefaultNotification *notification
|
||||
|
||||
DefaultSystemUser *systemUser
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user