Add sync redesign with offline fallback (M9)

- Migration 003: adds logged_at to sync_log for TTL pruning; migrates
  settings_history to UUID TEXT PK with updated_at column
- SyncStore: Prune() deletes rows older than 30d and writes a '_pruned'
  marker at the boundary version; Pull() calls Prune lazily and returns
  ErrSyncStale (410) when the client's since_version is behind the marker
- sync_handler.go: GET /api/sync/pull?since=N; POST /api/sync/push with
  last-updated_at-wins conflict resolution for entries, balance_adjustments,
  settings_history; closed_days/closed_weeks skipped (server-only mutations)
- router.go: passes entryStore, adjustmentStore, settingsStore to SyncHandler
- settings_store.go: UUID PK, updated_at column, Upsert() for push path
- settings_service.go: generates UUID on create, sets updated_at on update
- settings_handler.go: ID params changed from int64 to string
- domain.go: Settings.ID string, Settings.UpdatedAt added
- client.ts: all mutation methods catch TypeError (offline) and fall back
  to Dexie write + outbox enqueue; crypto.randomUUID() for offline creates;
  Settings.id type changed to string
- db.ts: Dexie v3 — settings_history key path changed to string UUID;
  upgrade handler clears table for repopulation via pull
- sync.ts: real pushOutbox to POST /api/sync/push; pullChanges uses GET
  with ?since=N; 410 triggers coldStart() + retry; coldStart() wipes all
  tables and resets last_version
- 4 new Go store tests covering normal pull, stale client, empty prune,
  client-ahead-of-marker; all tests pass (store + service, 19 Vitest)
This commit is contained in:
2026-04-30 22:50:33 +02:00
parent 3214f48a6f
commit d8366f5c25
15 changed files with 864 additions and 144 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"time"
"github.com/google/uuid"
"github.com/wotra/wotra/internal/domain"
"github.com/wotra/wotra/internal/store"
)
@@ -78,12 +79,15 @@ func (s *SettingsService) Upsert(ctx context.Context, input UpsertSettingsInput)
return nil, fmt.Errorf("invalid effective_from: %w", err)
}
now := time.Now().UnixMilli()
set := &domain.Settings{
ID: uuid.New().String(),
EffectiveFrom: input.EffectiveFrom,
HoursPerWeek: input.HoursPerWeek,
WorkdaysMask: input.WorkdaysMask,
Timezone: input.Timezone,
CreatedAt: time.Now().UnixMilli(),
CreatedAt: now,
UpdatedAt: now,
}
if err := s.store.Insert(ctx, set); err != nil {
return nil, err
@@ -100,7 +104,7 @@ type UpdateSettingsInput struct {
}
// UpdateSettings edits an existing settings row in-place.
func (s *SettingsService) UpdateSettings(ctx context.Context, id int64, input UpdateSettingsInput) (*domain.Settings, error) {
func (s *SettingsService) UpdateSettings(ctx context.Context, id string, input UpdateSettingsInput) (*domain.Settings, error) {
if input.HoursPerWeek <= 0 {
return nil, ErrInvalidHours
}
@@ -129,6 +133,7 @@ func (s *SettingsService) UpdateSettings(ctx context.Context, id int64, input Up
set.HoursPerWeek = input.HoursPerWeek
set.WorkdaysMask = input.WorkdaysMask
set.Timezone = input.Timezone
set.UpdatedAt = time.Now().UnixMilli()
if err := s.store.Update(ctx, set); err != nil {
return nil, err
@@ -137,7 +142,7 @@ func (s *SettingsService) UpdateSettings(ctx context.Context, id int64, input Up
}
// DeleteSettings removes a settings row. Refuses if it is the only row.
func (s *SettingsService) DeleteSettings(ctx context.Context, id int64) error {
func (s *SettingsService) DeleteSettings(ctx context.Context, id string) error {
count, err := s.store.Count(ctx)
if err != nil {
return err