feat: add BadgeRefresher interface and badge types

This commit is contained in:
Alex Dunmow 2026-05-02 11:03:09 +08:00
parent 1ede7d50be
commit 2c89ce4d42
2 changed files with 50 additions and 0 deletions

42
badges/badges.go Normal file
View File

@ -0,0 +1,42 @@
package badges
import "encoding/json"
// BadgeRule defines a condition for automatic badge assignment.
type BadgeRule struct {
Field string `json:"field"`
Operator string `json:"operator"` // eq, neq, gt, gte, lt, lte, contains, exists, true, false
Value any `json:"value"`
}
// BadgeDefinition defines a badge with auto-computation rules.
type BadgeDefinition struct {
Key string `json:"key"`
Label string `json:"label"`
Description string `json:"description"`
Icon string `json:"icon"`
Rules []BadgeRule `json:"rules"`
RulesMode string `json:"rules_mode"` // "and" or "or"
ManualOnly bool `json:"manual_only"`
}
// ParseDefinitions extracts badge definitions from a data table schema JSON.
func ParseDefinitions(schemaJSON []byte) []BadgeDefinition {
var schema map[string]any
if err := json.Unmarshal(schemaJSON, &schema); err != nil {
return nil
}
badgesRaw, ok := schema["badges"]
if !ok {
return nil
}
raw, err := json.Marshal(badgesRaw)
if err != nil {
return nil
}
var definitions []BadgeDefinition
if err := json.Unmarshal(raw, &definitions); err != nil {
return nil
}
return definitions
}

View File

@ -49,6 +49,7 @@ type ServiceDeps struct {
// Core RPC services — pre-built bindings for CMS-provided services
CoreServiceBindings CoreServiceBindings
ReviewSubmitter ReviewSubmitter
BadgeRefresher BadgeRefresher
// Extension points — typed as narrow interfaces where possible
JobRunner JobRunner
@ -85,3 +86,10 @@ type RAGResult struct {
Score float64
Metadata map[string]string
}
// BadgeRefresher recomputes badges for a data table row.
// The CMS handles loading the table schema, aggregating ratings,
// evaluating badge rules, and persisting the updated badge list.
type BadgeRefresher interface {
RefreshBadges(ctx context.Context, tableID, rowID uuid.UUID) error
}