Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d190cc7a8 | |||
| 8b2dc6652a | |||
| cea979e327 | |||
| 4d002671ae | |||
| 949479550c | |||
|
|
677b5d898f | ||
|
|
b3cab3477b | ||
|
|
6d4a37df8b | ||
|
|
7cc101d24d |
2
bible
2
bible
Submodule bible updated: 52444350c1...1977730d93
@@ -894,6 +894,27 @@ func setupRouter(cfg *config.Config, local *localdb.LocalDB, connMgr *db.Connect
|
|||||||
router.GET("/pricelists/:id", webHandler.PricelistDetail)
|
router.GET("/pricelists/:id", webHandler.PricelistDetail)
|
||||||
router.GET("/partnumber-books", webHandler.PartnumberBooks)
|
router.GET("/partnumber-books", webHandler.PartnumberBooks)
|
||||||
|
|
||||||
|
// Short project URLs: /:code → main variant, /:code/:variant → named variant
|
||||||
|
router.GET("/:code", func(c *gin.Context) {
|
||||||
|
code := c.Param("code")
|
||||||
|
project, err := projectService.GetByCode(code)
|
||||||
|
if err != nil {
|
||||||
|
c.Redirect(http.StatusFound, "/projects")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Redirect(http.StatusFound, "/projects/"+project.UUID)
|
||||||
|
})
|
||||||
|
router.GET("/:code/:variant", func(c *gin.Context) {
|
||||||
|
code := c.Param("code")
|
||||||
|
variant := c.Param("variant")
|
||||||
|
project, err := projectService.GetByCodeAndVariant(code, variant)
|
||||||
|
if err != nil {
|
||||||
|
c.Redirect(http.StatusFound, "/projects")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Redirect(http.StatusFound, "/projects/"+project.UUID)
|
||||||
|
})
|
||||||
|
|
||||||
// htmx partials
|
// htmx partials
|
||||||
partials := router.Group("/partials")
|
partials := router.Group("/partials")
|
||||||
{
|
{
|
||||||
@@ -1148,6 +1169,15 @@ func setupRouter(cfg *config.Config, local *localdb.LocalDB, connMgr *db.Connect
|
|||||||
c.JSON(http.StatusOK, config)
|
c.JSON(http.StatusOK, config)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
configs.POST("/:uuid/snapshot", func(c *gin.Context) {
|
||||||
|
uuid := c.Param("uuid")
|
||||||
|
if err := configService.SnapshotCurrentState(uuid); err != nil {
|
||||||
|
respondError(c, http.StatusInternalServerError, "internal server error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||||
|
})
|
||||||
|
|
||||||
configs.PATCH("/:uuid/project", func(c *gin.Context) {
|
configs.PATCH("/:uuid/project", func(c *gin.Context) {
|
||||||
uuid := c.Param("uuid")
|
uuid := c.Param("uuid")
|
||||||
var req struct {
|
var req struct {
|
||||||
@@ -1517,7 +1547,9 @@ func setupRouter(cfg *config.Config, local *localdb.LocalDB, connMgr *db.Connect
|
|||||||
project, err := projectService.Create(dbUsername, &req)
|
project, err := projectService.Create(dbUsername, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, services.ErrReservedMainVariant):
|
case errors.Is(err, services.ErrReservedMainVariant),
|
||||||
|
errors.Is(err, services.ErrProjectCodeInvalidChars),
|
||||||
|
errors.Is(err, services.ErrProjectVariantInvalidChars):
|
||||||
respondError(c, http.StatusBadRequest, "invalid request", err)
|
respondError(c, http.StatusBadRequest, "invalid request", err)
|
||||||
case errors.Is(err, services.ErrProjectCodeExists):
|
case errors.Is(err, services.ErrProjectCodeExists):
|
||||||
respondError(c, http.StatusConflict, "conflict detected", err)
|
respondError(c, http.StatusConflict, "conflict detected", err)
|
||||||
@@ -1555,7 +1587,9 @@ func setupRouter(cfg *config.Config, local *localdb.LocalDB, connMgr *db.Connect
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, services.ErrReservedMainVariant),
|
case errors.Is(err, services.ErrReservedMainVariant),
|
||||||
errors.Is(err, services.ErrCannotRenameMainVariant):
|
errors.Is(err, services.ErrCannotRenameMainVariant),
|
||||||
|
errors.Is(err, services.ErrProjectCodeInvalidChars),
|
||||||
|
errors.Is(err, services.ErrProjectVariantInvalidChars):
|
||||||
respondError(c, http.StatusBadRequest, "invalid request", err)
|
respondError(c, http.StatusBadRequest, "invalid request", err)
|
||||||
case errors.Is(err, services.ErrProjectCodeExists):
|
case errors.Is(err, services.ErrProjectCodeExists):
|
||||||
respondError(c, http.StatusConflict, "conflict detected", err)
|
respondError(c, http.StatusConflict, "conflict detected", err)
|
||||||
@@ -1777,7 +1811,6 @@ func setupRouter(cfg *config.Config, local *localdb.LocalDB, connMgr *db.Connect
|
|||||||
syncAPI.GET("/readiness", syncHandler.GetReadiness)
|
syncAPI.GET("/readiness", syncHandler.GetReadiness)
|
||||||
syncAPI.GET("/info", syncHandler.GetInfo)
|
syncAPI.GET("/info", syncHandler.GetInfo)
|
||||||
syncAPI.GET("/users-status", syncHandler.GetUsersStatus)
|
syncAPI.GET("/users-status", syncHandler.GetUsersStatus)
|
||||||
syncAPI.POST("/components", syncHandler.SyncComponents)
|
|
||||||
syncAPI.POST("/pricelists", syncHandler.SyncPricelists)
|
syncAPI.POST("/pricelists", syncHandler.SyncPricelists)
|
||||||
syncAPI.POST("/partnumber-books", syncHandler.SyncPartnumberBooks)
|
syncAPI.POST("/partnumber-books", syncHandler.SyncPartnumberBooks)
|
||||||
syncAPI.POST("/partnumber-seen", syncHandler.ReportPartnumberSeen)
|
syncAPI.POST("/partnumber-seen", syncHandler.ReportPartnumberSeen)
|
||||||
|
|||||||
@@ -45,38 +45,55 @@ func TestResolveLotCategoriesStrict_MissingCategoryReturnsError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveLotCategoriesStrict_FallbackToLocalComponents(t *testing.T) {
|
func TestResolveLotCategoriesStrict_FallbackToLatestPricelist(t *testing.T) {
|
||||||
local, err := localdb.New(filepath.Join(t.TempDir(), "local.db"))
|
local, err := localdb.New(filepath.Join(t.TempDir(), "local.db"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("init local db: %v", err)
|
t.Fatalf("init local db: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() { _ = local.Close() })
|
t.Cleanup(func() { _ = local.Close() })
|
||||||
|
|
||||||
|
// Older pricelist used by the configuration — CPU_B has no category here
|
||||||
if err := local.SaveLocalPricelist(&localdb.LocalPricelist{
|
if err := local.SaveLocalPricelist(&localdb.LocalPricelist{
|
||||||
ServerID: 2,
|
ServerID: 2,
|
||||||
Source: "estimate",
|
Source: "estimate",
|
||||||
Version: "S-2026-02-11-002",
|
Version: "S-2026-02-11-002",
|
||||||
Name: "test",
|
Name: "old",
|
||||||
|
IsActive: false,
|
||||||
|
CreatedAt: time.Now().Add(-time.Hour),
|
||||||
|
SyncedAt: time.Now().Add(-time.Hour),
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("save old pricelist: %v", err)
|
||||||
|
}
|
||||||
|
oldPL, err := local.GetLocalPricelistByServerID(2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get old pricelist: %v", err)
|
||||||
|
}
|
||||||
|
if err := local.SaveLocalPricelistItems([]localdb.LocalPricelistItem{
|
||||||
|
{PricelistID: oldPL.ID, LotName: "CPU_B", LotCategory: "", Price: 10},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("save old pricelist items: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Newer active pricelist — CPU_B has category set
|
||||||
|
if err := local.SaveLocalPricelist(&localdb.LocalPricelist{
|
||||||
|
ServerID: 3,
|
||||||
|
Source: "estimate",
|
||||||
|
Version: "S-2026-02-11-003",
|
||||||
|
Name: "latest",
|
||||||
|
IsActive: true,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
SyncedAt: time.Now(),
|
SyncedAt: time.Now(),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatalf("save local pricelist: %v", err)
|
t.Fatalf("save latest pricelist: %v", err)
|
||||||
}
|
}
|
||||||
localPL, err := local.GetLocalPricelistByServerID(2)
|
latestPL, err := local.GetLocalPricelistByServerID(3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("get local pricelist: %v", err)
|
t.Fatalf("get latest pricelist: %v", err)
|
||||||
}
|
}
|
||||||
if err := local.SaveLocalPricelistItems([]localdb.LocalPricelistItem{
|
if err := local.SaveLocalPricelistItems([]localdb.LocalPricelistItem{
|
||||||
{PricelistID: localPL.ID, LotName: "CPU_B", LotCategory: "", Price: 10},
|
{PricelistID: latestPL.ID, LotName: "CPU_B", LotCategory: "CPU", Price: 10},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatalf("save local items: %v", err)
|
t.Fatalf("save latest pricelist items: %v", err)
|
||||||
}
|
|
||||||
if err := local.DB().Create(&localdb.LocalComponent{
|
|
||||||
LotName: "CPU_B",
|
|
||||||
Category: "CPU",
|
|
||||||
LotDescription: "cpu",
|
|
||||||
}).Error; err != nil {
|
|
||||||
t.Fatalf("save local components: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cats, err := ResolveLotCategoriesStrict(local, 2, []string{"CPU_B"})
|
cats, err := ResolveLotCategoriesStrict(local, 2, []string{"CPU_B"})
|
||||||
|
|||||||
@@ -177,22 +177,12 @@ func (h *PricelistHandler) GetItems(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lotNames := make([]string, len(items))
|
|
||||||
for i, item := range items {
|
|
||||||
lotNames[i] = item.LotName
|
|
||||||
}
|
|
||||||
descMap, err := h.localDB.GetLocalComponentDescriptionsByLotNames(lotNames)
|
|
||||||
if err != nil {
|
|
||||||
RespondError(c, http.StatusInternalServerError, "internal server error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resultItems := make([]gin.H, 0, len(items))
|
resultItems := make([]gin.H, 0, len(items))
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
resultItems = append(resultItems, gin.H{
|
resultItems = append(resultItems, gin.H{
|
||||||
"id": item.ID,
|
"id": item.ID,
|
||||||
"lot_name": item.LotName,
|
"lot_name": item.LotName,
|
||||||
"lot_description": descMap[item.LotName],
|
"lot_description": "",
|
||||||
"price": item.Price,
|
"price": item.Price,
|
||||||
"category": item.LotCategory,
|
"category": item.LotCategory,
|
||||||
"available_qty": item.AvailableQty,
|
"available_qty": item.AvailableQty,
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func (h *SupportBundleHandler) DownloadBundle(c *gin.Context) {
|
|||||||
|
|
||||||
// local_db_stats.json
|
// local_db_stats.json
|
||||||
writeJSON("local_db_stats.json", map[string]any{
|
writeJSON("local_db_stats.json", map[string]any{
|
||||||
"components": h.localDB.CountLocalComponents(),
|
"components": h.localDB.CountComponents(),
|
||||||
"configurations": h.localDB.CountConfigurations(),
|
"configurations": h.localDB.CountConfigurations(),
|
||||||
"projects": h.localDB.CountProjects(),
|
"projects": h.localDB.CountProjects(),
|
||||||
"pricelists": h.localDB.CountLocalPricelists(),
|
"pricelists": h.localDB.CountLocalPricelists(),
|
||||||
@@ -139,6 +139,7 @@ func (h *SupportBundleHandler) DownloadBundle(c *gin.Context) {
|
|||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
SyncedAt time.Time `json:"synced_at"`
|
SyncedAt time.Time `json:"synced_at"`
|
||||||
IsUsed bool `json:"is_used"`
|
IsUsed bool `json:"is_used"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
}
|
}
|
||||||
bySource := map[string][]plEntry{}
|
bySource := map[string][]plEntry{}
|
||||||
for _, pl := range pricelists {
|
for _, pl := range pricelists {
|
||||||
@@ -150,12 +151,78 @@ func (h *SupportBundleHandler) DownloadBundle(c *gin.Context) {
|
|||||||
CreatedAt: pl.CreatedAt,
|
CreatedAt: pl.CreatedAt,
|
||||||
SyncedAt: pl.SyncedAt,
|
SyncedAt: pl.SyncedAt,
|
||||||
IsUsed: pl.IsUsed,
|
IsUsed: pl.IsUsed,
|
||||||
|
IsActive: pl.IsActive,
|
||||||
}
|
}
|
||||||
bySource[pl.Source] = append(bySource[pl.Source], e)
|
bySource[pl.Source] = append(bySource[pl.Source], e)
|
||||||
}
|
}
|
||||||
writeJSON("pricelists.json", bySource)
|
writeJSON("pricelists.json", bySource)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pricelist_coverage.json — for each local estimate pricelist: item count by lot_category
|
||||||
|
if pl, err := h.localDB.GetLatestLocalPricelist(); err == nil {
|
||||||
|
type catRow struct {
|
||||||
|
Category string `json:"category"`
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
}
|
||||||
|
type plCoverage struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
ServerID uint `json:"server_id"`
|
||||||
|
TotalItems int64 `json:"total_items"`
|
||||||
|
Categories []catRow `json:"categories"`
|
||||||
|
}
|
||||||
|
rows, total, catErr := h.localDB.GetLocalPricelistCoverageByCategory(pl.ID)
|
||||||
|
if catErr == nil {
|
||||||
|
cats := make([]catRow, 0, len(rows))
|
||||||
|
for cat, cnt := range rows {
|
||||||
|
cats = append(cats, catRow{Category: cat, Count: cnt})
|
||||||
|
}
|
||||||
|
writeJSON("pricelist_coverage.json", plCoverage{
|
||||||
|
Version: pl.Version,
|
||||||
|
ServerID: pl.ServerID,
|
||||||
|
TotalItems: total,
|
||||||
|
Categories: cats,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// configurator_settings.json — what /api/configurator-settings actually returns
|
||||||
|
if cfgSettings, err := h.localDB.GetConfiguratorSettings(); err == nil {
|
||||||
|
writeJSON("configurator_settings.json", cfgSettings)
|
||||||
|
} else {
|
||||||
|
writeJSON("configurator_settings.json", map[string]any{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
// component_categories.json — distinct categories in active estimate pricelist
|
||||||
|
if cats, err := h.localDB.GetLocalComponentCategories(); err == nil {
|
||||||
|
writeJSON("component_categories.json", cats)
|
||||||
|
}
|
||||||
|
|
||||||
|
// autocomplete_lots.json — per-category breakdown of lots with their prices
|
||||||
|
// Mirrors what filterAutocomplete() works with: lot_name + estimate_price per category.
|
||||||
|
if pl, err := h.localDB.GetLatestLocalPricelist(); err == nil {
|
||||||
|
if items, err := h.localDB.GetLocalPricelistItems(pl.ID); err == nil {
|
||||||
|
type lotEntry struct {
|
||||||
|
LotName string `json:"lot_name"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
HasPrice bool `json:"has_price"`
|
||||||
|
}
|
||||||
|
byCategory := map[string][]lotEntry{}
|
||||||
|
for _, it := range items {
|
||||||
|
entry := lotEntry{
|
||||||
|
LotName: it.LotName,
|
||||||
|
Price: it.Price,
|
||||||
|
HasPrice: it.Price > 0,
|
||||||
|
}
|
||||||
|
byCategory[it.LotCategory] = append(byCategory[it.LotCategory], entry)
|
||||||
|
}
|
||||||
|
writeJSON("autocomplete_lots.json", map[string]any{
|
||||||
|
"pricelist_version": pl.Version,
|
||||||
|
"pricelist_id": pl.ServerID,
|
||||||
|
"by_category": byCategory,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// schema_migrations.json
|
// schema_migrations.json
|
||||||
migrations, err := h.localDB.GetSchemaMigrations()
|
migrations, err := h.localDB.GetSchemaMigrations()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -163,6 +230,44 @@ func (h *SupportBundleHandler) DownloadBundle(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
writeJSON("schema_migrations.json", migrations)
|
writeJSON("schema_migrations.json", migrations)
|
||||||
|
|
||||||
|
// latest_pricelist_items.json — all items from the most recent active estimate pricelist
|
||||||
|
if pl, err := h.localDB.GetLatestLocalPricelist(); err == nil {
|
||||||
|
if items, err := h.localDB.GetLocalPricelistItems(pl.ID); err == nil {
|
||||||
|
type plItem struct {
|
||||||
|
LotName string `json:"lot_name"`
|
||||||
|
LotCategory string `json:"lot_category"`
|
||||||
|
Price float64 `json:"price"`
|
||||||
|
}
|
||||||
|
out := make([]plItem, len(items))
|
||||||
|
for i, it := range items {
|
||||||
|
out[i] = plItem{
|
||||||
|
LotName: it.LotName,
|
||||||
|
LotCategory: it.LotCategory,
|
||||||
|
Price: it.Price,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeJSON("latest_pricelist_items.json", map[string]any{
|
||||||
|
"pricelist_version": pl.Version,
|
||||||
|
"pricelist_id": pl.ServerID,
|
||||||
|
"source": pl.Source,
|
||||||
|
"item_count": len(out),
|
||||||
|
"items": out,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// local.db — full SQLite database file (for deep diagnostics)
|
||||||
|
if dbPath := h.localDB.DBFilePath(); dbPath != "" {
|
||||||
|
if f, err := os.Open(dbPath); err == nil {
|
||||||
|
defer f.Close()
|
||||||
|
if w, err := zw.Create("local.db"); err == nil {
|
||||||
|
if _, err := io.Copy(w, f); err != nil {
|
||||||
|
slog.Warn("support bundle: error copying local.db", "err", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// app.log (tail 5 MiB)
|
// app.log (tail 5 MiB)
|
||||||
if h.logFilePath != "" {
|
if h.logFilePath != "" {
|
||||||
if f, err := os.Open(h.logFilePath); err == nil {
|
if f, err := os.Open(h.logFilePath); err == nil {
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ func NewSyncHandler(localDB *localdb.LocalDB, syncService *sync.Service, connMgr
|
|||||||
|
|
||||||
// SyncStatusResponse represents the sync status
|
// SyncStatusResponse represents the sync status
|
||||||
type SyncStatusResponse struct {
|
type SyncStatusResponse struct {
|
||||||
LastComponentSync *time.Time `json:"last_component_sync"`
|
|
||||||
LastPricelistSync *time.Time `json:"last_pricelist_sync"`
|
LastPricelistSync *time.Time `json:"last_pricelist_sync"`
|
||||||
LastPricelistAttemptAt *time.Time `json:"last_pricelist_attempt_at,omitempty"`
|
LastPricelistAttemptAt *time.Time `json:"last_pricelist_attempt_at,omitempty"`
|
||||||
LastPricelistSyncStatus string `json:"last_pricelist_sync_status,omitempty"`
|
LastPricelistSyncStatus string `json:"last_pricelist_sync_status,omitempty"`
|
||||||
@@ -61,7 +60,6 @@ type SyncStatusResponse struct {
|
|||||||
ComponentsCount int64 `json:"components_count"`
|
ComponentsCount int64 `json:"components_count"`
|
||||||
PricelistsCount int64 `json:"pricelists_count"`
|
PricelistsCount int64 `json:"pricelists_count"`
|
||||||
ServerPricelists int `json:"server_pricelists"`
|
ServerPricelists int `json:"server_pricelists"`
|
||||||
NeedComponentSync bool `json:"need_component_sync"`
|
|
||||||
NeedPricelistSync bool `json:"need_pricelist_sync"`
|
NeedPricelistSync bool `json:"need_pricelist_sync"`
|
||||||
Readiness *sync.SyncReadiness `json:"readiness,omitempty"`
|
Readiness *sync.SyncReadiness `json:"readiness,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -80,19 +78,16 @@ type SyncReadinessResponse struct {
|
|||||||
func (h *SyncHandler) GetStatus(c *gin.Context) {
|
func (h *SyncHandler) GetStatus(c *gin.Context) {
|
||||||
connStatus := h.connMgr.GetStatus()
|
connStatus := h.connMgr.GetStatus()
|
||||||
isOnline := connStatus.IsConnected && strings.TrimSpace(connStatus.LastError) == ""
|
isOnline := connStatus.IsConnected && strings.TrimSpace(connStatus.LastError) == ""
|
||||||
lastComponentSync := h.localDB.GetComponentSyncTime()
|
|
||||||
lastPricelistSync := h.localDB.GetLastSyncTime()
|
lastPricelistSync := h.localDB.GetLastSyncTime()
|
||||||
componentsCount := h.localDB.CountLocalComponents()
|
componentsCount := h.localDB.CountComponents()
|
||||||
pricelistsCount := h.localDB.CountLocalPricelists()
|
pricelistsCount := h.localDB.CountLocalPricelists()
|
||||||
lastPricelistAttemptAt := h.localDB.GetLastPricelistSyncAttemptAt()
|
lastPricelistAttemptAt := h.localDB.GetLastPricelistSyncAttemptAt()
|
||||||
lastPricelistSyncStatus := h.localDB.GetLastPricelistSyncStatus()
|
lastPricelistSyncStatus := h.localDB.GetLastPricelistSyncStatus()
|
||||||
lastPricelistSyncError := h.localDB.GetLastPricelistSyncError()
|
lastPricelistSyncError := h.localDB.GetLastPricelistSyncError()
|
||||||
hasFailedSync := strings.EqualFold(lastPricelistSyncStatus, "failed")
|
hasFailedSync := strings.EqualFold(lastPricelistSyncStatus, "failed")
|
||||||
needComponentSync := h.localDB.NeedComponentSync(24)
|
|
||||||
readiness := h.getReadinessLocal()
|
readiness := h.getReadinessLocal()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, SyncStatusResponse{
|
c.JSON(http.StatusOK, SyncStatusResponse{
|
||||||
LastComponentSync: lastComponentSync,
|
|
||||||
LastPricelistSync: lastPricelistSync,
|
LastPricelistSync: lastPricelistSync,
|
||||||
LastPricelistAttemptAt: lastPricelistAttemptAt,
|
LastPricelistAttemptAt: lastPricelistAttemptAt,
|
||||||
LastPricelistSyncStatus: lastPricelistSyncStatus,
|
LastPricelistSyncStatus: lastPricelistSyncStatus,
|
||||||
@@ -103,7 +98,6 @@ func (h *SyncHandler) GetStatus(c *gin.Context) {
|
|||||||
ComponentsCount: componentsCount,
|
ComponentsCount: componentsCount,
|
||||||
PricelistsCount: pricelistsCount,
|
PricelistsCount: pricelistsCount,
|
||||||
ServerPricelists: 0,
|
ServerPricelists: 0,
|
||||||
NeedComponentSync: needComponentSync,
|
|
||||||
NeedPricelistSync: lastPricelistSync == nil || hasFailedSync,
|
NeedPricelistSync: lastPricelistSync == nil || hasFailedSync,
|
||||||
Readiness: readiness,
|
Readiness: readiness,
|
||||||
})
|
})
|
||||||
@@ -169,52 +163,6 @@ type SyncResultResponse struct {
|
|||||||
Duration string `json:"duration"`
|
Duration string `json:"duration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncComponents syncs components from MariaDB to local SQLite
|
|
||||||
// POST /api/sync/components
|
|
||||||
func (h *SyncHandler) SyncComponents(c *gin.Context) {
|
|
||||||
if !h.ensureSyncReadiness(c) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get database connection from ConnectionManager
|
|
||||||
mariaDB, err := h.connMgr.GetDB()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"error": "database connection failed",
|
|
||||||
})
|
|
||||||
_ = c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
result, err := h.localDB.SyncComponents(mariaDB)
|
|
||||||
if err != nil {
|
|
||||||
_ = h.localDB.SetComponentSyncResult("error", err.Error(), now)
|
|
||||||
h.localDB.AppendSyncLog("components", "error", err.Error(), 0, now, time.Since(now).Milliseconds())
|
|
||||||
slog.Error("component sync failed", "error", err)
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"error": "component sync failed",
|
|
||||||
})
|
|
||||||
_ = c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = h.localDB.SetComponentSyncResult("ok", "", now)
|
|
||||||
h.localDB.AppendSyncLog("components", "ok", "", result.TotalSynced, now, result.Duration.Milliseconds())
|
|
||||||
|
|
||||||
if err := h.localDB.SyncQtSettings(mariaDB); err != nil {
|
|
||||||
slog.Warn("qt_settings sync failed", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, SyncResultResponse{
|
|
||||||
Success: true,
|
|
||||||
Message: "Components synced successfully",
|
|
||||||
Synced: result.TotalSynced,
|
|
||||||
Duration: result.Duration.String(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncPricelists syncs pricelists from MariaDB to local SQLite
|
// SyncPricelists syncs pricelists from MariaDB to local SQLite
|
||||||
// POST /api/sync/pricelists
|
// POST /api/sync/pricelists
|
||||||
func (h *SyncHandler) SyncPricelists(c *gin.Context) {
|
func (h *SyncHandler) SyncPricelists(c *gin.Context) {
|
||||||
@@ -280,7 +228,6 @@ type SyncAllResponse struct {
|
|||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
PendingPushed int `json:"pending_pushed"`
|
PendingPushed int `json:"pending_pushed"`
|
||||||
ComponentsSynced int `json:"components_synced"`
|
|
||||||
PricelistsSynced int `json:"pricelists_synced"`
|
PricelistsSynced int `json:"pricelists_synced"`
|
||||||
ProjectsImported int `json:"projects_imported"`
|
ProjectsImported int `json:"projects_imported"`
|
||||||
ProjectsUpdated int `json:"projects_updated"`
|
ProjectsUpdated int `json:"projects_updated"`
|
||||||
@@ -301,7 +248,7 @@ func (h *SyncHandler) SyncAll(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
var pendingPushed, componentsSynced, pricelistsSynced int
|
var pricelistsSynced int
|
||||||
|
|
||||||
// Push local pending changes first (projects/configurations)
|
// Push local pending changes first (projects/configurations)
|
||||||
pendingPushed, err := h.syncService.PushPendingChanges()
|
pendingPushed, err := h.syncService.PushPendingChanges()
|
||||||
@@ -315,38 +262,6 @@ func (h *SyncHandler) SyncAll(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync components
|
|
||||||
mariaDB, err := h.connMgr.GetDB()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"error": "database connection failed",
|
|
||||||
})
|
|
||||||
_ = c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
compNow := time.Now()
|
|
||||||
compResult, err := h.localDB.SyncComponents(mariaDB)
|
|
||||||
if err != nil {
|
|
||||||
_ = h.localDB.SetComponentSyncResult("error", err.Error(), compNow)
|
|
||||||
h.localDB.AppendSyncLog("components", "error", err.Error(), 0, compNow, time.Since(compNow).Milliseconds())
|
|
||||||
slog.Error("component sync failed during full sync", "error", err)
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
|
||||||
"success": false,
|
|
||||||
"error": "component sync failed",
|
|
||||||
})
|
|
||||||
_ = c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = h.localDB.SetComponentSyncResult("ok", "", compNow)
|
|
||||||
h.localDB.AppendSyncLog("components", "ok", "", compResult.TotalSynced, compNow, compResult.Duration.Milliseconds())
|
|
||||||
componentsSynced = compResult.TotalSynced
|
|
||||||
|
|
||||||
if err := h.localDB.SyncQtSettings(mariaDB); err != nil {
|
|
||||||
slog.Warn("qt_settings sync failed", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sync pricelists
|
// Sync pricelists
|
||||||
plNow := time.Now()
|
plNow := time.Now()
|
||||||
pricelistsSynced, err = h.syncService.SyncPricelists()
|
pricelistsSynced, err = h.syncService.SyncPricelists()
|
||||||
@@ -357,7 +272,6 @@ func (h *SyncHandler) SyncAll(c *gin.Context) {
|
|||||||
"success": false,
|
"success": false,
|
||||||
"error": "pricelist sync failed",
|
"error": "pricelist sync failed",
|
||||||
"pending_pushed": pendingPushed,
|
"pending_pushed": pendingPushed,
|
||||||
"components_synced": componentsSynced,
|
|
||||||
})
|
})
|
||||||
_ = c.Error(err)
|
_ = c.Error(err)
|
||||||
return
|
return
|
||||||
@@ -375,7 +289,6 @@ func (h *SyncHandler) SyncAll(c *gin.Context) {
|
|||||||
"success": false,
|
"success": false,
|
||||||
"error": "project import failed",
|
"error": "project import failed",
|
||||||
"pending_pushed": pendingPushed,
|
"pending_pushed": pendingPushed,
|
||||||
"components_synced": componentsSynced,
|
|
||||||
"pricelists_synced": pricelistsSynced,
|
"pricelists_synced": pricelistsSynced,
|
||||||
})
|
})
|
||||||
_ = c.Error(err)
|
_ = c.Error(err)
|
||||||
@@ -389,7 +302,6 @@ func (h *SyncHandler) SyncAll(c *gin.Context) {
|
|||||||
"success": false,
|
"success": false,
|
||||||
"error": "configuration import failed",
|
"error": "configuration import failed",
|
||||||
"pending_pushed": pendingPushed,
|
"pending_pushed": pendingPushed,
|
||||||
"components_synced": componentsSynced,
|
|
||||||
"pricelists_synced": pricelistsSynced,
|
"pricelists_synced": pricelistsSynced,
|
||||||
"projects_imported": projectsResult.Imported,
|
"projects_imported": projectsResult.Imported,
|
||||||
"projects_updated": projectsResult.Updated,
|
"projects_updated": projectsResult.Updated,
|
||||||
@@ -403,7 +315,6 @@ func (h *SyncHandler) SyncAll(c *gin.Context) {
|
|||||||
Success: true,
|
Success: true,
|
||||||
Message: "Full sync completed successfully",
|
Message: "Full sync completed successfully",
|
||||||
PendingPushed: pendingPushed,
|
PendingPushed: pendingPushed,
|
||||||
ComponentsSynced: componentsSynced,
|
|
||||||
PricelistsSynced: pricelistsSynced,
|
PricelistsSynced: pricelistsSynced,
|
||||||
ProjectsImported: projectsResult.Imported,
|
ProjectsImported: projectsResult.Imported,
|
||||||
ProjectsUpdated: projectsResult.Updated,
|
ProjectsUpdated: projectsResult.Updated,
|
||||||
@@ -564,7 +475,7 @@ func (h *SyncHandler) GetInfo(c *gin.Context) {
|
|||||||
// Get local counts
|
// Get local counts
|
||||||
configCount := h.localDB.CountConfigurations()
|
configCount := h.localDB.CountConfigurations()
|
||||||
projectCount := h.localDB.CountProjects()
|
projectCount := h.localDB.CountProjects()
|
||||||
componentCount := h.localDB.CountLocalComponents()
|
componentCount := h.localDB.CountComponents()
|
||||||
pricelistCount := h.localDB.CountLocalPricelists()
|
pricelistCount := h.localDB.CountLocalPricelists()
|
||||||
|
|
||||||
// Get error count (only changes with LastError != "")
|
// Get error count (only changes with LastError != "")
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
||||||
|
"git.mchus.pro/mchus/quoteforge/internal/models"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/repository"
|
"git.mchus.pro/mchus/quoteforge/internal/repository"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/services"
|
"git.mchus.pro/mchus/quoteforge/internal/services"
|
||||||
syncsvc "git.mchus.pro/mchus/quoteforge/internal/services/sync"
|
syncsvc "git.mchus.pro/mchus/quoteforge/internal/services/sync"
|
||||||
@@ -172,7 +172,7 @@ func normalizeLotMappings(in []localdb.VendorSpecLotMapping) []localdb.VendorSpe
|
|||||||
merged := make(map[string]int, len(in))
|
merged := make(map[string]int, len(in))
|
||||||
order := make([]string, 0, len(in))
|
order := make([]string, 0, len(in))
|
||||||
for _, m := range in {
|
for _, m := range in {
|
||||||
lot := strings.TrimSpace(m.LotName)
|
lot := models.NormalizeLotName(m.LotName)
|
||||||
if lot == "" {
|
if lot == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ package localdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ComponentFilter for searching with filters
|
// ComponentFilter for searching with filters
|
||||||
@@ -24,344 +21,213 @@ type ComponentSyncResult struct {
|
|||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncComponents loads components from MariaDB (lot + qt_lot_metadata) into local_components
|
// latestActivePricelistID returns the local DB id of the most recently created
|
||||||
func (l *LocalDB) SyncComponents(mariaDB *gorm.DB) (*ComponentSyncResult, error) {
|
// active pricelist for the given source ("estimate", "warehouse", etc.).
|
||||||
startTime := time.Now()
|
func (l *LocalDB) latestActivePricelistID(source string) (uint, error) {
|
||||||
|
var id uint
|
||||||
// Build the component catalog from every runtime source of LOT names.
|
err := l.db.Table("local_pricelists").
|
||||||
// Storage lots may exist in qt_lot_metadata / qt_pricelist_items before they appear in lot,
|
Select("id").
|
||||||
// so the sync cannot start from lot alone.
|
Where("is_active = ? AND source = ?", true, source).
|
||||||
type componentRow struct {
|
Order("created_at DESC, id DESC").
|
||||||
LotName string
|
Limit(1).
|
||||||
LotDescription string
|
Scan(&id).Error
|
||||||
Category *string
|
|
||||||
Model *string
|
|
||||||
}
|
|
||||||
|
|
||||||
var rows []componentRow
|
|
||||||
err := mariaDB.Raw(`
|
|
||||||
SELECT
|
|
||||||
src.lot_name,
|
|
||||||
COALESCE(MAX(NULLIF(TRIM(l.lot_description), '')), '') AS lot_description,
|
|
||||||
COALESCE(
|
|
||||||
MAX(NULLIF(TRIM(c.code), '')),
|
|
||||||
MAX(NULLIF(TRIM(l.lot_category), '')),
|
|
||||||
SUBSTRING_INDEX(src.lot_name, '_', 1)
|
|
||||||
) AS category,
|
|
||||||
MAX(NULLIF(TRIM(m.model), '')) AS model
|
|
||||||
FROM (
|
|
||||||
SELECT lot_name FROM lot
|
|
||||||
UNION
|
|
||||||
SELECT lot_name FROM qt_lot_metadata
|
|
||||||
WHERE is_hidden = FALSE OR is_hidden IS NULL
|
|
||||||
UNION
|
|
||||||
SELECT lot_name FROM qt_pricelist_items
|
|
||||||
) src
|
|
||||||
LEFT JOIN lot l ON l.lot_name = src.lot_name
|
|
||||||
LEFT JOIN qt_lot_metadata m
|
|
||||||
ON m.lot_name = src.lot_name
|
|
||||||
AND (m.is_hidden = FALSE OR m.is_hidden IS NULL)
|
|
||||||
LEFT JOIN qt_categories c ON m.category_id = c.id
|
|
||||||
GROUP BY src.lot_name
|
|
||||||
ORDER BY src.lot_name
|
|
||||||
`).Scan(&rows).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("querying components from MariaDB: %w", err)
|
return 0, err
|
||||||
|
}
|
||||||
|
if id == 0 {
|
||||||
|
return 0, fmt.Errorf("no active %s pricelist", source)
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(rows) == 0 {
|
// pricelistItemRow is used for scanning rows from local_pricelist_items.
|
||||||
slog.Warn("no components found in MariaDB")
|
type pricelistItemRow struct {
|
||||||
return &ComponentSyncResult{
|
LotName string `gorm:"column:lot_name"`
|
||||||
Duration: time.Since(startTime),
|
Category string `gorm:"column:lot_category"`
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get existing local components for comparison
|
func (r pricelistItemRow) toLocalComponent() LocalComponent {
|
||||||
existingMap := make(map[string]bool)
|
return LocalComponent{
|
||||||
var existing []LocalComponent
|
LotName: r.LotName,
|
||||||
if err := l.db.Find(&existing).Error; err != nil {
|
Category: r.Category,
|
||||||
return nil, fmt.Errorf("reading existing local components: %w", err)
|
|
||||||
}
|
|
||||||
for _, c := range existing {
|
|
||||||
existingMap[c.LotName] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare components for batch insert/update.
|
|
||||||
// Source joins may duplicate the same lot_name, so collapse them before insert.
|
|
||||||
syncTime := time.Now()
|
|
||||||
components := make([]LocalComponent, 0, len(rows))
|
|
||||||
componentIndex := make(map[string]int, len(rows))
|
|
||||||
newCount := 0
|
|
||||||
|
|
||||||
for _, row := range rows {
|
|
||||||
lotName := strings.TrimSpace(row.LotName)
|
|
||||||
if lotName == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
category := ""
|
|
||||||
if row.Category != nil {
|
|
||||||
category = strings.TrimSpace(*row.Category)
|
|
||||||
} else {
|
|
||||||
// Parse category from lot_name (e.g., "CPU_AMD_9654" -> "CPU")
|
|
||||||
parts := strings.SplitN(lotName, "_", 2)
|
|
||||||
if len(parts) >= 1 {
|
|
||||||
category = parts[0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
model := ""
|
|
||||||
if row.Model != nil {
|
|
||||||
model = strings.TrimSpace(*row.Model)
|
|
||||||
}
|
|
||||||
|
|
||||||
comp := LocalComponent{
|
// SearchLocalComponents searches components in the latest active estimate
|
||||||
LotName: lotName,
|
// pricelist by lot_name.
|
||||||
LotDescription: strings.TrimSpace(row.LotDescription),
|
|
||||||
Category: category,
|
|
||||||
Model: model,
|
|
||||||
}
|
|
||||||
|
|
||||||
if idx, exists := componentIndex[lotName]; exists {
|
|
||||||
// Keep the first row, but fill any missing metadata from duplicates.
|
|
||||||
if components[idx].LotDescription == "" && comp.LotDescription != "" {
|
|
||||||
components[idx].LotDescription = comp.LotDescription
|
|
||||||
}
|
|
||||||
if components[idx].Category == "" && comp.Category != "" {
|
|
||||||
components[idx].Category = comp.Category
|
|
||||||
}
|
|
||||||
if components[idx].Model == "" && comp.Model != "" {
|
|
||||||
components[idx].Model = comp.Model
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
componentIndex[lotName] = len(components)
|
|
||||||
components = append(components, comp)
|
|
||||||
|
|
||||||
if !existingMap[lotName] {
|
|
||||||
newCount++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use transaction for bulk upsert
|
|
||||||
err = l.db.Transaction(func(tx *gorm.DB) error {
|
|
||||||
// Delete all existing and insert new (simpler than upsert for SQLite)
|
|
||||||
if err := tx.Where("1=1").Delete(&LocalComponent{}).Error; err != nil {
|
|
||||||
return fmt.Errorf("clearing local components: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Batch insert
|
|
||||||
batchSize := 500
|
|
||||||
for i := 0; i < len(components); i += batchSize {
|
|
||||||
end := i + batchSize
|
|
||||||
if end > len(components) {
|
|
||||||
end = len(components)
|
|
||||||
}
|
|
||||||
if err := tx.CreateInBatches(components[i:end], batchSize).Error; err != nil {
|
|
||||||
return fmt.Errorf("inserting components batch: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update last sync time
|
|
||||||
if err := l.SetComponentSyncTime(syncTime); err != nil {
|
|
||||||
slog.Warn("failed to update component sync time", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := &ComponentSyncResult{
|
|
||||||
TotalSynced: len(components),
|
|
||||||
NewCount: newCount,
|
|
||||||
UpdateCount: len(components) - newCount,
|
|
||||||
Duration: time.Since(startTime),
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("components synced",
|
|
||||||
"total", result.TotalSynced,
|
|
||||||
"new", result.NewCount,
|
|
||||||
"updated", result.UpdateCount,
|
|
||||||
"duration", result.Duration)
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SearchLocalComponents searches components in local cache by query string
|
|
||||||
// Searches in lot_name, lot_description, category, and model fields
|
|
||||||
func (l *LocalDB) SearchLocalComponents(query string, limit int) ([]LocalComponent, error) {
|
func (l *LocalDB) SearchLocalComponents(query string, limit int) ([]LocalComponent, error) {
|
||||||
if limit <= 0 {
|
if limit <= 0 {
|
||||||
limit = 50
|
limit = 50
|
||||||
}
|
}
|
||||||
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
var components []LocalComponent
|
|
||||||
|
|
||||||
if query == "" {
|
|
||||||
// Return all components with limit
|
|
||||||
err := l.db.Order("lot_name").Limit(limit).Find(&components).Error
|
|
||||||
return components, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search with LIKE on multiple fields
|
|
||||||
searchPattern := "%" + strings.ToLower(query) + "%"
|
|
||||||
|
|
||||||
err := l.db.Where(
|
|
||||||
"LOWER(lot_name) LIKE ? OR LOWER(lot_description) LIKE ? OR LOWER(category) LIKE ? OR LOWER(model) LIKE ?",
|
|
||||||
searchPattern, searchPattern, searchPattern, searchPattern,
|
|
||||||
).Order("lot_name").Limit(limit).Find(&components).Error
|
|
||||||
|
|
||||||
return components, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SearchLocalComponentsByCategory searches components by category and optional query
|
|
||||||
func (l *LocalDB) SearchLocalComponentsByCategory(category string, query string, limit int) ([]LocalComponent, error) {
|
|
||||||
if limit <= 0 {
|
|
||||||
limit = 50
|
|
||||||
}
|
|
||||||
|
|
||||||
var components []LocalComponent
|
|
||||||
db := l.db.Where("LOWER(category) = ?", strings.ToLower(category))
|
|
||||||
|
|
||||||
if query != "" {
|
|
||||||
searchPattern := "%" + strings.ToLower(query) + "%"
|
|
||||||
db = db.Where(
|
|
||||||
"LOWER(lot_name) LIKE ? OR LOWER(lot_description) LIKE ? OR LOWER(model) LIKE ?",
|
|
||||||
searchPattern, searchPattern, searchPattern,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := db.Order("lot_name").Limit(limit).Find(&components).Error
|
|
||||||
return components, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListComponents returns components with filtering and pagination
|
|
||||||
func (l *LocalDB) ListComponents(filter ComponentFilter, offset, limit int) ([]LocalComponent, int64, error) {
|
|
||||||
db := l.db
|
|
||||||
|
|
||||||
// Apply category filter
|
|
||||||
if filter.Category != "" {
|
|
||||||
db = db.Where("LOWER(category) = ?", strings.ToLower(filter.Category))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply search filter
|
|
||||||
if filter.Search != "" {
|
|
||||||
searchPattern := "%" + strings.ToLower(filter.Search) + "%"
|
|
||||||
db = db.Where(
|
|
||||||
"LOWER(lot_name) LIKE ? OR LOWER(lot_description) LIKE ? OR LOWER(category) LIKE ? OR LOWER(model) LIKE ?",
|
|
||||||
searchPattern, searchPattern, searchPattern, searchPattern,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get total count
|
|
||||||
var total int64
|
|
||||||
if err := db.Model(&LocalComponent{}).Count(&total).Error; err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply pagination and get results
|
|
||||||
var components []LocalComponent
|
|
||||||
if err := db.Order("lot_name").Offset(offset).Limit(limit).Find(&components).Error; err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return components, total, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetLocalComponent returns a single component by lot_name
|
|
||||||
func (l *LocalDB) GetLocalComponent(lotName string) (*LocalComponent, error) {
|
|
||||||
var component LocalComponent
|
|
||||||
err := l.db.Where("lot_name = ?", lotName).First(&component).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &component, nil
|
|
||||||
|
db := l.db.Table("local_pricelist_items").
|
||||||
|
Where("pricelist_id = ?", pricelistID)
|
||||||
|
if query != "" {
|
||||||
|
db = db.Where("LOWER(lot_name) LIKE ?", "%"+strings.ToLower(query)+"%")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalComponentCategoriesByLotNames returns category for each lot_name in the local component cache.
|
var rows []pricelistItemRow
|
||||||
// Missing lots are not included in the map; caller is responsible for strict validation.
|
if err := db.Select("lot_name, lot_category").Order("lot_name").Limit(limit).Scan(&rows).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
components := make([]LocalComponent, len(rows))
|
||||||
|
for i, r := range rows {
|
||||||
|
components[i] = r.toLocalComponent()
|
||||||
|
}
|
||||||
|
return components, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchLocalComponentsByCategory searches components in the latest active
|
||||||
|
// estimate pricelist filtered by category.
|
||||||
|
func (l *LocalDB) SearchLocalComponentsByCategory(category, query string, limit int) ([]LocalComponent, error) {
|
||||||
|
if limit <= 0 {
|
||||||
|
limit = 50
|
||||||
|
}
|
||||||
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
db := l.db.Table("local_pricelist_items").
|
||||||
|
Where("pricelist_id = ? AND UPPER(lot_category) = ?", pricelistID, strings.ToUpper(category))
|
||||||
|
if query != "" {
|
||||||
|
db = db.Where("LOWER(lot_name) LIKE ?", "%"+strings.ToLower(query)+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows []pricelistItemRow
|
||||||
|
if err := db.Select("lot_name, lot_category").Order("lot_name").Limit(limit).Scan(&rows).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
components := make([]LocalComponent, len(rows))
|
||||||
|
for i, r := range rows {
|
||||||
|
components[i] = r.toLocalComponent()
|
||||||
|
}
|
||||||
|
return components, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListComponents returns components from the latest active estimate pricelist
|
||||||
|
// with optional category/search filtering and pagination.
|
||||||
|
func (l *LocalDB) ListComponents(filter ComponentFilter, offset, limit int) ([]LocalComponent, int64, error) {
|
||||||
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
db := l.db.Table("local_pricelist_items").
|
||||||
|
Where("pricelist_id = ?", pricelistID)
|
||||||
|
|
||||||
|
if filter.Category != "" {
|
||||||
|
db = db.Where("UPPER(lot_category) = ?", strings.ToUpper(filter.Category))
|
||||||
|
}
|
||||||
|
if filter.Search != "" {
|
||||||
|
db = db.Where("LOWER(lot_name) LIKE ?", "%"+strings.ToLower(filter.Search)+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
var total int64
|
||||||
|
if err := db.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows []pricelistItemRow
|
||||||
|
if err := db.Select("lot_name, lot_category").Order("lot_name").Offset(offset).Limit(limit).Scan(&rows).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
components := make([]LocalComponent, len(rows))
|
||||||
|
for i, r := range rows {
|
||||||
|
components[i] = r.toLocalComponent()
|
||||||
|
}
|
||||||
|
return components, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLocalComponent returns a single component by lot_name from the latest
|
||||||
|
// active estimate pricelist.
|
||||||
|
func (l *LocalDB) GetLocalComponent(lotName string) (*LocalComponent, error) {
|
||||||
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var row pricelistItemRow
|
||||||
|
if err := l.db.Table("local_pricelist_items").
|
||||||
|
Select("lot_name, lot_category").
|
||||||
|
Where("pricelist_id = ? AND UPPER(lot_name) = ?", pricelistID, strings.ToUpper(lotName)).
|
||||||
|
First(&row).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c := row.toLocalComponent()
|
||||||
|
return &c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLocalComponentCategoriesByLotNames returns category for each lot_name
|
||||||
|
// from the latest active estimate pricelist.
|
||||||
func (l *LocalDB) GetLocalComponentCategoriesByLotNames(lotNames []string) (map[string]string, error) {
|
func (l *LocalDB) GetLocalComponentCategoriesByLotNames(lotNames []string) (map[string]string, error) {
|
||||||
result := make(map[string]string, len(lotNames))
|
result := make(map[string]string, len(lotNames))
|
||||||
if len(lotNames) == 0 {
|
if len(lotNames) == 0 {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
type row struct {
|
if err != nil {
|
||||||
LotName string `gorm:"column:lot_name"`
|
return result, nil
|
||||||
Category string `gorm:"column:category"`
|
|
||||||
}
|
}
|
||||||
var rows []row
|
|
||||||
if err := l.db.Model(&LocalComponent{}).
|
// Build uppercase → original mapping so result keys match what the caller passed.
|
||||||
Select("lot_name, category").
|
upperToOrig := make(map[string]string, len(lotNames))
|
||||||
Where("lot_name IN ?", lotNames).
|
upper := make([]string, len(lotNames))
|
||||||
Find(&rows).Error; err != nil {
|
for i, n := range lotNames {
|
||||||
|
u := strings.ToUpper(n)
|
||||||
|
upper[i] = u
|
||||||
|
upperToOrig[u] = n
|
||||||
|
}
|
||||||
|
var rows []pricelistItemRow
|
||||||
|
if err := l.db.Table("local_pricelist_items").
|
||||||
|
Select("lot_name, lot_category").
|
||||||
|
Where("pricelist_id = ? AND UPPER(lot_name) IN ?", pricelistID, upper).
|
||||||
|
Scan(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, r := range rows {
|
for _, r := range rows {
|
||||||
result[r.LotName] = r.Category
|
orig := upperToOrig[strings.ToUpper(r.LotName)]
|
||||||
|
if orig == "" {
|
||||||
|
orig = r.LotName
|
||||||
|
}
|
||||||
|
result[orig] = r.Category
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalComponentCategories returns distinct categories from local components
|
// GetLocalComponentCategories returns distinct categories from the latest
|
||||||
|
// active estimate pricelist.
|
||||||
func (l *LocalDB) GetLocalComponentCategories() ([]string, error) {
|
func (l *LocalDB) GetLocalComponentCategories() ([]string, error) {
|
||||||
var categories []string
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
err := l.db.Model(&LocalComponent{}).
|
|
||||||
Distinct("category").
|
|
||||||
Where("category != ''").
|
|
||||||
Order("category").
|
|
||||||
Pluck("category", &categories).Error
|
|
||||||
return categories, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountLocalComponents returns the total number of local components
|
|
||||||
func (l *LocalDB) CountLocalComponents() int64 {
|
|
||||||
var count int64
|
|
||||||
l.db.Model(&LocalComponent{}).Count(&count)
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountLocalComponentsByCategory returns component count by category
|
|
||||||
func (l *LocalDB) CountLocalComponentsByCategory(category string) int64 {
|
|
||||||
var count int64
|
|
||||||
l.db.Model(&LocalComponent{}).Where("LOWER(category) = ?", strings.ToLower(category)).Count(&count)
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetComponentSyncTime returns the last component sync timestamp
|
|
||||||
func (l *LocalDB) GetComponentSyncTime() *time.Time {
|
|
||||||
var setting struct {
|
|
||||||
Value string
|
|
||||||
}
|
|
||||||
if err := l.db.Table("app_settings").
|
|
||||||
Where("key = ?", "last_component_sync").
|
|
||||||
First(&setting).Error; err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
t, err := time.Parse(time.RFC3339, setting.Value)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil, err
|
||||||
}
|
|
||||||
return &t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetComponentSyncTime sets the last component sync timestamp
|
var categories []string
|
||||||
func (l *LocalDB) SetComponentSyncTime(t time.Time) error {
|
if err := l.db.Table("local_pricelist_items").
|
||||||
return l.db.Exec(`
|
Where("pricelist_id = ? AND lot_category != ''", pricelistID).
|
||||||
INSERT INTO app_settings (key, value, updated_at)
|
Distinct("lot_category").
|
||||||
VALUES (?, ?, ?)
|
Order("lot_category").
|
||||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
|
Pluck("lot_category", &categories).Error; err != nil {
|
||||||
`, "last_component_sync", t.Format(time.RFC3339), time.Now().Format(time.RFC3339)).Error
|
return nil, err
|
||||||
|
}
|
||||||
|
return categories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NeedComponentSync checks if component sync is needed (older than specified hours)
|
// CountComponents returns the number of distinct lot names in the latest
|
||||||
func (l *LocalDB) NeedComponentSync(maxAgeHours int) bool {
|
// active estimate pricelist (used to check if data is available).
|
||||||
syncTime := l.GetComponentSyncTime()
|
func (l *LocalDB) CountComponents() int64 {
|
||||||
if syncTime == nil {
|
pricelistID, err := l.latestActivePricelistID("estimate")
|
||||||
return true
|
if err != nil {
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return time.Since(*syncTime).Hours() > float64(maxAgeHours)
|
var count int64
|
||||||
|
l.db.Table("local_pricelist_items").Where("pricelist_id = ?", pricelistID).Count(&count)
|
||||||
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func ConfigurationToLocal(cfg *models.Configuration) *LocalConfiguration {
|
|||||||
items := make(LocalConfigItems, len(cfg.Items))
|
items := make(LocalConfigItems, len(cfg.Items))
|
||||||
for i, item := range cfg.Items {
|
for i, item := range cfg.Items {
|
||||||
items[i] = LocalConfigItem{
|
items[i] = LocalConfigItem{
|
||||||
LotName: item.LotName,
|
LotName: models.NormalizeLotName(item.LotName),
|
||||||
Quantity: item.Quantity,
|
Quantity: item.Quantity,
|
||||||
UnitPrice: item.UnitPrice,
|
UnitPrice: item.UnitPrice,
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,7 @@ func PricelistItemToLocal(item *models.PricelistItem, localPricelistID uint) *Lo
|
|||||||
partnumbers = append(partnumbers, item.Partnumbers...)
|
partnumbers = append(partnumbers, item.Partnumbers...)
|
||||||
return &LocalPricelistItem{
|
return &LocalPricelistItem{
|
||||||
PricelistID: localPricelistID,
|
PricelistID: localPricelistID,
|
||||||
LotName: item.LotName,
|
LotName: models.NormalizeLotName(item.LotName),
|
||||||
LotCategory: item.LotCategory,
|
LotCategory: item.LotCategory,
|
||||||
Price: item.Price,
|
Price: item.Price,
|
||||||
AvailableQty: item.AvailableQty,
|
AvailableQty: item.AvailableQty,
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ type LocalDB struct {
|
|||||||
var localReadOnlyCacheTables = []string{
|
var localReadOnlyCacheTables = []string{
|
||||||
"local_pricelist_items",
|
"local_pricelist_items",
|
||||||
"local_pricelists",
|
"local_pricelists",
|
||||||
"local_components",
|
|
||||||
"local_partnumber_book_items",
|
"local_partnumber_book_items",
|
||||||
"local_partnumber_books",
|
"local_partnumber_books",
|
||||||
}
|
}
|
||||||
@@ -78,7 +77,6 @@ func ResetData(dbPath string) error {
|
|||||||
"local_configuration_versions",
|
"local_configuration_versions",
|
||||||
"local_pricelists",
|
"local_pricelists",
|
||||||
"local_pricelist_items",
|
"local_pricelist_items",
|
||||||
"local_components",
|
|
||||||
"local_sync_guard_state",
|
"local_sync_guard_state",
|
||||||
"pending_changes",
|
"pending_changes",
|
||||||
"app_settings",
|
"app_settings",
|
||||||
@@ -224,7 +222,6 @@ func autoMigrateLocalSchema(db *gorm.DB) error {
|
|||||||
&LocalConfigurationVersion{},
|
&LocalConfigurationVersion{},
|
||||||
&LocalPricelist{},
|
&LocalPricelist{},
|
||||||
&LocalPricelistItem{},
|
&LocalPricelistItem{},
|
||||||
&LocalComponent{},
|
|
||||||
&AppSetting{},
|
&AppSetting{},
|
||||||
&LocalSyncGuardState{},
|
&LocalSyncGuardState{},
|
||||||
&PendingChange{},
|
&PendingChange{},
|
||||||
@@ -692,6 +689,22 @@ func (l *LocalDB) GetProjectByUUID(uuid string) (*LocalProject, error) {
|
|||||||
return &project, nil
|
return &project, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LocalDB) GetProjectByCode(code string) (*LocalProject, error) {
|
||||||
|
var project LocalProject
|
||||||
|
if err := l.db.Where("LOWER(code) = LOWER(?) AND variant = ''", code).First(&project).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &project, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LocalDB) GetProjectByCodeAndVariant(code, variant string) (*LocalProject, error) {
|
||||||
|
var project LocalProject
|
||||||
|
if err := l.db.Where("LOWER(code) = LOWER(?) AND LOWER(variant) = LOWER(?)", code, variant).First(&project).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &project, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (l *LocalDB) GetProjectByName(ownerUsername, name string) (*LocalProject, error) {
|
func (l *LocalDB) GetProjectByName(ownerUsername, name string) (*LocalProject, error) {
|
||||||
var project LocalProject
|
var project LocalProject
|
||||||
if err := l.db.Where("owner_username = ? AND name = ?", ownerUsername, name).First(&project).Error; err != nil {
|
if err := l.db.Where("owner_username = ? AND name = ?", ownerUsername, name).First(&project).Error; err != nil {
|
||||||
@@ -1221,25 +1234,6 @@ func (l *LocalDB) GetLastComponentSyncError() string {
|
|||||||
return strings.TrimSpace(value)
|
return strings.TrimSpace(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalDB) SetComponentSyncResult(status, errorText string, attemptedAt time.Time) error {
|
|
||||||
status = strings.TrimSpace(status)
|
|
||||||
errorText = strings.TrimSpace(errorText)
|
|
||||||
if status == "" {
|
|
||||||
status = "unknown"
|
|
||||||
}
|
|
||||||
return l.db.Transaction(func(tx *gorm.DB) error {
|
|
||||||
if err := l.upsertAppSetting(tx, "last_component_sync_status", status, attemptedAt); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := l.upsertAppSetting(tx, "last_component_sync_error", errorText, attemptedAt); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := l.upsertAppSetting(tx, "last_component_sync_attempt_at", attemptedAt.Format(time.RFC3339), attemptedAt); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountLocalPricelists returns the number of local pricelists
|
// CountLocalPricelists returns the number of local pricelists
|
||||||
func (l *LocalDB) CountLocalPricelists() int64 {
|
func (l *LocalDB) CountLocalPricelists() int64 {
|
||||||
@@ -1255,11 +1249,10 @@ func (l *LocalDB) CountAllPricelistItems() int64 {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountComponents returns the number of rows in local_components.
|
|
||||||
func (l *LocalDB) CountComponents() int64 {
|
// DBFilePath returns the path to the SQLite database file.
|
||||||
var count int64
|
func (l *LocalDB) DBFilePath() string {
|
||||||
l.db.Model(&LocalComponent{}).Count(&count)
|
return l.path
|
||||||
return count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBFileSizeBytes returns the size of the SQLite database file in bytes.
|
// DBFileSizeBytes returns the size of the SQLite database file in bytes.
|
||||||
@@ -1271,11 +1264,11 @@ func (l *LocalDB) DBFileSizeBytes() int64 {
|
|||||||
return info.Size()
|
return info.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLatestLocalPricelist returns the most recently synced pricelist
|
// GetLatestLocalPricelist returns the most recently synced active estimate pricelist.
|
||||||
func (l *LocalDB) GetLatestLocalPricelist() (*LocalPricelist, error) {
|
func (l *LocalDB) GetLatestLocalPricelist() (*LocalPricelist, error) {
|
||||||
var pricelist LocalPricelist
|
var pricelist LocalPricelist
|
||||||
if err := l.db.
|
if err := l.db.
|
||||||
Where("source = ?", "estimate").
|
Where("source = ? AND is_active = ?", "estimate", true).
|
||||||
Where("EXISTS (SELECT 1 FROM local_pricelist_items WHERE local_pricelist_items.pricelist_id = local_pricelists.id)").
|
Where("EXISTS (SELECT 1 FROM local_pricelist_items WHERE local_pricelist_items.pricelist_id = local_pricelists.id)").
|
||||||
Order("created_at DESC, id DESC").
|
Order("created_at DESC, id DESC").
|
||||||
First(&pricelist).Error; err != nil {
|
First(&pricelist).Error; err != nil {
|
||||||
@@ -1284,11 +1277,11 @@ func (l *LocalDB) GetLatestLocalPricelist() (*LocalPricelist, error) {
|
|||||||
return &pricelist, nil
|
return &pricelist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLatestLocalPricelistBySource returns the most recently synced pricelist for a source.
|
// GetLatestLocalPricelistBySource returns the most recently synced active pricelist for a source.
|
||||||
func (l *LocalDB) GetLatestLocalPricelistBySource(source string) (*LocalPricelist, error) {
|
func (l *LocalDB) GetLatestLocalPricelistBySource(source string) (*LocalPricelist, error) {
|
||||||
var pricelist LocalPricelist
|
var pricelist LocalPricelist
|
||||||
if err := l.db.
|
if err := l.db.
|
||||||
Where("source = ?", source).
|
Where("source = ? AND is_active = ?", source, true).
|
||||||
Where("EXISTS (SELECT 1 FROM local_pricelist_items WHERE local_pricelist_items.pricelist_id = local_pricelists.id)").
|
Where("EXISTS (SELECT 1 FROM local_pricelist_items WHERE local_pricelist_items.pricelist_id = local_pricelists.id)").
|
||||||
Order("created_at DESC, id DESC").
|
Order("created_at DESC, id DESC").
|
||||||
First(&pricelist).Error; err != nil {
|
First(&pricelist).Error; err != nil {
|
||||||
@@ -1297,6 +1290,17 @@ func (l *LocalDB) GetLatestLocalPricelistBySource(source string) (*LocalPricelis
|
|||||||
return &pricelist, nil
|
return &pricelist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeactivateLocalPricelistsNotIn marks all local pricelists with is_active=true whose
|
||||||
|
// server_id is not in activeServerIDs as inactive. Used after each pricelist sync to
|
||||||
|
// mirror server-side deactivations locally.
|
||||||
|
func (l *LocalDB) DeactivateLocalPricelistsNotIn(activeServerIDs []uint) error {
|
||||||
|
q := l.db.Model(&LocalPricelist{}).Where("is_active = ?", true)
|
||||||
|
if len(activeServerIDs) > 0 {
|
||||||
|
q = q.Where("server_id NOT IN ?", activeServerIDs)
|
||||||
|
}
|
||||||
|
return q.Update("is_active", false).Error
|
||||||
|
}
|
||||||
|
|
||||||
// GetLocalPricelistByServerID returns a local pricelist by its server ID
|
// GetLocalPricelistByServerID returns a local pricelist by its server ID
|
||||||
func (l *LocalDB) GetLocalPricelistByServerID(serverID uint) (*LocalPricelist, error) {
|
func (l *LocalDB) GetLocalPricelistByServerID(serverID uint) (*LocalPricelist, error) {
|
||||||
var pricelist LocalPricelist
|
var pricelist LocalPricelist
|
||||||
@@ -1364,6 +1368,30 @@ func (l *LocalDB) CountLocalPricelistItems(pricelistID uint) int64 {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLocalPricelistCoverageByCategory returns item count per lot_category and the total
|
||||||
|
// for the given local pricelist ID. Only items with price > 0 are counted.
|
||||||
|
func (l *LocalDB) GetLocalPricelistCoverageByCategory(pricelistID uint) (map[string]int64, int64, error) {
|
||||||
|
type row struct {
|
||||||
|
Category string `gorm:"column:lot_category"`
|
||||||
|
Count int64 `gorm:"column:cnt"`
|
||||||
|
}
|
||||||
|
var rows []row
|
||||||
|
if err := l.db.Model(&LocalPricelistItem{}).
|
||||||
|
Select("COALESCE(NULLIF(TRIM(lot_category),''), '?') AS lot_category, COUNT(*) AS cnt").
|
||||||
|
Where("pricelist_id = ? AND price > 0", pricelistID).
|
||||||
|
Group("lot_category").
|
||||||
|
Scan(&rows).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
result := make(map[string]int64, len(rows))
|
||||||
|
var total int64
|
||||||
|
for _, r := range rows {
|
||||||
|
result[r.Category] = r.Count
|
||||||
|
total += r.Count
|
||||||
|
}
|
||||||
|
return result, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CountLocalPricelistItemsWithEmptyCategory returns the number of items for a pricelist with missing lot_category.
|
// CountLocalPricelistItemsWithEmptyCategory returns the number of items for a pricelist with missing lot_category.
|
||||||
func (l *LocalDB) CountLocalPricelistItemsWithEmptyCategory(pricelistID uint) (int64, error) {
|
func (l *LocalDB) CountLocalPricelistItemsWithEmptyCategory(pricelistID uint) (int64, error) {
|
||||||
var count int64
|
var count int64
|
||||||
@@ -1428,10 +1456,11 @@ func (l *LocalDB) GetLocalPricelistItems(pricelistID uint) ([]LocalPricelistItem
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalPriceForLot returns the price for a lot from a local pricelist
|
// GetLocalPriceForLot returns the price for a lot from a local pricelist.
|
||||||
|
// Matching is case-insensitive via UPPER(lot_name) to handle legacy mixed-case rows.
|
||||||
func (l *LocalDB) GetLocalPriceForLot(pricelistID uint, lotName string) (float64, error) {
|
func (l *LocalDB) GetLocalPriceForLot(pricelistID uint, lotName string) (float64, error) {
|
||||||
var item LocalPricelistItem
|
var item LocalPricelistItem
|
||||||
if err := l.db.Where("pricelist_id = ? AND lot_name = ?", pricelistID, lotName).
|
if err := l.db.Where("pricelist_id = ? AND UPPER(lot_name) = ?", pricelistID, strings.ToUpper(lotName)).
|
||||||
First(&item).Error; err != nil {
|
First(&item).Error; err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -1439,26 +1468,32 @@ func (l *LocalDB) GetLocalPriceForLot(pricelistID uint, lotName string) (float64
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalPricesForLots returns prices for multiple lots from a local pricelist in a single query.
|
// GetLocalPricesForLots returns prices for multiple lots from a local pricelist in a single query.
|
||||||
// Uses the composite index (pricelist_id, lot_name). Missing lots are omitted from the result.
|
// Missing lots are omitted from the result.
|
||||||
|
// lotNames must already be normalized (uppercased); matching is done via UPPER(lot_name) to handle
|
||||||
|
// legacy rows that were stored in mixed case before normalization was enforced at sync time.
|
||||||
|
// Keys in the returned map are uppercased (matching the input lotNames).
|
||||||
func (l *LocalDB) GetLocalPricesForLots(pricelistID uint, lotNames []string) (map[string]float64, error) {
|
func (l *LocalDB) GetLocalPricesForLots(pricelistID uint, lotNames []string) (map[string]float64, error) {
|
||||||
result := make(map[string]float64, len(lotNames))
|
result := make(map[string]float64, len(lotNames))
|
||||||
if len(lotNames) == 0 {
|
if len(lotNames) == 0 {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type row struct {
|
type row struct {
|
||||||
LotName string `gorm:"column:lot_name"`
|
LotName string `gorm:"column:lot_name"`
|
||||||
Price float64 `gorm:"column:price"`
|
Price float64 `gorm:"column:price"`
|
||||||
}
|
}
|
||||||
var rows []row
|
var rows []row
|
||||||
|
// Use UPPER(lot_name) so rows synced before normalization (mixed-case) are still matched.
|
||||||
if err := l.db.Model(&LocalPricelistItem{}).
|
if err := l.db.Model(&LocalPricelistItem{}).
|
||||||
Select("lot_name, price").
|
Select("lot_name, price").
|
||||||
Where("pricelist_id = ? AND lot_name IN ?", pricelistID, lotNames).
|
Where("pricelist_id = ? AND UPPER(lot_name) IN ?", pricelistID, lotNames).
|
||||||
Find(&rows).Error; err != nil {
|
Find(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, r := range rows {
|
for _, r := range rows {
|
||||||
if r.Price > 0 {
|
if r.Price > 0 {
|
||||||
result[r.LotName] = r.Price
|
// Key must be uppercase to match callers that normalise lot names before lookup.
|
||||||
|
result[strings.ToUpper(r.LotName)] = r.Price
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -1481,15 +1516,27 @@ func (l *LocalDB) GetLocalLotCategoriesByServerPricelistID(serverPricelistID uin
|
|||||||
LotName string `gorm:"column:lot_name"`
|
LotName string `gorm:"column:lot_name"`
|
||||||
LotCategory string `gorm:"column:lot_category"`
|
LotCategory string `gorm:"column:lot_category"`
|
||||||
}
|
}
|
||||||
|
// Build uppercase → original mapping so result keys match what the caller passed.
|
||||||
|
upperToOrig := make(map[string]string, len(lotNames))
|
||||||
|
upper := make([]string, len(lotNames))
|
||||||
|
for i, n := range lotNames {
|
||||||
|
u := strings.ToUpper(n)
|
||||||
|
upper[i] = u
|
||||||
|
upperToOrig[u] = n
|
||||||
|
}
|
||||||
var rows []row
|
var rows []row
|
||||||
if err := l.db.Model(&LocalPricelistItem{}).
|
if err := l.db.Model(&LocalPricelistItem{}).
|
||||||
Select("lot_name, lot_category").
|
Select("lot_name, lot_category").
|
||||||
Where("pricelist_id = ? AND lot_name IN ?", localPL.ID, lotNames).
|
Where("pricelist_id = ? AND UPPER(lot_name) IN ?", localPL.ID, upper).
|
||||||
Find(&rows).Error; err != nil {
|
Find(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, r := range rows {
|
for _, r := range rows {
|
||||||
result[r.LotName] = r.LotCategory
|
orig := upperToOrig[strings.ToUpper(r.LotName)]
|
||||||
|
if orig == "" {
|
||||||
|
orig = r.LotName
|
||||||
|
}
|
||||||
|
result[orig] = r.LotCategory
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
@@ -1855,28 +1902,6 @@ func (l *LocalDB) GetLocalPricelistItemsPage(pricelistID uint, search string, pa
|
|||||||
return items, total, nil
|
return items, total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalComponentDescriptionsByLotNames returns a map of lot_name → lot_description for the given lots.
|
|
||||||
func (l *LocalDB) GetLocalComponentDescriptionsByLotNames(lotNames []string) (map[string]string, error) {
|
|
||||||
if len(lotNames) == 0 {
|
|
||||||
return map[string]string{}, nil
|
|
||||||
}
|
|
||||||
type row struct {
|
|
||||||
LotName string
|
|
||||||
LotDescription string
|
|
||||||
}
|
|
||||||
var rows []row
|
|
||||||
if err := l.db.Table("local_components").
|
|
||||||
Select("lot_name, lot_description").
|
|
||||||
Where("lot_name IN ?", lotNames).
|
|
||||||
Scan(&rows).Error; err != nil {
|
|
||||||
return nil, fmt.Errorf("fetch component descriptions: %w", err)
|
|
||||||
}
|
|
||||||
m := make(map[string]string, len(rows))
|
|
||||||
for _, r := range rows {
|
|
||||||
m[r.LotName] = r.LotDescription
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSchemaMigrations returns all applied local schema migrations ordered by applied_at.
|
// GetSchemaMigrations returns all applied local schema migrations ordered by applied_at.
|
||||||
func (l *LocalDB) GetSchemaMigrations() ([]LocalSchemaMigration, error) {
|
func (l *LocalDB) GetSchemaMigrations() ([]LocalSchemaMigration, error) {
|
||||||
|
|||||||
@@ -1120,3 +1120,4 @@ func deduplicatePricelistItemsAndAddUniqueIndex(tx *gorm.DB) error {
|
|||||||
slog.Info("deduplicated local_pricelist_items and added unique index")
|
slog.Info("deduplicated local_pricelist_items and added unique index")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.mchus.pro/mchus/quoteforge/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AppSetting stores application settings in local SQLite
|
// AppSetting stores application settings in local SQLite
|
||||||
@@ -46,7 +48,13 @@ func (c *LocalConfigItems) Scan(value interface{}) error {
|
|||||||
default:
|
default:
|
||||||
return errors.New("type assertion failed for LocalConfigItems")
|
return errors.New("type assertion failed for LocalConfigItems")
|
||||||
}
|
}
|
||||||
return json.Unmarshal(bytes, c)
|
if err := json.Unmarshal(bytes, c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := range *c {
|
||||||
|
(*c)[i].LotName = models.NormalizeLotName((*c)[i].LotName)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c LocalConfigItems) Total() float64 {
|
func (c LocalConfigItems) Total() float64 {
|
||||||
@@ -170,6 +178,7 @@ type LocalPricelist struct {
|
|||||||
CreatedAt time.Time `gorm:"index:idx_local_pricelists_source_created_at,priority:2,sort:desc" json:"created_at"`
|
CreatedAt time.Time `gorm:"index:idx_local_pricelists_source_created_at,priority:2,sort:desc" json:"created_at"`
|
||||||
SyncedAt time.Time `json:"synced_at"`
|
SyncedAt time.Time `json:"synced_at"`
|
||||||
IsUsed bool `gorm:"default:false" json:"is_used"` // Used by any local configuration
|
IsUsed bool `gorm:"default:false" json:"is_used"` // Used by any local configuration
|
||||||
|
IsActive bool `gorm:"not null;default:true;index" json:"is_active"` // Mirrors qt_pricelists.is_active
|
||||||
}
|
}
|
||||||
|
|
||||||
func (LocalPricelist) TableName() string {
|
func (LocalPricelist) TableName() string {
|
||||||
|
|||||||
@@ -42,25 +42,29 @@ type ConfiguratorSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SyncQtSettings reads all rows from qt_settings on MariaDB and replaces the
|
// SyncQtSettings reads all rows from qt_settings on MariaDB and replaces the
|
||||||
// local_qt_settings cache in a single SQLite transaction. Returns an error if
|
// local_qt_settings cache in a single SQLite transaction.
|
||||||
// the qt_settings table doesn't exist on the server (old server without the
|
// If the read fails (no connection, table missing on old server) or the server
|
||||||
// table) or on any query/write failure.
|
// returns an empty table, the existing local_qt_settings are preserved so the
|
||||||
|
// configurator keeps working offline or against old server versions.
|
||||||
func (l *LocalDB) SyncQtSettings(mariaDB *gorm.DB) error {
|
func (l *LocalDB) SyncQtSettings(mariaDB *gorm.DB) error {
|
||||||
var rows []LocalQtSetting
|
var rows []LocalQtSetting
|
||||||
if err := mariaDB.
|
if err := mariaDB.
|
||||||
Table("qt_settings").
|
Table("qt_settings").
|
||||||
Select("name, value").
|
Select("name, value").
|
||||||
Find(&rows).Error; err != nil {
|
Find(&rows).Error; err != nil {
|
||||||
|
slog.Warn("qt_settings: read from MariaDB failed, keeping existing local cache", "error", err)
|
||||||
return fmt.Errorf("reading qt_settings from MariaDB: %w", err)
|
return fmt.Errorf("reading qt_settings from MariaDB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(rows) == 0 {
|
||||||
|
slog.Warn("qt_settings: server returned empty table, keeping existing local cache")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return l.db.Transaction(func(tx *gorm.DB) error {
|
return l.db.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := tx.Exec("DELETE FROM local_qt_settings").Error; err != nil {
|
if err := tx.Exec("DELETE FROM local_qt_settings").Error; err != nil {
|
||||||
return fmt.Errorf("clearing local_qt_settings: %w", err)
|
return fmt.Errorf("clearing local_qt_settings: %w", err)
|
||||||
}
|
}
|
||||||
if len(rows) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := tx.Create(&rows).Error; err != nil {
|
if err := tx.Create(&rows).Error; err != nil {
|
||||||
return fmt.Errorf("inserting local_qt_settings: %w", err)
|
return fmt.Errorf("inserting local_qt_settings: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// NormalizeLotName returns the canonical form of a lot name: trimmed and uppercased.
|
||||||
|
// Apply at every point where a lot name enters the system (sync, API input, config load).
|
||||||
|
func NormalizeLotName(s string) string {
|
||||||
|
return strings.ToUpper(strings.TrimSpace(s))
|
||||||
|
}
|
||||||
|
|
||||||
// Lot represents existing lot table
|
// Lot represents existing lot table
|
||||||
type Lot struct {
|
type Lot struct {
|
||||||
LotName string `gorm:"column:lot_name;primaryKey;size:255" json:"lot_name"`
|
LotName string `gorm:"column:lot_name;primaryKey;size:255" json:"lot_name"`
|
||||||
|
|||||||
@@ -269,12 +269,21 @@ func (r *PricelistRepository) GetPriceForLot(pricelistID uint, lotName string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetPricesForLots returns price map for given lots within a pricelist.
|
// GetPricesForLots returns price map for given lots within a pricelist.
|
||||||
|
// Keys in the returned map match the requested lot names (case-preserving) so that
|
||||||
|
// callers using Go map lookups are not confused by case differences between the
|
||||||
|
// requested name and the stored value (e.g. pricelist renamed lots to UPPERCASE).
|
||||||
func (r *PricelistRepository) GetPricesForLots(pricelistID uint, lotNames []string) (map[string]float64, error) {
|
func (r *PricelistRepository) GetPricesForLots(pricelistID uint, lotNames []string) (map[string]float64, error) {
|
||||||
result := make(map[string]float64, len(lotNames))
|
result := make(map[string]float64, len(lotNames))
|
||||||
if pricelistID == 0 || len(lotNames) == 0 {
|
if pricelistID == 0 || len(lotNames) == 0 {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build case-insensitive index: lowercase → original requested name.
|
||||||
|
lotIndex := make(map[string]string, len(lotNames))
|
||||||
|
for _, n := range lotNames {
|
||||||
|
lotIndex[strings.ToLower(n)] = n
|
||||||
|
}
|
||||||
|
|
||||||
var rows []models.PricelistItem
|
var rows []models.PricelistItem
|
||||||
if err := r.db.Select("lot_name, price").
|
if err := r.db.Select("lot_name, price").
|
||||||
Where("pricelist_id = ? AND lot_name IN ?", pricelistID, lotNames).
|
Where("pricelist_id = ? AND lot_name IN ?", pricelistID, lotNames).
|
||||||
@@ -284,7 +293,11 @@ func (r *PricelistRepository) GetPricesForLots(pricelistID uint, lotNames []stri
|
|||||||
|
|
||||||
for _, row := range rows {
|
for _, row := range rows {
|
||||||
if row.Price > 0 {
|
if row.Price > 0 {
|
||||||
result[row.LotName] = row.Price
|
key := row.LotName
|
||||||
|
if requested, ok := lotIndex[strings.ToLower(row.LotName)]; ok {
|
||||||
|
key = requested
|
||||||
|
}
|
||||||
|
result[key] = row.Price
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
|
|||||||
@@ -656,17 +656,9 @@ func (s *ExportService) batchLookupPrices(serverPricelistID *uint, lots []string
|
|||||||
return prices
|
return prices
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ExportService) resolveLotDescriptions(cfg *models.Configuration, localCfg *localdb.LocalConfiguration) map[string]string {
|
func (s *ExportService) resolveLotDescriptions(_ *models.Configuration, _ *localdb.LocalConfiguration) map[string]string {
|
||||||
lots := collectPricingLots(cfg, localCfg, true)
|
|
||||||
if s.localDB == nil || len(lots) == 0 {
|
|
||||||
return map[string]string{}
|
return map[string]string{}
|
||||||
}
|
}
|
||||||
descriptions, err := s.localDB.GetLocalComponentDescriptionsByLotNames(lots)
|
|
||||||
if err != nil {
|
|
||||||
return map[string]string{}
|
|
||||||
}
|
|
||||||
return descriptions
|
|
||||||
}
|
|
||||||
|
|
||||||
func collectPricingLots(cfg *models.Configuration, localCfg *localdb.LocalConfiguration, includeBOM bool) []string {
|
func collectPricingLots(cfg *models.Configuration, localCfg *localdb.LocalConfiguration, includeBOM bool) []string {
|
||||||
seen := map[string]struct{}{}
|
seen := map[string]struct{}{}
|
||||||
|
|||||||
@@ -423,6 +423,13 @@ func (s *LocalConfigurationService) RefreshPrices(uuid string, ownerUsername str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture fingerprint of the current state before any mutations.
|
||||||
|
preRefreshFP, err := localdb.BuildConfigurationSpecPriceFingerprint(localCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("build pre-refresh fingerprint: %w", err)
|
||||||
|
}
|
||||||
|
preRefreshCfg := *localCfg
|
||||||
|
|
||||||
// Update prices for all items from pricelist
|
// Update prices for all items from pricelist
|
||||||
updatedItems := make(localdb.LocalConfigItems, len(localCfg.Items))
|
updatedItems := make(localdb.LocalConfigItems, len(localCfg.Items))
|
||||||
for i, item := range localCfg.Items {
|
for i, item := range localCfg.Items {
|
||||||
@@ -462,6 +469,18 @@ func (s *LocalConfigurationService) RefreshPrices(uuid string, ownerUsername str
|
|||||||
localCfg.UpdatedAt = now
|
localCfg.UpdatedAt = now
|
||||||
localCfg.SyncStatus = "pending"
|
localCfg.SyncStatus = "pending"
|
||||||
|
|
||||||
|
// Before saving the new prices, snapshot the pre-refresh state so the revision
|
||||||
|
// history shows a clear before/after for every price update.
|
||||||
|
postRefreshFP, err := localdb.BuildConfigurationSpecPriceFingerprint(localCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("build post-refresh fingerprint: %w", err)
|
||||||
|
}
|
||||||
|
if preRefreshFP != postRefreshFP {
|
||||||
|
if err := s.snapshotPreRefreshTx(&preRefreshCfg, ownerUsername); err != nil {
|
||||||
|
return nil, fmt.Errorf("snapshot pre-refresh state: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cfg, err := s.saveWithVersionAndPending(localCfg, "update", ownerUsername)
|
cfg, err := s.saveWithVersionAndPending(localCfg, "update", ownerUsername)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("refresh prices with version: %w", err)
|
return nil, fmt.Errorf("refresh prices with version: %w", err)
|
||||||
@@ -820,6 +839,13 @@ func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string, pricelistSe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture fingerprint of the current state before any mutations.
|
||||||
|
preRefreshFP, err := localdb.BuildConfigurationSpecPriceFingerprint(localCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("build pre-refresh fingerprint: %w", err)
|
||||||
|
}
|
||||||
|
preRefreshCfg := *localCfg
|
||||||
|
|
||||||
// Update prices for all items from pricelist
|
// Update prices for all items from pricelist
|
||||||
updatedItems := make(localdb.LocalConfigItems, len(localCfg.Items))
|
updatedItems := make(localdb.LocalConfigItems, len(localCfg.Items))
|
||||||
for i, item := range localCfg.Items {
|
for i, item := range localCfg.Items {
|
||||||
@@ -859,6 +885,18 @@ func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string, pricelistSe
|
|||||||
localCfg.UpdatedAt = now
|
localCfg.UpdatedAt = now
|
||||||
localCfg.SyncStatus = "pending"
|
localCfg.SyncStatus = "pending"
|
||||||
|
|
||||||
|
// Before saving the new prices, snapshot the pre-refresh state so the revision
|
||||||
|
// history shows a clear before/after for every price update.
|
||||||
|
postRefreshFP, err := localdb.BuildConfigurationSpecPriceFingerprint(localCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("build post-refresh fingerprint: %w", err)
|
||||||
|
}
|
||||||
|
if preRefreshFP != postRefreshFP {
|
||||||
|
if err := s.snapshotPreRefreshTx(&preRefreshCfg, ""); err != nil {
|
||||||
|
return nil, fmt.Errorf("snapshot pre-refresh state: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cfg, err := s.saveWithVersionAndPending(localCfg, "update", "")
|
cfg, err := s.saveWithVersionAndPending(localCfg, "update", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("refresh prices without auth with version: %w", err)
|
return nil, fmt.Errorf("refresh prices without auth with version: %w", err)
|
||||||
@@ -866,6 +904,16 @@ func (s *LocalConfigurationService) RefreshPricesNoAuth(uuid string, pricelistSe
|
|||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SnapshotCurrentState creates a revision of the current configuration state without modifying it.
|
||||||
|
// Called before a client-side price refresh so the revision history has a clear before/after.
|
||||||
|
func (s *LocalConfigurationService) SnapshotCurrentState(uuid string) error {
|
||||||
|
localCfg, err := s.localDB.GetConfigurationByUUID(uuid)
|
||||||
|
if err != nil {
|
||||||
|
return ErrConfigNotFound
|
||||||
|
}
|
||||||
|
return s.snapshotPreRefreshTx(localCfg, "")
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateServerCount updates server count and recalculates total price without creating a new version.
|
// UpdateServerCount updates server count and recalculates total price without creating a new version.
|
||||||
func (s *LocalConfigurationService) UpdateServerCount(configUUID string, serverCount int) (*models.Configuration, error) {
|
func (s *LocalConfigurationService) UpdateServerCount(configUUID string, serverCount int) (*models.Configuration, error) {
|
||||||
if serverCount < 1 {
|
if serverCount < 1 {
|
||||||
@@ -1432,12 +1480,25 @@ func (s *LocalConfigurationService) appendVersionTx(
|
|||||||
localCfg *localdb.LocalConfiguration,
|
localCfg *localdb.LocalConfiguration,
|
||||||
operation string,
|
operation string,
|
||||||
createdBy string,
|
createdBy string,
|
||||||
|
) (*localdb.LocalConfigurationVersion, error) {
|
||||||
|
return s.appendVersionTxNote(tx, localCfg, operation, createdBy, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LocalConfigurationService) appendVersionTxNote(
|
||||||
|
tx *gorm.DB,
|
||||||
|
localCfg *localdb.LocalConfiguration,
|
||||||
|
operation string,
|
||||||
|
createdBy string,
|
||||||
|
noteOverride string,
|
||||||
) (*localdb.LocalConfigurationVersion, error) {
|
) (*localdb.LocalConfigurationVersion, error) {
|
||||||
snapshot, err := s.buildConfigurationSnapshot(localCfg)
|
snapshot, err := s.buildConfigurationSnapshot(localCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("build snapshot: %w", err)
|
return nil, fmt.Errorf("build snapshot: %w", err)
|
||||||
}
|
}
|
||||||
changeNote := fmt.Sprintf("%s via local-first flow", operation)
|
changeNote := fmt.Sprintf("%s via local-first flow", operation)
|
||||||
|
if noteOverride != "" {
|
||||||
|
changeNote = noteOverride
|
||||||
|
}
|
||||||
|
|
||||||
var createdByPtr *string
|
var createdByPtr *string
|
||||||
if createdBy != "" {
|
if createdBy != "" {
|
||||||
@@ -1478,6 +1539,35 @@ func (s *LocalConfigurationService) appendVersionTx(
|
|||||||
return nil, fmt.Errorf("%w: exceeded retries for %s", ErrVersionConflict, localCfg.UUID)
|
return nil, fmt.Errorf("%w: exceeded retries for %s", ErrVersionConflict, localCfg.UUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// snapshotPreRefreshTx creates a revision of the current configuration state before a price
|
||||||
|
// refresh so the history clearly shows what existed before prices were updated.
|
||||||
|
// Called only when prices are about to change (fingerprints differ).
|
||||||
|
func (s *LocalConfigurationService) snapshotPreRefreshTx(localCfg *localdb.LocalConfiguration, createdBy string) error {
|
||||||
|
return s.localDB.DB().Transaction(func(tx *gorm.DB) error {
|
||||||
|
var locked localdb.LocalConfiguration
|
||||||
|
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||||
|
Where("uuid = ?", localCfg.UUID).
|
||||||
|
First(&locked).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return ErrConfigNotFound
|
||||||
|
}
|
||||||
|
return fmt.Errorf("lock row for pre-refresh snapshot: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := s.appendVersionTxNote(tx, localCfg, "update", createdBy, "до обновления цен")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("append pre-refresh version: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Model(&localdb.LocalConfiguration{}).
|
||||||
|
Where("uuid = ?", localCfg.UUID).
|
||||||
|
Update("current_version_id", version.ID).Error; err != nil {
|
||||||
|
return fmt.Errorf("set current_version_id for pre-refresh snapshot: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *LocalConfigurationService) buildConfigurationSnapshot(localCfg *localdb.LocalConfiguration) (string, error) {
|
func (s *LocalConfigurationService) buildConfigurationSnapshot(localCfg *localdb.LocalConfiguration) (string, error) {
|
||||||
return localdb.BuildConfigurationSnapshot(localCfg)
|
return localdb.BuildConfigurationSnapshot(localCfg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -22,8 +23,13 @@ var (
|
|||||||
ErrCannotDeleteMainVariant = errors.New("cannot delete main variant")
|
ErrCannotDeleteMainVariant = errors.New("cannot delete main variant")
|
||||||
ErrReservedMainVariant = errors.New("variant name 'main' is reserved")
|
ErrReservedMainVariant = errors.New("variant name 'main' is reserved")
|
||||||
ErrCannotRenameMainVariant = errors.New("cannot rename main variant")
|
ErrCannotRenameMainVariant = errors.New("cannot rename main variant")
|
||||||
|
ErrProjectCodeInvalidChars = errors.New("код опти содержит недопустимые символы (разрешены: буквы, цифры, дефис, точка, подчёркивание)")
|
||||||
|
ErrProjectVariantInvalidChars = errors.New("имя варианта содержит недопустимые символы (разрешены: буквы, цифры, дефис, точка, подчёркивание)")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// projectCodeRe allows only URL-path-safe characters so project codes can appear directly in URLs.
|
||||||
|
var projectCodeRe = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
|
||||||
|
|
||||||
type ProjectService struct {
|
type ProjectService struct {
|
||||||
localDB *localdb.LocalDB
|
localDB *localdb.LocalDB
|
||||||
}
|
}
|
||||||
@@ -64,6 +70,9 @@ func (s *ProjectService) Create(ownerUsername string, req *CreateProjectRequest)
|
|||||||
if code == "" {
|
if code == "" {
|
||||||
return nil, fmt.Errorf("project code is required")
|
return nil, fmt.Errorf("project code is required")
|
||||||
}
|
}
|
||||||
|
if !projectCodeRe.MatchString(code) {
|
||||||
|
return nil, ErrProjectCodeInvalidChars
|
||||||
|
}
|
||||||
variant := strings.TrimSpace(req.Variant)
|
variant := strings.TrimSpace(req.Variant)
|
||||||
if err := validateProjectVariantName(variant); err != nil {
|
if err := validateProjectVariantName(variant); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -106,6 +115,9 @@ func (s *ProjectService) Update(projectUUID, ownerUsername string, req *UpdatePr
|
|||||||
if code == "" {
|
if code == "" {
|
||||||
return nil, fmt.Errorf("project code is required")
|
return nil, fmt.Errorf("project code is required")
|
||||||
}
|
}
|
||||||
|
if !projectCodeRe.MatchString(code) {
|
||||||
|
return nil, ErrProjectCodeInvalidChars
|
||||||
|
}
|
||||||
localProject.Code = code
|
localProject.Code = code
|
||||||
}
|
}
|
||||||
if req.Variant != nil {
|
if req.Variant != nil {
|
||||||
@@ -183,6 +195,9 @@ func validateProjectVariantName(variant string) error {
|
|||||||
if normalizeProjectVariant(variant) == "main" {
|
if normalizeProjectVariant(variant) == "main" {
|
||||||
return ErrReservedMainVariant
|
return ErrReservedMainVariant
|
||||||
}
|
}
|
||||||
|
if variant != "" && !projectCodeRe.MatchString(variant) {
|
||||||
|
return ErrProjectVariantInvalidChars
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,6 +297,24 @@ func (s *ProjectService) GetByUUID(projectUUID, ownerUsername string) (*models.P
|
|||||||
return localdb.LocalToProject(localProject), nil
|
return localdb.LocalToProject(localProject), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetByCode finds the main variant of a project by its code (case-insensitive).
|
||||||
|
func (s *ProjectService) GetByCode(code string) (*models.Project, error) {
|
||||||
|
localProject, err := s.localDB.GetProjectByCode(code)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrProjectNotFound
|
||||||
|
}
|
||||||
|
return localdb.LocalToProject(localProject), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByCodeAndVariant finds a project by code + variant (both case-insensitive).
|
||||||
|
func (s *ProjectService) GetByCodeAndVariant(code, variant string) (*models.Project, error) {
|
||||||
|
localProject, err := s.localDB.GetProjectByCodeAndVariant(code, variant)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrProjectNotFound
|
||||||
|
}
|
||||||
|
return localdb.LocalToProject(localProject), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ProjectService) ListConfigurations(projectUUID, ownerUsername, status string) (*ProjectConfigurationsResult, error) {
|
func (s *ProjectService) ListConfigurations(projectUUID, ownerUsername, status string) (*ProjectConfigurationsResult, error) {
|
||||||
project, err := s.GetByUUID(projectUUID, ownerUsername)
|
project, err := s.GetByUUID(projectUUID, ownerUsername)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -111,6 +111,9 @@ func (s *QuoteService) ValidateAndCalculate(req *QuoteRequest) (*QuoteValidation
|
|||||||
if len(req.Items) == 0 {
|
if len(req.Items) == 0 {
|
||||||
return nil, ErrEmptyQuote
|
return nil, ErrEmptyQuote
|
||||||
}
|
}
|
||||||
|
for i := range req.Items {
|
||||||
|
req.Items[i].LotName = models.NormalizeLotName(req.Items[i].LotName)
|
||||||
|
}
|
||||||
|
|
||||||
// Strict local-first path: calculations use local SQLite snapshot regardless of online status.
|
// Strict local-first path: calculations use local SQLite snapshot regardless of online status.
|
||||||
if s.localDB != nil {
|
if s.localDB != nil {
|
||||||
@@ -245,6 +248,16 @@ func (s *QuoteService) CalculatePriceLevels(req *PriceLevelsRequest) (*PriceLeve
|
|||||||
if len(req.Items) == 0 {
|
if len(req.Items) == 0 {
|
||||||
return nil, ErrEmptyQuote
|
return nil, ErrEmptyQuote
|
||||||
}
|
}
|
||||||
|
// Keep original lot names so the response mirrors what the caller sent.
|
||||||
|
// Normalization is applied only for internal DB lookups.
|
||||||
|
originalLotNames := make(map[string]string, len(req.Items))
|
||||||
|
for i := range req.Items {
|
||||||
|
upper := models.NormalizeLotName(req.Items[i].LotName)
|
||||||
|
if _, exists := originalLotNames[upper]; !exists {
|
||||||
|
originalLotNames[upper] = req.Items[i].LotName
|
||||||
|
}
|
||||||
|
req.Items[i].LotName = upper
|
||||||
|
}
|
||||||
|
|
||||||
lotNames := make([]string, 0, len(req.Items))
|
lotNames := make([]string, 0, len(req.Items))
|
||||||
seenLots := make(map[string]struct{}, len(req.Items))
|
seenLots := make(map[string]struct{}, len(req.Items))
|
||||||
@@ -303,8 +316,12 @@ func (s *QuoteService) CalculatePriceLevels(req *PriceLevelsRequest) (*PriceLeve
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, reqItem := range req.Items {
|
for _, reqItem := range req.Items {
|
||||||
|
responseLotName := originalLotNames[reqItem.LotName]
|
||||||
|
if responseLotName == "" {
|
||||||
|
responseLotName = reqItem.LotName
|
||||||
|
}
|
||||||
item := PriceLevelsItem{
|
item := PriceLevelsItem{
|
||||||
LotName: reqItem.LotName,
|
LotName: responseLotName,
|
||||||
Quantity: reqItem.Quantity,
|
Quantity: reqItem.Quantity,
|
||||||
PriceMissing: make([]string, 0, 3),
|
PriceMissing: make([]string, 0, 3),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -322,6 +322,12 @@ func (s *Service) NeedSync() (bool, error) {
|
|||||||
|
|
||||||
// SyncPricelists synchronizes all active pricelists from server to local SQLite
|
// SyncPricelists synchronizes all active pricelists from server to local SQLite
|
||||||
func (s *Service) SyncPricelists() (int, error) {
|
func (s *Service) SyncPricelists() (int, error) {
|
||||||
|
s.pricelistMu.Lock()
|
||||||
|
defer s.pricelistMu.Unlock()
|
||||||
|
return s.syncPricelists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) syncPricelists() (int, error) {
|
||||||
slog.Info("starting pricelist sync")
|
slog.Info("starting pricelist sync")
|
||||||
plSyncStart := time.Now()
|
plSyncStart := time.Now()
|
||||||
if _, err := s.EnsureReadinessForSync(); err != nil {
|
if _, err := s.EnsureReadinessForSync(); err != nil {
|
||||||
@@ -336,6 +342,12 @@ func (s *Service) SyncPricelists() (int, error) {
|
|||||||
return 0, fmt.Errorf("database not available: %w", err)
|
return 0, fmt.Errorf("database not available: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if reportErr := s.reportClientSchemaState(mariaDB, time.Now().UTC()); reportErr != nil {
|
||||||
|
slog.Warn("failed to report client state after pricelist sync", "error", reportErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Create repository
|
// Create repository
|
||||||
pricelistRepo := repository.NewPricelistRepository(mariaDB)
|
pricelistRepo := repository.NewPricelistRepository(mariaDB)
|
||||||
|
|
||||||
@@ -392,6 +404,7 @@ func (s *Service) SyncPricelists() (int, error) {
|
|||||||
CreatedAt: pl.CreatedAt,
|
CreatedAt: pl.CreatedAt,
|
||||||
SyncedAt: time.Now(),
|
SyncedAt: time.Now(),
|
||||||
IsUsed: false,
|
IsUsed: false,
|
||||||
|
IsActive: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
itemCount, err := s.syncNewPricelistSnapshot(localPL)
|
itemCount, err := s.syncNewPricelistSnapshot(localPL)
|
||||||
@@ -414,6 +427,12 @@ func (s *Service) SyncPricelists() (int, error) {
|
|||||||
slog.Info("deleted stale local pricelists", "deleted", removed)
|
slog.Info("deleted stale local pricelists", "deleted", removed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mirror server-side deactivations: any local pricelist not in the current active set
|
||||||
|
// is marked is_active=false so offline lookups skip it.
|
||||||
|
if err := s.localDB.DeactivateLocalPricelistsNotIn(serverPricelistIDs); err != nil {
|
||||||
|
slog.Warn("failed to deactivate stale local pricelists", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Backfill lot_category for used pricelists (older local caches may miss the column values).
|
// Backfill lot_category for used pricelists (older local caches may miss the column values).
|
||||||
s.backfillUsedPricelistItemCategories(pricelistRepo, serverPricelistIDs)
|
s.backfillUsedPricelistItemCategories(pricelistRepo, serverPricelistIDs)
|
||||||
|
|
||||||
@@ -764,9 +783,16 @@ func (s *Service) fetchServerPricelistItems(serverPricelistID uint) ([]localdb.L
|
|||||||
return nil, fmt.Errorf("getting server pricelist items: %w", err)
|
return nil, fmt.Errorf("getting server pricelist items: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
localItems := make([]localdb.LocalPricelistItem, len(serverItems))
|
seen := make(map[string]struct{}, len(serverItems))
|
||||||
for i, item := range serverItems {
|
localItems := make([]localdb.LocalPricelistItem, 0, len(serverItems))
|
||||||
localItems[i] = *localdb.PricelistItemToLocal(&item, 0)
|
for i := range serverItems {
|
||||||
|
lotName := serverItems[i].LotName
|
||||||
|
if _, dup := seen[lotName]; dup {
|
||||||
|
slog.Warn("duplicate lot_name in server pricelist, skipping", "pricelist_id", serverPricelistID, "lot_name", lotName)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[lotName] = struct{}{}
|
||||||
|
localItems = append(localItems, *localdb.PricelistItemToLocal(&serverItems[i], 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
return localItems, nil
|
return localItems, nil
|
||||||
@@ -843,7 +869,7 @@ func (s *Service) SyncPricelistsIfNeeded() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("new pricelists detected, syncing...")
|
slog.Info("new pricelists detected, syncing...")
|
||||||
_, err = s.SyncPricelists()
|
_, err = s.syncPricelists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("syncing pricelists: %w", err)
|
return fmt.Errorf("syncing pricelists: %w", err)
|
||||||
}
|
}
|
||||||
@@ -888,7 +914,10 @@ func (s *Service) PushPendingChanges() (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("pushing pending changes", "count", len(changes))
|
slog.Info("pushing pending changes", "count", len(changes))
|
||||||
|
pushStart := time.Now()
|
||||||
pushed := 0
|
pushed := 0
|
||||||
|
failed := 0
|
||||||
|
var firstErr string
|
||||||
var syncedIDs []int64
|
var syncedIDs []int64
|
||||||
sortedChanges := prioritizeProjectChanges(changes)
|
sortedChanges := prioritizeProjectChanges(changes)
|
||||||
|
|
||||||
@@ -899,6 +928,10 @@ func (s *Service) PushPendingChanges() (int, error) {
|
|||||||
slog.Warn("failed to push change", "id", change.ID, "type", change.EntityType, "operation", change.Operation, "error", err)
|
slog.Warn("failed to push change", "id", change.ID, "type", change.EntityType, "operation", change.Operation, "error", err)
|
||||||
newAttempts := change.Attempts + 1
|
newAttempts := change.Attempts + 1
|
||||||
s.localDB.IncrementPendingChangeAttempts(change.ID, err.Error())
|
s.localDB.IncrementPendingChangeAttempts(change.ID, err.Error())
|
||||||
|
if firstErr == "" {
|
||||||
|
firstErr = err.Error()
|
||||||
|
}
|
||||||
|
failed++
|
||||||
if newAttempts >= maxPendingChangeAttempts {
|
if newAttempts >= maxPendingChangeAttempts {
|
||||||
slog.Error("abandoning pending change after max attempts",
|
slog.Error("abandoning pending change after max attempts",
|
||||||
"id", change.ID, "type", change.EntityType, "op", change.Operation,
|
"id", change.ID, "type", change.EntityType, "op", change.Operation,
|
||||||
@@ -919,7 +952,13 @@ func (s *Service) PushPendingChanges() (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("pending changes pushed", "pushed", pushed, "failed", len(changes)-pushed)
|
if failed > 0 {
|
||||||
|
s.localDB.AppendSyncLog("changes", "error", firstErr, pushed, pushStart, time.Since(pushStart).Milliseconds())
|
||||||
|
} else {
|
||||||
|
s.localDB.AppendSyncLog("changes", "ok", "", pushed, pushStart, time.Since(pushStart).Milliseconds())
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("pending changes pushed", "pushed", pushed, "failed", failed)
|
||||||
return pushed, nil
|
return pushed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1606,24 +1645,3 @@ func (s *Service) getConnectionStatus() db.ConnectionStatus {
|
|||||||
return s.connMgr.GetStatus()
|
return s.connMgr.GetStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncComponentsIfEmpty syncs components from MariaDB when local_components is empty.
|
|
||||||
// Used by the background worker on first run to populate the catalog for new users.
|
|
||||||
func (s *Service) SyncComponentsIfEmpty() error {
|
|
||||||
if s.localDB.CountComponents() > 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
mariaDB, err := s.getDB()
|
|
||||||
if err != nil {
|
|
||||||
_ = s.localDB.SetComponentSyncResult("error", err.Error(), time.Now())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
result, err := s.localDB.SyncComponents(mariaDB)
|
|
||||||
now := time.Now()
|
|
||||||
if err != nil {
|
|
||||||
_ = s.localDB.SetComponentSyncResult("error", err.Error(), now)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_ = s.localDB.SetComponentSyncResult("ok", "", now)
|
|
||||||
slog.Info("background sync: initial component sync completed", "synced", result.TotalSynced)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -80,11 +80,6 @@ func (w *Worker) runSync() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate component catalog on first run (empty local_components)
|
|
||||||
if err := w.service.SyncComponentsIfEmpty(); err != nil {
|
|
||||||
w.logger.Warn("background sync: initial component sync failed", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push pending changes first
|
// Push pending changes first
|
||||||
pushed, err := w.service.PushPendingChanges()
|
pushed, err := w.service.PushPendingChanges()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
35
releases/v2.19/RELEASE_NOTES.md
Normal file
35
releases/v2.19/RELEASE_NOTES.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# QuoteForge v2.19
|
||||||
|
|
||||||
|
Дата релиза: 2026-06-23
|
||||||
|
Тег: `v2.19`
|
||||||
|
|
||||||
|
## Что нового
|
||||||
|
|
||||||
|
### Серверно-управляемые настройки конфигуратора
|
||||||
|
|
||||||
|
Типы устройств, структура вкладок и фильтры категорий теперь приезжают с сервера вместо жёстко заданных JS-констант.
|
||||||
|
|
||||||
|
- новая таблица `qt_settings` на стороне сервера (контракт в `bible-local/server-contract-qt-settings.md`);
|
||||||
|
- QF синхронизирует `qt_settings` → `local_qt_settings` (SQLite) после каждой синхронизации компонентов;
|
||||||
|
- новый endpoint `GET /api/configurator-settings` отдаёт четыре настройки: `config_types`, `tab_config`, `always_visible_tabs`, `required_categories`;
|
||||||
|
- при недоступности сервера или отсутствии таблицы QF автоматически использует прежние захардкоженные значения — поведение не меняется.
|
||||||
|
|
||||||
|
### Динамический выбор типа оборудования
|
||||||
|
|
||||||
|
- модальное окно «Новая конфигурация» загружает типы устройств с сервера: названия и количество кнопок определяются в `qt_settings.config_types`;
|
||||||
|
- добавление новых типов устройств не требует обновления QF.
|
||||||
|
|
||||||
|
### Серверно-управляемая фильтрация категорий
|
||||||
|
|
||||||
|
- конфигуратор фильтрует LOT-категории по списку из `qt_settings.config_types[].categories`;
|
||||||
|
- структура вкладок обновляется из `qt_settings.tab_config` (порядок вкладок, подразделы, single-select режим);
|
||||||
|
- бейдж на вкладке при незаполненных обязательных категориях (`qt_settings.required_categories`).
|
||||||
|
|
||||||
|
### Прочее
|
||||||
|
|
||||||
|
- тайтлы страниц переименованы с OFS на QFS.
|
||||||
|
|
||||||
|
## Запуск на macOS
|
||||||
|
|
||||||
|
Снимите карантинный атрибут через терминал: `xattr -d com.apple.quarantine /path/to/qfs-darwin-arm64`
|
||||||
|
После этого бинарник запустится без предупреждения Gatekeeper.
|
||||||
29
releases/v2.21/RELEASE_NOTES.md
Normal file
29
releases/v2.21/RELEASE_NOTES.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# QuoteForge v2.21
|
||||||
|
|
||||||
|
Дата релиза: 2026-06-25
|
||||||
|
Тег: `v2.21`
|
||||||
|
|
||||||
|
## Что нового
|
||||||
|
|
||||||
|
### Короткие ссылки на проекты и варианты
|
||||||
|
|
||||||
|
- `GET /:code` — редирект на проект по коду опти (регистронезависимо);
|
||||||
|
- `GET /:code/:variant` — редирект на конкретный вариант проекта;
|
||||||
|
- валидация кода опти и имени варианта: только URL-безопасные символы `[A-Za-z0-9._-]` — проверка на бэкенде и в форме с подсказкой `«Используется в URL: /КОД/Вариант»`.
|
||||||
|
|
||||||
|
### Ревизия «до обновления цен»
|
||||||
|
|
||||||
|
При нажатии «Обновить цены» автоматически создаётся ревизия текущего состояния конфигурации до применения новых цен, после чего сохраняется ревизия с обновлёнными ценами. История изменений теперь полная.
|
||||||
|
|
||||||
|
### Исправления
|
||||||
|
|
||||||
|
- Старая цена в итоге конфигурации больше не зачёркивается, если цены фактически не изменились.
|
||||||
|
- Устранён race condition: `SyncPricelists()` теперь защищена мьютексом — параллельный запуск фонового тикера и ручной синхронизации больше не приводит к `UNIQUE constraint failed`.
|
||||||
|
- Дублирующиеся `lot_name` в серверном прайслисте пропускаются при загрузке вместо аварийного завершения синхронизации.
|
||||||
|
- Ошибки отправки конфигураций и проектов на сервер теперь видны в диалоге «Информация о синхронизации» и в support bundle (`sync_log`, тип `changes`).
|
||||||
|
- Состояние клиента (`last_sync_error_code` и др.) отправляется на сервер по завершении синхронизации независимо от её результата.
|
||||||
|
|
||||||
|
## Запуск на macOS
|
||||||
|
|
||||||
|
Снимите карантинный атрибут через терминал: `xattr -d com.apple.quarantine /path/to/qfs-darwin-arm64`
|
||||||
|
После этого бинарник запустится без предупреждения Gatekeeper.
|
||||||
@@ -629,11 +629,13 @@
|
|||||||
|
|
||||||
const totalColor = totalDelta > 0 ? 'text-red-600' : totalDelta < 0 ? 'text-green-600' : 'text-gray-600';
|
const totalColor = totalDelta > 0 ? 'text-red-600' : totalDelta < 0 ? 'text-green-600' : 'text-gray-600';
|
||||||
const totalArrow = _fmtArrow(r.prevTotal || 0, r.newTotal || 0);
|
const totalArrow = _fmtArrow(r.prevTotal || 0, r.newTotal || 0);
|
||||||
|
const totalPrevHtml = totalDelta !== 0
|
||||||
|
? `<span class="text-gray-400 line-through text-xs mr-1">${_fmtMoneyDiff(r.prevTotal || 0)}</span>`
|
||||||
|
: '';
|
||||||
html += `<div class="flex justify-between items-center text-sm bg-gray-50 rounded px-3 py-2 mb-1">
|
html += `<div class="flex justify-between items-center text-sm bg-gray-50 rounded px-3 py-2 mb-1">
|
||||||
<span class="text-gray-600 font-medium">Итог конфигурации</span>
|
<span class="text-gray-600 font-medium">Итог конфигурации</span>
|
||||||
<span>
|
<span>
|
||||||
<span class="text-gray-400 line-through text-xs mr-1">${_fmtMoneyDiff(r.prevTotal || 0)}</span>
|
${totalPrevHtml}<span class="${totalColor} font-semibold">${_fmtMoneyDiff(r.newTotal || 0)}</span>${totalArrow}
|
||||||
<span class="${totalColor} font-semibold">${_fmtMoneyDiff(r.newTotal || 0)}</span>${totalArrow}
|
|
||||||
</span>
|
</span>
|
||||||
</div>`;
|
</div>`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -713,7 +713,7 @@ async function loadWarehouseInStockLots() {
|
|||||||
const lotNames = Array.isArray(data.lot_names) ? data.lot_names : [];
|
const lotNames = Array.isArray(data.lot_names) ? data.lot_names : [];
|
||||||
lotNames.forEach(lot => {
|
lotNames.forEach(lot => {
|
||||||
if (typeof lot === 'string' && lot.trim() !== '') {
|
if (typeof lot === 'string' && lot.trim() !== '') {
|
||||||
result.add(lot);
|
result.add(lot.toUpperCase());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -748,7 +748,7 @@ function isComponentAllowedByStockFilter(comp) {
|
|||||||
const availableLots = warehouseStockLotsByPricelist.get(pricelistID);
|
const availableLots = warehouseStockLotsByPricelist.get(pricelistID);
|
||||||
// Don't block UI while stock set is being loaded.
|
// Don't block UI while stock set is being loaded.
|
||||||
if (!availableLots) return true;
|
if (!availableLots) return true;
|
||||||
return availableLots.has(comp.lot_name);
|
return availableLots.has((comp.lot_name || '').toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load categories from API and update tab configuration
|
// Load categories from API and update tab configuration
|
||||||
@@ -853,7 +853,7 @@ function updateRequiredCategoryBadges() {
|
|||||||
|
|
||||||
// Build set of categories that have at least one cart item
|
// Build set of categories that have at least one cart item
|
||||||
const filledCategories = new Set(
|
const filledCategories = new Set(
|
||||||
cart.map(item => (item.category || getCategoryFromLotName(item.lot_name) || '').toUpperCase())
|
cart.map(item => (item.category || '').toUpperCase())
|
||||||
);
|
);
|
||||||
|
|
||||||
// For each tab, check if it contains any required-but-unfilled category
|
// For each tab, check if it contains any required-but-unfilled category
|
||||||
@@ -925,8 +925,7 @@ document.addEventListener('DOMContentLoaded', async function() {
|
|||||||
warehouse_price: null,
|
warehouse_price: null,
|
||||||
competitor_price: null,
|
competitor_price: null,
|
||||||
description: item.description || '',
|
description: item.description || '',
|
||||||
category: item.category || getCategoryFromLotName(item.lot_name)
|
category: item.category }));
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
serverModelForQuote = config.server_model || '';
|
serverModelForQuote = config.server_model || '';
|
||||||
supportCode = config.support_code || '';
|
supportCode = config.support_code || '';
|
||||||
@@ -1003,7 +1002,7 @@ const BOM_LOT_DATALIST_DIVIDER = '────────';
|
|||||||
function _bomLotValid(v) {
|
function _bomLotValid(v) {
|
||||||
const lot = (v || '').trim();
|
const lot = (v || '').trim();
|
||||||
if (!lot || lot === BOM_LOT_DATALIST_DIVIDER) return false;
|
if (!lot || lot === BOM_LOT_DATALIST_DIVIDER) return false;
|
||||||
return (window._bomAllComponents || allComponents).some(c => c.lot_name === lot);
|
return (window._bomAllComponents || allComponents).some(c => c.lot_name.toUpperCase() === lot.toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateServerCount() {
|
function updateServerCount() {
|
||||||
@@ -1219,13 +1218,8 @@ function applyPriceSettings() {
|
|||||||
schedulePriceLevelsRefresh({ delay: 0, rerender: true, autosave: true });
|
schedulePriceLevelsRefresh({ delay: 0, rerender: true, autosave: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCategoryFromLotName(lotName) {
|
|
||||||
const parts = lotName.split('_');
|
|
||||||
return parts[0] || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
function getComponentCategory(comp) {
|
function getComponentCategory(comp) {
|
||||||
return (comp.category || getCategoryFromLotName(comp.lot_name)).toUpperCase();
|
return (comp.category || '').toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTabForCategory(category) {
|
function getTabForCategory(category) {
|
||||||
@@ -1323,7 +1317,7 @@ function updateTabVisibility() {
|
|||||||
if (!btn) continue;
|
if (!btn) continue;
|
||||||
const hasComponents = getComponentsForTab(tabId).length > 0;
|
const hasComponents = getComponentsForTab(tabId).length > 0;
|
||||||
const hasCartItems = cart.some(item => {
|
const hasCartItems = cart.some(item => {
|
||||||
const cat = (item.category || getCategoryFromLotName(item.lot_name) || '').toUpperCase();
|
const cat = (item.category || '').toUpperCase();
|
||||||
return getTabForCategory(cat) === tabId;
|
return getTabForCategory(cat) === tabId;
|
||||||
});
|
});
|
||||||
const visible = hasComponents || hasCartItems;
|
const visible = hasComponents || hasCartItems;
|
||||||
@@ -1410,10 +1404,10 @@ function renderSingleSelectTab(categories) {
|
|||||||
categories.forEach(cat => {
|
categories.forEach(cat => {
|
||||||
const catLabel = cat === 'MB' ? 'MB' : cat === 'CPU' ? 'CPU' : cat === 'MEM' ? 'MEM' : cat;
|
const catLabel = cat === 'MB' ? 'MB' : cat === 'CPU' ? 'CPU' : cat === 'MEM' ? 'MEM' : cat;
|
||||||
const selectedItem = cart.find(item =>
|
const selectedItem = cart.find(item =>
|
||||||
(item.category || getCategoryFromLotName(item.lot_name)).toUpperCase() === cat.toUpperCase()
|
(item.category).toUpperCase() === cat.toUpperCase()
|
||||||
);
|
);
|
||||||
|
|
||||||
const comp = selectedItem ? allComponents.find(c => c.lot_name === selectedItem.lot_name) : null;
|
const comp = selectedItem ? allComponents.find(c => c.lot_name.toUpperCase() === (selectedItem.lot_name || '').toUpperCase()) : null;
|
||||||
const price = comp?.current_price || 0;
|
const price = comp?.current_price || 0;
|
||||||
const estimate = selectedItem?.estimate_price ?? price;
|
const estimate = selectedItem?.estimate_price ?? price;
|
||||||
const qty = selectedItem?.quantity || 1;
|
const qty = selectedItem?.quantity || 1;
|
||||||
@@ -1463,7 +1457,7 @@ function renderSingleSelectTab(categories) {
|
|||||||
function renderMultiSelectTab(components) {
|
function renderMultiSelectTab(components) {
|
||||||
// Get cart items for this tab
|
// Get cart items for this tab
|
||||||
const tabItems = cart.filter(item => {
|
const tabItems = cart.filter(item => {
|
||||||
const cat = (item.category || getCategoryFromLotName(item.lot_name)).toUpperCase();
|
const cat = (item.category).toUpperCase();
|
||||||
const tab = getTabForCategory(cat);
|
const tab = getTabForCategory(cat);
|
||||||
return tab === currentTab;
|
return tab === currentTab;
|
||||||
});
|
});
|
||||||
@@ -1485,7 +1479,7 @@ function renderMultiSelectTab(components) {
|
|||||||
|
|
||||||
// Render existing cart items for this tab
|
// Render existing cart items for this tab
|
||||||
tabItems.forEach((item, idx) => {
|
tabItems.forEach((item, idx) => {
|
||||||
const comp = allComponents.find(c => c.lot_name === item.lot_name);
|
const comp = allComponents.find(c => c.lot_name.toUpperCase() === (item.lot_name || '').toUpperCase());
|
||||||
const total = getDisplayPrice(item) * item.quantity;
|
const total = getDisplayPrice(item) * item.quantity;
|
||||||
|
|
||||||
html += `
|
html += `
|
||||||
@@ -1552,7 +1546,7 @@ function renderMultiSelectTab(components) {
|
|||||||
function renderMultiSelectTabWithSections(sections) {
|
function renderMultiSelectTabWithSections(sections) {
|
||||||
// Get cart items for this tab
|
// Get cart items for this tab
|
||||||
const tabItems = cart.filter(item => {
|
const tabItems = cart.filter(item => {
|
||||||
const cat = (item.category || getCategoryFromLotName(item.lot_name)).toUpperCase();
|
const cat = (item.category).toUpperCase();
|
||||||
const tab = getTabForCategory(cat);
|
const tab = getTabForCategory(cat);
|
||||||
return tab === currentTab;
|
return tab === currentTab;
|
||||||
});
|
});
|
||||||
@@ -1571,7 +1565,7 @@ function renderMultiSelectTabWithSections(sections) {
|
|||||||
|
|
||||||
// Get cart items for this section
|
// Get cart items for this section
|
||||||
const sectionItems = tabItems.filter(item => {
|
const sectionItems = tabItems.filter(item => {
|
||||||
const cat = (item.category || getCategoryFromLotName(item.lot_name)).toUpperCase();
|
const cat = (item.category).toUpperCase();
|
||||||
return sectionCategories.includes(cat);
|
return sectionCategories.includes(cat);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1599,7 +1593,7 @@ function renderMultiSelectTabWithSections(sections) {
|
|||||||
|
|
||||||
// Render existing cart items for this section
|
// Render existing cart items for this section
|
||||||
sectionItems.forEach((item) => {
|
sectionItems.forEach((item) => {
|
||||||
const comp = allComponents.find(c => c.lot_name === item.lot_name);
|
const comp = allComponents.find(c => c.lot_name.toUpperCase() === (item.lot_name || '').toUpperCase());
|
||||||
const total = getDisplayPrice(item) * item.quantity;
|
const total = getDisplayPrice(item) * item.quantity;
|
||||||
|
|
||||||
html += `
|
html += `
|
||||||
@@ -1812,7 +1806,7 @@ function selectAutocompleteItem(index) {
|
|||||||
|
|
||||||
// Remove existing item of this category
|
// Remove existing item of this category
|
||||||
cart = cart.filter(item =>
|
cart = cart.filter(item =>
|
||||||
(item.category || getCategoryFromLotName(item.lot_name)).toUpperCase() !== autocompleteCategory.toUpperCase()
|
(item.category).toUpperCase() !== autocompleteCategory.toUpperCase()
|
||||||
);
|
);
|
||||||
|
|
||||||
const qtyInput = document.getElementById('qty-' + autocompleteCategory);
|
const qtyInput = document.getElementById('qty-' + autocompleteCategory);
|
||||||
@@ -1868,11 +1862,11 @@ function filterAutocompleteMulti(search) {
|
|||||||
const searchLower = search.toLowerCase();
|
const searchLower = search.toLowerCase();
|
||||||
|
|
||||||
// Filter out already added items
|
// Filter out already added items
|
||||||
const addedLots = new Set(cart.map(i => i.lot_name));
|
const addedLots = new Set(cart.map(i => (i.lot_name || '').toUpperCase()));
|
||||||
|
|
||||||
autocompleteFiltered = components.filter(c => {
|
autocompleteFiltered = components.filter(c => {
|
||||||
if (!hasComponentPrice(c.lot_name)) return false;
|
if (!hasComponentPrice(c.lot_name)) return false;
|
||||||
if (addedLots.has(c.lot_name)) return false;
|
if (addedLots.has((c.lot_name || '').toUpperCase())) return false;
|
||||||
if (!isComponentAllowedByStockFilter(c)) return false;
|
if (!isComponentAllowedByStockFilter(c)) return false;
|
||||||
const text = (c.lot_name + ' ' + (c.description || '')).toLowerCase();
|
const text = (c.lot_name + ' ' + (c.description || '')).toLowerCase();
|
||||||
return text.includes(searchLower);
|
return text.includes(searchLower);
|
||||||
@@ -1973,11 +1967,11 @@ function filterAutocompleteSection(sectionId, search, inputElement) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Filter out already added items
|
// Filter out already added items
|
||||||
const addedLots = new Set(cart.map(i => i.lot_name));
|
const addedLots = new Set(cart.map(i => (i.lot_name || '').toUpperCase()));
|
||||||
|
|
||||||
autocompleteFiltered = sectionComponents.filter(c => {
|
autocompleteFiltered = sectionComponents.filter(c => {
|
||||||
if (!hasComponentPrice(c.lot_name)) return false;
|
if (!hasComponentPrice(c.lot_name)) return false;
|
||||||
if (addedLots.has(c.lot_name)) return false;
|
if (addedLots.has((c.lot_name || '').toUpperCase())) return false;
|
||||||
if (!isComponentAllowedByStockFilter(c)) return false;
|
if (!isComponentAllowedByStockFilter(c)) return false;
|
||||||
const text = (c.lot_name + ' ' + (c.description || '')).toLowerCase();
|
const text = (c.lot_name + ' ' + (c.description || '')).toLowerCase();
|
||||||
return text.includes(searchLower);
|
return text.includes(searchLower);
|
||||||
@@ -2143,14 +2137,14 @@ function showAutocompleteBOM(rowIdx, input) {
|
|||||||
|
|
||||||
function filterAutocompleteBOM(rowIdx, search) {
|
function filterAutocompleteBOM(rowIdx, search) {
|
||||||
const searchLower = (search || '').toLowerCase();
|
const searchLower = (search || '').toLowerCase();
|
||||||
const cartLots = new Set(cart.map(i => i.lot_name));
|
const cartLots = new Set(cart.map(i => (i.lot_name || '').toUpperCase()));
|
||||||
const all = (window._bomAllComponents || allComponents).filter(c => {
|
const all = (window._bomAllComponents || allComponents).filter(c => {
|
||||||
const text = (c.lot_name + ' ' + (c.description || '')).toLowerCase();
|
const text = (c.lot_name + ' ' + (c.description || '')).toLowerCase();
|
||||||
return text.includes(searchLower);
|
return text.includes(searchLower);
|
||||||
});
|
});
|
||||||
const inCart = all.filter(c => cartLots.has(c.lot_name))
|
const inCart = all.filter(c => cartLots.has((c.lot_name || '').toUpperCase()))
|
||||||
.sort((a, b) => a.lot_name.localeCompare(b.lot_name));
|
.sort((a, b) => a.lot_name.localeCompare(b.lot_name));
|
||||||
const notInCart = all.filter(c => !cartLots.has(c.lot_name))
|
const notInCart = all.filter(c => !cartLots.has((c.lot_name || '').toUpperCase()))
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
const popDiff = (b.popularity_score || 0) - (a.popularity_score || 0);
|
const popDiff = (b.popularity_score || 0) - (a.popularity_score || 0);
|
||||||
if (popDiff !== 0) return popDiff;
|
if (popDiff !== 0) return popDiff;
|
||||||
@@ -2195,7 +2189,7 @@ function selectAutocompleteItemBOM(index, rowIdx) {
|
|||||||
|
|
||||||
function clearSingleSelect(category) {
|
function clearSingleSelect(category) {
|
||||||
cart = cart.filter(item =>
|
cart = cart.filter(item =>
|
||||||
(item.category || getCategoryFromLotName(item.lot_name)).toUpperCase() !== category.toUpperCase()
|
(item.category).toUpperCase() !== category.toUpperCase()
|
||||||
);
|
);
|
||||||
renderTab();
|
renderTab();
|
||||||
updateCartUI();
|
updateCartUI();
|
||||||
@@ -2205,7 +2199,7 @@ function clearSingleSelect(category) {
|
|||||||
function updateSingleQuantity(category, value) {
|
function updateSingleQuantity(category, value) {
|
||||||
const qty = parseInt(value) || 1;
|
const qty = parseInt(value) || 1;
|
||||||
const item = cart.find(i =>
|
const item = cart.find(i =>
|
||||||
(i.category || getCategoryFromLotName(i.lot_name)).toUpperCase() === category.toUpperCase()
|
(i.category).toUpperCase() === category.toUpperCase()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (item) {
|
if (item) {
|
||||||
@@ -2264,8 +2258,8 @@ function updateCartUI() {
|
|||||||
|
|
||||||
// Sort cart items by category display order
|
// Sort cart items by category display order
|
||||||
const sortedCart = [...cart].sort((a, b) => {
|
const sortedCart = [...cart].sort((a, b) => {
|
||||||
const catA = (a.category || getCategoryFromLotName(a.lot_name)).toUpperCase();
|
const catA = (a.category).toUpperCase();
|
||||||
const catB = (b.category || getCategoryFromLotName(b.lot_name)).toUpperCase();
|
const catB = (b.category).toUpperCase();
|
||||||
const orderA = categoryOrderMap[catA] || 9999;
|
const orderA = categoryOrderMap[catA] || 9999;
|
||||||
const orderB = categoryOrderMap[catB] || 9999;
|
const orderB = categoryOrderMap[catB] || 9999;
|
||||||
return orderA - orderB;
|
return orderA - orderB;
|
||||||
@@ -2273,7 +2267,7 @@ function updateCartUI() {
|
|||||||
|
|
||||||
const grouped = {};
|
const grouped = {};
|
||||||
sortedCart.forEach(item => {
|
sortedCart.forEach(item => {
|
||||||
const cat = item.category || getCategoryFromLotName(item.lot_name);
|
const cat = item.category;
|
||||||
const tab = getTabForCategory(cat);
|
const tab = getTabForCategory(cat);
|
||||||
if (!grouped[tab]) grouped[tab] = [];
|
if (!grouped[tab]) grouped[tab] = [];
|
||||||
grouped[tab].push(item);
|
grouped[tab].push(item);
|
||||||
@@ -2282,11 +2276,11 @@ function updateCartUI() {
|
|||||||
// Sort tabs by minimum display order of their categories
|
// Sort tabs by minimum display order of their categories
|
||||||
const sortedTabs = Object.entries(grouped).sort((a, b) => {
|
const sortedTabs = Object.entries(grouped).sort((a, b) => {
|
||||||
const minOrderA = Math.min(...a[1].map(item => {
|
const minOrderA = Math.min(...a[1].map(item => {
|
||||||
const cat = (item.category || getCategoryFromLotName(item.lot_name)).toUpperCase();
|
const cat = (item.category).toUpperCase();
|
||||||
return categoryOrderMap[cat] || 9999;
|
return categoryOrderMap[cat] || 9999;
|
||||||
}));
|
}));
|
||||||
const minOrderB = Math.min(...b[1].map(item => {
|
const minOrderB = Math.min(...b[1].map(item => {
|
||||||
const cat = (item.category || getCategoryFromLotName(item.lot_name)).toUpperCase();
|
const cat = (item.category).toUpperCase();
|
||||||
return categoryOrderMap[cat] || 9999;
|
return categoryOrderMap[cat] || 9999;
|
||||||
}));
|
}));
|
||||||
return minOrderA - minOrderB;
|
return minOrderA - minOrderB;
|
||||||
@@ -2517,8 +2511,7 @@ function restoreAutosaveDraftIfAny() {
|
|||||||
warehouse_price: null,
|
warehouse_price: null,
|
||||||
competitor_price: null,
|
competitor_price: null,
|
||||||
description: item.description || '',
|
description: item.description || '',
|
||||||
category: item.category || getCategoryFromLotName(item.lot_name)
|
category: item.category }));
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
if (typeof payload.server_count === 'number' && payload.server_count > 0) {
|
if (typeof payload.server_count === 'number' && payload.server_count > 0) {
|
||||||
serverCount = payload.server_count;
|
serverCount = payload.server_count;
|
||||||
@@ -2738,8 +2731,8 @@ function renderSalePriceTable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sortedCart = [...cart].sort((a, b) => {
|
const sortedCart = [...cart].sort((a, b) => {
|
||||||
const catA = (a.category || getCategoryFromLotName(a.lot_name)).toUpperCase();
|
const catA = (a.category).toUpperCase();
|
||||||
const catB = (b.category || getCategoryFromLotName(b.lot_name)).toUpperCase();
|
const catB = (b.category).toUpperCase();
|
||||||
const orderA = categoryOrderMap[catA] || 9999;
|
const orderA = categoryOrderMap[catA] || 9999;
|
||||||
const orderB = categoryOrderMap[catB] || 9999;
|
const orderB = categoryOrderMap[catB] || 9999;
|
||||||
return orderA - orderB;
|
return orderA - orderB;
|
||||||
@@ -2842,8 +2835,8 @@ function calculateCustomPrice() {
|
|||||||
// Build adjusted prices table
|
// Build adjusted prices table
|
||||||
// Sort cart items by category display order
|
// Sort cart items by category display order
|
||||||
const sortedCart = [...cart].sort((a, b) => {
|
const sortedCart = [...cart].sort((a, b) => {
|
||||||
const catA = (a.category || getCategoryFromLotName(a.lot_name)).toUpperCase();
|
const catA = (a.category).toUpperCase();
|
||||||
const catB = (b.category || getCategoryFromLotName(b.lot_name)).toUpperCase();
|
const catB = (b.category).toUpperCase();
|
||||||
const orderA = categoryOrderMap[catA] || 9999;
|
const orderA = categoryOrderMap[catA] || 9999;
|
||||||
const orderB = categoryOrderMap[catB] || 9999;
|
const orderB = categoryOrderMap[catB] || 9999;
|
||||||
return orderA - orderB;
|
return orderA - orderB;
|
||||||
@@ -2975,6 +2968,15 @@ async function refreshPrices() {
|
|||||||
}
|
}
|
||||||
beforeTotal *= serverCount;
|
beforeTotal *= serverCount;
|
||||||
|
|
||||||
|
// Create a revision of the current state before prices are updated
|
||||||
|
if (configUUID) {
|
||||||
|
try {
|
||||||
|
await fetch('/api/configs/' + configUUID + '/snapshot', { method: 'POST' });
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('pre-refresh snapshot failed', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await saveConfig(false);
|
await saveConfig(false);
|
||||||
await refreshPriceLevels({ force: true, noCache: true });
|
await refreshPriceLevels({ force: true, noCache: true });
|
||||||
renderTab();
|
renderTab();
|
||||||
@@ -4144,8 +4146,8 @@ async function renderPricingTab() {
|
|||||||
|
|
||||||
if (!bomRows.length) {
|
if (!bomRows.length) {
|
||||||
const sortedByCategory = [...cart].sort((a, b) => {
|
const sortedByCategory = [...cart].sort((a, b) => {
|
||||||
const catA = (a.category || getCategoryFromLotName(a.lot_name)).toUpperCase();
|
const catA = (a.category).toUpperCase();
|
||||||
const catB = (b.category || getCategoryFromLotName(b.lot_name)).toUpperCase();
|
const catB = (b.category).toUpperCase();
|
||||||
return (categoryOrderMap[catA] || 9999) - (categoryOrderMap[catB] || 9999);
|
return (categoryOrderMap[catA] || 9999) - (categoryOrderMap[catB] || 9999);
|
||||||
});
|
});
|
||||||
sortedByCategory.forEach(item => { _pushCartRow(item, false); coveredLots.add(item.lot_name); });
|
sortedByCategory.forEach(item => { _pushCartRow(item, false); coveredLots.add(item.lot_name); });
|
||||||
|
|||||||
@@ -207,9 +207,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="new-variant-value" class="block text-sm font-medium text-gray-700 mb-1">Вариант</label>
|
<label for="new-variant-value" class="block text-sm font-medium text-gray-700 mb-1">Вариант</label>
|
||||||
<input id="new-variant-value" type="text" placeholder="Например: Lenovo"
|
<input id="new-variant-value" type="text" placeholder="Например: B200"
|
||||||
|
pattern="[A-Za-z0-9._-]+"
|
||||||
|
title="Только буквы, цифры, дефис, точка, подчёркивание"
|
||||||
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
||||||
<div class="text-xs text-gray-500 mt-1">Оставьте пустым для main нельзя — нужно уникальное значение.</div>
|
<div class="text-xs text-gray-500 mt-1">Буквы, цифры, дефис, точка, подчёркивание. Используется в URL: /КОД/Вариант.</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-6 flex justify-end gap-2">
|
<div class="mt-6 flex justify-end gap-2">
|
||||||
@@ -842,6 +844,10 @@ async function createNewVariant() {
|
|||||||
showToast('Укажите вариант', 'error');
|
showToast('Укажите вариант', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!/^[A-Za-z0-9._-]+$/.test(variant)) {
|
||||||
|
showToast('Имя варианта содержит недопустимые символы. Разрешены: буквы, цифры, дефис, точка, подчёркивание.', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
const payload = {
|
const payload = {
|
||||||
code: code,
|
code: code,
|
||||||
variant: variant,
|
variant: variant,
|
||||||
|
|||||||
@@ -39,12 +39,18 @@
|
|||||||
<div>
|
<div>
|
||||||
<label for="create-project-code" class="block text-sm font-medium text-gray-700 mb-1">Код проекта</label>
|
<label for="create-project-code" class="block text-sm font-medium text-gray-700 mb-1">Код проекта</label>
|
||||||
<input id="create-project-code" type="text" placeholder="Например: OPS-123"
|
<input id="create-project-code" type="text" placeholder="Например: OPS-123"
|
||||||
|
pattern="[A-Za-z0-9._-]+"
|
||||||
|
title="Только буквы, цифры, дефис, точка, подчёркивание"
|
||||||
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
||||||
|
<p class="text-xs text-gray-400 mt-1">Буквы, цифры, дефис, точка, подчёркивание. Код используется в URL.</p>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="create-project-variant" class="block text-sm font-medium text-gray-700 mb-1">Вариант (необязательно)</label>
|
<label for="create-project-variant" class="block text-sm font-medium text-gray-700 mb-1">Вариант (необязательно)</label>
|
||||||
<input id="create-project-variant" type="text" placeholder="Например: Lenovo"
|
<input id="create-project-variant" type="text" placeholder="Например: B200"
|
||||||
|
pattern="[A-Za-z0-9._-]*"
|
||||||
|
title="Только буквы, цифры, дефис, точка, подчёркивание"
|
||||||
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
||||||
|
<p class="text-xs text-gray-400 mt-1">Используется в URL: /КОД/Вариант</p>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="create-project-tracker-url" class="block text-sm font-medium text-gray-700 mb-1">Ссылка на трекер</label>
|
<label for="create-project-tracker-url" class="block text-sm font-medium text-gray-700 mb-1">Ссылка на трекер</label>
|
||||||
@@ -396,6 +402,14 @@ async function createProject() {
|
|||||||
alert('Введите код проекта');
|
alert('Введите код проекта');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!/^[A-Za-z0-9._-]+$/.test(code)) {
|
||||||
|
alert('Код проекта содержит недопустимые символы.\nРазрешены: буквы, цифры, дефис, точка, подчёркивание.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (variant && !/^[A-Za-z0-9._-]+$/.test(variant)) {
|
||||||
|
alert('Имя варианта содержит недопустимые символы.\nРазрешены: буквы, цифры, дефис, точка, подчёркивание.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
const resp = await fetch('/api/projects', {
|
const resp = await fetch('/api/projects', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
@@ -411,6 +425,11 @@ async function createProject() {
|
|||||||
alert('Проект с таким кодом и вариантом уже существует');
|
alert('Проект с таким кодом и вариантом уже существует');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (resp.status === 400) {
|
||||||
|
const body = await resp.json().catch(() => ({}));
|
||||||
|
alert(body.error || 'Некорректный запрос');
|
||||||
|
return;
|
||||||
|
}
|
||||||
alert('Не удалось создать проект');
|
alert('Не удалось создать проект');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user