forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_handler.go
More file actions
347 lines (303 loc) · 14.3 KB
/
Copy pathuser_handler.go
File metadata and controls
347 lines (303 loc) · 14.3 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
338
339
340
341
342
343
344
345
346
347
package handlers
import (
"fmt"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/validators"
"github.com/davecgh/go-spew/spew"
"github.com/NdoleStudio/httpsms/pkg/services"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/gofiber/fiber/v2"
"github.com/palantir/stacktrace"
)
// UserHandler handles user http requests.
type UserHandler struct {
handler
logger telemetry.Logger
tracer telemetry.Tracer
validator *validators.UserHandlerValidator
service *services.UserService
}
// NewUserHandler creates a new UserHandler
func NewUserHandler(
logger telemetry.Logger,
tracer telemetry.Tracer,
validator *validators.UserHandlerValidator,
service *services.UserService,
) (h *UserHandler) {
return &UserHandler{
logger: logger.WithService(fmt.Sprintf("%T", h)),
tracer: tracer,
validator: validator,
service: service,
}
}
// RegisterRoutes registers the routes for the MessageHandler
func (h *UserHandler) RegisterRoutes(router fiber.Router, middlewares ...fiber.Handler) {
router.Get("/v1/users/me", h.computeRoute(middlewares, h.Show)...)
router.Put("/v1/users/me", h.computeRoute(middlewares, h.Update)...)
router.Delete("/v1/users/me", h.computeRoute(middlewares, h.Delete)...)
router.Delete("/v1/users/:userID/api-keys", h.computeRoute(middlewares, h.DeleteAPIKey)...)
router.Put("/v1/users/:userID/notifications", h.computeRoute(middlewares, h.UpdateNotifications)...)
router.Get("/v1/users/subscription-update-url", h.computeRoute(middlewares, h.subscriptionUpdateURL)...)
router.Delete("/v1/users/subscription", h.computeRoute(middlewares, h.cancelSubscription)...)
router.Get("/v1/users/subscription/payments", h.computeRoute(middlewares, h.subscriptionPayments)...)
router.Post("/v1/users/subscription/invoices/:subscriptionInvoiceID", h.computeRoute(middlewares, h.subscriptionInvoice)...)
}
// Show returns an entities.User
// @Summary Get current user
// @Description Get details of the currently authenticated user
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce json
// @Success 200 {object} responses.UserResponse
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/me [get]
func (h *UserHandler) Show(c *fiber.Ctx) error {
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
defer span.End()
authUser := h.userFromContext(c)
user, err := h.service.Get(ctx, c.OriginalURL(), authUser)
if err != nil {
msg := fmt.Sprintf("cannot get user with ID [%s]", authUser.ID)
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
return h.responseOK(c, "user fetched successfully", user)
}
// Update an entities.User
// @Summary Update a user
// @Description Updates the details of the currently authenticated user
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce json
// @Param payload body requests.UserUpdate true "Payload of user details to update"
// @Success 200 {object} responses.PhoneResponse
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/me [put]
func (h *UserHandler) Update(c *fiber.Ctx) error {
ctx, span := h.tracer.StartFromFiberCtx(c)
defer span.End()
ctxLogger := h.tracer.CtxLogger(h.logger, span)
var request requests.UserUpdate
if err := c.BodyParser(&request); err != nil {
msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.OriginalURL(), request)
ctxLogger.Warn(stacktrace.Propagate(err, msg))
return h.responseBadRequest(c, err)
}
if errors := h.validator.ValidateUpdate(ctx, request.Sanitize()); len(errors) != 0 {
msg := fmt.Sprintf("validation errors [%s], while updating user [%+#v]", spew.Sdump(errors), request)
ctxLogger.Warn(stacktrace.NewError(msg))
return h.responseUnprocessableEntity(c, errors, "validation errors while updating user")
}
user, err := h.service.Update(ctx, c.OriginalURL(), h.userFromContext(c), request.ToUpdateParams())
if err != nil {
msg := fmt.Sprintf("cannot update user with params [%+#v]", request)
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
return h.responseOK(c, "user updated successfully", user)
}
// Delete an entities.User
// @Summary Delete a user
// @Description Deletes the currently authenticated user together with all their data.
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce json
// @Success 201 {object} responses.NoContent
// @Failure 401 {object} responses.Unauthorized
// @Failure 500 {object} responses.InternalServerError
// @Router /users/me [delete]
func (h *UserHandler) Delete(c *fiber.Ctx) error {
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
defer span.End()
if err := h.service.Delete(ctx, c.OriginalURL(), h.userIDFomContext(c)); err != nil {
msg := fmt.Sprintf("cannot delete user user with ID [%s]", h.userIDFomContext(c))
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
return h.responseNoContent(c, "user deleted successfully")
}
// UpdateNotifications an entities.User
// @Summary Update notification settings
// @Description Update the email notification settings for a user
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce json
// @Param userID path string true "ID of the user to update" default(32343a19-da5e-4b1b-a767-3298a73703ca)
// @Param payload body requests.UserNotificationUpdate true "User notification details to update"
// @Success 200 {object} responses.UserResponse
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/{userID}/notifications [put]
func (h *UserHandler) UpdateNotifications(c *fiber.Ctx) error {
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
defer span.End()
var request requests.UserNotificationUpdate
if err := c.BodyParser(&request); err != nil {
msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.OriginalURL(), request)
ctxLogger.Warn(stacktrace.Propagate(err, msg))
return h.responseBadRequest(c, err)
}
user, err := h.service.UpdateNotificationSettings(ctx, h.userIDFomContext(c), request.ToUserNotificationUpdateParams())
if err != nil {
msg := fmt.Sprintf("cannot update notification for [%T] with ID [%s]", user, h.userIDFomContext(c))
ctxLogger.Error(h.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)))
return h.responseInternalServerError(c)
}
return h.responseOK(c, "user notification settings updated successfully", user)
}
// subscriptionUpdateURL returns the subscription update URL for the authenticated entities.User
// @Summary Currently authenticated user subscription update URL
// @Description Fetches the subscription URL of the authenticated user.
// @Security ApiKeyAuth
// @Tags Users
// @Produce json
// @Success 200 {object} responses.OkString
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/subscription-update-url [get]
func (h *UserHandler) subscriptionUpdateURL(c *fiber.Ctx) error {
ctx, span := h.tracer.StartFromFiberCtx(c)
defer span.End()
ctxLogger := h.tracer.CtxLogger(h.logger, span)
authUser := h.userFromContext(c)
url, err := h.service.GetSubscriptionUpdateURL(ctx, authUser.ID)
if err != nil {
msg := fmt.Sprintf("cannot get user with ID [%s]", authUser.ID)
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
return h.responseOK(c, "Subscription update URL fetched successfully", url)
}
// cancelSubscription cancels the subscription for the authenticated entities.User
// @Summary Cancel the user's subscription
// @Description Cancel the subscription of the authenticated user.
// @Security ApiKeyAuth
// @Tags Users
// @Produce json
// @Success 200 {object} responses.NoContent
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/subscription [delete]
func (h *UserHandler) cancelSubscription(c *fiber.Ctx) error {
ctx, span := h.tracer.StartFromFiberCtx(c)
defer span.End()
ctxLogger := h.tracer.CtxLogger(h.logger, span)
authUser := h.userFromContext(c)
err := h.service.InitiateSubscriptionCancel(ctx, authUser.ID)
if err != nil {
msg := fmt.Sprintf("cannot get user with ID [%s]", authUser.ID)
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
return h.responseNoContent(c, "Subscription cancelled successfully")
}
// DeleteAPIKey rotates the API Key for a user
// @Summary Rotate the user's API Key
// @Description Rotate the user's API key in case the current API Key is compromised
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce json
// @Param userID path string true "ID of the user to update" default(32343a19-da5e-4b1b-a767-3298a73703ca)
// @Success 200 {object} responses.UserResponse
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/{userID}/api-keys [delete]
func (h *UserHandler) DeleteAPIKey(c *fiber.Ctx) error {
ctx, span := h.tracer.StartFromFiberCtx(c)
defer span.End()
ctxLogger := h.tracer.CtxLogger(h.logger, span)
if c.Params("userID") != string(h.userIDFomContext(c)) {
return h.responseUnauthorized(c)
}
user, err := h.service.RotateAPIKey(ctx, c.OriginalURL(), h.userIDFomContext(c))
if err != nil {
msg := fmt.Sprintf("cannot rotate the api key for [%T] with ID [%s]", user, h.userIDFomContext(c))
ctxLogger.Error(h.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)))
return h.responseInternalServerError(c)
}
return h.responseOK(c, "API Key rotated successfully", user)
}
// subscriptionPayments returns the last 10 payments of the currently authenticated user
// @Summary Get the last 10 subscription payments.
// @Description Subscription payments are generated throughout the lifecycle of a subscription, typically there is one at the time of purchase and then one for each renewal.
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce json
// @Success 200 {object} responses.UserSubscriptionPaymentsResponse
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/subscription/payments [get]
func (h *UserHandler) subscriptionPayments(c *fiber.Ctx) error {
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
defer span.End()
invoices, err := h.service.GetSubscriptionPayments(ctx, h.userIDFomContext(c))
if err != nil {
msg := fmt.Sprintf("cannot get current subscription invoices for user [%s]", h.userFromContext(c))
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
return h.responseOK(c, "fetched subscription invoices billing usage", invoices)
}
// subscriptionInvoice generates an invoice for a given subscription invoice ID
// @Summary Generate a subscription payment invoice
// @Description Generates a new invoice PDF file for the given subscription payment with given parameters.
// @Security ApiKeyAuth
// @Tags Users
// @Accept json
// @Produce application/pdf
// @Param payload body requests.UserPaymentInvoice true "Generate subscription payment invoice parameters"
// @Param subscriptionInvoiceID path string true "ID of the subscription invoice to generate the PDF for"
// @Success 200 {file} file
// @Failure 400 {object} responses.BadRequest
// @Failure 401 {object} responses.Unauthorized
// @Failure 422 {object} responses.UnprocessableEntity
// @Failure 500 {object} responses.InternalServerError
// @Router /users/subscription/invoices/{subscriptionInvoiceID} [post]
func (h *UserHandler) subscriptionInvoice(c *fiber.Ctx) error {
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
defer span.End()
var request requests.UserPaymentInvoice
if err := c.BodyParser(&request); err != nil {
msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.Body(), request)
ctxLogger.Warn(stacktrace.Propagate(err, msg))
return h.responseBadRequest(c, err)
}
request.SubscriptionInvoiceID = c.Params("subscriptionInvoiceID")
if errors := h.validator.ValidatePaymentInvoice(ctx, h.userIDFomContext(c), request.Sanitize()); len(errors) != 0 {
msg := fmt.Sprintf("validation errors [%s], while validating subscription payment invoice request [%s]", spew.Sdump(errors), c.Body())
ctxLogger.Warn(stacktrace.NewError(msg))
return h.responseUnprocessableEntity(c, errors, "validation errors while generating payment invoice")
}
data, err := h.service.GenerateReceipt(ctx, request.UserInvoiceGenerateParams(h.userIDFomContext(c)))
if err != nil {
msg := fmt.Sprintf("cannot generate receipt for invoice ID [%s] and user [%s]", request.SubscriptionInvoiceID, h.userFromContext(c))
ctxLogger.Error(stacktrace.Propagate(err, msg))
return h.responseInternalServerError(c)
}
c.Set(fiber.HeaderContentType, "application/pdf")
c.Set(fiber.HeaderContentDisposition, fmt.Sprintf("attachment; filename=\"httpsms.com - %s.pdf\"", request.SubscriptionInvoiceID))
return c.SendStream(data)
}