Skip to content

Commit 9de4156

Browse files
committed
fix: handle review feedback
1 parent 6cfa208 commit 9de4156

7 files changed

Lines changed: 884 additions & 254 deletions

File tree

api/pkg/handlers/send_schedule_handler.go

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@ type SendScheduleHandler struct {
2121
service *services.SendScheduleService
2222
}
2323

24-
func NewSendScheduleHandler(logger telemetry.Logger, tracer telemetry.Tracer, validator *validators.SendScheduleHandlerValidator, service *services.SendScheduleService) *SendScheduleHandler {
25-
return &SendScheduleHandler{logger: logger.WithService(fmt.Sprintf("%T", &SendScheduleHandler{})), tracer: tracer, validator: validator, service: service}
24+
func NewSendScheduleHandler(
25+
logger telemetry.Logger,
26+
tracer telemetry.Tracer,
27+
validator *validators.SendScheduleHandlerValidator,
28+
service *services.SendScheduleService,
29+
) *SendScheduleHandler {
30+
return &SendScheduleHandler{
31+
logger: logger.WithService(fmt.Sprintf("%T", &SendScheduleHandler{})),
32+
tracer: tracer,
33+
validator: validator,
34+
service: service,
35+
}
2636
}
2737

2838
func (h *SendScheduleHandler) RegisterRoutes(router fiber.Router, middlewares ...fiber.Handler) {
2939
router.Get("/v1/send-schedules", h.computeRoute(middlewares, h.Index)...)
3040
router.Post("/v1/send-schedules", h.computeRoute(middlewares, h.Store)...)
31-
router.Get("/v1/send-schedules/:scheduleID", h.computeRoute(middlewares, h.Show)...)
3241
router.Put("/v1/send-schedules/:scheduleID", h.computeRoute(middlewares, h.Update)...)
3342
router.Delete("/v1/send-schedules/:scheduleID", h.computeRoute(middlewares, h.Delete)...)
3443
}
3544

3645
// Index godoc
3746
// @Summary List send schedules
38-
// @Description Lists the send schedules owned by the authenticated user.
47+
// @Description List all send schedules owned by the authenticated user.
3948
// @Security ApiKeyAuth
4049
// @Tags Send Schedules
4150
// @Produce json
@@ -46,47 +55,19 @@ func (h *SendScheduleHandler) RegisterRoutes(router fiber.Router, middlewares ..
4655
func (h *SendScheduleHandler) Index(c *fiber.Ctx) error {
4756
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
4857
defer span.End()
58+
4959
schedules, err := h.service.Index(ctx, h.userIDFomContext(c))
5060
if err != nil {
5161
ctxLogger.Error(stacktrace.Propagate(err, "cannot list send schedules"))
5262
return h.responseInternalServerError(c)
5363
}
54-
return h.responseOK(c, "send schedules fetched successfully", schedules)
55-
}
5664

57-
// Show godoc
58-
// @Summary Show send schedule
59-
// @Description Loads a single send schedule owned by the authenticated user.
60-
// @Security ApiKeyAuth
61-
// @Tags Send Schedules
62-
// @Produce json
63-
// @Param scheduleID path string true "Schedule ID"
64-
// @Success 200 {object} responses.SendScheduleResponse
65-
// @Failure 401 {object} responses.Unauthorized
66-
// @Failure 404 {object} responses.NotFound
67-
// @Failure 500 {object} responses.InternalServerError
68-
// @Router /send-schedules/{scheduleID} [get]
69-
func (h *SendScheduleHandler) Show(c *fiber.Ctx) error {
70-
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
71-
defer span.End()
72-
scheduleID, err := uuid.Parse(c.Params("scheduleID"))
73-
if err != nil {
74-
return h.responseBadRequest(c, err)
75-
}
76-
schedule, err := h.service.Load(ctx, h.userIDFomContext(c), scheduleID)
77-
if err != nil {
78-
ctxLogger.Error(stacktrace.Propagate(err, "cannot load send schedule"))
79-
if stacktrace.GetCode(err) == 404 {
80-
return h.responseNotFound(c, err.Error())
81-
}
82-
return h.responseInternalServerError(c)
83-
}
84-
return h.responseOK(c, "send schedule fetched successfully", schedule)
65+
return h.responseOK(c, "send schedules fetched successfully", schedules)
8566
}
8667

8768
// Store godoc
8869
// @Summary Create send schedule
89-
// @Description Creates a send schedule for the authenticated user.
70+
// @Description Create a new send schedule for the authenticated user.
9071
// @Security ApiKeyAuth
9172
// @Tags Send Schedules
9273
// @Accept json
@@ -101,26 +82,34 @@ func (h *SendScheduleHandler) Show(c *fiber.Ctx) error {
10182
func (h *SendScheduleHandler) Store(c *fiber.Ctx) error {
10283
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
10384
defer span.End()
85+
10486
var request requests.SendScheduleStore
10587
if err := c.BodyParser(&request); err != nil {
10688
return h.responseBadRequest(c, err)
10789
}
90+
10891
request = request.Sanitize()
10992
if errors := h.validator.ValidateStore(ctx, request); len(errors) != 0 {
110-
ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("validation errors [%s], while storing send schedule [%+#v]", spew.Sdump(errors), request)))
93+
ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf(
94+
"validation errors [%s], while storing send schedule [%+#v]",
95+
spew.Sdump(errors),
96+
request,
97+
)))
11198
return h.responseUnprocessableEntity(c, errors, "validation errors while saving send schedule")
11299
}
100+
113101
schedule, err := h.service.Store(ctx, request.ToParams(h.userFromContext(c)))
114102
if err != nil {
115103
ctxLogger.Error(stacktrace.Propagate(err, "cannot create send schedule"))
116104
return h.responseInternalServerError(c)
117105
}
106+
118107
return h.responseCreated(c, "send schedule created successfully", schedule)
119108
}
120109

