forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_service.go
More file actions
337 lines (272 loc) · 12.2 KB
/
Copy pathuser_service.go
File metadata and controls
337 lines (272 loc) · 12.2 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package services
import (
"context"
"fmt"
"time"
"github.com/NdoleStudio/httpsms/pkg/events"
"github.com/NdoleStudio/httpsms/pkg/emails"
lemonsqueezy "github.com/NdoleStudio/lemonsqueezy-go"
"github.com/NdoleStudio/httpsms/pkg/repositories"
"github.com/google/uuid"
"github.com/palantir/stacktrace"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
)
// UserService is handles user requests
type UserService struct {
service
logger telemetry.Logger
tracer telemetry.Tracer
emailFactory emails.UserEmailFactory
mailer emails.Mailer
repository repositories.UserRepository
marketingService *MarketingService
lemonsqueezyClient *lemonsqueezy.Client
}
// NewUserService creates a new UserService
func NewUserService(
logger telemetry.Logger,
tracer telemetry.Tracer,
repository repositories.UserRepository,
mailer emails.Mailer,
emailFactory emails.UserEmailFactory,
marketingService *MarketingService,
lemonsqueezyClient *lemonsqueezy.Client,
) (s *UserService) {
return &UserService{
logger: logger.WithService(fmt.Sprintf("%T", s)),
tracer: tracer,
mailer: mailer,
marketingService: marketingService,
emailFactory: emailFactory,
repository: repository,
lemonsqueezyClient: lemonsqueezyClient,
}
}
// Get fetches or creates an entities.User
func (service *UserService) Get(ctx context.Context, authUser entities.AuthUser) (*entities.User, error) {
ctx, span := service.tracer.Start(ctx)
defer span.End()
user, isNew, err := service.repository.LoadOrStore(ctx, authUser)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with from [%+#v]", user, authUser)
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
if isNew {
service.marketingService.AddToList(ctx, user)
}
return user, nil
}
// GetByID fetches an entities.User
func (service *UserService) GetByID(ctx context.Context, userID entities.UserID) (*entities.User, error) {
ctx, span, _ := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
user, err := service.repository.Load(ctx, userID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with ID [%s]", user, userID)
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
return user, nil
}
// UserUpdateParams are parameters for updating an entities.User
type UserUpdateParams struct {
Timezone *time.Location
ActivePhoneID uuid.UUID
}
// Update an entities.User
func (service *UserService) Update(ctx context.Context, authUser entities.AuthUser, params UserUpdateParams) (*entities.User, error) {
ctx, span := service.tracer.Start(ctx)
defer span.End()
ctxLogger := service.tracer.CtxLogger(service.logger, span)
user, isNew, err := service.repository.LoadOrStore(ctx, authUser)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with from [%+#v]", user, authUser)
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
if isNew {
service.marketingService.AddToList(ctx, user)
}
user.Timezone = params.Timezone.String()
user.ActivePhoneID = ¶ms.ActivePhoneID
if err = service.repository.Update(ctx, user); err != nil {
msg := fmt.Sprintf("cannot save user with id [%s]", user.ID)
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
ctxLogger.Info(fmt.Sprintf("user saved with id [%s] in the userRepository", user.ID))
return user, nil
}
// UserNotificationUpdateParams are parameters for updating the notifications of a user
type UserNotificationUpdateParams struct {
MessageStatusEnabled bool
WebhookEnabled bool
HeartbeatEnabled bool
}
// UpdateNotificationSettings for an entities.User
func (service *UserService) UpdateNotificationSettings(ctx context.Context, userID entities.UserID, params *UserNotificationUpdateParams) (*entities.User, error) {
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
user, err := service.repository.Load(ctx, userID)
if err != nil {
msg := fmt.Sprintf("could not load [%T] with ID [%s]", user, userID)
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
user.NotificationWebhookEnabled = params.WebhookEnabled
user.NotificationHeartbeatEnabled = params.HeartbeatEnabled
user.NotificationMessageStatusEnabled = params.MessageStatusEnabled
if err = service.repository.Update(ctx, user); err != nil {
msg := fmt.Sprintf("cannot save user with id [%s] in [%T]", user.ID, service.repository)
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
ctxLogger.Info(fmt.Sprintf("updated notification settings for [%T] with ID [%s] in the [%T]", user, user.ID, service.repository))
return user, nil
}
// UserSendPhoneDeadEmailParams are parameters for notifying a user when a phone is dead
type UserSendPhoneDeadEmailParams struct {
UserID entities.UserID
PhoneID uuid.UUID
Owner string
LastHeartbeatTimestamp time.Time
}
// SendPhoneDeadEmail sends an email to an entities.User when a phone is dead
func (service *UserService) SendPhoneDeadEmail(ctx context.Context, params *UserSendPhoneDeadEmailParams) error {
ctx, span := service.tracer.Start(ctx)
defer span.End()
ctxLogger := service.tracer.CtxLogger(service.logger, span)
user, err := service.repository.Load(ctx, params.UserID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with ID [%s]", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
if !user.NotificationHeartbeatEnabled {
ctxLogger.Info(fmt.Sprintf("[%s] email notifications disabled for user [%s] with owner [%s]", events.EventTypePhoneHeartbeatOffline, params.UserID, params.Owner))
return nil
}
email, err := service.emailFactory.PhoneDead(user, params.LastHeartbeatTimestamp, params.Owner)
if err != nil {
msg := fmt.Sprintf("cannot create phone dead email for user [%s]", params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
if err = service.mailer.Send(ctx, email); err != nil {
msg := fmt.Sprintf("canot send phone dead notification to user [%s]", params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
ctxLogger.Info(fmt.Sprintf("phone dead notification sent successfully to [%s] about [%s]", user.Email, params.Owner))
return nil
}
// StartSubscription starts a subscription for an entities.User
func (service *UserService) StartSubscription(ctx context.Context, params *events.UserSubscriptionCreatedPayload) error {
ctx, span := service.tracer.Start(ctx)
defer span.End()
user, err := service.repository.Load(ctx, params.UserID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with with ID [%s]", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
user.SubscriptionID = ¶ms.SubscriptionID
user.SubscriptionName = params.SubscriptionName
user.SubscriptionRenewsAt = ¶ms.SubscriptionRenewsAt
user.SubscriptionStatus = ¶ms.SubscriptionStatus
user.SubscriptionEndsAt = nil
if err = service.repository.Update(ctx, user); err != nil {
msg := fmt.Sprintf("could not update [%T] with with ID [%s] after update", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
return nil
}
// InitiateSubscriptionCancel initiates the cancelling of a subscription on lemonsqueezy
func (service *UserService) InitiateSubscriptionCancel(ctx context.Context, userID entities.UserID) error {
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
user, err := service.repository.Load(ctx, userID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with with ID [%s]", user, userID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
if _, _, err = service.lemonsqueezyClient.Subscriptions.Cancel(ctx, *user.SubscriptionID); err != nil {
msg := fmt.Sprintf("could not cancel subscription [%s] for [%T] with with ID [%s]", *user.SubscriptionID, user, user.ID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
ctxLogger.Info(fmt.Sprintf("cancelled subscription [%s] for user [%s]", *user.SubscriptionID, user.ID))
return nil
}
// GetSubscriptionUpdateURL initiates the cancelling of a subscription on lemonsqueezy
func (service *UserService) GetSubscriptionUpdateURL(ctx context.Context, userID entities.UserID) (url string, err error) {
ctx, span := service.tracer.Start(ctx)
defer span.End()
user, err := service.repository.Load(ctx, userID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with with ID [%s]", user, userID)
return "", service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
subscription, _, err := service.lemonsqueezyClient.Subscriptions.Get(ctx, *user.SubscriptionID)
if err != nil {
msg := fmt.Sprintf("could not get subscription [%s] for [%T] with with ID [%s]", *user.SubscriptionID, user, user.ID)
return url, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
return subscription.Data.Attributes.Urls.UpdatePaymentMethod, nil
}
// CancelSubscription starts a subscription for an entities.User
func (service *UserService) CancelSubscription(ctx context.Context, params *events.UserSubscriptionCancelledPayload) error {
ctx, span := service.tracer.Start(ctx)
defer span.End()
user, err := service.repository.Load(ctx, params.UserID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with with ID [%s]", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
user.SubscriptionID = ¶ms.SubscriptionID
user.SubscriptionName = params.SubscriptionName
user.SubscriptionRenewsAt = nil
user.SubscriptionStatus = ¶ms.SubscriptionStatus
user.SubscriptionEndsAt = ¶ms.SubscriptionEndsAt
if err = service.repository.Update(ctx, user); err != nil {
msg := fmt.Sprintf("could not update [%T] with with ID [%s] after update", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
return nil
}
// ExpireSubscription finishes a subscription for an entities.User
func (service *UserService) ExpireSubscription(ctx context.Context, params *events.UserSubscriptionExpiredPayload) error {
ctx, span := service.tracer.Start(ctx)
defer span.End()
user, err := service.repository.Load(ctx, params.UserID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with with ID [%s]", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
user.SubscriptionID = nil
user.SubscriptionName = entities.SubscriptionNameFree
user.SubscriptionRenewsAt = nil
user.SubscriptionStatus = nil
user.SubscriptionEndsAt = nil
if err = service.repository.Update(ctx, user); err != nil {
msg := fmt.Sprintf("could not update [%T] with with ID [%s] after expired subscription update", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
return nil
}
// UpdateSubscription updates a subscription for an entities.User
func (service *UserService) UpdateSubscription(ctx context.Context, params *events.UserSubscriptionUpdatedPayload) error {
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()
user, err := service.repository.Load(ctx, params.UserID)
if err != nil {
msg := fmt.Sprintf("could not get [%T] with with ID [%s]", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
if params.SubscriptionStatus != "active" {
msg := fmt.Sprintf("subscription status is [%s] for [%T] with with ID [%s]", params.SubscriptionStatus, user, params.UserID)
ctxLogger.Info(msg)
return nil
}
user.SubscriptionID = ¶ms.SubscriptionID
user.SubscriptionName = params.SubscriptionName
user.SubscriptionEndsAt = params.SubscriptionEndsAt
user.SubscriptionRenewsAt = ¶ms.SubscriptionRenewsAt
user.SubscriptionStatus = ¶ms.SubscriptionStatus
if err = service.repository.Update(ctx, user); err != nil {
msg := fmt.Sprintf("could not update [%T] with with ID [%s] after subscription update", user, params.UserID)
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
return nil
}