Skip to content

Commit

Permalink
chore: refactor mail service to be acessed at the usecase level (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellgithinji committed Sep 8, 2021
1 parent b8f7d6d commit 636492c
Show file tree
Hide file tree
Showing 11 changed files with 1,183 additions and 369 deletions.
358 changes: 133 additions & 225 deletions pkg/engagement/infrastructure/services/mail/service_unit_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/engagement/presentation/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (p PresentationHandlersImpl) GoogleCloudPubSubHandler(
return
}
case helpers.AddPubSubNamespace(common.SentEmailTopic):
err = p.usecases.SendEmail(ctx, m)
err = p.usecases.SendNotificationEmail(ctx, m)
if err != nil {
serverutils.WriteJSONResponse(
w,
Expand Down
8 changes: 4 additions & 4 deletions pkg/engagement/usecases/feed/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type NotificationUsecases interface {
m *pubsubtools.PubSubPayload,
) error

SendEmail(
SendNotificationEmail(
ctx context.Context,
m *pubsubtools.PubSubPayload,
) error
Expand Down Expand Up @@ -915,12 +915,12 @@ func (n NotificationImpl) SendNotificationViaFCM(
return nil
}

// SendEmail sends an email
func (n NotificationImpl) SendEmail(
// SendNotificationEmail sends an email
func (n NotificationImpl) SendNotificationEmail(
ctx context.Context,
m *pubsubtools.PubSubPayload,
) error {
ctx, span := tracer.Start(ctx, "SendEmail")
ctx, span := tracer.Start(ctx, "SendNotificationEmail")
defer span.End()
if m == nil {
return fmt.Errorf("nil pub sub payload")
Expand Down
6 changes: 3 additions & 3 deletions pkg/engagement/usecases/feed/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ func TestUpdateInbox(t *testing.T) {
}
}

func TestNotificationImpl_SendEmail(t *testing.T) {
func TestNotificationImpl_SendNotificationEmail(t *testing.T) {
ctx := firebasetools.GetAuthenticatedContext(t)

notify, err := InitializeTestNewNotification(ctx)
Expand Down Expand Up @@ -1532,9 +1532,9 @@ func TestNotificationImpl_SendEmail(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := notify.SendEmail(tt.args.ctx, tt.args.m)
err := notify.SendNotificationEmail(tt.args.ctx, tt.args.m)
if (err != nil) != tt.wantErr {
t.Errorf("NotificationImpl.SendEmail() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("NotificationImpl.SendNotificationEmail() error = %v, wantErr %v", err, tt.wantErr)
}

if tt.wantErr {
Expand Down
158 changes: 158 additions & 0 deletions pkg/engagement/usecases/mail/mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package mail

import (
"context"

"github.com/savannahghi/engagementcore/pkg/engagement/application/common/dto"
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure"
)

// UsecaseMail defines mail service usecases interface
type UsecaseMail interface {
SendInBlue(
ctx context.Context,
subject,
text string,
to ...string,
) (string, string, error)

SendMailgun(
ctx context.Context,
subject,
text string,
body *string,
to ...string,
) (string, string, error)

SendEmail(
ctx context.Context,
subject,
text string,
body *string,
to ...string,
) (string, string, error)

SimpleEmail(
ctx context.Context,
subject,
text string,
body *string,
to ...string,
) (string, error)

UpdateMailgunDeliveryStatus(
ctx context.Context,
payload *dto.MailgunEvent,
) (*dto.OutgoingEmailsLog, error)

GenerateEmailTemplate(
name string,
templateName string,
) string
}

// ImplMail is the mail service implementation
type ImplMail struct {
infrastructure infrastructure.Interactor
}

// NewMail initializes a mail service instance
func NewMail(infrastructure infrastructure.Interactor) *ImplMail {
return &ImplMail{
infrastructure: infrastructure,
}
}

// SendInBlue sends email using sendinblue service
func (m *ImplMail) SendInBlue(
ctx context.Context,
subject,
text string,
to ...string,
) (string, string, error) {
i := m.infrastructure.ServiceMailImpl
return i.SendInBlue(
ctx,
subject,
text,
to...,
)
}

// SendMailgun sends email using mailgun service
func (m *ImplMail) SendMailgun(
ctx context.Context,
subject,
text string,
body *string,
to ...string,
) (string, string, error) {
i := m.infrastructure.ServiceMailImpl
return i.SendMailgun(
ctx,
subject,
text,
body,
to...,
)
}

// SendEmail sends email using mailgun service
func (m *ImplMail) SendEmail(
ctx context.Context,
subject,
text string,
body *string,
to ...string,
) (string, string, error) {
i := m.infrastructure.ServiceMailImpl
return i.SendEmail(
ctx,
subject,
text,
body,
to...,
)
}

// SimpleEmail sends email using mailgun service
func (m *ImplMail) SimpleEmail(
ctx context.Context,
subject,
text string,
body *string,
to ...string,
) (string, error) {
i := m.infrastructure.ServiceMailImpl
return i.SimpleEmail(
ctx,
subject,
text,
body,
to...,
)
}

// UpdateMailgunDeliveryStatus updates mailgun delivery status
func (m *ImplMail) UpdateMailgunDeliveryStatus(
ctx context.Context,
payload *dto.MailgunEvent,
) (*dto.OutgoingEmailsLog, error) {
i := m.infrastructure.ServiceMailImpl
return i.UpdateMailgunDeliveryStatus(
ctx,
payload,
)
}

// GenerateEmailTemplate generates templates for email
func (m *ImplMail) GenerateEmailTemplate(
name string,
templateName string,
) string {
i := m.infrastructure.ServiceMailImpl
return i.GenerateEmailTemplate(
name,
templateName,
)
}
Loading

0 comments on commit 636492c

Please sign in to comment.