213 lines
7.0 KiB
TypeScript
213 lines
7.0 KiB
TypeScript
import type { PrismaClient } from '@prisma/client';
|
||
import { assertActiveTrainee } from './trainer.js';
|
||
|
||
export interface ExerciseProgressPoint {
|
||
date: string;
|
||
oneRm: number | null;
|
||
volumeKg: number | null;
|
||
reps: number;
|
||
sets: number;
|
||
}
|
||
|
||
export interface ExerciseProgress {
|
||
exerciseId: string;
|
||
name: string;
|
||
muscle: string;
|
||
/** Ascending, one point per COMPLETED session. */
|
||
points: ExerciseProgressPoint[];
|
||
}
|
||
|
||
export interface RawSet {
|
||
reps: number | null;
|
||
loadKg: number | null;
|
||
}
|
||
|
||
export interface RawExercise {
|
||
exerciseId: string;
|
||
name: string;
|
||
muscle: string;
|
||
sets: RawSet[];
|
||
}
|
||
|
||
export interface RawSession {
|
||
date: string;
|
||
exercises: RawExercise[];
|
||
}
|
||
|
||
/**
|
||
* Pure aggregation (unit-testable): one point per session per exercise.
|
||
* - oneRm = max(loadKg × (1 + reps/30)) across sets with both values (Epley);
|
||
* - volumeKg = Σ(loadKg × reps) across sets with both values;
|
||
* - reps = Σ reps; sets = count of sets with reps.
|
||
*/
|
||
export function aggregateExerciseProgress(sessions: RawSession[]): ExerciseProgress[] {
|
||
const byExercise = new Map<string, { name: string; muscle: string; points: ExerciseProgressPoint[] }>();
|
||
for (const s of sessions) {
|
||
for (const ex of s.exercises) {
|
||
let oneRm: number | null = null;
|
||
let volumeKg: number | null = null;
|
||
let reps = 0;
|
||
let sets = 0;
|
||
for (const st of ex.sets) {
|
||
if (st.reps == null) continue;
|
||
reps += st.reps;
|
||
sets += 1;
|
||
if (st.loadKg != null) {
|
||
const epley = st.loadKg * (1 + st.reps / 30);
|
||
if (oneRm == null || epley > oneRm) oneRm = epley;
|
||
volumeKg = (volumeKg ?? 0) + st.loadKg * st.reps;
|
||
}
|
||
}
|
||
const entry = byExercise.get(ex.exerciseId) ?? { name: ex.name, muscle: ex.muscle, points: [] };
|
||
entry.points.push({
|
||
date: s.date,
|
||
oneRm: oneRm == null ? null : Math.round(oneRm * 10) / 10,
|
||
volumeKg: volumeKg == null ? null : Math.round(volumeKg),
|
||
reps,
|
||
sets,
|
||
});
|
||
byExercise.set(ex.exerciseId, entry);
|
||
}
|
||
}
|
||
return [...byExercise.entries()]
|
||
.filter(([, v]) => v.points.length > 0)
|
||
.map(([exerciseId, v]) => ({
|
||
exerciseId,
|
||
name: v.name,
|
||
muscle: v.muscle,
|
||
points: [...v.points].sort((a, b) => a.date.localeCompare(b.date)),
|
||
}));
|
||
}
|
||
|
||
export interface LatestExerciseSet {
|
||
reps: number | null;
|
||
loadKg: number | null;
|
||
}
|
||
|
||
export interface LatestExerciseResult {
|
||
exerciseId: string;
|
||
name: string;
|
||
muscle: string;
|
||
/** ISO date of the most recent COMPLETED session that included the exercise. */
|
||
date: string;
|
||
/** Sets of that session (reps/loadKg, null when unrecorded) — for plan prefill. */
|
||
sets: LatestExerciseSet[];
|
||
/** Epley 1RM max across the session's sets (null when no set has both values). */
|
||
oneRm: number | null;
|
||
}
|
||
|
||
/**
|
||
* Pure aggregation (unit-testable): for each exercise, the most recent session
|
||
* (by date). oneRm = max(loadKg × (1 + reps/30)) across sets with both values;
|
||
* sets are returned raw (reps/loadKg, null when unrecorded) for plan prefill.
|
||
*/
|
||
export function latestExerciseResults(sessions: RawSession[]): LatestExerciseResult[] {
|
||
const latest = new Map<string, { name: string; muscle: string; date: string; sets: RawSet[] }>();
|
||
for (const s of sessions) {
|
||
for (const ex of s.exercises) {
|
||
const existing = latest.get(ex.exerciseId);
|
||
if (!existing || s.date > existing.date) {
|
||
latest.set(ex.exerciseId, { name: ex.name, muscle: ex.muscle, date: s.date, sets: ex.sets });
|
||
}
|
||
}
|
||
}
|
||
return [...latest.entries()].map(([exerciseId, v]) => {
|
||
let oneRm: number | null = null;
|
||
for (const st of v.sets) {
|
||
if (st.reps == null || st.loadKg == null) continue;
|
||
const epley = st.loadKg * (1 + st.reps / 30);
|
||
if (oneRm == null || epley > oneRm) oneRm = epley;
|
||
}
|
||
return {
|
||
exerciseId,
|
||
name: v.name,
|
||
muscle: v.muscle,
|
||
date: v.date,
|
||
sets: v.sets.map((st) => ({ reps: st.reps, loadKg: st.loadKg })),
|
||
oneRm: oneRm == null ? null : Math.round(oneRm * 10) / 10,
|
||
};
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Per-exercise progress for the user: all COMPLETED sessions (no 30-session
|
||
* limit), exercises and sets not removed/hidden.
|
||
*/
|
||
export async function getExerciseProgress(prisma: PrismaClient, userId: string) {
|
||
const sessions = await prisma.trainingSession.findMany({
|
||
where: { status: 'COMPLETED', day: { week: { plan: { userId } } } },
|
||
orderBy: { startedAt: 'asc' },
|
||
include: {
|
||
exercises: {
|
||
where: { removed: false, hidden: false },
|
||
orderBy: { order: 'asc' },
|
||
include: {
|
||
exercise: { select: { id: true, name: true, primaryMuscle: true } },
|
||
sets: {
|
||
where: { removed: false },
|
||
orderBy: { index: 'asc' },
|
||
include: { planSet: { select: { loadKg: true } } },
|
||
},
|
||
},
|
||
},
|
||
},
|
||
});
|
||
const raw: RawSession[] = sessions.map((s) => ({
|
||
date: s.startedAt.toISOString(),
|
||
exercises: s.exercises.map((e) => ({
|
||
exerciseId: e.exercise.id,
|
||
name: e.exercise.name,
|
||
muscle: e.exercise.primaryMuscle,
|
||
sets: e.sets.map((st) => ({ reps: st.reps, loadKg: st.planSet?.loadKg ?? null })),
|
||
})),
|
||
}));
|
||
return { exercises: aggregateExerciseProgress(raw) };
|
||
}
|
||
|
||
/** Per-exercise progress for the trainee (trainer must have an ACTIVE relation). */
|
||
export async function getTraineeExerciseProgress(prisma: PrismaClient, trainerId: string, userId: string) {
|
||
await assertActiveTrainee(prisma, trainerId, userId);
|
||
return getExerciseProgress(prisma, userId);
|
||
}
|
||
|
||
/**
|
||
* Most recent COMPLETED session per exercise (exercises/sets not removed/hidden),
|
||
* with the raw sets for plan prefill. Same query shape as getExerciseProgress.
|
||
*/
|
||
export async function getLatestExerciseResults(prisma: PrismaClient, userId: string) {
|
||
const sessions = await prisma.trainingSession.findMany({
|
||
where: { status: 'COMPLETED', day: { week: { plan: { userId } } } },
|
||
orderBy: { startedAt: 'asc' },
|
||
include: {
|
||
exercises: {
|
||
where: { removed: false, hidden: false },
|
||
orderBy: { order: 'asc' },
|
||
include: {
|
||
exercise: { select: { id: true, name: true, primaryMuscle: true } },
|
||
sets: {
|
||
where: { removed: false },
|
||
orderBy: { index: 'asc' },
|
||
include: { planSet: { select: { loadKg: true } } },
|
||
},
|
||
},
|
||
},
|
||
},
|
||
});
|
||
const raw: RawSession[] = sessions.map((s) => ({
|
||
date: s.startedAt.toISOString(),
|
||
exercises: s.exercises.map((e) => ({
|
||
exerciseId: e.exercise.id,
|
||
name: e.exercise.name,
|
||
muscle: e.exercise.primaryMuscle,
|
||
sets: e.sets.map((st) => ({ reps: st.reps, loadKg: st.planSet?.loadKg ?? null })),
|
||
})),
|
||
}));
|
||
return { results: latestExerciseResults(raw) };
|
||
}
|
||
|
||
/** Latest results for the trainee (trainer must have an ACTIVE relation). */
|
||
export async function getTraineeLatestResults(prisma: PrismaClient, trainerId: string, userId: string) {
|
||
await assertActiveTrainee(prisma, trainerId, userId);
|
||
return getLatestExerciseResults(prisma, userId);
|
||
}
|