-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
520 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ TOKEN_SECRET={w:ae2@:;'aE.VW@FP0g813Lo-J\exKAf(0f5pt"+@V,z\2k(jk{MX?F6WK3|;Z"a,B | |
|
||
GRPC_PORT=50051 | ||
|
||
HOST_DOMAIN=localhost | ||
NO_REPLAY_EMAIL=[email protected] | ||
|
||
RABBITMQ_PASSWORD=taskem | ||
RABBITMQ_URL=amqp://taskem:${RABBITMQ_PASSWORD}@rabbitmq:5672 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
with-expecter: True | ||
inpackage: True | ||
all: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package notifier | ||
|
||
import ( | ||
"github.com/go-faster/errors" | ||
"github.com/google/uuid" | ||
"github.com/taskemapp/server/apps/server/internal/config" | ||
"github.com/taskemapp/server/apps/server/internal/repositories/user" | ||
"github.com/taskemapp/server/libs/queue" | ||
"github.com/taskemapp/server/libs/template" | ||
"go.uber.org/fx" | ||
"go.uber.org/multierr" | ||
"strings" | ||
) | ||
|
||
type AccountNotifier interface { | ||
VerifyEmail(username string, email string) error | ||
} | ||
|
||
type EmailAccountNotifier struct { | ||
config config.Config | ||
q queue.Queue | ||
userRepo user.Repository | ||
} | ||
|
||
type OptsEmailAccNotifier struct { | ||
fx.In | ||
Config config.Config | ||
Queue queue.Queue | ||
UserRepo user.Repository | ||
} | ||
|
||
func NewEmailAccountNotifier(opts OptsEmailAccNotifier) *EmailAccountNotifier { | ||
return &EmailAccountNotifier{ | ||
config: opts.Config, | ||
q: opts.Queue, | ||
userRepo: opts.UserRepo, | ||
} | ||
} | ||
|
||
func (n *EmailAccountNotifier) VerifyEmail(username string, email string) error { | ||
confirmID, err := uuid.NewUUID() | ||
if err != nil { | ||
return errors.Wrap(err, "verify to") | ||
} | ||
|
||
confirmLink, err := buildConfirmLink(n.config.HostDomain, confirmID.String()) | ||
if err != nil { | ||
return errors.Wrap(err, "verify to") | ||
} | ||
|
||
temp, err := template.Get(template.VerifyEmailTemplate) | ||
if err != nil { | ||
return errors.Wrap(err, "verify to") | ||
} | ||
|
||
err = sendEmail(sendEmailOpts{ | ||
temp: temp, | ||
data: template.VerifyEmail{ | ||
Name: username, | ||
ConfirmationLink: confirmLink, | ||
UnsubscribeLink: "unsubscribe-link", | ||
}, | ||
q: n.q, | ||
title: "Verify to", | ||
to: email, | ||
from: n.config.NoReplayEmail, | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "verify to") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildConfirmLink(host string, confirmID string) (string, error) { | ||
var sb strings.Builder | ||
var err error | ||
|
||
_, err = sb.WriteString(host) | ||
err = multierr.Append(err, err) | ||
|
||
_, err = sb.WriteString("/verify?id=") | ||
err = multierr.Append(err, err) | ||
|
||
_, err = sb.WriteString(confirmID) | ||
err = multierr.Append(err, err) | ||
|
||
return sb.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package notifier | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"testing" | ||
) | ||
|
||
func Test_buildConfirmLink(t *testing.T) { | ||
type args struct { | ||
host string | ||
confirmID string | ||
} | ||
tt := struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
|
||
"Basic link", | ||
args{ | ||
"localhost", | ||
uuid.Nil.String(), | ||
}, | ||
"localhost/verify?id=00000000-0000-0000-0000-000000000000", | ||
false, | ||
} | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
got, err := buildConfirmLink(tt.args.host, tt.args.confirmID) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("buildConfirmLink() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("buildConfirmLink() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package notifier | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/go-faster/errors" | ||
"github.com/taskemapp/server/apps/notification/pkg/notifier" | ||
"github.com/taskemapp/server/libs/queue" | ||
"html/template" | ||
) | ||
|
||
type sendEmailOpts struct { | ||
temp *template.Template | ||
data any | ||
q queue.Queue | ||
title string | ||
to string | ||
from string | ||
} | ||
|
||
func sendEmail(opts sendEmailOpts) error { | ||
var buff bytes.Buffer | ||
err := opts.temp.Execute(&buff, opts.data) | ||
if err != nil { | ||
return errors.Wrap(err, "send email") | ||
} | ||
|
||
body, err := json.Marshal(notifier.EmailNotification{ | ||
Notification: notifier.Notification{ | ||
Title: "Verify email", | ||
Message: buff.String(), | ||
}, | ||
To: opts.to, | ||
From: opts.from, | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "send email") | ||
} | ||
|
||
err = opts.q.Publish(notifier.ChannelEmail, queue.Message{ | ||
ContentType: "application/json", | ||
Body: body, | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "send email") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package notifier | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/taskemapp/server/apps/notification/pkg/notifier" | ||
"github.com/taskemapp/server/libs/queue" | ||
"github.com/taskemapp/server/libs/template" | ||
"testing" | ||
) | ||
|
||
func Test_sendEmail(t *testing.T) { | ||
type args struct { | ||
name string | ||
to string | ||
title string | ||
from string | ||
} | ||
tt := args{ | ||
name: "", | ||
to: "", | ||
title: "", | ||
from: "", | ||
} | ||
|
||
//Setup mock | ||
queueMock := queue.NewMockQueue(t) | ||
defer queueMock.AssertExpectations(t) | ||
|
||
temp, err := template.Get(template.VerifyEmailTemplate, template.WithDir("../../../../..")) | ||
assert.NoError(t, err) | ||
|
||
data := template.VerifyEmail{ | ||
Name: tt.name, | ||
ConfirmationLink: "", | ||
UnsubscribeLink: "", | ||
} | ||
|
||
var buff bytes.Buffer | ||
err = temp.Execute(&buff, data) | ||
assert.NoError(t, err) | ||
|
||
body, err := json.Marshal(notifier.EmailNotification{ | ||
Notification: notifier.Notification{ | ||
Title: "Verify email", | ||
Message: buff.String(), | ||
}, | ||
To: tt.to, | ||
From: tt.from, | ||
}) | ||
|
||
message := queue.Message{ | ||
ContentType: "application/json", | ||
Body: body, | ||
} | ||
|
||
queueMock.EXPECT().Publish(notifier.ChannelEmail, message).Return(nil) | ||
|
||
//Test starting | ||
err = queueMock.Publish(notifier.ChannelEmail, message) | ||
assert.NoError(t, err) | ||
|
||
err = sendEmail(sendEmailOpts{ | ||
temp: temp, | ||
data: data, | ||
q: queueMock, | ||
title: tt.title, | ||
to: tt.to, | ||
from: tt.from, | ||
}) | ||
assert.NoError(t, err) | ||
} |
Oops, something went wrong.