From 38082afdb9c24da1e29cf20bf5b0ed6adf9418b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB?= Date: Sat, 8 Aug 2026 12:19:57 +0200 Subject: [PATCH] fix(watch): handle missing workout and remove conflicting delegate --- modules/watch-connectivity/index.swift | 5 ----- src/db/operations.ts | 30 +++++++++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/modules/watch-connectivity/index.swift b/modules/watch-connectivity/index.swift index c7b51f1..e7e391a 100644 --- a/modules/watch-connectivity/index.swift +++ b/modules/watch-connectivity/index.swift @@ -15,11 +15,6 @@ private class SessionDelegate: NSObject, WCSessionDelegate { onActivationComplete?(activationState) } - func session(_ session: WCSession, didReceiveMessage message: [String: Any]) { - print("[WatchConnectivity] Received message: \(message["type"] ?? "unknown")") - onMessageReceived?(message) - } - func session(_ session: WCSession, didReceiveMessage message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) { print("[WatchConnectivity] Received message with reply: \(message["type"] ?? "unknown")") replyHandler(["status": "received"]) diff --git a/src/db/operations.ts b/src/db/operations.ts index fb26f91..b55977f 100644 --- a/src/db/operations.ts +++ b/src/db/operations.ts @@ -153,17 +153,37 @@ export async function saveWatchCompletedWorkout(data: { ? Math.round(data.laps.reduce((sum, l) => sum + (l.heartRate || 0), 0) / data.laps.length) : 0; - await db - .update(workouts) - .set({ + const existing = await db + .select({ id: workouts.id }) + .from(workouts) + .where(eq(workouts.id, data.workoutId)) + .limit(1); + + if (existing.length === 0) { + await db.insert(workouts).values({ + id: data.workoutId, + name: "Watch Workout", + poolLength: 25, finishedAt: new Date().toISOString(), totalDuration: data.duration, totalDistance: data.totalDistance || 0, calories: data.calories || 0, avgHeartRate, source: "watch", - }) - .where(eq(workouts.id, data.workoutId)); + }); + } else { + await db + .update(workouts) + .set({ + finishedAt: new Date().toISOString(), + totalDuration: data.duration, + totalDistance: data.totalDistance || 0, + calories: data.calories || 0, + avgHeartRate, + source: "watch", + }) + .where(eq(workouts.id, data.workoutId)); + } for (const lap of data.laps) { await db.insert(laps).values({