110 lines
4.1 KiB
TypeScript
110 lines
4.1 KiB
TypeScript
import type { PrismaClient } from '@prisma/client';
|
|
|
|
export interface EffectiveSession {
|
|
exercises: {
|
|
hidden: boolean;
|
|
removed: boolean;
|
|
planExercise: { sets: { id: string }[] } | null;
|
|
sets: { planSetId: string | null; reps: number | null }[];
|
|
}[];
|
|
}
|
|
|
|
/**
|
|
* A training is "effective" (counts toward killstreak) when the user performed
|
|
* the session: every planned (norm) set has reps recorded, and every exercise
|
|
* added beyond the plan has at least one recorded set.
|
|
* Extra (bonus) sets are exactly that — a bonus: recorded or not, they never
|
|
* affect effectiveness (same rule as completing the session, which extras
|
|
* never block). Only REMOVED exercises drop out of the check; hiding is
|
|
* cosmetic and never "forgives" missing sets (same as completing the session,
|
|
* which blocks on unrecorded sets of hidden exercises too).
|
|
*/
|
|
export function isSessionEffective(session: EffectiveSession): boolean {
|
|
for (const ex of session.exercises) {
|
|
if (ex.removed) continue;
|
|
if (ex.planExercise) {
|
|
for (const ps of ex.planExercise.sets) {
|
|
const done = ex.sets.some((s) => s.planSetId === ps.id && s.reps != null);
|
|
if (!done) return false;
|
|
}
|
|
} else {
|
|
// Ćwiczenie dodane: bez ani jednej zapisanej serii trening się nie liczy.
|
|
if (!ex.sets.some((s) => s.reps != null)) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Killstreak = number of consecutive effective trainings (regardless of day).
|
|
* Grows by 1 for each effective training; resets to 0 after a skip
|
|
* (including auto-expired days) or a partially completed day.
|
|
* Counts days of ACTIVE, SUSPENDED and COMPLETED plans — finishing a plan does
|
|
* not wipe the streak built the day before its end, and a suspended plan keeps
|
|
* its history (pause must not erase the streak built while it was active).
|
|
*/
|
|
export async function getKillstreak(prisma: PrismaClient, userId: string): Promise<number> {
|
|
return (await getKillstreakStats(prisma, userId)).current;
|
|
}
|
|
|
|
/**
|
|
* Jak getKillstreak, ale z jednym przebiegiem historii liczy też
|
|
* `best` — najlepszą passę z całej dotychczasowej historii (te same
|
|
* zasady skuteczności i resetów). Gdy podano `effectiveSince`, dodatkowo
|
|
* zwraca `effectiveDates` — daty (PlanDay.date) dni ze skutecznym treningiem
|
|
* od tej chwili włącznie (np. okno 30 dni na wykres kropek).
|
|
*/
|
|
export async function getKillstreakStats(
|
|
prisma: PrismaClient,
|
|
userId: string,
|
|
effectiveSince?: Date,
|
|
): Promise<{ current: number; best: number; effectiveDates: Date[] }> {
|
|
const days = await prisma.planDay.findMany({
|
|
where: {
|
|
week: { plan: { userId, status: { in: ['ACTIVE', 'SUSPENDED', 'COMPLETED'] } } },
|
|
removedAt: null,
|
|
date: { not: null },
|
|
},
|
|
orderBy: { date: 'asc' },
|
|
include: {
|
|
sessions: {
|
|
where: { status: 'COMPLETED' },
|
|
// dzień może mieć wiele ukończonych sesji (po porzuconych) — liczy się najnowsza
|
|
orderBy: { endedAt: 'desc' },
|
|
include: {
|
|
exercises: {
|
|
include: {
|
|
sets: { select: { planSetId: true, reps: true } },
|
|
planExercise: { include: { sets: { select: { id: true } } } },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const now = new Date();
|
|
const startOfTomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
|
|
let streak = 0;
|
|
let best = 0;
|
|
const effectiveDates: Date[] = [];
|
|
|
|
for (const day of days) {
|
|
if (day.date! >= startOfTomorrow) break; // future day — not yet relevant
|
|
if (day.status === 'COMPLETED') {
|
|
const session = day.sessions[0];
|
|
if (session && isSessionEffective(session)) {
|
|
streak++; // effective training
|
|
if (streak > best) best = streak;
|
|
if (effectiveSince && day.date! >= effectiveSince) effectiveDates.push(day.date!);
|
|
} else {
|
|
streak = 0; // partially completed day resets the streak
|
|
}
|
|
} else if (day.status === 'SKIPPED') {
|
|
streak = 0; // skipped (manual or auto-expired) resets the streak
|
|
}
|
|
// PLANNED days (today, not yet done) do not change the streak
|
|
}
|
|
return { current: streak, best, effectiveDates };
|
|
}
|