Fix incomplete pricelist sync status

This commit is contained in:
Mikhail Chusavitin
2026-03-17 12:05:02 +03:00
parent 579ff46a7f
commit 68cd087356
8 changed files with 427 additions and 79 deletions

View File

@@ -1066,6 +1066,26 @@ func (l *LocalDB) GetLastSyncTime() *time.Time {
return &t
}
func (l *LocalDB) getAppSettingValue(key string) (string, bool) {
var setting struct {
Value string
}
if err := l.db.Table("app_settings").
Where("key = ?", key).
First(&setting).Error; err != nil {
return "", false
}
return setting.Value, true
}
func (l *LocalDB) upsertAppSetting(tx *gorm.DB, key, value string, updatedAt time.Time) error {
return tx.Exec(`
INSERT INTO app_settings (key, value, updated_at)
VALUES (?, ?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
`, key, value, updatedAt.Format(time.RFC3339)).Error
}
// SetLastSyncTime sets the last sync timestamp
func (l *LocalDB) SetLastSyncTime(t time.Time) error {
// Using raw SQL for upsert since SQLite doesn't have native UPSERT in all versions
@@ -1076,6 +1096,55 @@ func (l *LocalDB) SetLastSyncTime(t time.Time) error {
`, "last_pricelist_sync", t.Format(time.RFC3339), time.Now().Format(time.RFC3339)).Error
}
func (l *LocalDB) GetLastPricelistSyncAttemptAt() *time.Time {
value, ok := l.getAppSettingValue("last_pricelist_sync_attempt_at")
if !ok {
return nil
}
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return nil
}
return &t
}
func (l *LocalDB) GetLastPricelistSyncStatus() string {
value, ok := l.getAppSettingValue("last_pricelist_sync_status")
if !ok {
return ""
}
return strings.TrimSpace(value)
}
func (l *LocalDB) GetLastPricelistSyncError() string {
value, ok := l.getAppSettingValue("last_pricelist_sync_error")
if !ok {
return ""
}
return strings.TrimSpace(value)
}
func (l *LocalDB) SetPricelistSyncResult(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_pricelist_sync_status", status, attemptedAt); err != nil {
return err
}
if err := l.upsertAppSetting(tx, "last_pricelist_sync_error", errorText, attemptedAt); err != nil {
return err
}
if err := l.upsertAppSetting(tx, "last_pricelist_sync_attempt_at", attemptedAt.Format(time.RFC3339), attemptedAt); err != nil {
return err
}
return nil
})
}
// CountLocalPricelists returns the number of local pricelists
func (l *LocalDB) CountLocalPricelists() int64 {
var count int64