Record completed training sessions, credit them, and add session-count goals
A driver completing the practice timer, "Jämna varv", or "Stegträning" now gets a permanent history entry instead of the outcome only existing as a one-off notification - visible on both their mobile profile and the admin Training page, so progress over past sessions is actually reviewable. Each completion earns a prominent emoji-badged credit. The completion is verified server-side against the driver's real recorded laps before being saved, reusing the same progress builders live tracking already uses - the client can only ask for a completion to be checked and recorded, never supply the outcome itself, so a driver can't fabricate a completed session (and the reward tied to it) any more than they could fabricate a lap time. Both per-driver and club-wide goals can now target "complete N sessions of one exercise kind" as an alternative to "N laps", sharing the existing reward/claim mechanism and forms. Fixes a real bug found while building this: a pre-existing two-argument wrapper around getTrainingGoalProgress in app.js silently dropped the new third argument, so every sessions-metric goal displayed 0 progress regardless of how many sessions were actually completed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bed0dfd9f2
commit
1c4a06b69e
14 files changed
+800
-52
No files matched your search
+102
-15
@@ -20,30 +20,46 @@ function getTrainingDriverIds(state, driverId, classId = "") {
|
||||
export function normalizeTrainingState(training, { uid }) {
|
||||
const source = training && typeof training === "object" ? training : {};
|
||||
const clubGoalSource = source.clubGoal && typeof source.clubGoal === "object" ? source.clubGoal : null;
|
||||
const clubGoal = clubGoalSource && Number(clubGoalSource.targetLaps || 0) > 0
|
||||
const clubGoalMetric = String(clubGoalSource?.metric || "") === "sessions" ? "sessions" : "laps";
|
||||
const clubGoalExerciseKind = ["timer", "evenLaps", "stepTraining"].includes(String(clubGoalSource?.exerciseKind || "")) ? String(clubGoalSource.exerciseKind) : "";
|
||||
const clubGoalHasTarget = clubGoalSource && (clubGoalMetric === "sessions"
|
||||
? Number(clubGoalSource.targetSessions || 0) > 0 && clubGoalExerciseKind
|
||||
: Number(clubGoalSource.targetLaps || 0) > 0);
|
||||
const clubGoal = clubGoalHasTarget
|
||||
? {
|
||||
id: String(clubGoalSource.id || uid("club_goal")),
|
||||
title: String(clubGoalSource.title || "").trim(),
|
||||
reward: String(clubGoalSource.reward || "").trim(),
|
||||
targetLaps: Math.max(1, Math.round(Number(clubGoalSource.targetLaps || 0) || 0)),
|
||||
baselineLaps: Math.max(0, Math.round(Number(clubGoalSource.baselineLaps || 0) || 0)),
|
||||
metric: clubGoalMetric,
|
||||
exerciseKind: clubGoalMetric === "sessions" ? clubGoalExerciseKind : "",
|
||||
targetSessions: Math.max(1, Math.round(Number(clubGoalSource.targetSessions || 0) || 0)),
|
||||
baselineSessions: Math.max(0, Math.round(Number(clubGoalSource.baselineSessions || 0) || 0)),
|
||||
createdAt: String(clubGoalSource.createdAt || new Date().toISOString()),
|
||||
celebratedAt: clubGoalSource.celebratedAt ? String(clubGoalSource.celebratedAt) : "",
|
||||
}
|
||||
: null;
|
||||
const goals = Array.isArray(source.goals)
|
||||
? source.goals
|
||||
.map((goal) => ({
|
||||
id: String(goal?.id || uid("goal")),
|
||||
driverId: String(goal?.driverId || ""),
|
||||
classId: String(goal?.classId || ""),
|
||||
title: String(goal?.title || "").trim(),
|
||||
reward: String(goal?.reward || "").trim(),
|
||||
targetLaps: Math.max(1, Math.round(Number(goal?.targetLaps || 0) || 0)),
|
||||
createdAt: String(goal?.createdAt || new Date().toISOString()),
|
||||
claimedAt: goal?.claimedAt ? String(goal.claimedAt) : "",
|
||||
}))
|
||||
.filter((goal) => goal.driverId && goal.targetLaps > 0)
|
||||
.map((goal) => {
|
||||
const metric = String(goal?.metric || "") === "sessions" ? "sessions" : "laps";
|
||||
const exerciseKind = ["timer", "evenLaps", "stepTraining"].includes(String(goal?.exerciseKind || "")) ? String(goal.exerciseKind) : "";
|
||||
return {
|
||||
id: String(goal?.id || uid("goal")),
|
||||
driverId: String(goal?.driverId || ""),
|
||||
classId: String(goal?.classId || ""),
|
||||
title: String(goal?.title || "").trim(),
|
||||
reward: String(goal?.reward || "").trim(),
|
||||
targetLaps: Math.max(1, Math.round(Number(goal?.targetLaps || 0) || 0)),
|
||||
metric,
|
||||
exerciseKind: metric === "sessions" ? exerciseKind : "",
|
||||
targetSessions: Math.max(1, Math.round(Number(goal?.targetSessions || 0) || 0)),
|
||||
createdAt: String(goal?.createdAt || new Date().toISOString()),
|
||||
claimedAt: goal?.claimedAt ? String(goal.claimedAt) : "",
|
||||
};
|
||||
})
|
||||
.filter((goal) => goal.driverId && (goal.metric === "sessions" ? goal.exerciseKind && goal.targetSessions > 0 : goal.targetLaps > 0))
|
||||
: [];
|
||||
|
||||
const seenLedgerKeys = new Set();
|
||||
@@ -156,7 +172,29 @@ export function normalizeTrainingState(training, { uid }) {
|
||||
})
|
||||
: [];
|
||||
|
||||
return { goals, ledger, shares, markers, clubGoal, evenLaps, stepTraining };
|
||||
const seenExerciseSessions = new Set();
|
||||
const exerciseSessions = Array.isArray(source.exerciseSessions)
|
||||
? source.exerciseSessions
|
||||
.map((entry) => ({
|
||||
id: String(entry?.id || uid("exercise_session")),
|
||||
driverId: String(entry?.driverId || ""),
|
||||
classId: String(entry?.classId || ""),
|
||||
kind: ["timer", "evenLaps", "stepTraining"].includes(String(entry?.kind || "")) ? String(entry.kind) : "",
|
||||
startedAt: Math.max(0, Number(entry?.startedAt || 0) || 0),
|
||||
completedAt: String(entry?.completedAt || new Date().toISOString()),
|
||||
summary: entry?.summary && typeof entry.summary === "object" ? entry.summary : {},
|
||||
}))
|
||||
.filter((entry) => {
|
||||
if (!entry.driverId || !entry.classId || !entry.kind || !entry.startedAt) return false;
|
||||
const key = `${entry.driverId}:${entry.classId}:${entry.kind}:${entry.startedAt}`;
|
||||
if (seenExerciseSessions.has(key)) return false;
|
||||
seenExerciseSessions.add(key);
|
||||
return true;
|
||||
})
|
||||
.sort((left, right) => right.startedAt - left.startedAt)
|
||||
: [];
|
||||
|
||||
return { goals, ledger, shares, markers, clubGoal, evenLaps, stepTraining, exerciseSessions };
|
||||
}
|
||||
|
||||
function getPassingSourceKey(sessionId, passing, index) {
|
||||
@@ -954,7 +992,42 @@ export function buildTrainingRecommendation(stats) {
|
||||
};
|
||||
}
|
||||
|
||||
export function getTrainingGoalProgress(goal, stats) {
|
||||
// Goals and the club goal share this same {currentLaps, targetLaps, ...} shape whether they
|
||||
// track laps or completed guided-exercise sessions, so every existing progress-bar/reward
|
||||
// renderer keeps working unchanged - callers branch on goal.metric only to pick the right
|
||||
// label ("laps" vs "sessions"), never to reshape this result.
|
||||
export function countExerciseSessions(exerciseSessions, { driverId = "", classId = "", kind = "" } = {}) {
|
||||
return (Array.isArray(exerciseSessions) ? exerciseSessions : []).filter((entry) =>
|
||||
(!driverId || entry.driverId === driverId) &&
|
||||
(!classId || entry.classId === classId) &&
|
||||
(!kind || entry.kind === kind)
|
||||
).length;
|
||||
}
|
||||
|
||||
// Admin goals store driverId as the *profile* id (see the "training-goal-form" submit handler
|
||||
// in training_views.js), the same value buildDriverTrainingStats resolves through
|
||||
// getTrainingDriverIds - but exerciseSessions store the specific per-class driver row id
|
||||
// (whichever the mobile link's completion request resolved to), the same as ledger entries do.
|
||||
// This bridges the two the same way buildDriverTrainingStats already does for laps.
|
||||
export function countDriverExerciseSessions(state, driverId, classId, kind) {
|
||||
const driverIds = getTrainingDriverIds(state, driverId, classId);
|
||||
return (Array.isArray(state?.training?.exerciseSessions) ? state.training.exerciseSessions : [])
|
||||
.filter((entry) => driverIds.has(String(entry?.driverId || "")) && String(entry?.classId || "") === String(classId || "") && entry?.kind === kind)
|
||||
.length;
|
||||
}
|
||||
|
||||
export function getTrainingGoalProgress(goal, stats, exerciseSessionCount = 0) {
|
||||
if (goal?.metric === "sessions") {
|
||||
const targetSessions = Math.max(1, Number(goal?.targetSessions || 1));
|
||||
const currentSessions = Math.max(0, Number(exerciseSessionCount || 0));
|
||||
return {
|
||||
currentLaps: currentSessions,
|
||||
targetLaps: targetSessions,
|
||||
remainingLaps: Math.max(0, targetSessions - currentSessions),
|
||||
percent: Math.min(100, Math.max(0, (currentSessions / targetSessions) * 100)),
|
||||
reached: currentSessions >= targetSessions,
|
||||
};
|
||||
}
|
||||
const targetLaps = Math.max(1, Number(goal?.targetLaps || 1));
|
||||
const currentLaps = Math.max(0, Number(stats?.totalLaps || 0));
|
||||
return {
|
||||
@@ -1008,7 +1081,21 @@ export function buildTrainingHighlights(stats) {
|
||||
|
||||
export function getClubTrainingGoalProgress(training) {
|
||||
const goal = training?.clubGoal && typeof training.clubGoal === "object" ? training.clubGoal : null;
|
||||
if (!goal || Number(goal.targetLaps || 0) <= 0) return null;
|
||||
if (!goal) return null;
|
||||
if (goal.metric === "sessions") {
|
||||
if (!(Number(goal.targetSessions || 0) > 0) || !goal.exerciseKind) return null;
|
||||
const totalSessions = countExerciseSessions(training?.exerciseSessions, { kind: goal.exerciseKind });
|
||||
const currentSessions = Math.max(0, totalSessions - Math.max(0, Number(goal.baselineSessions || 0)));
|
||||
const targetSessions = Math.max(1, Number(goal.targetSessions || 1));
|
||||
return {
|
||||
currentLaps: currentSessions,
|
||||
targetLaps: targetSessions,
|
||||
remainingLaps: Math.max(0, targetSessions - currentSessions),
|
||||
percent: Math.min(100, Math.max(0, (currentSessions / targetSessions) * 100)),
|
||||
reached: currentSessions >= targetSessions,
|
||||
};
|
||||
}
|
||||
if (!(Number(goal.targetLaps || 0) > 0)) return null;
|
||||
const totalLaps = Array.isArray(training?.ledger) ? training.ledger.length : 0;
|
||||
const currentLaps = Math.max(0, totalLaps - Math.max(0, Number(goal.baselineLaps || 0)));
|
||||
const targetLaps = Math.max(1, Number(goal.targetLaps || 1));
|
||||
|
||||
Reference in new issue
Block a user