MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / BulkSend

Method BulkSend

api/pkg/handlers/message_handler.go:131–177  ·  view source on GitHub ↗

BulkSend a bulk entities.Message @Summary Send bulk SMS messages @Description Add bulk SMS messages to be sent by the android phone @Security ApiKeyAuth @Tags Messages @Accept json @Produce json @Param payload body requests.MessageBulkSend true "Bulk send message

(c fiber.Ctx)

Source from the content-addressed store, hash-verified

129// @Failure 500 {object} responses.InternalServerError
130// @Router /messages/bulk-send [post]
131func (h *MessageHandler) BulkSend(c fiber.Ctx) error {
132 ctx, span := h.tracer.StartFromFiberCtx(c)
133 defer span.End()
134
135 ctxLogger := h.tracer.CtxLogger(h.logger, span)
136
137 var request requests.MessageBulkSend
138 if err := c.Bind().Body(&request); err != nil {
139 msg := fmt.Sprintf("cannot marshall [%s] into %T", c.Body(), request)
140 ctxLogger.Warn(stacktrace.Propagate(err, msg))
141 return h.responseBadRequest(c, err)
142 }
143
144 if errors := h.validator.ValidateMessageBulkSend(ctx, h.userIDFomContext(c), request.Sanitize()); len(errors) != 0 {
145 msg := fmt.Sprintf("validation errors [%s], while sending payload [%s]", spew.Sdump(errors), c.Body())
146 ctxLogger.Warn(stacktrace.NewError(msg))
147 return h.responseUnprocessableEntity(c, errors, "validation errors while sending messages")
148 }
149
150 if msg := h.billingService.IsEntitledWithCount(ctx, h.userIDFomContext(c), uint(len(request.To))); msg != nil {
151 ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("user with ID [%s] is not entitled to send [%d] messages", h.userIDFomContext(c), len(request.To))))
152 return h.responsePaymentRequired(c, *msg)
153 }
154
155 wg := sync.WaitGroup{}
156 params := request.ToMessageSendParams(h.userIDFomContext(c), c.OriginalURL())
157 responses := make([]*entities.Message, len(params))
158 count := atomic.Int64{}
159
160 for index, message := range params {
161 wg.Add(1)
162 go func(message services.MessageSendParams, index int) {
163 count.Add(1)
164 response, err := h.service.SendMessage(ctx, message)
165 if err != nil {
166 count.Add(-1)
167 msg := fmt.Sprintf("cannot send message with paylod [%s] at index [%d]", spew.Sdump(message), index)
168 ctxLogger.Error(stacktrace.Propagate(err, msg))
169 }
170 responses[index] = response
171 wg.Done()
172 }(message, index)
173 }
174
175 wg.Wait()
176 return h.responseOK(c, fmt.Sprintf("%d out of %d messages processed successfully", count.Load(), len(responses)), responses)
177}
178
179// GetOutstanding returns an entities.Message which is still to be sent by the mobile phone
180// @Summary Get an outstanding message

Callers

nothing calls this directly

Calls 15

SanitizeMethod · 0.95
ToMessageSendParamsMethod · 0.95
responseBadRequestMethod · 0.80
userIDFomContextMethod · 0.80
IsEntitledWithCountMethod · 0.80
SendMessageMethod · 0.80
responseOKMethod · 0.80
StartFromFiberCtxMethod · 0.65
CtxLoggerMethod · 0.65

Tested by

no test coverage detected