Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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)
|
||||||
|
|||||||
@@ -692,6 +692,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 {
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|
||||||
@@ -16,14 +17,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrProjectNotFound = errors.New("project not found")
|
ErrProjectNotFound = errors.New("project not found")
|
||||||
ErrProjectForbidden = errors.New("access to project forbidden")
|
ErrProjectForbidden = errors.New("access to project forbidden")
|
||||||
ErrProjectCodeExists = errors.New("project code and variant already exist")
|
ErrProjectCodeExists = errors.New("project code and variant already exist")
|
||||||
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 {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
@@ -764,9 +776,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 +862,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 +907,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 +921,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 +945,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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>`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2975,6 +2975,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();
|
||||||
|
|||||||
@@ -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