This commit is contained in:
greeneclipse committed 2026-10-20 17:50:32 +02:00
1 parent fd83a7c523
commit edbbf127ba
12 files changed
+246 -207

No files matched your search

+11 -45
View File
@@ -20,15 +20,13 @@ var contentFeatures = map[string]string{
"seo_meta": "seo_meta_ai",
}
var defaultContentUnits = map[string]int64{
"title": 1000,
"description": 2500,
"seo_meta": 1000,
}
// ContentDebitUnits calculates exactly one charge per selected output. A missing
// selection means the legacy job mode determines which outputs were requested.
// ContentDebitUnits bills one credit for one processed product in one language,
// regardless of which supported outputs were selected. The EUR value of a
// credit is a separate reference value and is never a processing multiplier.
// rates is retained for existing callers, but output-level admin prices no
// longer affect the product enrichment charge.
func ContentDebitUnits(selected []string, processingType string, rates map[string]int64) (int64, error) {
_ = rates
if len(selected) == 0 {
switch processingType {
case "seo", "seo_meta", "seo_ai":
@@ -41,40 +39,12 @@ func ContentDebitUnits(selected []string, processingType string, rates map[strin
selected = []string{"title", "description"}
}
}
// Never let an unsupported output bypass validation in flat-rate mode.
for _, item := range selected {
if _, known := contentFeatures[item]; !known {
return 0, fmt.Errorf("unknown content processing type %q", item)
for _, output := range selected {
if _, ok := contentFeatures[output]; !ok {
return 0, fmt.Errorf("unknown content processing type %q", output)
}
}
// A flat product rate is opt-in through the assigned plan. It takes
// precedence over per-output rates even when callers request all outputs.
if productUnits, ok := rates["product"]; ok {
if productUnits < 0 || productUnits > 1000000*contentCreditScale {
return 0, errors.New("invalid per-product credit price")
}
return productUnits, nil
}
seen := make(map[string]bool, 3)
var result int64
for _, item := range selected {
if _, ok := contentFeatures[item]; !ok {
return 0, fmt.Errorf("unknown content processing type %q", item)
}
if seen[item] {
continue
}
seen[item] = true
units, ok := rates[item]
if !ok {
units = defaultContentUnits[item]
}
if units < 0 || units > 1000000*contentCreditScale {
return 0, errors.New("invalid content credit price")
}
result += units
}
return result, nil
return contentCreditScale, nil
}
// ContentRates resolves company overrides > active plan overrides > global default.
@@ -150,11 +120,7 @@ func (s *Service) ConsumeContentCreditsTx(ctx context.Context, tx pgx.Tx, compan
if tx == nil {
return 0, errors.New("missing billing transaction")
}
rates, err := contentRatesFrom(ctx, tx, companyID)
if err != nil {
return 0, err
}
units, err := ContentDebitUnits(selected, processingType, rates)
units, err := ContentDebitUnits(selected, processingType, nil)
if err != nil {
return 0, err
}
@@ -9,12 +9,12 @@ func TestContentDebitUnits(t *testing.T) {
processingType string
want int64
}{
{[]string{"title"}, "title", 1000},
{[]string{"title", "description"}, "enhance", 3500},
{[]string{"title", "description", "seo_meta"}, "enhance", 4500},
{[]string{"seo_meta"}, "seo_meta", 1000},
{[]string{"title", "title"}, "title", 1000},
{nil, "description", 2500},
{[]string{"title"}, "title", 10000},
{[]string{"title", "description"}, "enhance", 10000},
{[]string{"title", "description", "seo_meta"}, "enhance", 10000},
{[]string{"seo_meta"}, "seo_meta", 10000},
{[]string{"title", "title"}, "title", 10000},
{nil, "description", 10000},
}
for _, tt := range cases {
got, err := ContentDebitUnits(tt.selected, tt.processingType, rates)
@@ -27,12 +27,12 @@ func TestContentDebitUnits(t *testing.T) {
}
for _, outputs := range [][]string{nil, {"title"}, {"title", "description", "seo_meta"}} {
got, err := ContentDebitUnits(outputs, "enhance", map[string]int64{"title": 1000, "description": 2500, "seo_meta": 1000, "product": 3750})
if err != nil || got != 3750 {
if err != nil || got != 10000 {
t.Errorf("per-product price outputs=%v got=%d err=%v", outputs, got, err)
}
}
zero, err := ContentDebitUnits([]string{"title"}, "title", map[string]int64{"title": 0})
if err != nil || zero != 0 {
if err != nil || zero != 10000 {
t.Fatalf("zero cost %v %v", zero, err)
}
}