diff --git a/backend/app/workers/tasks.py b/backend/app/workers/tasks.py index 80ac5cd..1228553 100644 --- a/backend/app/workers/tasks.py +++ b/backend/app/workers/tasks.py @@ -82,15 +82,16 @@ def process_activity_file(self, file_path: str, user_id: int, source_type: str, with SyncSessionLocal() as db: start_time = datetime.fromisoformat(parsed["start_time"]) - # Deduplicate by (user_id, sport_type, start_time date + within 60s) + # Deduplicate: same user + sport_type + start_time within ±60s + from datetime import timedelta existing = db.execute( select(Activity).where( Activity.user_id == user_id, Activity.sport_type == parsed["sport_type"], - func.date(Activity.start_time) == start_time.date(), - Activity.start_time >= start_time.replace(second=0, microsecond=0), + Activity.start_time >= start_time - timedelta(seconds=60), + Activity.start_time <= start_time + timedelta(seconds=60), ) - ).scalar_one_or_none() + ).scalars().first() if existing: # Stamp garmin_activity_id if this came from a Garmin Connect sync