fix in the catlogue

This commit is contained in:
2026-08-10 15:36:39 +05:30
parent 5c04d40f0e
commit 3531c656d4
2 changed files with 72 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package repositories
import (
"errors"
"fmt"
"log"
"nearle/db"
"nearle/models"
"sort"
@@ -118,15 +119,29 @@ func (r *catalogueRepository) GetBrands() ([]models.CatalogueBrand, error) {
}
var brands []models.CatalogueBrand
var failures int
for brand, table := range catalogueBrandTables {
var count int64
if err := r.db.Table(table).Count(&count).Error; err != nil {
return nil, fmt.Errorf("counting %s: %w", table, err)
// One brand's table being absent or unreadable must not hide the
// others. It used to abort the whole call, so a single missing
// table emptied the brand filter and — through
// getProductsAllBrands, which had the same flaw — the entire
// import screen, for every brand.
log.Printf("catalogue: skipping brand %q (%s): %v", brand, table, err)
failures++
continue
}
brands = append(brands, models.CatalogueBrand{Brand: brand, ProductCount: count})
}
// Every table failing is a different thing from every table being empty:
// the first is an outage and must be reported, the second is a fact.
if failures == len(catalogueBrandTables) {
return nil, fmt.Errorf("no catalogue brand table could be read (%d configured)", failures)
}
return brands, nil
}
@@ -220,19 +235,32 @@ func (r *catalogueRepository) getProductsAllBrands(category, keyword string, pag
sort.Strings(brands)
var all []models.CatalogueProduct
var failures int
for _, brand := range brands {
table := catalogueBrandTables[brand]
var rows []catalogueProductRow
dataQuery := fmt.Sprintf(`SELECT %s FROM %s WHERE %s ORDER BY id`, catalogueProductColumns, table, whereClause)
if err := r.db.Raw(dataQuery, args...).Scan(&rows).Error; err != nil {
return nil, 0, fmt.Errorf("querying %s: %w", table, err)
// Skip the brand rather than abandoning the merge. This aborted on
// the first failure, so one absent table returned 500 for a browse
// across all brands — which is what the import screen asks for by
// default, leaving it permanently empty while five of six brands
// were perfectly readable.
log.Printf("catalogue: skipping brand %q (%s) in all-brands browse: %v", brand, table, err)
failures++
continue
}
for _, row := range rows {
all = append(all, row.toModel(brand))
}
}
if failures == len(brands) {
return nil, 0, fmt.Errorf("no catalogue brand table could be read (%d configured)", failures)
}
sort.Slice(all, func(i, j int) bool {
return strings.ToLower(all[i].ProductName) < strings.ToLower(all[j].ProductName)
})