Implement persistent Line ordering for project specs and update bible
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -42,6 +43,7 @@ type ExportItem struct {
|
||||
// ConfigExportBlock represents one configuration (server) in the export.
|
||||
type ConfigExportBlock struct {
|
||||
Article string
|
||||
Line int
|
||||
ServerCount int
|
||||
UnitPrice float64 // sum of component prices for one server
|
||||
Items []ExportItem
|
||||
@@ -92,7 +94,10 @@ func (s *ExportService) ToCSV(w io.Writer, data *ProjectExportData) error {
|
||||
}
|
||||
|
||||
for i, block := range data.Configs {
|
||||
lineNo := (i + 1) * 10
|
||||
lineNo := block.Line
|
||||
if lineNo <= 0 {
|
||||
lineNo = (i + 1) * 10
|
||||
}
|
||||
|
||||
serverCount := block.ServerCount
|
||||
if serverCount < 1 {
|
||||
@@ -104,13 +109,13 @@ func (s *ExportService) ToCSV(w io.Writer, data *ProjectExportData) error {
|
||||
// Server summary row
|
||||
serverRow := []string{
|
||||
fmt.Sprintf("%d", lineNo), // Line
|
||||
"", // Type
|
||||
block.Article, // p/n
|
||||
"", // Description
|
||||
"", // Qty (1 pcs.)
|
||||
fmt.Sprintf("%d", serverCount), // Qty (total)
|
||||
formatPriceInt(block.UnitPrice), // Price (1 pcs.)
|
||||
formatPriceWithSpace(totalPrice), // Price (total)
|
||||
"", // Type
|
||||
block.Article, // p/n
|
||||
"", // Description
|
||||
"", // Qty (1 pcs.)
|
||||
fmt.Sprintf("%d", serverCount), // Qty (total)
|
||||
formatPriceInt(block.UnitPrice), // Price (1 pcs.)
|
||||
formatPriceWithSpace(totalPrice), // Price (total)
|
||||
}
|
||||
if err := csvWriter.Write(serverRow); err != nil {
|
||||
return fmt.Errorf("failed to write server row: %w", err)
|
||||
@@ -124,14 +129,14 @@ func (s *ExportService) ToCSV(w io.Writer, data *ProjectExportData) error {
|
||||
// Component rows
|
||||
for _, item := range sortedItems {
|
||||
componentRow := []string{
|
||||
"", // Line
|
||||
item.Category, // Type
|
||||
item.LotName, // p/n
|
||||
"", // Description
|
||||
fmt.Sprintf("%d", item.Quantity), // Qty (1 pcs.)
|
||||
"", // Qty (total)
|
||||
formatPriceComma(item.UnitPrice), // Price (1 pcs.)
|
||||
"", // Price (total)
|
||||
"", // Line
|
||||
item.Category, // Type
|
||||
item.LotName, // p/n
|
||||
"", // Description
|
||||
fmt.Sprintf("%d", item.Quantity), // Qty (1 pcs.)
|
||||
"", // Qty (total)
|
||||
formatPriceComma(item.UnitPrice), // Price (1 pcs.)
|
||||
"", // Price (total)
|
||||
}
|
||||
if err := csvWriter.Write(componentRow); err != nil {
|
||||
return fmt.Errorf("failed to write component row: %w", err)
|
||||
@@ -174,9 +179,30 @@ func (s *ExportService) ConfigToExportData(cfg *models.Configuration) *ProjectEx
|
||||
|
||||
// ProjectToExportData converts multiple configurations into ProjectExportData.
|
||||
func (s *ExportService) ProjectToExportData(configs []models.Configuration) *ProjectExportData {
|
||||
sortedConfigs := make([]models.Configuration, len(configs))
|
||||
copy(sortedConfigs, configs)
|
||||
sort.Slice(sortedConfigs, func(i, j int) bool {
|
||||
leftLine := sortedConfigs[i].Line
|
||||
rightLine := sortedConfigs[j].Line
|
||||
|
||||
if leftLine <= 0 {
|
||||
leftLine = int(^uint(0) >> 1)
|
||||
}
|
||||
if rightLine <= 0 {
|
||||
rightLine = int(^uint(0) >> 1)
|
||||
}
|
||||
if leftLine != rightLine {
|
||||
return leftLine < rightLine
|
||||
}
|
||||
if !sortedConfigs[i].CreatedAt.Equal(sortedConfigs[j].CreatedAt) {
|
||||
return sortedConfigs[i].CreatedAt.After(sortedConfigs[j].CreatedAt)
|
||||
}
|
||||
return sortedConfigs[i].UUID > sortedConfigs[j].UUID
|
||||
})
|
||||
|
||||
blocks := make([]ConfigExportBlock, 0, len(configs))
|
||||
for i := range configs {
|
||||
blocks = append(blocks, s.buildExportBlock(&configs[i]))
|
||||
for i := range sortedConfigs {
|
||||
blocks = append(blocks, s.buildExportBlock(&sortedConfigs[i]))
|
||||
}
|
||||
return &ProjectExportData{
|
||||
Configs: blocks,
|
||||
@@ -214,6 +240,7 @@ func (s *ExportService) buildExportBlock(cfg *models.Configuration) ConfigExport
|
||||
|
||||
return ConfigExportBlock{
|
||||
Article: cfg.Article,
|
||||
Line: cfg.Line,
|
||||
ServerCount: serverCount,
|
||||
UnitPrice: unitTotal,
|
||||
Items: items,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/config"
|
||||
"git.mchus.pro/mchus/quoteforge/internal/models"
|
||||
)
|
||||
|
||||
func newTestProjectData(items []ExportItem, article string, serverCount int) *ProjectExportData {
|
||||
@@ -357,6 +358,51 @@ func TestToCSV_MultipleBlocks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectToExportData_SortsByLine(t *testing.T) {
|
||||
svc := NewExportService(config.ExportConfig{}, nil, nil)
|
||||
|
||||
configs := []models.Configuration{
|
||||
{
|
||||
UUID: "cfg-1",
|
||||
Line: 30,
|
||||
Article: "ART-30",
|
||||
ServerCount: 1,
|
||||
Items: models.ConfigItems{{LotName: "LOT-30", Quantity: 1, UnitPrice: 300}},
|
||||
CreatedAt: time.Now().Add(-1 * time.Hour),
|
||||
},
|
||||
{
|
||||
UUID: "cfg-2",
|
||||
Line: 10,
|
||||
Article: "ART-10",
|
||||
ServerCount: 1,
|
||||
Items: models.ConfigItems{{LotName: "LOT-10", Quantity: 1, UnitPrice: 100}},
|
||||
CreatedAt: time.Now().Add(-2 * time.Hour),
|
||||
},
|
||||
{
|
||||
UUID: "cfg-3",
|
||||
Line: 20,
|
||||
Article: "ART-20",
|
||||
ServerCount: 1,
|
||||
Items: models.ConfigItems{{LotName: "LOT-20", Quantity: 1, UnitPrice: 200}},
|
||||
CreatedAt: time.Now().Add(-3 * time.Hour),
|
||||
},
|
||||
}
|
||||
|
||||
data := svc.ProjectToExportData(configs)
|
||||
if len(data.Configs) != 3 {
|
||||
t.Fatalf("expected 3 blocks, got %d", len(data.Configs))
|
||||
}
|
||||
if data.Configs[0].Article != "ART-10" || data.Configs[0].Line != 10 {
|
||||
t.Fatalf("first block must be line 10, got article=%s line=%d", data.Configs[0].Article, data.Configs[0].Line)
|
||||
}
|
||||
if data.Configs[1].Article != "ART-20" || data.Configs[1].Line != 20 {
|
||||
t.Fatalf("second block must be line 20, got article=%s line=%d", data.Configs[1].Article, data.Configs[1].Line)
|
||||
}
|
||||
if data.Configs[2].Article != "ART-30" || data.Configs[2].Line != 30 {
|
||||
t.Fatalf("third block must be line 30, got article=%s line=%d", data.Configs[2].Article, data.Configs[2].Line)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatPriceWithSpace(t *testing.T) {
|
||||
tests := []struct {
|
||||
input float64
|
||||
|
||||
@@ -107,6 +107,7 @@ func (s *LocalConfigurationService) Create(ownerUsername string, req *CreateConf
|
||||
if err := s.createWithVersion(localCfg, ownerUsername); err != nil {
|
||||
return nil, fmt.Errorf("create configuration with version: %w", err)
|
||||
}
|
||||
cfg.Line = localCfg.Line
|
||||
|
||||
// Record usage stats
|
||||
_ = s.quoteService.RecordUsage(req.Items)
|
||||
@@ -325,6 +326,7 @@ func (s *LocalConfigurationService) CloneToProject(configUUID string, ownerUsern
|
||||
if err := s.createWithVersion(localCfg, ownerUsername); err != nil {
|
||||
return nil, fmt.Errorf("clone configuration with version: %w", err)
|
||||
}
|
||||
clone.Line = localCfg.Line
|
||||
|
||||
return clone, nil
|
||||
}
|
||||
@@ -640,6 +642,7 @@ func (s *LocalConfigurationService) CloneNoAuthToProjectFromVersion(configUUID s
|
||||
if err := s.createWithVersion(localCfg, ownerUsername); err != nil {
|
||||
return nil, fmt.Errorf("clone configuration without auth with version: %w", err)
|
||||
}
|
||||
clone.Line = localCfg.Line
|
||||
|
||||
return clone, nil
|
||||
}
|
||||
@@ -826,21 +829,13 @@ func (s *LocalConfigurationService) UpdateServerCount(configUUID string, serverC
|
||||
return fmt.Errorf("save local configuration: %w", err)
|
||||
}
|
||||
|
||||
// Use existing current version for the pending change
|
||||
var version localdb.LocalConfigurationVersion
|
||||
if localCfg.CurrentVersionID != nil && *localCfg.CurrentVersionID != "" {
|
||||
if err := tx.Where("id = ?", *localCfg.CurrentVersionID).First(&version).Error; err != nil {
|
||||
return fmt.Errorf("load current version: %w", err)
|
||||
}
|
||||
} else {
|
||||
if err := tx.Where("configuration_uuid = ?", localCfg.UUID).
|
||||
Order("version_no DESC").First(&version).Error; err != nil {
|
||||
return fmt.Errorf("load latest version: %w", err)
|
||||
}
|
||||
version, err := s.loadVersionForPendingTx(tx, localCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg = localdb.LocalToConfiguration(localCfg)
|
||||
if err := s.enqueueConfigurationPendingChangeTx(tx, localCfg, "update", &version, ""); err != nil {
|
||||
if err := s.enqueueConfigurationPendingChangeTx(tx, localCfg, "update", version, ""); err != nil {
|
||||
return fmt.Errorf("enqueue server-count pending change: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -852,6 +847,99 @@ func (s *LocalConfigurationService) UpdateServerCount(configUUID string, serverC
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (s *LocalConfigurationService) ReorderProjectConfigurationsNoAuth(projectUUID string, orderedUUIDs []string) ([]models.Configuration, error) {
|
||||
projectUUID = strings.TrimSpace(projectUUID)
|
||||
if projectUUID == "" {
|
||||
return nil, ErrProjectNotFound
|
||||
}
|
||||
if _, err := s.localDB.GetProjectByUUID(projectUUID); err != nil {
|
||||
return nil, ErrProjectNotFound
|
||||
}
|
||||
if len(orderedUUIDs) == 0 {
|
||||
return []models.Configuration{}, nil
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(orderedUUIDs))
|
||||
normalized := make([]string, 0, len(orderedUUIDs))
|
||||
for _, raw := range orderedUUIDs {
|
||||
u := strings.TrimSpace(raw)
|
||||
if u == "" {
|
||||
return nil, fmt.Errorf("ordered_uuids contains empty uuid")
|
||||
}
|
||||
if _, exists := seen[u]; exists {
|
||||
return nil, fmt.Errorf("ordered_uuids contains duplicate uuid: %s", u)
|
||||
}
|
||||
seen[u] = struct{}{}
|
||||
normalized = append(normalized, u)
|
||||
}
|
||||
|
||||
err := s.localDB.DB().Transaction(func(tx *gorm.DB) error {
|
||||
var active []localdb.LocalConfiguration
|
||||
if err := tx.Where("project_uuid = ? AND is_active = ?", projectUUID, true).
|
||||
Find(&active).Error; err != nil {
|
||||
return fmt.Errorf("load project active configurations: %w", err)
|
||||
}
|
||||
if len(active) != len(normalized) {
|
||||
return fmt.Errorf("ordered_uuids count mismatch: expected %d got %d", len(active), len(normalized))
|
||||
}
|
||||
|
||||
byUUID := make(map[string]*localdb.LocalConfiguration, len(active))
|
||||
for i := range active {
|
||||
cfg := active[i]
|
||||
byUUID[cfg.UUID] = &cfg
|
||||
}
|
||||
for _, id := range normalized {
|
||||
if _, ok := byUUID[id]; !ok {
|
||||
return fmt.Errorf("configuration %s not found in project %s", id, projectUUID)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
for idx, id := range normalized {
|
||||
cfg := byUUID[id]
|
||||
newLine := (idx + 1) * 10
|
||||
if cfg.Line == newLine {
|
||||
continue
|
||||
}
|
||||
|
||||
cfg.Line = newLine
|
||||
cfg.UpdatedAt = now
|
||||
cfg.SyncStatus = "pending"
|
||||
|
||||
if err := tx.Save(cfg).Error; err != nil {
|
||||
return fmt.Errorf("save reordered configuration %s: %w", cfg.UUID, err)
|
||||
}
|
||||
|
||||
version, err := s.loadVersionForPendingTx(tx, cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.enqueueConfigurationPendingChangeTx(tx, cfg, "update", version, ""); err != nil {
|
||||
return fmt.Errorf("enqueue reorder pending change for %s: %w", cfg.UUID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var localConfigs []localdb.LocalConfiguration
|
||||
if err := s.localDB.DB().
|
||||
Preload("CurrentVersion").
|
||||
Where("project_uuid = ? AND is_active = ?", projectUUID, true).
|
||||
Order("CASE WHEN COALESCE(line_no, 0) <= 0 THEN 2147483647 ELSE line_no END ASC, created_at DESC, id DESC").
|
||||
Find(&localConfigs).Error; err != nil {
|
||||
return nil, fmt.Errorf("load reordered configurations: %w", err)
|
||||
}
|
||||
|
||||
result := make([]models.Configuration, 0, len(localConfigs))
|
||||
for i := range localConfigs {
|
||||
result = append(result, *localdb.LocalToConfiguration(&localConfigs[i]))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ImportFromServer imports configurations from MariaDB to local SQLite cache.
|
||||
func (s *LocalConfigurationService) ImportFromServer() (*sync.ConfigImportResult, error) {
|
||||
return s.syncService.ImportConfigurationsToLocal()
|
||||
@@ -965,6 +1053,11 @@ func (s *LocalConfigurationService) isOwner(cfg *localdb.LocalConfiguration, own
|
||||
|
||||
func (s *LocalConfigurationService) createWithVersion(localCfg *localdb.LocalConfiguration, createdBy string) error {
|
||||
return s.localDB.DB().Transaction(func(tx *gorm.DB) error {
|
||||
if localCfg.IsActive && localCfg.Line <= 0 {
|
||||
if err := s.ensureConfigurationLineTx(tx, localCfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Create(localCfg).Error; err != nil {
|
||||
return fmt.Errorf("create local configuration: %w", err)
|
||||
}
|
||||
@@ -1021,12 +1114,31 @@ func (s *LocalConfigurationService) saveWithVersionAndPending(localCfg *localdb.
|
||||
return fmt.Errorf("compare revision content: %w", err)
|
||||
}
|
||||
if sameRevisionContent {
|
||||
cfg = localdb.LocalToConfiguration(&locked)
|
||||
if !hasNonRevisionConfigurationChanges(&locked, localCfg) {
|
||||
cfg = localdb.LocalToConfiguration(&locked)
|
||||
return nil
|
||||
}
|
||||
if err := tx.Save(localCfg).Error; err != nil {
|
||||
return fmt.Errorf("save local configuration (no new revision): %w", err)
|
||||
}
|
||||
cfg = localdb.LocalToConfiguration(localCfg)
|
||||
if err := s.enqueueConfigurationPendingChangeTx(tx, localCfg, operation, currentVersion, createdBy); err != nil {
|
||||
return fmt.Errorf("enqueue %s pending change without revision: %w", operation, err)
|
||||
}
|
||||
if err := s.recalculateLocalPricelistUsageTx(tx); err != nil {
|
||||
return fmt.Errorf("recalculate local pricelist usage: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if localCfg.IsActive && localCfg.Line <= 0 {
|
||||
if err := s.ensureConfigurationLineTx(tx, localCfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Save(localCfg).Error; err != nil {
|
||||
return fmt.Errorf("save local configuration: %w", err)
|
||||
}
|
||||
@@ -1061,6 +1173,50 @@ func (s *LocalConfigurationService) saveWithVersionAndPending(localCfg *localdb.
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func hasNonRevisionConfigurationChanges(current *localdb.LocalConfiguration, next *localdb.LocalConfiguration) bool {
|
||||
if current == nil || next == nil {
|
||||
return true
|
||||
}
|
||||
if current.Name != next.Name ||
|
||||
current.Notes != next.Notes ||
|
||||
current.IsTemplate != next.IsTemplate ||
|
||||
current.ServerModel != next.ServerModel ||
|
||||
current.SupportCode != next.SupportCode ||
|
||||
current.Article != next.Article ||
|
||||
current.OnlyInStock != next.OnlyInStock ||
|
||||
current.IsActive != next.IsActive ||
|
||||
current.Line != next.Line {
|
||||
return true
|
||||
}
|
||||
if !equalUintPtr(current.PricelistID, next.PricelistID) ||
|
||||
!equalUintPtr(current.WarehousePricelistID, next.WarehousePricelistID) ||
|
||||
!equalUintPtr(current.CompetitorPricelistID, next.CompetitorPricelistID) ||
|
||||
!equalStringPtr(current.ProjectUUID, next.ProjectUUID) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func equalStringPtr(a, b *string) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
return strings.TrimSpace(*a) == strings.TrimSpace(*b)
|
||||
}
|
||||
|
||||
func equalUintPtr(a, b *uint) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
|
||||
func (s *LocalConfigurationService) loadCurrentVersionTx(tx *gorm.DB, localCfg *localdb.LocalConfiguration) (*localdb.LocalConfigurationVersion, error) {
|
||||
var version localdb.LocalConfigurationVersion
|
||||
if localCfg.CurrentVersionID != nil && *localCfg.CurrentVersionID != "" {
|
||||
@@ -1082,6 +1238,32 @@ func (s *LocalConfigurationService) loadCurrentVersionTx(tx *gorm.DB, localCfg *
|
||||
return &version, nil
|
||||
}
|
||||
|
||||
func (s *LocalConfigurationService) loadVersionForPendingTx(tx *gorm.DB, localCfg *localdb.LocalConfiguration) (*localdb.LocalConfigurationVersion, error) {
|
||||
if localCfg.CurrentVersionID != nil && *localCfg.CurrentVersionID != "" {
|
||||
var current localdb.LocalConfigurationVersion
|
||||
if err := tx.Where("id = ?", *localCfg.CurrentVersionID).First(¤t).Error; err == nil {
|
||||
return ¤t, nil
|
||||
}
|
||||
}
|
||||
|
||||
var latest localdb.LocalConfigurationVersion
|
||||
if err := tx.Where("configuration_uuid = ?", localCfg.UUID).
|
||||
Order("version_no DESC").
|
||||
First(&latest).Error; err != nil {
|
||||
return nil, fmt.Errorf("load version for pending change: %w", err)
|
||||
}
|
||||
return &latest, nil
|
||||
}
|
||||
|
||||
func (s *LocalConfigurationService) ensureConfigurationLineTx(tx *gorm.DB, localCfg *localdb.LocalConfiguration) error {
|
||||
line, err := localdb.NextConfigurationLineTx(tx, localCfg.ProjectUUID, localCfg.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("assign line_no for configuration %s: %w", localCfg.UUID, err)
|
||||
}
|
||||
localCfg.Line = line
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LocalConfigurationService) hasSameRevisionContent(localCfg *localdb.LocalConfiguration, currentVersion *localdb.LocalConfigurationVersion) (bool, error) {
|
||||
currentSnapshotCfg, err := s.decodeConfigurationSnapshot(currentVersion.Data)
|
||||
if err != nil {
|
||||
@@ -1195,6 +1377,9 @@ func (s *LocalConfigurationService) rollbackToVersion(configurationUUID string,
|
||||
current.ServerCount = rollbackData.ServerCount
|
||||
current.PricelistID = rollbackData.PricelistID
|
||||
current.OnlyInStock = rollbackData.OnlyInStock
|
||||
if rollbackData.Line > 0 {
|
||||
current.Line = rollbackData.Line
|
||||
}
|
||||
current.PriceUpdatedAt = rollbackData.PriceUpdatedAt
|
||||
current.UpdatedAt = time.Now()
|
||||
current.SyncStatus = "pending"
|
||||
|
||||
@@ -137,6 +137,78 @@ func TestUpdateNoAuthSkipsRevisionWhenSpecAndPriceUnchanged(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReorderProjectConfigurationsDoesNotCreateNewVersions(t *testing.T) {
|
||||
service, local := newLocalConfigServiceForTest(t)
|
||||
|
||||
project := &localdb.LocalProject{
|
||||
UUID: "project-reorder",
|
||||
OwnerUsername: "tester",
|
||||
Code: "PRJ-ORDER",
|
||||
Variant: "",
|
||||
Name: ptrString("Project Reorder"),
|
||||
IsActive: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
SyncStatus: "pending",
|
||||
}
|
||||
if err := local.SaveProject(project); err != nil {
|
||||
t.Fatalf("save project: %v", err)
|
||||
}
|
||||
|
||||
first, err := service.Create("tester", &CreateConfigRequest{
|
||||
Name: "Cfg A",
|
||||
Items: models.ConfigItems{{LotName: "CPU_A", Quantity: 1, UnitPrice: 100}},
|
||||
ServerCount: 1,
|
||||
ProjectUUID: &project.UUID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create first config: %v", err)
|
||||
}
|
||||
second, err := service.Create("tester", &CreateConfigRequest{
|
||||
Name: "Cfg B",
|
||||
Items: models.ConfigItems{{LotName: "CPU_B", Quantity: 1, UnitPrice: 200}},
|
||||
ServerCount: 1,
|
||||
ProjectUUID: &project.UUID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create second config: %v", err)
|
||||
}
|
||||
|
||||
beforeFirst := loadVersions(t, local, first.UUID)
|
||||
beforeSecond := loadVersions(t, local, second.UUID)
|
||||
|
||||
reordered, err := service.ReorderProjectConfigurationsNoAuth(project.UUID, []string{second.UUID, first.UUID})
|
||||
if err != nil {
|
||||
t.Fatalf("reorder configurations: %v", err)
|
||||
}
|
||||
if len(reordered) != 2 {
|
||||
t.Fatalf("expected 2 reordered configs, got %d", len(reordered))
|
||||
}
|
||||
if reordered[0].UUID != second.UUID || reordered[0].Line != 10 {
|
||||
t.Fatalf("expected second config first with line 10, got uuid=%s line=%d", reordered[0].UUID, reordered[0].Line)
|
||||
}
|
||||
if reordered[1].UUID != first.UUID || reordered[1].Line != 20 {
|
||||
t.Fatalf("expected first config second with line 20, got uuid=%s line=%d", reordered[1].UUID, reordered[1].Line)
|
||||
}
|
||||
|
||||
afterFirst := loadVersions(t, local, first.UUID)
|
||||
afterSecond := loadVersions(t, local, second.UUID)
|
||||
if len(afterFirst) != len(beforeFirst) || len(afterSecond) != len(beforeSecond) {
|
||||
t.Fatalf("reorder must not create new versions")
|
||||
}
|
||||
|
||||
var pendingCount int64
|
||||
if err := local.DB().
|
||||
Table("pending_changes").
|
||||
Where("entity_type = ? AND operation = ? AND entity_uuid IN ?", "configuration", "update", []string{first.UUID, second.UUID}).
|
||||
Count(&pendingCount).Error; err != nil {
|
||||
t.Fatalf("count reorder pending changes: %v", err)
|
||||
}
|
||||
if pendingCount < 2 {
|
||||
t.Fatalf("expected at least 2 pending update changes for reorder, got %d", pendingCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOnlyInvariantOldRowsUnchanged(t *testing.T) {
|
||||
service, local := newLocalConfigServiceForTest(t)
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrProjectNotFound = errors.New("project not found")
|
||||
ErrProjectForbidden = errors.New("access to project forbidden")
|
||||
ErrProjectCodeExists = errors.New("project code and variant already exist")
|
||||
ErrCannotDeleteMainVariant = errors.New("cannot delete main variant")
|
||||
ErrProjectNotFound = errors.New("project not found")
|
||||
ErrProjectForbidden = errors.New("access to project forbidden")
|
||||
ErrProjectCodeExists = errors.New("project code and variant already exist")
|
||||
ErrCannotDeleteMainVariant = errors.New("cannot delete main variant")
|
||||
)
|
||||
|
||||
type ProjectService struct {
|
||||
@@ -31,10 +31,10 @@ func NewProjectService(localDB *localdb.LocalDB) *ProjectService {
|
||||
}
|
||||
|
||||
type CreateProjectRequest struct {
|
||||
Code string `json:"code"`
|
||||
Variant string `json:"variant,omitempty"`
|
||||
Code string `json:"code"`
|
||||
Variant string `json:"variant,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
TrackerURL string `json:"tracker_url"`
|
||||
TrackerURL string `json:"tracker_url"`
|
||||
}
|
||||
|
||||
type UpdateProjectRequest struct {
|
||||
@@ -275,8 +275,23 @@ func (s *ProjectService) ListConfigurations(projectUUID, ownerUsername, status s
|
||||
}, nil
|
||||
}
|
||||
|
||||
query := s.localDB.DB().
|
||||
Preload("CurrentVersion").
|
||||
Where("project_uuid = ?", projectUUID).
|
||||
Order("CASE WHEN COALESCE(line_no, 0) <= 0 THEN 2147483647 ELSE line_no END ASC, created_at DESC, id DESC")
|
||||
|
||||
switch status {
|
||||
case "active", "":
|
||||
query = query.Where("is_active = ?", true)
|
||||
case "archived":
|
||||
query = query.Where("is_active = ?", false)
|
||||
case "all":
|
||||
default:
|
||||
query = query.Where("is_active = ?", true)
|
||||
}
|
||||
|
||||
var localConfigs []localdb.LocalConfiguration
|
||||
if err := s.localDB.DB().Preload("CurrentVersion").Order("created_at DESC").Find(&localConfigs).Error; err != nil {
|
||||
if err := query.Find(&localConfigs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -284,25 +299,6 @@ func (s *ProjectService) ListConfigurations(projectUUID, ownerUsername, status s
|
||||
total := 0.0
|
||||
for i := range localConfigs {
|
||||
localCfg := localConfigs[i]
|
||||
if localCfg.ProjectUUID == nil || *localCfg.ProjectUUID != projectUUID {
|
||||
continue
|
||||
}
|
||||
switch status {
|
||||
case "active", "":
|
||||
if !localCfg.IsActive {
|
||||
continue
|
||||
}
|
||||
case "archived":
|
||||
if localCfg.IsActive {
|
||||
continue
|
||||
}
|
||||
case "all":
|
||||
default:
|
||||
if !localCfg.IsActive {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
cfg := localdb.LocalToConfiguration(&localCfg)
|
||||
if cfg.TotalPrice != nil {
|
||||
total += *cfg.TotalPrice
|
||||
|
||||
@@ -145,6 +145,9 @@ func (s *Service) ImportConfigurationsToLocal() (*ConfigImportResult, error) {
|
||||
|
||||
if existing != nil && err == nil {
|
||||
localCfg.ID = existing.ID
|
||||
if localCfg.Line <= 0 && existing.Line > 0 {
|
||||
localCfg.Line = existing.Line
|
||||
}
|
||||
result.Updated++
|
||||
} else {
|
||||
result.Imported++
|
||||
|
||||
@@ -250,6 +250,71 @@ func TestPushPendingChangesCreateIsIdempotent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushPendingChangesConfigurationPushesLine(t *testing.T) {
|
||||
local := newLocalDBForSyncTest(t)
|
||||
serverDB := newServerDBForSyncTest(t)
|
||||
|
||||
localSync := syncsvc.NewService(nil, local)
|
||||
configService := services.NewLocalConfigurationService(local, localSync, &services.QuoteService{}, func() bool { return false })
|
||||
pushService := syncsvc.NewServiceWithDB(serverDB, local)
|
||||
|
||||
created, err := configService.Create("tester", &services.CreateConfigRequest{
|
||||
Name: "Cfg Line Push",
|
||||
Items: models.ConfigItems{{LotName: "CPU_LINE", Quantity: 1, UnitPrice: 1000}},
|
||||
ServerCount: 1,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create config: %v", err)
|
||||
}
|
||||
if created.Line != 10 {
|
||||
t.Fatalf("expected local create line=10, got %d", created.Line)
|
||||
}
|
||||
|
||||
if _, err := pushService.PushPendingChanges(); err != nil {
|
||||
t.Fatalf("push pending changes: %v", err)
|
||||
}
|
||||
|
||||
var serverCfg models.Configuration
|
||||
if err := serverDB.Where("uuid = ?", created.UUID).First(&serverCfg).Error; err != nil {
|
||||
t.Fatalf("load server config: %v", err)
|
||||
}
|
||||
if serverCfg.Line != 10 {
|
||||
t.Fatalf("expected server line=10 after push, got %d", serverCfg.Line)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportConfigurationsToLocalPullsLine(t *testing.T) {
|
||||
local := newLocalDBForSyncTest(t)
|
||||
serverDB := newServerDBForSyncTest(t)
|
||||
|
||||
cfg := models.Configuration{
|
||||
UUID: "server-line-config",
|
||||
OwnerUsername: "tester",
|
||||
Name: "Cfg Line Pull",
|
||||
Items: models.ConfigItems{{LotName: "CPU_PULL", Quantity: 1, UnitPrice: 900}},
|
||||
ServerCount: 1,
|
||||
Line: 40,
|
||||
}
|
||||
total := cfg.Items.Total()
|
||||
cfg.TotalPrice = &total
|
||||
if err := serverDB.Create(&cfg).Error; err != nil {
|
||||
t.Fatalf("seed server config: %v", err)
|
||||
}
|
||||
|
||||
svc := syncsvc.NewServiceWithDB(serverDB, local)
|
||||
if _, err := svc.ImportConfigurationsToLocal(); err != nil {
|
||||
t.Fatalf("import configurations to local: %v", err)
|
||||
}
|
||||
|
||||
localCfg, err := local.GetConfigurationByUUID(cfg.UUID)
|
||||
if err != nil {
|
||||
t.Fatalf("load local config: %v", err)
|
||||
}
|
||||
if localCfg.Line != 40 {
|
||||
t.Fatalf("expected imported line=40, got %d", localCfg.Line)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushPendingChangesCreateThenUpdateBeforeFirstPush(t *testing.T) {
|
||||
local := newLocalDBForSyncTest(t)
|
||||
serverDB := newServerDBForSyncTest(t)
|
||||
@@ -361,6 +426,7 @@ CREATE TABLE qt_configurations (
|
||||
competitor_pricelist_id INTEGER NULL,
|
||||
disable_price_refresh INTEGER NOT NULL DEFAULT 0,
|
||||
only_in_stock INTEGER NOT NULL DEFAULT 0,
|
||||
line_no INTEGER NULL,
|
||||
price_updated_at DATETIME NULL,
|
||||
created_at DATETIME
|
||||
);`).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user