feat: implement offline sync endpoints with Last-Write-Wins strategy
Add comprehensive offline synchronization support for habits and entries: ## Infrastructure (Phase 1) - Add UpdatedAt and DeletedAt timestamps to Habit and HabitEntry entities - Implement soft delete with Delete(), Touch(), and IsDeleted() methods - Create SQL migration with optimized composite indexes for sync queries - Add GetChangesSince() and SoftDelete() to both repositories - Update all Find* methods to exclude soft-deleted records - 13 comprehensive TDD tests for sync repository methods ## HTTP Endpoints (Phase 2) - GET /api/v1/sync/changes: retrieve all changes since timestamp - POST /api/v1/sync/batch: apply client changes with conflict resolution - Implement Last-Write-Wins strategy using UpdatedAt timestamps - Add authentication and rate limiting (100 req/min) - Validate user ownership for all sync operations - 9 tests for sync handlers (3 queries + 6 commands) ## Technical Details - Composite indexes: (user_id, updated_at) for optimal query performance - No pagination: atomic sync operations for data consistency - Upsert behavior: create resources if not found on server - DTOs with full entity state including timestamps - Swagger documentation updated for new endpoints All 220+ tests passing ✓
This commit is contained in:
@@ -19,7 +19,9 @@ type Habit struct {
|
||||
IsNegative bool
|
||||
TargetValue *float64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ArchivedAt *time.Time
|
||||
DeletedAt *time.Time
|
||||
}
|
||||
|
||||
func NewHabit(
|
||||
@@ -30,6 +32,7 @@ func NewHabit(
|
||||
carryOver bool,
|
||||
isNegative bool,
|
||||
) *Habit {
|
||||
now := time.Now()
|
||||
return &Habit{
|
||||
UserID: userID,
|
||||
Name: name,
|
||||
@@ -37,15 +40,31 @@ func NewHabit(
|
||||
Frequency: frequency,
|
||||
CarryOver: carryOver,
|
||||
IsNegative: isNegative,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Habit) Archive() {
|
||||
now := time.Now()
|
||||
h.ArchivedAt = &now
|
||||
h.UpdatedAt = now
|
||||
}
|
||||
|
||||
func (h *Habit) IsActive() bool {
|
||||
return h.ArchivedAt == nil
|
||||
}
|
||||
|
||||
func (h *Habit) Delete() {
|
||||
now := time.Now()
|
||||
h.DeletedAt = &now
|
||||
h.UpdatedAt = now
|
||||
}
|
||||
|
||||
func (h *Habit) IsDeleted() bool {
|
||||
return h.DeletedAt != nil
|
||||
}
|
||||
|
||||
func (h *Habit) Touch() {
|
||||
h.UpdatedAt = time.Now()
|
||||
}
|
||||
|
||||
@@ -8,13 +8,27 @@ type HabitEntry struct {
|
||||
ScheduledDate time.Time
|
||||
CompletedAt time.Time
|
||||
Value *float64
|
||||
UpdatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
}
|
||||
|
||||
func NewHabitEntry(habitID string, scheduledDate time.Time, value *float64) *HabitEntry {
|
||||
now := time.Now()
|
||||
return &HabitEntry{
|
||||
HabitID: habitID,
|
||||
ScheduledDate: scheduledDate,
|
||||
CompletedAt: time.Now(),
|
||||
CompletedAt: now,
|
||||
Value: value,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *HabitEntry) Delete() {
|
||||
now := time.Now()
|
||||
e.DeletedAt = &now
|
||||
e.UpdatedAt = now
|
||||
}
|
||||
|
||||
func (e *HabitEntry) IsDeleted() bool {
|
||||
return e.DeletedAt != nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,12 @@ import (
|
||||
"apocapoc-api/internal/domain/entities"
|
||||
)
|
||||
|
||||
type HabitEntryChanges struct {
|
||||
Created []*entities.HabitEntry
|
||||
Updated []*entities.HabitEntry
|
||||
Deleted []string
|
||||
}
|
||||
|
||||
type HabitEntryRepository interface {
|
||||
Create(ctx context.Context, entry *entities.HabitEntry) error
|
||||
FindByID(ctx context.Context, id string) (*entities.HabitEntry, error)
|
||||
@@ -16,4 +22,8 @@ type HabitEntryRepository interface {
|
||||
FindPendingByHabitID(ctx context.Context, habitID string, beforeDate time.Time) ([]*entities.HabitEntry, error)
|
||||
Update(ctx context.Context, entry *entities.HabitEntry) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
|
||||
// Sync methods
|
||||
GetChangesSince(ctx context.Context, userID string, since time.Time) (*HabitEntryChanges, error)
|
||||
SoftDelete(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"apocapoc-api/internal/domain/entities"
|
||||
"apocapoc-api/internal/domain/value_objects"
|
||||
@@ -15,6 +16,12 @@ type HabitFilter struct {
|
||||
Search string
|
||||
}
|
||||
|
||||
type HabitChanges struct {
|
||||
Created []*entities.Habit
|
||||
Updated []*entities.Habit
|
||||
Deleted []string
|
||||
}
|
||||
|
||||
type HabitRepository interface {
|
||||
Create(ctx context.Context, habit *entities.Habit) error
|
||||
FindByID(ctx context.Context, id string) (*entities.Habit, error)
|
||||
@@ -26,4 +33,8 @@ type HabitRepository interface {
|
||||
CountByUserIDFiltered(ctx context.Context, userID string, filter HabitFilter) (int, error)
|
||||
Update(ctx context.Context, habit *entities.Habit) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
|
||||
// Sync methods
|
||||
GetChangesSince(ctx context.Context, userID string, since time.Time) (*HabitChanges, error)
|
||||
SoftDelete(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user