Plany i sesje: porzucenie/konczenie tylko z ACTIVE, przyszle dni bez skip, usuwanie tylko cwiczen dodanych do sesji

This commit is contained in:
tisachi
2026-10-11 16:34:47 +02:00
parent 42798939a5
commit cfac9dadd0
10 changed files with 415 additions and 86 deletions

View File

@@ -387,8 +387,8 @@ async function applyStartDate(
export async function abandonPlan(prisma: PrismaClient, planId: string, userId: string) {
const plan = await assertPlanAccess(prisma, planId, userId);
if (plan.status === 'COMPLETED' || plan.status === 'ABANDONED')
throw badRequest('Plan is already closed');
// Tylko aktywny plan można porzucić — DRAFT kończy się przez edycję/usunięcie.
if (plan.status !== 'ACTIVE') throw badRequest('Only an active plan can be abandoned');
await prisma.trainingPlan.update({
where: { id: plan.id },
data: { status: 'ABANDONED' },
@@ -398,8 +398,8 @@ export async function abandonPlan(prisma: PrismaClient, planId: string, userId:
export async function completePlan(prisma: PrismaClient, planId: string, userId: string) {
const plan = await assertPlanAccess(prisma, planId, userId);
if (plan.status === 'COMPLETED' || plan.status === 'ABANDONED')
throw badRequest('Plan is already closed');
// Tylko aktywny plan można ukończyć — DRAFT nie zaczął się jeszcze realizować.
if (plan.status !== 'ACTIVE') throw badRequest('Only an active plan can be completed');
await prisma.trainingPlan.update({
where: { id: plan.id },
data: { status: 'COMPLETED' },

View File

@@ -42,9 +42,15 @@ export function startScheduler(prisma: PrismaClient): void {
data: { status: 'ABANDONED', endedAt: now },
});
// Porzucona sesja nie może pozwalać na ponowne wykonanie dnia —
// dokładnie tak samo jak przy ręcznym porzuceniu sesji.
// dokładnie tak samo jak przy ręcznym porzuceniu sesji. Dni z przyszłą
// datą zostają PLANNED (sesja startowana wczoraj nie skreśla jutra).
const startOfTomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
await prisma.planDay.updateMany({
where: { id: { in: [...new Set(stale.map((s) => s.dayId))] }, status: 'PLANNED' },
where: {
id: { in: [...new Set(stale.map((s) => s.dayId))] },
status: 'PLANNED',
date: { lt: startOfTomorrow },
},
data: { status: 'SKIPPED', skippedAt: now },
});
logger.info({ count: stale.length }, 'Auto-abandoned sessions (24h)');

View File

@@ -200,6 +200,11 @@ export async function updateExercise(
) {
const exercise = await getExerciseOwned(prisma, exerciseId, userId);
if (exercise.session.status !== 'IN_PROGRESS') throw badRequest('Session is not in progress');
// Ćwiczenie z planu można ukryć, ale nie usunąć — usuwanie nie może „wybaczać"
// nieodpisanych serii planowych; kasować wolno tylko ćwiczenia dodane do sesji.
if (data.removed === true && exercise.planExerciseId != null) {
throw badRequest('Only exercises added to the session can be removed');
}
await prisma.sessionExercise.update({
where: { id: exercise.id },
data,
@@ -322,6 +327,9 @@ export async function abandonSession(prisma: PrismaClient, sessionId: string, us
const session = await getSessionOwned(prisma, sessionId, userId);
if (session.status !== 'IN_PROGRESS') throw badRequest('Session is not in progress');
const now = new Date();
// Dzień w przyszłości nie zostaje „skipnięty" przez porzuconą sesję — wraca do
// PLANNED i da się go wykonać normalnie; SKIPPED tylko dla dni, które nadszedł czas.
const startOfTomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
await prisma.$transaction(async (tx) => {
const s = await tx.trainingSession.updateMany({
where: { id: session.id, status: 'IN_PROGRESS' },
@@ -331,7 +339,7 @@ export async function abandonSession(prisma: PrismaClient, sessionId: string, us
// Dzień oznaczamy jako pominięty tylko gdy wciąż jest zaplanowany
// (mógł już wygasnąć w schedulerze — wtedy nie nadpisujemy).
await tx.planDay.updateMany({
where: { id: session.dayId, status: 'PLANNED' },
where: { id: session.dayId, status: 'PLANNED', date: { lt: startOfTomorrow } },
data: { status: 'SKIPPED', skippedAt: now },
});
});