File size: 1,377 Bytes
cccf200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
SyncOperation model for offline change synchronization.
"""
from typing import Optional
from datetime import datetime
from sqlmodel import SQLModel, Field
from enum import Enum


class SyncOperationType(str, Enum):
    """Types of sync operations."""
    CREATE = "create"
    UPDATE = "update"
    DELETE = "delete"


class SyncOperation(SQLModel, table=True):
    """Entity for tracking offline changes to be synchronized."""

    __tablename__ = "sync_operations"

    id: Optional[int] = Field(default=None, primary_key=True)
    user_id: int = Field(foreign_key="users.id", index=True)
    task_id: Optional[int] = Field(default=None, foreign_key="tasks.id")
    operation_type: str = Field(max_length=20)  # create, update, delete
    client_id: str = Field(max_length=100, index=True)
    payload: Optional[str] = Field(default=None)  # JSON string of operation data
    created_at: datetime = Field(default_factory=datetime.utcnow)
    synced_at: Optional[datetime] = Field(default=None)
    is_synced: bool = Field(default=False)


class SyncRequest(SQLModel):
    """Schema for sync request from client."""

    operations: list[dict]  # List of pending operations from client


class SyncResponse(SQLModel):
    """Schema for sync response to client."""

    success: bool
    synced_count: int
    conflicts: list[dict] = []
    server_updates: list[dict] = []