mirror of
https://github.com/warmbly/warmbly.git
synced 2026-10-03 08:02:04 +00:00
feat: answer an out-of-range contact import list limit with the range that endpoint accepts (1 to 100, default 50) instead of the generic limit message
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -10,9 +11,16 @@ import (
|
||||
"github.com/warmbly/warmbly/internal/api/middleware"
|
||||
"github.com/warmbly/warmbly/internal/errx"
|
||||
"github.com/warmbly/warmbly/internal/models"
|
||||
"github.com/warmbly/warmbly/internal/utils/validate"
|
||||
)
|
||||
|
||||
// The imports list pages between 1 and contactImportListMax, 50 by default.
|
||||
const (
|
||||
contactImportListDefault = 50
|
||||
contactImportListMax = 100
|
||||
)
|
||||
|
||||
var errContactImportListLimit = errx.New(errx.BadRequest, "limit must be between 1 and 100")
|
||||
|
||||
// CreateContactImport is POST /contacts/imports: upload a file once and get a
|
||||
// draft back with the preview the column mapper needs.
|
||||
func (h *Handler) CreateContactImport(c *gin.Context) {
|
||||
@@ -49,16 +57,16 @@ func (h *Handler) ListContactImports(c *gin.Context) {
|
||||
errx.Handle(c, errx.ErrNoOrganization)
|
||||
return
|
||||
}
|
||||
limit, xerr := validate.Limit(c.Query("limit"))
|
||||
if xerr != nil {
|
||||
errx.Handle(c, xerr)
|
||||
return
|
||||
limit := contactImportListDefault
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 || n > contactImportListMax {
|
||||
errx.Handle(c, errContactImportListLimit)
|
||||
return
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
if limit < 1 || limit > 100 {
|
||||
errx.Handle(c, errx.ErrLimit)
|
||||
return
|
||||
}
|
||||
list, xerr := h.ContactImportService.List(c.Request.Context(), *orgID, c.Query("cursor"), int(limit))
|
||||
list, xerr := h.ContactImportService.List(c.Request.Context(), *orgID, c.Query("cursor"), limit)
|
||||
if xerr != nil {
|
||||
errx.Handle(c, xerr)
|
||||
return
|
||||
|
||||
@@ -168,7 +168,7 @@ func (s *Service) Get(ctx context.Context, orgID, id uuid.UUID) (*models.Contact
|
||||
// List pages the workspace's imports, newest first. The cursor is opaque.
|
||||
func (s *Service) List(ctx context.Context, orgID uuid.UUID, cursor string, limit int) (*models.ContactImportList, *errx.Error) {
|
||||
if limit < 1 {
|
||||
return nil, errx.ErrLimit
|
||||
return nil, errx.New(errx.BadRequest, "limit must be at least 1")
|
||||
}
|
||||
var before *time.Time
|
||||
var beforeID *uuid.UUID
|
||||
|
||||
Reference in New Issue
Block a user