fix(watch): handle missing workout and remove conflicting delegate

This commit is contained in:
2026-08-08 12:19:57 +02:00
parent f3b3a8b944
commit 38082afdb9
2 changed files with 25 additions and 10 deletions
+25 -5
View File
@@ -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({