diff --git a/backend/internal/domain/discovery/crawler/merger.go b/backend/internal/domain/discovery/crawler/merger.go index db62b4c..52a1626 100644 --- a/backend/internal/domain/discovery/crawler/merger.go +++ b/backend/internal/domain/discovery/crawler/merger.go @@ -95,7 +95,7 @@ func mergeKey(r RawEvent) string { func mergeGroup(raws []RawEvent) MergedEvent { // Sort by source rank so "first non-empty" == "best source's value". sort.SliceStable(raws, func(i, j int) bool { - return rankOf(raws[i].SourceName) < rankOf(raws[j].SourceName) + return SourceRank(raws[i].SourceName) < SourceRank(raws[j].SourceName) }) m := MergedEvent{} @@ -176,7 +176,7 @@ func mergeGroup(raws []RawEvent) MergedEvent { return m } -func rankOf(name string) int { +func SourceRank(name string) int { if r, ok := sourceRank[name]; ok { return r } diff --git a/backend/internal/domain/discovery/repository.go b/backend/internal/domain/discovery/repository.go index 9f47913..6ba332b 100644 --- a/backend/internal/domain/discovery/repository.go +++ b/backend/internal/domain/discovery/repository.go @@ -6,12 +6,14 @@ import ( "encoding/json" "errors" "fmt" + "sort" "time" "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" + "marktvogt.de/backend/internal/domain/discovery/crawler" "marktvogt.de/backend/internal/domain/discovery/enrich" ) @@ -350,6 +352,14 @@ func scanDiscoveredMarket(s scanner) (DiscoveredMarket, error) { if d.SourceContributions == nil { d.SourceContributions = []SourceContribution{} } + // Persisted order (alphabetical from MergePendingSources) doesn't match the + // rank order the UI relies on for "Rang 1 = winning source". Sort by rank + // here so every read path sees contributions in rank order regardless of + // how they were stored. + sort.SliceStable(d.SourceContributions, func(i, j int) bool { + return crawler.SourceRank(d.SourceContributions[i].SourceName) < + crawler.SourceRank(d.SourceContributions[j].SourceName) + }) return d, nil }