Store the heartbeat of a phone number @Summary Register heartbeat of an owner phone number @Description Store the heartbeat to make notify that a phone number is still active @Security ApiKeyAuth @Tags Heartbeats @Accept json @Produce json @Param payload body re
(c fiber.Ctx)
| 110 | // @Failure 500 {object} responses.InternalServerError |
| 111 | // @Router /heartbeats [post] |
| 112 | func (h *HeartbeatHandler) Store(c fiber.Ctx) error { |
| 113 | ctx, span := h.tracer.StartFromFiberCtx(c) |
| 114 | defer span.End() |
| 115 | |
| 116 | ctxLogger := h.tracer.CtxLogger(h.logger, span) |
| 117 | |
| 118 | var request requests.HeartbeatStore |
| 119 | if err := c.Bind().Body(&request); err != nil { |
| 120 | msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.OriginalURL(), request) |
| 121 | ctxLogger.Warn(stacktrace.Propagate(err, msg)) |
| 122 | return h.responseBadRequest(c, err) |
| 123 | } |
| 124 | |
| 125 | if errors := h.validator.ValidateStore(ctx, request.Sanitize()); len(errors) != 0 { |
| 126 | msg := fmt.Sprintf("validation errors [%s], while storing heartbeat [%+#v]", spew.Sdump(errors), request) |
| 127 | ctxLogger.Warn(stacktrace.NewError(msg)) |
| 128 | return h.responseUnprocessableEntity(c, errors, "validation errors while storing heartbeat") |
| 129 | } |
| 130 | |
| 131 | for _, phoneNumber := range request.PhoneNumbers { |
| 132 | if !h.authorizePhoneAPIKey(c, phoneNumber) { |
| 133 | ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("phone API Key ID [%s] is not authorized to store heartbeat for phone number [%s]", h.userFromContext(c).PhoneAPIKeyID, phoneNumber))) |
| 134 | return h.responsePhoneAPIKeyUnauthorized(c, phoneNumber, h.userFromContext(c)) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | params := request.ToStoreParams(h.userFromContext(c), c.OriginalURL(), c.Get("X-Client-Version")) |
| 139 | |
| 140 | wg := sync.WaitGroup{} |
| 141 | responses := make([]*entities.Heartbeat, len(params)) |
| 142 | for index, value := range params { |
| 143 | wg.Add(1) |
| 144 | go func(input services.HeartbeatStoreParams, index int) { |
| 145 | response, err := h.service.Store(ctx, input) |
| 146 | if err != nil { |
| 147 | msg := fmt.Sprintf("cannot store heartbeat with params [%+#v]", request) |
| 148 | ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 149 | } |
| 150 | responses[index] = response |
| 151 | wg.Done() |
| 152 | }(value, index) |
| 153 | } |
| 154 | |
| 155 | wg.Wait() |
| 156 | return h.responseCreated(c, fmt.Sprintf("[%d] heartbeats received successfully", len(responses)), responses) |
| 157 | } |
nothing calls this directly
no test coverage detected