Skip to content

Commit 067c6fc

Browse files
committed
Add contact type support to SendEmail method
Signed-off-by: WashingtonKK <washingtonkigan@gmail.com>
1 parent a7c82f7 commit 067c6fc

File tree

11 files changed

+283
-45
lines changed

11 files changed

+283
-45
lines changed

users/api/grpc/endpoint.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"text/template"
1010

11+
grpcUsersV1 "github.com/absmach/supermq/api/grpc/emails/v1"
1112
"github.com/absmach/supermq/pkg/errors"
1213
"github.com/absmach/supermq/users"
1314
"github.com/go-kit/kit/endpoint"
@@ -22,7 +23,7 @@ func sendEmailEndpoint(svc users.Service) endpoint.Endpoint {
2223
}, err
2324
}
2425

25-
if err := svc.SendEmailWithUserId(ctx, req.to, req.from, req.subject, req.header, req.user, req.content, req.footer); err != nil {
26+
if err := svc.SendEmail(ctx, req.to, req.toType, req.from, req.fromType, req.subject, req.header, req.user, req.content, req.footer); err != nil {
2627
return sendEmailRes{
2728
err: err,
2829
}, err
@@ -36,7 +37,9 @@ func sendEmailEndpoint(svc users.Service) endpoint.Endpoint {
3637

3738
type sendEmailReq struct {
3839
to []string
40+
toType grpcUsersV1.ContactType
3941
from string
42+
fromType grpcUsersV1.ContactType
4043
subject string
4144
header string
4245
user string

users/api/grpc/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ func decodeSendEmailRequest(_ context.Context, grpcReq any) (any, error) {
4646

4747
return sendEmailReq{
4848
to: req.GetTos(),
49+
toType: req.GetToType(),
4950
from: req.GetFrom(),
51+
fromType: req.GetFromType(),
5052
subject: req.GetSubject(),
5153
header: opts["header"],
5254
user: opts["user"],

users/events/streams.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package events
66
import (
77
"context"
88

9+
grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
910
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1011
"github.com/absmach/supermq/pkg/authn"
1112
"github.com/absmach/supermq/pkg/events"
@@ -445,6 +446,6 @@ func (es *eventStore) OAuthAddUserPolicy(ctx context.Context, user users.User) e
445446
return es.Publish(ctx, addPolicyStream, event)
446447
}
447448

448-
func (es *eventStore) SendEmailWithUserId(ctx context.Context, to []string, from, subject, header, user, content, footer string) error {
449-
return es.svc.SendEmailWithUserId(ctx, to, from, subject, header, user, content, footer)
449+
func (es *eventStore) SendEmail(ctx context.Context, to []string, toType grpcEmailsV1.ContactType, from string, fromType grpcEmailsV1.ContactType, subject, header, user, content, footer string) error {
450+
return es.svc.SendEmail(ctx, to, toType, from, fromType, subject, header, user, content, footer)
450451
}

users/middleware/authorization.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package middleware
66
import (
77
"context"
88

9+
grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
910
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1011
smqauth "github.com/absmach/supermq/auth"
1112
"github.com/absmach/supermq/pkg/authn"
@@ -335,8 +336,8 @@ func (am *authorizationMiddleware) OAuthAddUserPolicy(ctx context.Context, user
335336
return am.svc.OAuthAddUserPolicy(ctx, user)
336337
}
337338

338-
func (am *authorizationMiddleware) SendEmailWithUserId(ctx context.Context, to []string, from, subject, header, user, content, footer string) error {
339-
return am.svc.SendEmailWithUserId(ctx, to, from, subject, header, user, content, footer)
339+
func (am *authorizationMiddleware) SendEmail(ctx context.Context, to []string, toType grpcEmailsV1.ContactType, from string, fromType grpcEmailsV1.ContactType, subject, header, user, content, footer string) error {
340+
return am.svc.SendEmail(ctx, to, toType, from, fromType, subject, header, user, content, footer)
340341
}
341342

342343
func (am *authorizationMiddleware) checkSuperAdmin(ctx context.Context, session authn.Session) error {

users/middleware/logging.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log/slog"
99
"time"
1010

11+
grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
1112
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1213
"github.com/absmach/supermq/pkg/authn"
1314
"github.com/absmach/supermq/users"
@@ -522,13 +523,16 @@ func (lm *loggingMiddleware) OAuthAddUserPolicy(ctx context.Context, user users.
522523
return lm.svc.OAuthAddUserPolicy(ctx, user)
523524
}
524525

525-
// SendEmailWithUserId logs the send_email request. It logs the recipients and the time it took to complete the request.
526-
func (lm *loggingMiddleware) SendEmailWithUserId(ctx context.Context, to []string, from, subject, header, user, content, footer string) (err error) {
526+
// SendEmail logs the send_email request. It logs the recipients and the time it took to complete the request.
527+
func (lm *loggingMiddleware) SendEmail(ctx context.Context, to []string, toType grpcEmailsV1.ContactType, from string, fromType grpcEmailsV1.ContactType, subject, header, user, content, footer string) (err error) {
527528
defer func(begin time.Time) {
528529
args := []any{
529530
slog.String("duration", time.Since(begin).String()),
530531
slog.String("request_id", middleware.GetReqID(ctx)),
531532
slog.Any("to", to),
533+
slog.String("to_type", toType.String()),
534+
slog.String("from", from),
535+
slog.String("from_type", fromType.String()),
532536
slog.String("subject", subject),
533537
}
534538
if err != nil {
@@ -538,5 +542,5 @@ func (lm *loggingMiddleware) SendEmailWithUserId(ctx context.Context, to []strin
538542
}
539543
lm.logger.Info("Send email completed successfully", args...)
540544
}(time.Now())
541-
return lm.svc.SendEmailWithUserId(ctx, to, from, subject, header, user, content, footer)
545+
return lm.svc.SendEmail(ctx, to, toType, from, fromType, subject, header, user, content, footer)
542546
}

users/middleware/metrics.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"time"
99

10+
grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
1011
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1112
"github.com/absmach/supermq/pkg/authn"
1213
"github.com/absmach/supermq/users"
@@ -246,11 +247,11 @@ func (ms *metricsMiddleware) OAuthAddUserPolicy(ctx context.Context, user users.
246247
return ms.svc.OAuthAddUserPolicy(ctx, user)
247248
}
248249

249-
// SendEmailWithUserId instruments SendEmail method with metrics.
250-
func (ms *metricsMiddleware) SendEmailWithUserId(ctx context.Context, to []string, from, subject, header, user, content, footer string) error {
250+
// SendEmail instruments SendEmail method with metrics.
251+
func (ms *metricsMiddleware) SendEmail(ctx context.Context, to []string, toType grpcEmailsV1.ContactType, from string, fromType grpcEmailsV1.ContactType, subject, header, user, content, footer string) error {
251252
defer func(begin time.Time) {
252253
ms.counter.With("method", "send_email").Add(1)
253254
ms.latency.With("method", "send_email").Observe(time.Since(begin).Seconds())
254255
}(time.Now())
255-
return ms.svc.SendEmailWithUserId(ctx, to, from, subject, header, user, content, footer)
256+
return ms.svc.SendEmail(ctx, to, toType, from, fromType, subject, header, user, content, footer)
256257
}

users/middleware/tracing.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package middleware
66
import (
77
"context"
88

9+
grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
910
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1011
"github.com/absmach/supermq/pkg/authn"
1112
"github.com/absmach/supermq/pkg/tracing"
@@ -249,13 +250,16 @@ func (tm *tracingMiddleware) OAuthAddUserPolicy(ctx context.Context, user users.
249250
return tm.svc.OAuthAddUserPolicy(ctx, user)
250251
}
251252

252-
// SendEmailWithUserId traces the "SendEmailWithUserId" operation of the wrapped users.Service.
253-
func (tm *tracingMiddleware) SendEmailWithUserId(ctx context.Context, to []string, from, subject, header, user, content, footer string) error {
253+
// SendEmail traces the "SendEmail" operation of the wrapped users.Service.
254+
func (tm *tracingMiddleware) SendEmail(ctx context.Context, to []string, toType grpcEmailsV1.ContactType, from string, fromType grpcEmailsV1.ContactType, subject, header, user, content, footer string) error {
254255
ctx, span := tracing.StartSpan(ctx, tm.tracer, "svc_send_email", trace.WithAttributes(
255256
attribute.StringSlice("to", to),
257+
attribute.String("to_type", toType.String()),
258+
attribute.String("from", from),
259+
attribute.String("from_type", fromType.String()),
256260
attribute.String("subject", subject),
257261
))
258262
defer span.End()
259263

260-
return tm.svc.SendEmailWithUserId(ctx, to, from, subject, header, user, content, footer)
264+
return tm.svc.SendEmail(ctx, to, toType, from, fromType, subject, header, user, content, footer)
261265
}

users/mocks/service.go

Lines changed: 31 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

users/service.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/absmach/supermq"
18+
grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
1819
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1920
apiutil "github.com/absmach/supermq/api/http/util"
2021
smqauth "github.com/absmach/supermq/auth"
@@ -815,20 +816,38 @@ func changed(updated *string, old string) bool {
815816
return *updated != old
816817
}
817818

818-
func (svc service) SendEmailWithUserId(ctx context.Context, userIds []string, from, subject, header, user, content, footer string) error {
819-
for i, userId := range userIds {
820-
u, err := svc.users.RetrieveByID(ctx, userId)
821-
if err != nil {
822-
return errors.Wrap(svcerr.ErrViewEntity, err)
819+
func (svc service) SendEmail(ctx context.Context, to []string, toType grpcEmailsV1.ContactType, from string, fromType grpcEmailsV1.ContactType, subject, header, user, content, footer string) error {
820+
// Convert recipients based on contact type
821+
emails := make([]string, len(to))
822+
for i, contact := range to {
823+
switch toType {
824+
case grpcEmailsV1.ContactType_CONTACT_TYPE_ID:
825+
u, err := svc.users.RetrieveByID(ctx, contact)
826+
if err != nil {
827+
return errors.Wrap(svcerr.ErrViewEntity, err)
828+
}
829+
emails[i] = u.Email
830+
case grpcEmailsV1.ContactType_CONTACT_TYPE_EMAIL:
831+
emails[i] = contact
832+
default:
833+
return errors.Wrap(svcerr.ErrMalformedEntity, errors.New("invalid contact type for recipients"))
823834
}
824-
825-
userIds[i] = u.Email
826835
}
827836

828-
inviter, err := svc.users.RetrieveByID(ctx, from)
829-
if err != nil {
830-
return err
837+
// Convert sender based on contact type
838+
var senderName string
839+
switch fromType {
840+
case grpcEmailsV1.ContactType_CONTACT_TYPE_ID:
841+
inviter, err := svc.users.RetrieveByID(ctx, from)
842+
if err != nil {
843+
return errors.Wrap(svcerr.ErrViewEntity, err)
844+
}
845+
senderName = inviter.FirstName + " " + inviter.LastName
846+
case grpcEmailsV1.ContactType_CONTACT_TYPE_EMAIL:
847+
senderName = from
848+
default:
849+
return errors.Wrap(svcerr.ErrMalformedEntity, errors.New("invalid contact type for sender"))
831850
}
832851

833-
return svc.email.SendCustom(userIds, inviter.FirstName+" "+inviter.LastName, subject, header, user, content, footer)
852+
return svc.email.SendCustom(emails, senderName, subject, header, user, content, footer)
834853
}

0 commit comments

Comments
 (0)