fix: preserve GPS data when recalculating workout sessions
Replace .save() with $set updateOne so only computed fields (totalVolume, totalDistance, prs, gpsPreview) are written. Previously the full document re-serialization could strip gpsTrack arrays.
This commit is contained in:
@@ -40,7 +40,9 @@ export const POST: RequestHandler = async ({ params, locals }) => {
|
|||||||
// Recompute totalVolume and totalDistance
|
// Recompute totalVolume and totalDistance
|
||||||
let totalVolume = 0;
|
let totalVolume = 0;
|
||||||
let totalDistance = 0;
|
let totalDistance = 0;
|
||||||
for (const ex of workoutSession.exercises) {
|
const gpsPreviewUpdates: Record<string, number[][]> = {};
|
||||||
|
for (let i = 0; i < workoutSession.exercises.length; i++) {
|
||||||
|
const ex = workoutSession.exercises[i];
|
||||||
const exercise = getExerciseById(ex.exerciseId);
|
const exercise = getExerciseById(ex.exerciseId);
|
||||||
const metrics = getExerciseMetrics(exercise);
|
const metrics = getExerciseMetrics(exercise);
|
||||||
const isCardio = metrics.includes('distance');
|
const isCardio = metrics.includes('distance');
|
||||||
@@ -56,7 +58,7 @@ export const POST: RequestHandler = async ({ params, locals }) => {
|
|||||||
|
|
||||||
// Regenerate gpsPreview from gpsTrack if present
|
// Regenerate gpsPreview from gpsTrack if present
|
||||||
if (ex.gpsTrack && ex.gpsTrack.length >= 2) {
|
if (ex.gpsTrack && ex.gpsTrack.length >= 2) {
|
||||||
ex.gpsPreview = simplifyTrack(ex.gpsTrack);
|
gpsPreviewUpdates[`exercises.${i}.gpsPreview`] = simplifyTrack(ex.gpsTrack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,15 +122,20 @@ export const POST: RequestHandler = async ({ params, locals }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
workoutSession.totalVolume = totalVolume > 0 ? totalVolume : undefined;
|
// Use $set to only update computed fields, preserving gpsTrack data
|
||||||
workoutSession.totalDistance = totalDistance > 0 ? totalDistance : undefined;
|
await WorkoutSession.updateOne({ _id: workoutSession._id }, {
|
||||||
workoutSession.prs = prs.length > 0 ? prs : undefined;
|
$set: {
|
||||||
await workoutSession.save();
|
totalVolume: totalVolume > 0 ? totalVolume : undefined,
|
||||||
|
totalDistance: totalDistance > 0 ? totalDistance : undefined,
|
||||||
|
prs: prs.length > 0 ? prs : undefined,
|
||||||
|
...gpsPreviewUpdates
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return json({
|
return json({
|
||||||
totalVolume: workoutSession.totalVolume,
|
totalVolume: totalVolume > 0 ? totalVolume : undefined,
|
||||||
totalDistance: workoutSession.totalDistance,
|
totalDistance: totalDistance > 0 ? totalDistance : undefined,
|
||||||
prs: workoutSession.prs?.length ?? 0
|
prs: prs.length
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error recalculating session:', error);
|
console.error('Error recalculating session:', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user