feat: enhance limits

This commit is contained in:
Krispire Admin
2026-06-23 14:21:45 +05:30
parent eadef33eac
commit 96ebbfd719
12 changed files with 183 additions and 53 deletions
+34
View File
@@ -326,3 +326,37 @@ func (h *AdminHandler) setUserBanned(w http.ResponseWriter, r *http.Request, ban
}
response.JSON(w, http.StatusOK, map[string]any{"banned": banned})
}
// SetUserLimits — POST /admin/users/{id}/limits with body
// { "bookmark_limit": 10|null, "public_collection_limit": …, "private_collection_limit": … }.
// Each field is optional: a null (or omitted) value resets that limit to the
// plan default; a number caps that resource for this user regardless of plan.
func (h *AdminHandler) SetUserLimits(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
var in struct {
BookmarkLimit *int `json:"bookmark_limit"`
PublicCollectionLimit *int `json:"public_collection_limit"`
PrivateCollectionLimit *int `json:"private_collection_limit"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
response.Error(w, http.StatusBadRequest, "invalid request body")
return
}
const maxLimit = 100000
for _, v := range []*int{in.BookmarkLimit, in.PublicCollectionLimit, in.PrivateCollectionLimit} {
if v != nil && (*v < 0 || *v > maxLimit) {
response.Error(w, http.StatusBadRequest, "each limit must be between 0 and 100000")
return
}
}
ok, err := h.svc.SetUserLimits(r.Context(), id, in.BookmarkLimit, in.PublicCollectionLimit, in.PrivateCollectionLimit)
if err != nil {
response.Error(w, http.StatusInternalServerError, "could not update limits")
return
}
if !ok {
response.Error(w, http.StatusNotFound, "user not found")
return
}
response.JSON(w, http.StatusOK, map[string]any{"updated": true})
}