Sesje: start treningu tylko dla dni aktywnego planu (DRAFT/ABANDONED -> 400)

This commit is contained in:
tisachi
2026-10-11 16:17:35 +02:00
parent f505fa1910
commit 6cf71e4c15
3 changed files with 32 additions and 1 deletions

View File

@@ -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 } } } },

View File

@@ -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}`);

View File

@@ -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);