Plany: wizard 3-etapowy tworzenia/edycji, silownia i alias tygodnia (UX + API + migracja)

This commit is contained in:
tisachi
2026-09-12 15:16:13 +02:00
parent a2d31ee46b
commit ef2fa726bb
26 changed files with 1340 additions and 361 deletions

View File

@@ -390,6 +390,114 @@ describe('plans routes', () => {
});
});
describe('plans: gym and week alias', () => {
let token: string;
let exerciseId: string;
let gymId: string;
let otherGymId: string;
const body = (title: string) => ({
title,
weeksCount: 1,
weeks: [
{
index: 0,
customName: 'Deload',
days: [{ dayOfWeek: 1, exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10 }] }] }],
},
],
});
beforeAll(async () => {
const reg = await registerViaApi();
token = reg.token;
exerciseId = (await createExercise()).id;
const gym = await prisma.gym.create({ data: { name: `Plan Gym ${rand()}` } });
gymId = gym.id;
const other = await prisma.gym.create({ data: { name: `Other Gym ${rand()}` } });
otherGymId = other.id;
await prisma.gymMembership.create({ data: { userId: reg.user.id, gymId, roles: ['MEMBER'] as never } });
});
it('creates a plan with gymId and per-week customName', async () => {
const app = buildApp();
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send({ ...body('Z siłownią'), gymId });
expect(res.status).toBe(201);
expect(res.body.plan.gym).toMatchObject({ id: gymId });
expect(res.body.plan.gym.name).toBeTruthy();
const detail = await app.get(`/api/v1/plans/${res.body.plan.id}`).set('Authorization', `Bearer ${token}`);
expect(detail.body.plan.gym).toMatchObject({ id: gymId });
expect(detail.body.plan.weeks[0].customName).toBe('Deload');
});
it('lists plans with the gym attached', async () => {
const app = buildApp();
const res = await app.get('/api/v1/plans').set('Authorization', `Bearer ${token}`);
const found = res.body.plans.find((p: { title: string }) => p.title === 'Z siłownią');
expect(found.gym).toMatchObject({ id: gymId });
});
it('rejects creation with a gym the user is not a member of with 400', async () => {
const app = buildApp();
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send({ ...body('Bez członkostwa'), gymId: otherGymId });
expect(res.status).toBe(400);
expect(res.body.error.message).toBe('Plan owner is not a member of the gym');
});
it('rejects creation with an unknown gymId with 400', async () => {
const app = buildApp();
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send({ ...body('Nieznana siłownia'), gymId: 'nonexistent-gym' });
expect(res.status).toBe(400);
expect(res.body.error.message).toBe('Unknown gymId in plan');
});
it('rejects a week alias longer than 40 chars with 400', async () => {
const app = buildApp();
const b = body('Długi alias');
b.weeks[0].customName = 'a'.repeat(41);
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(b);
expect(res.status).toBe(400);
});
it('updates gymId and customName via PATCH', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body('Do zmiany'));
const id = created.body.plan.id;
const res = await app
.patch(`/api/v1/plans/${id}`)
.set('Authorization', `Bearer ${token}`)
.send({ ...body('Do zmiany v2'), gymId, weeks: [{ index: 0, customName: null, days: [] }] });
expect(res.status).toBe(200);
const detail = await app.get(`/api/v1/plans/${id}`).set('Authorization', `Bearer ${token}`);
expect(detail.body.plan.gym).toMatchObject({ id: gymId });
expect(detail.body.plan.weeks[0].customName).toBeNull();
});
it('clears gymId to null when the gym is deleted', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send({ ...body('Plan z siłownią do usunięcia'), gymId });
const id = created.body.plan.id;
await prisma.gym.delete({ where: { id: gymId } });
const detail = await app.get(`/api/v1/plans/${id}`).set('Authorization', `Bearer ${token}`);
expect(detail.body.plan.gym).toBeNull();
});
});
describe('plans: direct seeding sanity', () => {
it('seedPlan creates a consistent structure in the database', async () => {
const { user } = await createUser();
@@ -498,4 +606,26 @@ describe('plans: trainer-created plan (creator + owner access)', () => {
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${plainToken}`);
expect(res.status).toBe(404);
});
it('trainer can attach a gym the trainee is a member of', async () => {
const gym = await prisma.gym.create({ data: { name: `Trainee Gym ${rand()}` } });
await prisma.gymMembership.create({ data: { userId: traineeId, gymId: gym.id, roles: ['MEMBER'] as never } });
const res = await app
.post(`/api/v1/trainer/trainees/${traineeId}/plans`)
.set('Authorization', `Bearer ${trainerToken}`)
.send({ ...body('Plan z siłownią'), gymId: gym.id });
expect(res.status).toBe(201);
expect(res.body.plan.gym).toMatchObject({ id: gym.id });
});
it('trainer cannot attach a gym the trainee is NOT a member of (400)', async () => {
const gym = await prisma.gym.create({ data: { name: `Trainer-only Gym ${rand()}` } });
await prisma.gymMembership.create({ data: { userId: trainerId, gymId: gym.id, roles: ['TRAINER'] as never } });
const res = await app
.post(`/api/v1/trainer/trainees/${traineeId}/plans`)
.set('Authorization', `Bearer ${trainerToken}`)
.send({ ...body('Plan bez członkostwa'), gymId: gym.id });
expect(res.status).toBe(400);
expect(res.body.error.message).toBe('Plan owner is not a member of the gym');
});
});

View File

@@ -231,6 +231,7 @@ describe('listPlans', () => {
orderBy: { createdAt: 'desc' },
include: {
creator: { select: { id: true, firstName: true, lastName: true } },
gym: { select: { id: true, name: true } },
weeks: { select: { days: { select: { status: true } } } },
},
});