121110
// Update godoc
122111
// @Summary Update send schedule
123-
// @Description Updates a send schedule owned by the authenticated user.
112+
// @Description Update a send schedule owned by the authenticated user.
124113
// @Security ApiKeyAuth
125114
// @Tags Send Schedules
126115
// @Accept json
@@ -137,51 +126,68 @@ func (h *SendScheduleHandler) Store(c *fiber.Ctx) error {
137126
func (h *SendScheduleHandler) Update(c *fiber.Ctx) error {
138127
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
139128
defer span.End()
129+
140130
scheduleID, err := uuid.Parse(c.Params("scheduleID"))
141131
if err != nil {
142132
return h.responseBadRequest(c, err)
143133
}
134+
144135
var request requests.SendScheduleStore
145136
if err = c.BodyParser(&request); err != nil {
146137
return h.responseBadRequest(c, err)
147138
}
139+
148140
request = request.Sanitize()
149141
if errors := h.validator.ValidateStore(ctx, request); len(errors) != 0 {
150142
return h.responseUnprocessableEntity(c, errors, "validation errors while updating send schedule")
151143
}
152-
schedule, err := h.service.Update(ctx, h.userIDFomContext(c), scheduleID, request.ToParams(h.userFromContext(c)))
144+
145+
schedule, err := h.service.Update(
146+
ctx,
147+
h.userIDFomContext(c),
148+
scheduleID,
149+
request.ToParams(h.userFromContext(c)),
150+
)
153151
if err != nil {
154152
ctxLogger.Error(stacktrace.Propagate(err, "cannot update send schedule"))
155153
if stacktrace.GetCode(err) == 404 {
156154
return h.responseNotFound(c, err.Error())
157155
}
158156
return h.responseInternalServerError(c)
159157
}
158+
160159
return h.responseOK(c, "send schedule updated successfully", schedule)
161160
}
162161

163162
// Delete godoc
164163
// @Summary Delete send schedule
165-
// @Description Deletes a send schedule owned by the authenticated user.
164+
// @Description Delete a send schedule owned by the authenticated user.
166165
// @Security ApiKeyAuth
167166
// @Tags Send Schedules
168167
// @Produce json
169168
// @Param scheduleID path string true "Schedule ID"
170169
// @Success 204 {object} responses.NoContent
171170
// @Failure 400 {object} responses.BadRequest
172171
// @Failure 401 {object} responses.Unauthorized
172+
// @Failure 404 {object} responses.NotFound
173173
// @Failure 500 {object} responses.InternalServerError
174174
// @Router /send-schedules/{scheduleID} [delete]
175175
func (h *SendScheduleHandler) Delete(c *fiber.Ctx) error {
176176
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
177177
defer span.End()
178+
178179
scheduleID, err := uuid.Parse(c.Params("scheduleID"))
179180
if err != nil {
180181
return h.responseBadRequest(c, err)
181182
}
183+
182184
if err = h.service.Delete(ctx, h.userIDFomContext(c), scheduleID); err != nil {
183185
ctxLogger.Error(stacktrace.Propagate(err, "cannot delete send schedule"))
186+
if stacktrace.GetCode(err) == 404 {
187+
return h.responseNotFound(c, err.Error())
188+
}
184189
return h.responseInternalServerError(c)
185190
}
191+
186192
return h.responseNoContent(c, "send schedule deleted successfully")
187193
}

api/pkg/services/phone_notification_service.go

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewNotificationService(
4040
dispatcher *EventDispatcher,
4141
) (s *PhoneNotificationService) {
4242
return &PhoneNotificationService{
43-
logger: logger.WithService(fmt.Sprintf("%T", s)),
43+
logger: logger.WithService(fmt.Sprintf("%T", &PhoneNotificationService{})),
4444
tracer: tracer,
4545
messagingClient: messagingClient,
4646
phoneNotificationRepository: phoneNotificationRepository,
@@ -95,7 +95,13 @@ func (service *PhoneNotificationService) SendHeartbeatFCM(ctx context.Context, p
9595
return nil
9696
}
9797

98-
ctxLogger.Info(fmt.Sprintf("successfully sent heartbeat FCM [%s] to phone with ID [%s] for user [%s] and monitor [%s]", result, payload.PhoneID, payload.UserID, payload.MonitorID))
98+
ctxLogger.Info(fmt.Sprintf(
99+
"successfully sent heartbeat FCM [%s] to phone with ID [%s] for user [%s] and monitor [%s]",
100+
result,
101+
payload.PhoneID,
102+
payload.UserID,
103+
payload.MonitorID,
104+
))
99105
return nil
100106
}
101107

@@ -137,7 +143,15 @@ func (service *PhoneNotificationService) Send(ctx context.Context, params *Phone
137143
Token: *phone.FcmToken,
138144
})
139145
if err != nil {
140-
ctxLogger.Warn(stacktrace.Propagate(err, fmt.Sprintf("cannot send FCM to phone with ID [%s] for user with ID [%s] and message [%s]", phone.ID, phone.UserID, params.MessageID)))
146+
ctxLogger.Warn(stacktrace.Propagate(
147+
err,
148+
fmt.Sprintf(
149+
"cannot send FCM to phone with ID [%s] for user with ID [%s] and message [%s]",
150+
phone.ID,
151+
phone.UserID,
152+
params.MessageID,
153+
),
154+
))
141155
msg := fmt.Sprintf("cannot send notification for to your phone [%s]. Reinstall the httpSMS app on your Android phone.", phone.PhoneNumber)
142156
return service.handleNotificationFailed(ctx, errors.New(msg), params)
143157
}
@@ -184,12 +198,13 @@ func (service *PhoneNotificationService) Schedule(ctx context.Context, params *P
184198
var schedule *entities.SendSchedule
185199
if phone.ScheduleID != nil {
186200
schedule, err = service.sendScheduleRepository.Load(ctx, params.UserID, *phone.ScheduleID)
187-
if err != nil && stacktrace.GetCode(err) != repositories.ErrCodeNotFound {
188-
msg := fmt.Sprintf("cannot load send schedule [%s] for phone [%s]", *phone.ScheduleID, phone.ID)
189-
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
190-
}
191201
if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
192202
schedule = nil
203+
err = nil
204+
}
205+
if err != nil {
206+
msg := fmt.Sprintf("cannot load send schedule [%s] for phone [%s]", *phone.ScheduleID, phone.ID)
207+
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
193208
}
194209
}
195210

@@ -206,11 +221,20 @@ func (service *PhoneNotificationService) Schedule(ctx context.Context, params *P
206221
return service.tracer.WrapErrorSpan(span, err)
207222
}
208223

209-
ctxLogger.Info(fmt.Sprintf("message with id [%s] notification scheduled for [%s] with id [%s]", params.MessageID, notification.ScheduledAt, notification.ID))
224+
ctxLogger.Info(fmt.Sprintf(
225+
"message with id [%s] notification scheduled for [%s] with id [%s]",
226+
params.MessageID,
227+
notification.ScheduledAt,
228+
notification.ID,
229+
))
210230
return nil
211231
}
212232

