feat(watch): add lap detection via accelerometer and workout sync to phone
This commit is contained in:
@@ -135,6 +135,51 @@ export async function getWorkoutLaps(workoutId: string) {
|
||||
return db.select().from(laps).where(eq(laps.workoutId, workoutId));
|
||||
}
|
||||
|
||||
export async function saveWatchCompletedWorkout(data: {
|
||||
workoutId: string;
|
||||
duration: number;
|
||||
totalDistance: number;
|
||||
calories: number;
|
||||
laps: Array<{
|
||||
setPosition: number;
|
||||
repNumber: number;
|
||||
distance: number;
|
||||
duration: number;
|
||||
heartRate: number;
|
||||
}>;
|
||||
}) {
|
||||
const avgHeartRate =
|
||||
data.laps.length > 0
|
||||
? Math.round(data.laps.reduce((sum, l) => sum + (l.heartRate || 0), 0) / data.laps.length)
|
||||
: 0;
|
||||
|
||||
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({
|
||||
id: generateUUID(),
|
||||
workoutId: data.workoutId,
|
||||
setPosition: lap.setPosition,
|
||||
repNumber: lap.repNumber,
|
||||
distance: lap.distance,
|
||||
duration: lap.duration,
|
||||
heartRate: lap.heartRate || null,
|
||||
strokeCount: null,
|
||||
swolf: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Stats ───────────────────────────────────────────────────
|
||||
|
||||
export async function getWeeklyStats() {
|
||||
|
||||
Reference in New Issue
Block a user