all repos

onasty @ 5495cb456caa00f4564d1c1eed06a766811debaf

a one-time notes service

onasty/internal/events/mailermq/mailermq.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
refactor: fix annoyances (#97)..., 1 year ago
1
package mailermq
2
3
import (
4
	"context"
5
	"encoding/json"
6
7
	"github.com/nats-io/nats.go"
8
	"github.com/olexsmir/onasty/internal/events"
9
	"github.com/olexsmir/onasty/internal/transport/http/reqid"
10
)
11
12
type Mailer interface {
13
	SendVerificationEmail(ctx context.Context, input SendVerificationEmailRequest) error
14
}
15
16
type MailerMQ struct {
17
	nc *nats.Conn
18
}
19
20
func New(nc *nats.Conn) *MailerMQ {
21
	return &MailerMQ{
22
		nc: nc,
23
	}
24
}
25
26
type sendRequest struct {
27
	RequestID    string            `json:"request_id"`
28
	Receiver     string            `json:"receiver"`
29
	TemplateName string            `json:"template_name"`
30
	Options      map[string]string `json:"options"`
31
}
32
33
type SendVerificationEmailRequest struct {
34
	Receiver string
35
	Token    string
36
}
37
38
func (m MailerMQ) SendVerificationEmail(
39
	ctx context.Context,
40
	inp SendVerificationEmailRequest,
41
) error {
42
	req, err := json.Marshal(sendRequest{
43
		RequestID:    reqid.GetContext(ctx),
44
		Receiver:     inp.Receiver,
45
		TemplateName: "email_verification",
46
		Options: map[string]string{
47
			"token": inp.Token,
48
		},
49
	})
50
	if err != nil {
51
		return err
52
	}
53
54
	resp, err := m.nc.RequestWithContext(ctx, "mailer.send", req)
55
	if err != nil {
56
		return err
57
	}
58
59
	return events.CheckRespForError(resp)
60
}