refactor: привести кодовую базу в соответствие с канонами bible

- 400 → 422 для всех ошибок валидации входных данных (handlers: export, quote, sync, vendor_spec, partnumber_books, pricelist)
- SQL-запросы вынесены из handlers в localdb (partnumber_books, pricelist, support_bundle); ValidateMariaDBConnection перенесён в internal/db/validate.go
- List-ответы унифицированы: ключ items, поля total_count/page/per_page/total_pages (component, pricelist, partnumber_books); шаблоны обновлены
- Молчаливые ошибки заменены на slog.Warn/Error (support_bundle, vendor_spec, component, configuration, local_configuration, localdb)
- N+1 запросы устранены: batch-запросы в export.go и vendor_workspace_import.go
- fmt.Println → slog в cmd/ (qfs, migrate, migrate_ops_projects, migrate_project_updated_at)
- Заголовки recovery/verify добавлены во все 28 SQL-миграций
- Добавлены bible-local/runtime-flows.md и bible-local/decisions/
- Обновлён субмодуль bible до v0.2.0-13

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-13 14:38:01 +03:00
parent e548305396
commit 184f54b663
59 changed files with 1164 additions and 196 deletions
+18 -8
View File
@@ -2,6 +2,7 @@ package services
import (
"fmt"
"log/slog"
"strings"
"git.mchus.pro/mchus/quoteforge/internal/models"
@@ -41,10 +42,11 @@ func ParsePartNumber(lotName string) (category, model string) {
}
type ComponentListResult struct {
Components []ComponentView `json:"components"`
Total int64 `json:"total"`
Items []ComponentView `json:"items"`
TotalCount int64 `json:"total_count"`
Page int `json:"page"`
PerPage int `json:"per_page"`
TotalPages int `json:"total_pages"`
}
type ComponentView struct {
@@ -63,10 +65,11 @@ func (s *ComponentService) List(filter repository.ComponentFilter, page, perPage
// Components should be loaded via /api/sync/components first
if s.componentRepo == nil {
return &ComponentListResult{
Components: []ComponentView{},
Total: 0,
Items: []ComponentView{},
TotalCount: 0,
Page: page,
PerPage: perPage,
TotalPages: 1,
}, nil
}
@@ -107,11 +110,16 @@ func (s *ComponentService) List(filter repository.ComponentFilter, page, perPage
views[i] = view
}
totalPages := int((total + int64(perPage) - 1) / int64(perPage))
if totalPages < 1 {
totalPages = 1
}
return &ComponentListResult{
Components: views,
Total: total,
Items: views,
TotalCount: total,
Page: page,
PerPage: perPage,
TotalPages: totalPages,
}, nil
}
@@ -126,8 +134,10 @@ func (s *ComponentService) GetByLotName(lotName string) (*ComponentView, error)
return nil, err
}
// Track usage
_ = s.componentRepo.IncrementRequestCount(lotName)
// Track usage (best-effort)
if err := s.componentRepo.IncrementRequestCount(lotName); err != nil {
slog.Warn("component: could not increment request count", "lot", lotName, "err", err)
}
view := &ComponentView{
LotName: c.LotName,
+5 -2
View File
@@ -2,6 +2,7 @@ package services
import (
"errors"
"log/slog"
"time"
"git.mchus.pro/mchus/quoteforge/internal/models"
@@ -117,8 +118,10 @@ func (s *ConfigurationService) Create(ownerUsername string, req *CreateConfigReq
return nil, err
}
// Record usage stats
_ = s.quoteService.RecordUsage(req.Items)
// Record usage stats (best-effort)
if err := s.quoteService.RecordUsage(req.Items); err != nil {
slog.Warn("configuration: could not record usage stats", "err", err)
}
return config, nil
}
+25 -18
View File
@@ -567,45 +567,52 @@ func (s *ExportService) resolvePricingTotals(cfg *models.Configuration, localCfg
}
}
estimatePrices := s.batchLookupPrices(estimateID, lots)
stockPrices := s.batchLookupPrices(warehouseID, lots)
competitorPrices := s.batchLookupPrices(competitorID, lots)
for _, lot := range lots {
level := pricingLevels{}
level.Estimate = s.lookupPricePointer(estimateID, lot)
level.Stock = s.lookupPricePointer(warehouseID, lot)
level.Competitor = s.lookupPricePointer(competitorID, lot)
if p, ok := estimatePrices[lot]; ok {
level.Estimate = floatPtr(p)
}
if p, ok := stockPrices[lot]; ok {
level.Stock = floatPtr(p)
}
if p, ok := competitorPrices[lot]; ok {
level.Competitor = floatPtr(p)
}
result[lot] = level
}
return result
}
func (s *ExportService) lookupPricePointer(serverPricelistID *uint, lotName string) *float64 {
if s.localDB == nil || serverPricelistID == nil || *serverPricelistID == 0 || strings.TrimSpace(lotName) == "" {
// batchLookupPrices fetches prices for all lots from a pricelist in a single query.
func (s *ExportService) batchLookupPrices(serverPricelistID *uint, lots []string) map[string]float64 {
if s.localDB == nil || serverPricelistID == nil || *serverPricelistID == 0 || len(lots) == 0 {
return nil
}
localPL, err := s.localDB.GetLocalPricelistByServerID(*serverPricelistID)
if err != nil {
return nil
}
price, err := s.localDB.GetLocalPriceForLot(localPL.ID, lotName)
if err != nil || price <= 0 {
prices, err := s.localDB.GetLocalPricesForLots(localPL.ID, lots)
if err != nil {
return nil
}
return floatPtr(price)
return prices
}
func (s *ExportService) resolveLotDescriptions(cfg *models.Configuration, localCfg *localdb.LocalConfiguration) map[string]string {
lots := collectPricingLots(cfg, localCfg, true)
result := make(map[string]string, len(lots))
if s.localDB == nil {
return result
if s.localDB == nil || len(lots) == 0 {
return map[string]string{}
}
for _, lot := range lots {
component, err := s.localDB.GetLocalComponent(lot)
if err != nil {
continue
}
result[lot] = component.LotDescription
descriptions, err := s.localDB.GetLocalComponentDescriptionsByLotNames(lots)
if err != nil {
return map[string]string{}
}
return result
return descriptions
}
func collectPricingLots(cfg *models.Configuration, localCfg *localdb.LocalConfiguration, includeBOM bool) []string {
+11 -4
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"strings"
"time"
@@ -118,8 +119,10 @@ func (s *LocalConfigurationService) Create(ownerUsername string, req *CreateConf
}
cfg.Line = localCfg.Line
// Record usage stats
_ = s.quoteService.RecordUsage(req.Items)
// Record usage stats (best-effort)
if err := s.quoteService.RecordUsage(req.Items); err != nil {
slog.Warn("local configuration: could not record usage stats", "err", err)
}
return cfg, nil
}
@@ -407,7 +410,9 @@ func (s *LocalConfigurationService) RefreshPrices(uuid string, ownerUsername str
// Refresh local pricelists when online.
if s.isOnline() {
_ = s.syncService.SyncPricelistsIfNeeded()
if err := s.syncService.SyncPricelistsIfNeeded(); err != nil {
slog.Warn("local configuration: background pricelist sync failed", "err", err)
}
}
// Use the pricelist stored in the config; fall back to latest if unavailable.
@@ -791,7 +796,9 @@ func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string, pricelistSe
}
if s.isOnline() {
_ = s.syncService.SyncPricelistsIfNeeded()
if err := s.syncService.SyncPricelistsIfNeeded(); err != nil {
slog.Warn("local configuration: background pricelist sync failed", "err", err)
}
}
// Resolve which pricelist to use:
+8 -4
View File
@@ -269,13 +269,17 @@ func aggregateVendorSpecToItems(spec localdb.VendorSpec, estimatePricelist *loca
}
sort.Strings(order)
var priceMap map[string]float64
if estimatePricelist != nil && local != nil && len(order) > 0 {
priceMap, _ = local.GetLocalPricesForLots(estimatePricelist.ID, order)
}
items := make(localdb.LocalConfigItems, 0, len(order))
for _, lotName := range order {
unitPrice := 0.0
if estimatePricelist != nil && local != nil {
if price, err := local.GetLocalPriceForLot(estimatePricelist.ID, lotName); err == nil && price > 0 {
unitPrice = price
}
if priceMap != nil {
unitPrice = priceMap[lotName]
}
items = append(items, localdb.LocalConfigItem{
LotName: lotName,