Apply remaining pricelist and local-first updates
This commit is contained in:
@@ -63,6 +63,7 @@ type ConfigurationChangePayload struct {
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
ConfigurationUUID string `json:"configuration_uuid"`
|
||||
ProjectUUID *string `json:"project_uuid,omitempty"`
|
||||
PricelistID *uint `json:"pricelist_id,omitempty"`
|
||||
Operation string `json:"operation"` // create/update/rollback/deactivate/reactivate/delete
|
||||
CurrentVersionID string `json:"current_version_id,omitempty"`
|
||||
CurrentVersionNo int `json:"current_version_no,omitempty"`
|
||||
@@ -610,6 +611,9 @@ func (s *Service) pushConfigurationCreate(change *localdb.PendingChange) error {
|
||||
if err := s.ensureConfigurationProject(mariaDB, &cfg); err != nil {
|
||||
return fmt.Errorf("resolve configuration project: %w", err)
|
||||
}
|
||||
if err := s.ensureConfigurationPricelist(mariaDB, &cfg); err != nil {
|
||||
return fmt.Errorf("resolve configuration pricelist: %w", err)
|
||||
}
|
||||
|
||||
// Create on server
|
||||
if err := configRepo.Create(&cfg); err != nil {
|
||||
@@ -668,6 +672,9 @@ func (s *Service) pushConfigurationUpdate(change *localdb.PendingChange) error {
|
||||
if err := s.ensureConfigurationProject(mariaDB, &cfg); err != nil {
|
||||
return fmt.Errorf("resolve configuration project: %w", err)
|
||||
}
|
||||
if err := s.ensureConfigurationPricelist(mariaDB, &cfg); err != nil {
|
||||
return fmt.Errorf("resolve configuration pricelist: %w", err)
|
||||
}
|
||||
|
||||
// Ensure we have a server ID before updating
|
||||
// If the payload doesn't have ID, get it from local configuration
|
||||
@@ -801,6 +808,29 @@ func (s *Service) ensureConfigurationProject(mariaDB *gorm.DB, cfg *models.Confi
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) ensureConfigurationPricelist(mariaDB *gorm.DB, cfg *models.Configuration) error {
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("configuration is nil")
|
||||
}
|
||||
|
||||
pricelistRepo := repository.NewPricelistRepository(mariaDB)
|
||||
|
||||
if cfg.PricelistID != nil && *cfg.PricelistID > 0 {
|
||||
if _, err := pricelistRepo.GetByID(*cfg.PricelistID); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
latest, err := pricelistRepo.GetLatestActive()
|
||||
if err != nil {
|
||||
cfg.PricelistID = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg.PricelistID = &latest.ID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) pushConfigurationRollback(change *localdb.PendingChange) error {
|
||||
// Last-write-wins for now: rollback is pushed as an update with rollback metadata.
|
||||
return s.pushConfigurationUpdate(change)
|
||||
@@ -848,6 +878,7 @@ func (s *Service) resolveConfigurationPayloadForPush(change *localdb.PendingChan
|
||||
if currentVersionNo > 0 {
|
||||
payload.CurrentVersionNo = currentVersionNo
|
||||
}
|
||||
payload.PricelistID = currentCfg.PricelistID
|
||||
}
|
||||
|
||||
isStale := false
|
||||
@@ -885,6 +916,7 @@ func decodeConfigurationChangePayload(change *localdb.PendingChange) (Configurat
|
||||
IdempotencyKey: fmt.Sprintf("%s:%s:legacy", cfg.UUID, change.Operation),
|
||||
ConfigurationUUID: cfg.UUID,
|
||||
ProjectUUID: cfg.ProjectUUID,
|
||||
PricelistID: cfg.PricelistID,
|
||||
Operation: change.Operation,
|
||||
ConflictPolicy: "last_write_wins",
|
||||
Snapshot: cfg,
|
||||
|
||||
@@ -248,6 +248,7 @@ CREATE TABLE qt_configurations (
|
||||
notes TEXT NULL,
|
||||
is_template INTEGER NOT NULL DEFAULT 0,
|
||||
server_count INTEGER NOT NULL DEFAULT 1,
|
||||
pricelist_id INTEGER NULL,
|
||||
price_updated_at DATETIME NULL,
|
||||
created_at DATETIME
|
||||
);`).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user