Api: dzien z aktywna sesja nie wygasa o polnocy (trening nie jest przerywany; twardym limitem pozostaje 24h)

This commit is contained in:
Tilumca
2026-10-14 21:14:23 +02:00
parent 9937074577
commit c3f937f621
3 changed files with 26 additions and 9 deletions

View File

@@ -3,11 +3,13 @@ import { logger } from '../lib/logger.js';
/**
* Background jobs (every 60s):
* 1. Auto-expire training days whose date has passed -> skipped.
* 1. Auto-expire training days whose date has passed -> skipped, but only while
* they have no in-progress session (midnight must not kill a running training).
* 2. Abandon in-progress sessions belonging to expired days (the day is closed,
* the training can no longer be completed).
* 3. Auto-abandon sessions after 24h; the day they belong to is skipped too
* (same rule as manual abandonment — an abandoned training is not retryable).
* 3. Auto-abandon sessions after 24h from start — the real hard limit for a
* running session; the day they belong to is skipped too (same rule as manual
* abandonment — an abandoned training is not retryable).
* 4. Auto-complete active plans when all days are completed/skipped.
* 5. Remove gym schedule exceptions whose date is in the past.
*/
@@ -17,13 +19,22 @@ export function startScheduler(prisma: PrismaClient): void {
const now = new Date();
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
// Dzień z aktywną sesją IN_PROGRESS nie wygasa — inaczej północ przerwałaby
// trening w trakcie (sesja została porzucona, a jej ukończenie zwracałoby 409).
// Taki dzień domknie reguła 24h (porzucenie sesji + skip dnia) albo ukończenie
// sesji, które może nastąpić już po północy, gdy dzień wciąż jest PLANNED.
const expired = await prisma.planDay.updateMany({
where: { status: 'PLANNED', date: { lt: startOfToday } },
where: {
status: 'PLANNED',
date: { lt: startOfToday },
sessions: { none: { status: 'IN_PROGRESS' } },
},
data: { status: 'SKIPPED', skippedAt: now },
});
if (expired.count > 0) logger.info({ count: expired.count }, 'Auto-expired days -> skipped');
// Sesja dnia, który wygasł, nigdy nie zostanie już ukończona — porządek natychmiast.
// Sieć bezpieczeństwa: sesja dnia, który zdążył wygasnąć (np. stan sprzed tej
// zmiany), nigdy nie zostanie już ukończona — porządek natychmiast.
const orphaned = await prisma.trainingSession.updateMany({
where: { status: 'IN_PROGRESS', day: { status: 'SKIPPED' } },
data: { status: 'ABANDONED', endedAt: now },