From 6cf71e4c157b8413e88046b6b82da67fb2a82da1 Mon Sep 17 00:00:00 2001 From: tisachi Date: Sun, 11 Oct 2026 16:17:35 +0200 Subject: [PATCH] Sesje: start treningu tylko dla dni aktywnego planu (DRAFT/ABANDONED -> 400) --- api/src/services/sessions.ts | 2 ++ api/test/integration/sessions.test.ts | 17 +++++++++++++++++ api/test/unit/services-sessions.test.ts | 14 +++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/api/src/services/sessions.ts b/api/src/services/sessions.ts index c149dd0..151d178 100644 --- a/api/src/services/sessions.ts +++ b/api/src/services/sessions.ts @@ -45,6 +45,8 @@ export async function startSession(prisma: PrismaClient, dayId: string, userId: }); if (!day || day.week.plan.userId !== userId) throw notFound('Day not found'); if (day.status !== 'PLANNED') throw badRequest('Day is not in planned state'); + // dzień należy do planu — trening ma sens tylko w aktywnym planie (bez DRAFT/ABANDONED/COMPLETED) + if (day.week.plan.status !== 'ACTIVE') throw badRequest('Training plan is not active'); // Szybka ścieżka — rozstrzygające sprawdzenie jest w transakcji (pod blokadą). const active = await prisma.trainingSession.findFirst({ where: { status: 'IN_PROGRESS', day: { week: { plan: { userId } } } }, diff --git a/api/test/integration/sessions.test.ts b/api/test/integration/sessions.test.ts index 536b7ac..1816de3 100644 --- a/api/test/integration/sessions.test.ts +++ b/api/test/integration/sessions.test.ts @@ -70,6 +70,23 @@ describe('sessions routes', () => { expect(ex.sets.every((s: { reps: number | null }) => s.reps === null)).toBe(true); }); + it('rejects starting a session on a day of a non-active (DRAFT) plan with 400', async () => { + const app = buildApp(); + const { user } = await createUser(); + const draftToken = (await loginViaApi(user.email, PASSWORD)).token; + const draft = await seedPlan({ + userId: user.id, + status: 'DRAFT', + weeks: [{ index: 0, days: [{ dayOfWeek: 1, exercises: [] }] }], + }); + const draftDayId = draft.weeks[0].days[0].id; + const res = await app + .post(`/api/v1/days/${draftDayId}/sessions`) + .set('Authorization', `Bearer ${draftToken}`); + expect(res.status).toBe(400); + expect(res.body.error.message).toBe('Training plan is not active'); + }); + it('rejects a second active session for the same day with 409', async () => { const app = buildApp(); const res = await app.post(`/api/v1/days/${dayId}/sessions`).set('Authorization', `Bearer ${token}`); diff --git a/api/test/unit/services-sessions.test.ts b/api/test/unit/services-sessions.test.ts index 5ca7add..eca1fd7 100644 --- a/api/test/unit/services-sessions.test.ts +++ b/api/test/unit/services-sessions.test.ts @@ -42,7 +42,7 @@ function mockPrisma() { const ownedDay = { id: 'd1', status: 'PLANNED', - week: { plan: { userId: 'u1' } }, + week: { plan: { userId: 'u1', status: 'ACTIVE' } }, exercises: [ { id: 'pe1', exerciseId: 'pz1', order: 0, sets: [{ id: 'ps1', index: 0 }] }, { id: 'pe2', exerciseId: 'pz2', order: 1, sets: [] }, @@ -71,6 +71,18 @@ describe('startSession', () => { }); }); + it('400 when the plan behind the day is not ACTIVE', async () => { + const prisma = mockPrisma(); + prisma.planDay.findUnique.mockResolvedValue({ + ...ownedDay, + week: { plan: { userId: 'u1', status: 'DRAFT' } }, + }); + await expect(startSession(prisma, 'd1', 'u1')).rejects.toMatchObject({ + status: 400, + message: 'Training plan is not active', + }); + }); + it('409 when the user already has an active session', async () => { const prisma = mockPrisma(); prisma.planDay.findUnique.mockResolvedValue(ownedDay);