all repos

onasty @ eb4c60509f134e49485bff1ee0c6422ced9d5e03

a one-time notes service

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

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
feat(api): change email (#191)..., 9 months 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
const sendTopic = "mailer.send"
13
14
type Mailer interface {
15
	// SendVerificationEmail sends an email with a verification token to the user.
16
	SendVerificationEmail(ctx context.Context, input SendVerificationEmailRequest) error
17
18
	// SendPasswordResetEmail sends an email with a password reset token to the user.
19
	SendPasswordResetEmail(ctx context.Context, input SendPasswordResetEmailRequest) error
20
21
	// SendChangeEmailVerification sends an email with a change email verification token to the user.
22
	SendChangeEmailConfirmation(ctx context.Context, inp SendChangeEmailConfirmationRequest) error
23
}
24
25
type MailerMQ struct {
26
	nc *nats.Conn
27
}
28
29
func New(nc *nats.Conn) *MailerMQ {
30
	return &MailerMQ{
31
		nc: nc,
32
	}
33
}
34
35
type sendRequest struct {
36
	RequestID    string            `json:"request_id"`
37
	Receiver     string            `json:"receiver"`
38
	TemplateName string            `json:"template_name"`
39
	Options      map[string]string `json:"options"`
40
}
41
42
type SendVerificationEmailRequest struct {
43
	Receiver string
44
	Token    string
45
}
46
47
func (m MailerMQ) SendVerificationEmail(
48
	ctx context.Context,
49
	inp SendVerificationEmailRequest,
50
) error {
51
	req, err := json.Marshal(sendRequest{
52
		RequestID:    reqid.GetContext(ctx),
53
		Receiver:     inp.Receiver,
54
		TemplateName: "email_verification",
55
		Options: map[string]string{
56
			"token": inp.Token,
57
		},
58
	})
59
	if err != nil {
60
		return err
61
	}
62
63
	resp, err := m.nc.RequestWithContext(ctx, sendTopic, req)
64
	if err != nil {
65
		return err
66
	}
67
68
	return events.CheckRespForError(resp)
69
}
70
71
type SendPasswordResetEmailRequest struct {
72
	Receiver string
73
	Token    string
74
}
75
76
func (m MailerMQ) SendPasswordResetEmail(
77
	ctx context.Context,
78
	inp SendPasswordResetEmailRequest,
79
) error {
80
	req, err := json.Marshal(sendRequest{
81
		RequestID:    reqid.GetContext(ctx),
82
		Receiver:     inp.Receiver,
83
		TemplateName: "reset_password",
84
		Options: map[string]string{
85
			"token": inp.Token,
86
		},
87
	})
88
	if err != nil {
89
		return err
90
	}
91
92
	resp, err := m.nc.RequestWithContext(ctx, sendTopic, req)
93
	if err != nil {
94
		return err
95
	}
96
97
	return events.CheckRespForError(resp)
98
}
99
100
type SendChangeEmailConfirmationRequest struct {
101
	Receiver string
102
	Token    string
103
	NewEmail string
104
}
105
106
func (m MailerMQ) SendChangeEmailConfirmation(
107
	ctx context.Context,
108
	inp SendChangeEmailConfirmationRequest,
109
) error {
110
	req, err := json.Marshal(sendRequest{
111
		RequestID:    reqid.GetContext(ctx),
112
		Receiver:     inp.Receiver,
113
		TemplateName: "confirm_email_change",
114
		Options: map[string]string{
115
			"token": inp.Token,
116
			"email": inp.NewEmail,
117
		},
118
	})
119
	if err != nil {
120
		return err
121
	}
122
123
	resp, err := m.nc.RequestWithContext(ctx, sendTopic, req)
124
	if err != nil {
125
		return err
126
	}
127
128
	return events.CheckRespForError(resp)
129
}