forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhermes_user_email_factory.go
More file actions
212 lines (190 loc) · 6.32 KB
/
Copy pathhermes_user_email_factory.go
File metadata and controls
212 lines (190 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package emails
import (
"fmt"
"time"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/go-hermes/hermes/v2"
"github.com/palantir/stacktrace"
)
type hermesUserEmailFactory struct {
factory
config *HermesGeneratorConfig
generator hermes.Hermes
}
func (factory *hermesUserEmailFactory) APIKeyRotated(emailAddress string, timestamp time.Time, timezone string) (*Email, error) {
location, err := time.LoadLocation(timezone)
if err != nil {
location = time.UTC
}
email := hermes.Email{
Body: hermes.Body{
Intros: []string{
fmt.Sprintf("This is a confirmation email that your httpSMS API Key has been successfully rotated at %s.", timestamp.In(location).Format(time.RFC1123)),
},
Actions: []hermes.Action{
{
Instructions: "You can see your new API key in the httpSMS settings page.",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "httpSMS Settings",
Link: "https://httpsms.com/settings/",
},
},
},
Title: "Hey,",
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("If you did not trigger this API key rotation please contact us immediately by replying to this email."),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate text email")
}
return &Email{
ToEmail: emailAddress,
Subject: "Your httpSMS API Key has been rotated successfully",
HTML: html,
Text: text,
}, nil
}
// UsageLimitExceeded is the email sent when the plan limit is reached
func (factory *hermesUserEmailFactory) UsageLimitExceeded(user *entities.User) (*Email, error) {
email := hermes.Email{
Body: hermes.Body{
Intros: []string{
fmt.Sprintf("You have exceeded your limit of %d messages on your %s plan.", user.SubscriptionName.Limit(), user.SubscriptionName),
},
Actions: []hermes.Action{
{
Instructions: "Click the button below to upgrade your plan and continue sending more messages",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "UPGRADE YOUR PLAN",
Link: "https://httpsms.com/billing",
},
},
},
Title: "Hey,",
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email."),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: "⚠️ You have exceeded your plan limit",
HTML: html,
Text: text,
}, nil
}
// UsageLimitAlert is the email sent when the plan limit is reached
func (factory *hermesUserEmailFactory) UsageLimitAlert(user *entities.User, usage *entities.BillingUsage) (*Email, error) {
percent := (usage.TotalMessages() * 100) / user.SubscriptionName.Limit()
email := hermes.Email{
Body: hermes.Body{
Intros: []string{
fmt.Sprintf("This is a friendly notification that you have exceeded %d%% of your monthly SMS limit on the %s plan.", percent, user.SubscriptionName),
fmt.Sprintf("You have sent %d messages and received %d messages using httpSMS this month.", usage.SentMessages, usage.ReceivedMessages),
},
Actions: []hermes.Action{
{
Instructions: "Click the button below to upgrade your plan so you can continue without any disruptions",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "UPGRADE YOUR PLAN",
Link: "https://httpsms.com/billing",
},
},
},
Title: "Hey,",
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email."),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: fmt.Sprintf("⚠️ %d%% Usage Limit Alert", percent),
HTML: html,
Text: text,
}, nil
}
// NewHermesUserEmailFactory creates a new instance of the UserEmailFactory
func NewHermesUserEmailFactory(config *HermesGeneratorConfig) UserEmailFactory {
return &hermesUserEmailFactory{
config: config,
generator: config.Generator(),
}
}
// PhoneDead is the email sent to a user when their phone is dead
func (factory *hermesUserEmailFactory) PhoneDead(user *entities.User, lastHeartbeatTimestamp time.Time, owner string) (*Email, error) {
location, err := time.LoadLocation(user.Timezone)
if err != nil {
location = time.UTC
}
email := hermes.Email{
Body: hermes.Body{
Intros: []string{
fmt.Sprintf("We haven't received any heartbeat event from android phone %s since %s.", factory.formatPhoneNumber(owner), lastHeartbeatTimestamp.In(location).Format(time.RFC1123)),
fmt.Sprintf("Check if the mobile phone is powered on and if it has stable internet connection."),
},
Actions: []hermes.Action{
{
Instructions: "Check your heartbeat events on httpSMS",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "HEARTBEATS",
Link: fmt.Sprintf("https://httpsms.com/heartbeats/%s", owner),
},
},
},
Title: "Hey,",
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email. You can disable this email notification on https://httpsms.com/settings/#email-notifications"),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: fmt.Sprintf("⚠️ No heartbeat from android phone [%s]", factory.formatPhoneNumber(owner)),
HTML: html,
Text: text,
}, nil
}