Skip to content

Commit 48f9b0a

Browse files
committed
Add webhook restriction
1 parent 4ae351e commit 48f9b0a

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

api/pkg/handlers/webhook_handler.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package handlers
33
import (
44
"fmt"
55

6+
"github.com/NdoleStudio/httpsms/pkg/repositories"
7+
68
"github.com/NdoleStudio/httpsms/pkg/requests"
79
"github.com/google/uuid"
810

@@ -158,14 +160,25 @@ func (h *WebhookHandler) Store(c *fiber.Ctx) error {
158160
return h.responseUnprocessableEntity(c, errors, "validation errors while storing webhook")
159161
}
160162

161-
heartbeat, err := h.service.Store(ctx, request.ToStoreParams(h.userFromContext(c)))
163+
webhooks, err := h.service.Index(ctx, h.userIDFomContext(c), repositories.IndexParams{Skip: 0, Limit: 1})
164+
if err != nil {
165+
ctxLogger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot index webhooks for user [%s]", h.userIDFomContext(c))))
166+
return h.responsePaymentRequired(c, "You can't create more than 1 webhook contact us to upgrade your account.")
167+
}
168+
169+
if len(webhooks) > 0 {
170+
ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("user with ID [%s] wants to create more than 1 webhook", h.userIDFomContext(c))))
171+
return h.responsePaymentRequired(c, "You can't create more than 1 webhook contact us to upgrade your account.")
172+
}
173+
174+
webhook, err := h.service.Store(ctx, request.ToStoreParams(h.userFromContext(c)))
162175
if err != nil {
163176
msg := fmt.Sprintf("cannot store webhoook with params [%+#v]", request)
164177
ctxLogger.Error(stacktrace.Propagate(err, msg))
165178
return h.responseInternalServerError(c)
166179
}
167180

168-
return h.responseCreated(c, "webhook created successfully", heartbeat)
181+
return h.responseCreated(c, "webhook created successfully", webhook)
169182
}
170183

171184
// Update an entities.Webhook

0 commit comments

Comments
 (0)