Remove With(ctx) pattern from notifications service

This commit is contained in:
Denis Arh
2019-09-29 14:20:02 +02:00
parent c244e9becd
commit c626b82af3
3 changed files with 13 additions and 23 deletions
+9 -3
View File
@@ -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
+3 -19
View File
@@ -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 (&notification{
logger: DefaultLogger.Named("notification"),
}).With(context.Background())
}
func (svc notification) With(ctx context.Context) NotificationService {
func Notification() *notification {
return &notification{
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>
+1 -1
View File
@@ -65,7 +65,7 @@ var (
DefaultChart ChartService
DefaultPage PageService
DefaultAttachment AttachmentService
DefaultNotification NotificationService
DefaultNotification *notification
DefaultSystemUser *systemUser
)