diff --git a/api/src/routes/plans-schemas.ts b/api/src/routes/plans-schemas.ts index d87b06b..0cf949e 100644 --- a/api/src/routes/plans-schemas.ts +++ b/api/src/routes/plans-schemas.ts @@ -11,23 +11,38 @@ export const planSetSchema = z.object({ speed: z.number().positive().max(100).nullable().optional(), }); -const planExerciseSchema = z.object({ - exerciseId: z.string().min(1), - order: z.number().int().min(0), - sets: z.array(planSetSchema).min(1), -}); +const planExerciseSchema = z + .object({ + exerciseId: z.string().min(1), + order: z.number().int().min(0), + sets: z.array(planSetSchema).min(1), + }) + // duplikat indexu serii łoby na @@unique([exerciseId, index]) (500) — odrzucamy wcześniej + .refine((ex) => new Set(ex.sets.map((s) => s.index)).size === ex.sets.length, { + message: 'set index must be unique within an exercise', + }); -const planDaySchema = z.object({ - dayOfWeek: z.number().int().min(1).max(7), - exercises: z.array(planExerciseSchema), -}); +const planDaySchema = z + .object({ + dayOfWeek: z.number().int().min(1).max(7), + exercises: z.array(planExerciseSchema), + }) + // duplikat order ćwiczenia łoby na @@unique([dayId, order]) (500) + .refine((d) => new Set(d.exercises.map((e) => e.order)).size === d.exercises.length, { + message: 'exercise order must be unique within a day', + }); -const planWeekSchema = z.object({ - index: z.number().int().min(0), - // Opcjonalny alias tygodnia (np. „Deload"). - customName: z.string().max(40).nullable().optional(), - days: z.array(planDaySchema), -}); +const planWeekSchema = z + .object({ + index: z.number().int().min(0), + // Opcjonalny alias tygodnia (np. „Deload"). + customName: z.string().max(40).nullable().optional(), + days: z.array(planDaySchema), + }) + // w jednym tygodniu jeden dzień tygodnia maksymalnie raz (kalendarz/scheduler zakładają 1:1) + .refine((w) => new Set(w.days.map((d) => d.dayOfWeek)).size === w.days.length, { + message: 'dayOfWeek must be unique within a week', + }); const planBaseSchema = z.object({ title: z.string().min(1).max(200), @@ -37,6 +52,13 @@ const planBaseSchema = z.object({ weeks: z.array(planWeekSchema), }); +// indeksy tygodni: unikalne i dokładnie 0..weeksCount-1 (duplikat łoby na @@unique([planId, index])) +function weeksIndexShapeOk(d: { weeksCount: number; weeks: { index: number }[] }): boolean { + if (d.weeks.length !== d.weeksCount) return false; + if (d.weeks.some((w) => w.index >= d.weeksCount)) return false; + return new Set(d.weeks.map((w) => w.index)).size === d.weeksCount; +} + export const createPlanSchema = planBaseSchema .extend({ // Opcjonalna data startu (poniedziałek) — plan tworzony i aktywowany w jednym kroku. @@ -47,6 +69,9 @@ export const createPlanSchema = planBaseSchema }) .refine((d) => d.weeks.length === d.weeksCount, { message: 'weeks must have exactly weeksCount entries', + }) + .refine(weeksIndexShapeOk, { + message: 'weeks index must cover 0..weeksCount-1 exactly once', }); export const updatePlanSchema = planBaseSchema @@ -60,6 +85,9 @@ export const updatePlanSchema = planBaseSchema }) .refine((d) => d.weeks.length === d.weeksCount, { message: 'weeks must have exactly weeksCount entries', + }) + .refine(weeksIndexShapeOk, { + message: 'weeks index must cover 0..weeksCount-1 exactly once', }); export const activateSchema = z.object({ diff --git a/api/test/integration/plans.test.ts b/api/test/integration/plans.test.ts index c33548e..0c3a074 100644 --- a/api/test/integration/plans.test.ts +++ b/api/test/integration/plans.test.ts @@ -92,6 +92,50 @@ describe('plans routes', () => { expect(res.status).toBe(400); }); + it('rejects duplicated weeks index with 400', async () => { + const app = buildApp(); + const body = planBody('Dup week'); + body.weeks[1].index = 0; + const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body); + expect(res.status).toBe(400); + }); + + it('rejects a week index outside 0..weeksCount-1 with 400', async () => { + const app = buildApp(); + const body = planBody('Out of range'); + body.weeks[1].index = 5; + const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body); + expect(res.status).toBe(400); + }); + + it('rejects duplicated dayOfWeek within a week with 400', async () => { + const app = buildApp(); + const body = planBody('Dup day'); + body.weeks[0].days[1].dayOfWeek = 1; + const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body); + expect(res.status).toBe(400); + }); + + it('rejects duplicated set index within an exercise with 400', async () => { + const app = buildApp(); + const body = planBody('Dup set'); + body.weeks[0].days[0].exercises[0].sets[1].index = 0; + const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body); + expect(res.status).toBe(400); + }); + + it('rejects duplicated exercise order within a day with 400', async () => { + const app = buildApp(); + const body = planBody('Dup order'); + body.weeks[0].days[0].exercises.push({ + exerciseId, + order: 0, + sets: [{ index: 0, reps: 5 }], + }); + const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body); + expect(res.status).toBe(400); + }); + it('rejects a plan with more than 8 weeks with 400', async () => { const app = buildApp(); const body = planBody('Too long');