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 },
});
});

View File

@@ -610,17 +610,27 @@ export const openapi: Record<string, unknown> = {
'/plans/{id}/abandon': {
post: {
tags: ['Plans'],
summary: 'Abandon plan',
summary: 'Abandon plan (only from ACTIVE status)',
security: [{ bearerAuth: [] }],
parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }],
responses: {
200: { description: 'OK' },
400: { description: 'Only an active plan can be abandoned' },
404: { description: 'Plan not found' },
},
},
},
'/plans/{id}/complete': {
post: {
tags: ['Plans'],
summary: 'Manually archive plan',
summary: 'Manually archive plan (only from ACTIVE status)',
security: [{ bearerAuth: [] }],
parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }],
responses: {
200: { description: 'OK' },
400: { description: 'Only an active plan can be completed' },
404: { description: 'Plan not found' },
},
},
},
'/plans/{id}/share': {
@@ -670,9 +680,16 @@ export const openapi: Record<string, unknown> = {
'/days/{dayId}/sessions': {
post: {
tags: ['Sessions'],
summary: 'Start session for a planned day (one active session per user)',
summary:
'Start session for a planned day (requires ACTIVE plan, one active session per user)',
security: [{ bearerAuth: [] }],
parameters: [{ name: 'dayId', in: 'path', required: true, schema: { type: 'string' } }],
responses: {
201: { description: 'Session started' },
400: { description: 'Training plan is not active / Day is not in planned state' },
404: { description: 'Day not found' },
409: { description: 'You already have an active session' },
},
},
},
'/sessions/{id}': {
@@ -706,7 +723,8 @@ export const openapi: Record<string, unknown> = {
'/sessions/exercises/{id}': {
patch: {
tags: ['Sessions'],
summary: 'Hide or remove exercise (data kept for restore)',
summary:
'Hide or remove exercise (data kept for restore); removing allowed only for exercises added to the session',
security: [{ bearerAuth: [] }],
parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }],
requestBody: {
@@ -719,6 +737,11 @@ export const openapi: Record<string, unknown> = {
},
},
},
responses: {
200: { description: 'OK' },
400: { description: 'Only exercises added to the session can be removed' },
404: { description: 'Session exercise not found' },
},
},
},
'/sessions/exercises/{id}/sets': {