213-
func (service *PhoneNotificationService) dispatchMessageNotificationSend(ctx context.Context, source string, notification *entities.PhoneNotification) error {
233+
func (service *PhoneNotificationService) dispatchMessageNotificationSend(
234+
ctx context.Context,
235+
source string,
236+
notification *entities.PhoneNotification,
237+
) error {
214238
event, err := service.createMessageNotificationSendEvent(source, &events.MessageNotificationSendPayload{
215239
MessageID: notification.MessageID,
216240
UserID: notification.UserID,
@@ -228,7 +252,11 @@ func (service *PhoneNotificationService) dispatchMessageNotificationSend(ctx con
228252
return nil
229253
}
230254

231-
func (service *PhoneNotificationService) dispatchMessageNotificationScheduled(ctx context.Context, params *PhoneNotificationScheduleParams, notification *entities.PhoneNotification) error {
255+
func (service *PhoneNotificationService) dispatchMessageNotificationScheduled(
256+
ctx context.Context,
257+
params *PhoneNotificationScheduleParams,
258+
notification *entities.PhoneNotification,
259+
) error {
232260
event, err := service.createMessageNotificationScheduledEvent(params.Source, &events.MessageNotificationScheduledPayload{
233261
MessageID: notification.MessageID,
234262
Owner: params.Owner,
@@ -273,7 +301,12 @@ func (service *PhoneNotificationService) handleNotificationFailed(ctx context.Co
273301
return nil
274302
}
275303

276-
func (service *PhoneNotificationService) handleNotificationSent(ctx context.Context, phone *entities.Phone, result string, params *PhoneNotificationSendParams) error {
304+
func (service *PhoneNotificationService) handleNotificationSent(
305+
ctx context.Context,
306+
phone *entities.Phone,
307+
result string,
308+
params *PhoneNotificationSendParams,
309+
) error {
277310
ctx, span := service.tracer.Start(ctx)
278311
defer span.End()
279312

@@ -294,15 +327,26 @@ func (service *PhoneNotificationService) handleNotificationSent(ctx context.Cont
294327
return nil
295328
}
296329

297-
func (service *PhoneNotificationService) createMessageNotificationScheduledEvent(source string, payload *events.MessageNotificationScheduledPayload) (cloudevents.Event, error) {
330+
func (service *PhoneNotificationService) createMessageNotificationScheduledEvent(
331+
source string,
332+
payload *events.MessageNotificationScheduledPayload,
333+
) (cloudevents.Event, error) {
298334
return service.createEvent(events.EventTypeMessageNotificationScheduled, source, payload)
299335
}
300336

301-
func (service *PhoneNotificationService) createMessageNotificationSendEvent(source string, payload *events.MessageNotificationSendPayload) (cloudevents.Event, error) {
337+
func (service *PhoneNotificationService) createMessageNotificationSendEvent(
338+
source string,
339+
payload *events.MessageNotificationSendPayload,
340+
) (cloudevents.Event, error) {
302341
return service.createEvent(events.EventTypeMessageNotificationSend, source, payload)
303342
}
304343

305-
func (service *PhoneNotificationService) createMessageNotificationSentEvent(source string, phone *entities.Phone, fcmMessageID string, params *PhoneNotificationSendParams) (cloudevents.Event, error) {
344+
func (service *PhoneNotificationService) createMessageNotificationSentEvent(
345+
source string,
346+
phone *entities.Phone,
347+
fcmMessageID string,
348+
params *PhoneNotificationSendParams,
349+
) (cloudevents.Event, error) {
306350
event := cloudevents.NewEvent()
307351

308352
event.SetSource(source)
@@ -329,7 +373,11 @@ func (service *PhoneNotificationService) createMessageNotificationSentEvent(sour
329373
return event, nil
330374
}
331375

332-
func (service *PhoneNotificationService) createMessageNotificationFailedEvent(source string, errorMessage string, params *PhoneNotificationSendParams) (cloudevents.Event, error) {
376+
func (service *PhoneNotificationService) createMessageNotificationFailedEvent(
377+
source string,
378+
errorMessage string,
379+
params *PhoneNotificationSendParams,
380+
) (cloudevents.Event, error) {
333381
event := cloudevents.NewEvent()
334382

335383
event.SetSource(source)
@@ -354,17 +402,21 @@ func (service *PhoneNotificationService) createMessageNotificationFailedEvent(so
354402
return event, nil
355403
}
356404

357-
func (service *PhoneNotificationService) updateStatus(ctx context.Context, notificationID uuid.UUID, status entities.PhoneNotificationStatus) {
405+
func (service *PhoneNotificationService) updateStatus(
406+
ctx context.Context,
407+
notificationID uuid.UUID,
408+
status entities.PhoneNotificationStatus,
409+
) {
358410
ctx, span := service.tracer.Start(ctx)
359411
defer span.End()
360412

361413
ctxLogger := service.tracer.CtxLogger(service.logger, span)
362414

363415
err := service.phoneNotificationRepository.UpdateStatus(ctx, notificationID, status)
364416
if err != nil {
365-
msg := fmt.Sprintf("cannot update status of notificaiton with id [%s] to [%s]", notificationID, status)
417+
msg := fmt.Sprintf("cannot update status of notification with id [%s] to [%s]", notificationID, status)
366418
ctxLogger.Error(stacktrace.Propagate(err, msg))
367419
}
368420

369-
ctxLogger.Info(fmt.Sprintf("updated status of notificaiton with id [%s] to [%s]", notificationID, status))
421+
ctxLogger.Info(fmt.Sprintf("updated status of notification with id [%s] to [%s]", notificationID, status))
370422
}

0 commit comments

Comments
 (0)