fixes
This commit is contained in:
1 parent
6586722652
commit
1e7c7bbfbb
65 files changed
+1345
-640
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var editableCostFeatures = map[string]bool{
|
||||
"product_processing": true,
|
||||
"openai_token_k": true,
|
||||
"seo_meta_ai": true,
|
||||
"campaign_copy": true,
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminGetProcessingCosts(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := s.Pool.Query(r.Context(), `
|
||||
SELECT 'default', '', feature_name, cost_per_unit FROM processing_costs WHERE is_active = true
|
||||
UNION ALL
|
||||
SELECT 'plan', plan_id::text, feature_name, cost_per_unit FROM plan_processing_costs
|
||||
UNION ALL
|
||||
SELECT 'company', company_id::text, feature_name, cost_per_unit FROM company_processing_costs
|
||||
ORDER BY 1, 2, 3`)
|
||||
if err != nil {
|
||||
LogAndError(w, http.StatusInternalServerError, "could not load processing costs", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]map[string]any, 0, 16)
|
||||
for rows.Next() {
|
||||
var scope, id, feature string
|
||||
var cost int
|
||||
if err := rows.Scan(&scope, &id, &feature, &cost); err != nil {
|
||||
LogAndError(w, http.StatusInternalServerError, "could not scan processing costs", err)
|
||||
return
|
||||
}
|
||||
out = append(out, map[string]any{"scope": scope, "id": id, "feature": feature, "cost_per_unit": cost})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
LogAndError(w, http.StatusInternalServerError, "could not read processing costs", err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
JSON(w, http.StatusOK, map[string]any{"rules": out, "notes": "Company overrides take priority over plan rules. Changes affect future debits only."})
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminPutProcessingCost(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Scope string `json:"scope"`
|
||||
ID string `json:"id"`
|
||||
Feature string `json:"feature"`
|
||||
Cost *int `json:"cost_per_unit"` // null removes an override; cannot remove global rule
|
||||
}
|
||||
if err := DecodeJSON(r, &body); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid json")
|
||||
return
|
||||
}
|
||||
body.Scope = strings.TrimSpace(strings.ToLower(body.Scope))
|
||||
body.Feature = strings.TrimSpace(body.Feature)
|
||||
if !editableCostFeatures[body.Feature] || (body.Cost != nil && (*body.Cost < 1 || *body.Cost > 1000000)) {
|
||||
Error(w, http.StatusBadRequest, "invalid feature or credit cost")
|
||||
return
|
||||
}
|
||||
ctx := r.Context()
|
||||
var err error
|
||||
switch body.Scope {
|
||||
case "company":
|
||||
id, parseErr := uuid.Parse(body.ID)
|
||||
if parseErr != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid company id")
|
||||
return
|
||||
}
|
||||
if body.Cost == nil {
|
||||
_, err = s.Pool.Exec(ctx, `DELETE FROM company_processing_costs WHERE company_id = $1 AND feature_name = $2`, id, body.Feature)
|
||||
} else {
|
||||
_, err = s.Pool.Exec(ctx, `INSERT INTO company_processing_costs (company_id,feature_name,cost_per_unit) VALUES ($1,$2,$3)
|
||||
ON CONFLICT(company_id,feature_name) DO UPDATE SET cost_per_unit=EXCLUDED.cost_per_unit,updated_at=now()`, id, body.Feature, *body.Cost)
|
||||
}
|
||||
case "plan":
|
||||
id, parseErr := strconv.ParseInt(body.ID, 10, 64)
|
||||
if parseErr != nil || id <= 0 {
|
||||
Error(w, http.StatusBadRequest, "invalid plan id")
|
||||
return
|
||||
}
|
||||
if body.Cost == nil {
|
||||
_, err = s.Pool.Exec(ctx, `DELETE FROM plan_processing_costs WHERE plan_id = $1 AND feature_name = $2`, id, body.Feature)
|
||||
} else {
|
||||
_, err = s.Pool.Exec(ctx, `INSERT INTO plan_processing_costs (plan_id,feature_name,cost_per_unit) VALUES ($1,$2,$3)
|
||||
ON CONFLICT(plan_id,feature_name) DO UPDATE SET cost_per_unit=EXCLUDED.cost_per_unit,updated_at=now()`, id, body.Feature, *body.Cost)
|
||||
}
|
||||
default:
|
||||
Error(w, http.StatusBadRequest, "scope must be company or plan")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
LogAndError(w, http.StatusBadRequest, "could not save processing cost", err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
JSON(w, http.StatusOK, map[string]any{"status": "ok"})
|
||||
}
|
||||
Reference in new issue
Block a user