014640d82b
- SessionCard SVG: cosine-corrected coordinates with proper aspect ratio (xMidYMid meet) - SessionCard: use --color-primary for track/distance/pace, add Gauge icon for pace - History detail: theme-reactive pace chart colors via MutationObserver + matchMedia - History detail: add Gauge icon, accent color for distance/pace stats, remove "avg" label - Move GPS remove button from info view to edit screen - Add Leaflet map preview to edit screen - Remove data points count from GPS indicators
153 lines
5.5 KiB
TypeScript
153 lines
5.5 KiB
TypeScript
import { json } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { dbConnect } from '$utils/db';
|
|
import { WorkoutSession } from '$models/WorkoutSession';
|
|
import type { IPr } from '$models/WorkoutSession';
|
|
import { WorkoutTemplate } from '$models/WorkoutTemplate';
|
|
import { getExerciseById, getExerciseMetrics } from '$lib/data/exercises';
|
|
|
|
function estimatedOneRepMax(weight: number, reps: number): number {
|
|
if (reps <= 0 || weight <= 0) return 0;
|
|
if (reps === 1) return weight;
|
|
return Math.round(weight * (1 + reps / 30));
|
|
}
|
|
|
|
// GET /api/fitness/sessions - Get all workout sessions for the user
|
|
export const GET: RequestHandler = async ({ url, locals }) => {
|
|
const session = await locals.auth();
|
|
if (!session || !session.user?.nickname) {
|
|
return json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
await dbConnect();
|
|
|
|
const limit = parseInt(url.searchParams.get('limit') || '20');
|
|
const offset = parseInt(url.searchParams.get('offset') || '0');
|
|
|
|
const sessions = await WorkoutSession.find({ createdBy: session.user.nickname })
|
|
.select('-exercises.gpsTrack')
|
|
.sort({ startTime: -1 })
|
|
.limit(limit)
|
|
.skip(offset);
|
|
|
|
const total = await WorkoutSession.countDocuments({ createdBy: session.user.nickname });
|
|
|
|
return json({ sessions, total, limit, offset });
|
|
} catch (error) {
|
|
console.error('Error fetching workout sessions:', error);
|
|
return json({ error: 'Failed to fetch workout sessions' }, { status: 500 });
|
|
}
|
|
};
|
|
|
|
// POST /api/fitness/sessions - Create a new workout session
|
|
export const POST: RequestHandler = async ({ request, locals }) => {
|
|
const session = await locals.auth();
|
|
if (!session || !session.user?.nickname) {
|
|
return json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
await dbConnect();
|
|
|
|
const data = await request.json();
|
|
const { templateId, name, exercises, startTime, endTime, notes } = data;
|
|
|
|
if (!name || !exercises || !Array.isArray(exercises) || exercises.length === 0) {
|
|
return json({ error: 'Name and at least one exercise are required' }, { status: 400 });
|
|
}
|
|
|
|
let templateName;
|
|
if (templateId) {
|
|
const template = await WorkoutTemplate.findById(templateId);
|
|
if (template) {
|
|
templateName = template.name;
|
|
}
|
|
}
|
|
|
|
// Compute totalVolume and totalDistance
|
|
let totalVolume = 0;
|
|
let totalDistance = 0;
|
|
for (const ex of exercises) {
|
|
const exercise = getExerciseById(ex.exerciseId);
|
|
const metrics = getExerciseMetrics(exercise);
|
|
const isCardio = metrics.includes('distance');
|
|
const isBilateral = exercise?.bilateral ?? false;
|
|
for (const s of (ex.sets ?? [])) {
|
|
if (!s.completed) continue;
|
|
if (isCardio) {
|
|
totalDistance += s.distance ?? 0;
|
|
} else {
|
|
totalVolume += (s.weight ?? 0) * (s.reps ?? 0) * (isBilateral ? 2 : 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Detect PRs by comparing against previous best for each exercise
|
|
const prs: IPr[] = [];
|
|
for (const ex of exercises) {
|
|
const exercise = getExerciseById(ex.exerciseId);
|
|
const metrics = getExerciseMetrics(exercise);
|
|
const isCardio = metrics.includes('distance');
|
|
if (isCardio) continue;
|
|
|
|
const completedSets = (ex.sets ?? []).filter((s: { completed: boolean }) => s.completed);
|
|
if (completedSets.length === 0) continue;
|
|
|
|
// Find previous best for this exercise
|
|
const prevSessions = await WorkoutSession.find({
|
|
createdBy: session.user!.nickname,
|
|
'exercises.exerciseId': ex.exerciseId,
|
|
}).sort({ startTime: -1 }).limit(50).lean();
|
|
|
|
let prevBestWeight = 0;
|
|
let prevBestEst1rm = 0;
|
|
for (const ps of prevSessions) {
|
|
const pe = ps.exercises.find((e) => e.exerciseId === ex.exerciseId);
|
|
if (!pe) continue;
|
|
for (const s of pe.sets) {
|
|
if (!s.completed || !s.weight || !s.reps) continue;
|
|
prevBestWeight = Math.max(prevBestWeight, s.weight);
|
|
prevBestEst1rm = Math.max(prevBestEst1rm, estimatedOneRepMax(s.weight, s.reps));
|
|
}
|
|
}
|
|
|
|
let bestWeight = 0;
|
|
let bestEst1rm = 0;
|
|
for (const s of completedSets) {
|
|
if (!s.weight || !s.reps) continue;
|
|
bestWeight = Math.max(bestWeight, s.weight);
|
|
bestEst1rm = Math.max(bestEst1rm, estimatedOneRepMax(s.weight, s.reps));
|
|
}
|
|
|
|
if (bestWeight > prevBestWeight && prevBestWeight > 0) {
|
|
prs.push({ exerciseId: ex.exerciseId, type: 'maxWeight', value: bestWeight });
|
|
}
|
|
if (bestEst1rm > prevBestEst1rm && prevBestEst1rm > 0) {
|
|
prs.push({ exerciseId: ex.exerciseId, type: 'est1rm', value: bestEst1rm });
|
|
}
|
|
}
|
|
|
|
const workoutSession = new WorkoutSession({
|
|
templateId,
|
|
templateName,
|
|
name,
|
|
exercises,
|
|
startTime: startTime ? new Date(startTime) : new Date(),
|
|
endTime: endTime ? new Date(endTime) : undefined,
|
|
duration: endTime && startTime ? Math.round((new Date(endTime).getTime() - new Date(startTime).getTime()) / (1000 * 60)) : undefined,
|
|
totalVolume: totalVolume > 0 ? totalVolume : undefined,
|
|
totalDistance: totalDistance > 0 ? totalDistance : undefined,
|
|
prs: prs.length > 0 ? prs : undefined,
|
|
notes,
|
|
createdBy: session.user.nickname
|
|
});
|
|
|
|
await workoutSession.save();
|
|
|
|
return json({ session: workoutSession }, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Error creating workout session:', error);
|
|
return json({ error: 'Failed to create workout session' }, { status: 500 });
|
|
}
|
|
}; |