Api+app: usuniecie PlanSet.difficulty z kontraktu planu (trudnosc pozostaje wynikiem rzeczywistym serii)

This commit is contained in:
tisachi
2026-10-11 15:52:33 +02:00
parent d544f6521c
commit 2fb288e029
18 changed files with 230 additions and 99 deletions

View File

@@ -46,7 +46,7 @@ describe('plans routes', () => {
exerciseId,
order: 0,
sets: [
{ index: 0, reps: 10, loadKg: 60, restSec: 90, rir: 2, difficulty: 3 },
{ index: 0, reps: 10, loadKg: 60, restSec: 90, rir: 2 },
{ index: 1, reps: 8, loadKg: 62.5, restSec: 90 },
],
},
@@ -65,14 +65,20 @@ describe('plans routes', () => {
const exercise = await createExercise();
exerciseId = exercise.id;
const app = buildApp();
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('Mój plan'));
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('Mój plan'));
expect(res.status).toBe(201);
planId = res.body.plan.id;
});
it('creates a draft plan with the given structure', async () => {
const app = buildApp();
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('Draft 2'));
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('Draft 2'));
expect(res.status).toBe(201);
expect(res.body.plan).toMatchObject({ title: 'Draft 2', status: 'DRAFT', weeksCount: 2 });
expect(res.body.plan.startDate).toBeNull();
@@ -154,7 +160,9 @@ describe('plans routes', () => {
it('returns 404 for another user plan', async () => {
const app = buildApp();
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${otherToken}`);
const res = await app
.get(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${otherToken}`);
expect(res.status).toBe(404);
});
@@ -162,7 +170,10 @@ describe('plans routes', () => {
const app = buildApp();
const body = planBody('Zmieniony');
body.weeks[0].days[0].exercises[0].sets = [{ index: 0, reps: 5 }];
const res = await app.patch(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${token}`).send(body);
const res = await app
.patch(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${token}`)
.send(body);
expect(res.status).toBe(200);
const detail = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${token}`);
@@ -201,7 +212,10 @@ describe('plans routes', () => {
it('rejects activation with a past-week startDate with 400', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('Past activate'));
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('Past activate'));
const res = await app
.post(`/api/v1/plans/${created.body.plan.id}/activate`)
.set('Authorization', `Bearer ${token}`)
@@ -212,7 +226,10 @@ describe('plans routes', () => {
it('rejects activation on a non-Monday date with 400', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('Sunday plan'));
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('Sunday plan'));
const res = await app
.post(`/api/v1/plans/${created.body.plan.id}/activate`)
.set('Authorization', `Bearer ${token}`)
@@ -223,7 +240,10 @@ describe('plans routes', () => {
it('rejects an invalid start date with 400', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('Bad date'));
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('Bad date'));
const res = await app
.post(`/api/v1/plans/${created.body.plan.id}/activate`)
.set('Authorization', `Bearer ${token}`)
@@ -233,7 +253,10 @@ describe('plans routes', () => {
it('rejects activating a second plan while one is active with 409', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('Second active'));
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('Second active'));
const res = await app
.post(`/api/v1/plans/${created.body.plan.id}/activate`)
.set('Authorization', `Bearer ${token}`)
@@ -244,7 +267,10 @@ describe('plans routes', () => {
it('rejects editing an active plan with 400', async () => {
const app = buildApp();
const res = await app.patch(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${token}`).send(planBody('Edit active'));
const res = await app
.patch(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${token}`)
.send(planBody('Edit active'));
expect(res.status).toBe(400);
expect(res.body.error.message).toBe('Only draft plans can be edited');
});
@@ -258,32 +284,50 @@ describe('plans routes', () => {
it('abandons an active plan and refuses to close it twice', async () => {
const app = buildApp();
const res = await app.post(`/api/v1/plans/${planId}/abandon`).set('Authorization', `Bearer ${token}`);
const res = await app
.post(`/api/v1/plans/${planId}/abandon`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
const again = await app.post(`/api/v1/plans/${planId}/abandon`).set('Authorization', `Bearer ${token}`);
const again = await app
.post(`/api/v1/plans/${planId}/abandon`)
.set('Authorization', `Bearer ${token}`);
expect(again.status).toBe(400);
expect(again.body.error.message).toBe('Plan is already closed');
});
it('completes a draft plan and refuses to close it twice', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('To complete'));
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('To complete'));
const id = created.body.plan.id;
const res = await app.post(`/api/v1/plans/${id}/complete`).set('Authorization', `Bearer ${token}`);
const res = await app
.post(`/api/v1/plans/${id}/complete`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
const again = await app.post(`/api/v1/plans/${id}/complete`).set('Authorization', `Bearer ${token}`);
const again = await app
.post(`/api/v1/plans/${id}/complete`)
.set('Authorization', `Bearer ${token}`);
expect(again.status).toBe(400);
});
it('deletes a draft plan', async () => {
const app = buildApp();
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(planBody('To delete'));
const res = await app.delete(`/api/v1/plans/${created.body.plan.id}`).set('Authorization', `Bearer ${token}`);
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(planBody('To delete'));
const res = await app
.delete(`/api/v1/plans/${created.body.plan.id}`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(204);
const detail = await app.get(`/api/v1/plans/${created.body.plan.id}`).set('Authorization', `Bearer ${token}`);
const detail = await app
.get(`/api/v1/plans/${created.body.plan.id}`)
.set('Authorization', `Bearer ${token}`);
expect(detail.status).toBe(404);
});
@@ -320,7 +364,9 @@ describe('plans routes', () => {
expect(res.body.plan).toMatchObject({ title: 'One-step plan', status: 'ACTIVE' });
expect(res.body.plan.startDate).not.toBeNull();
const detail = await app.get(`/api/v1/plans/${res.body.plan.id}`).set('Authorization', `Bearer ${token}`);
const detail = await app
.get(`/api/v1/plans/${res.body.plan.id}`)
.set('Authorization', `Bearer ${token}`);
expect(detail.body.plan.status).toBe('ACTIVE');
const localDate = (iso: string) => {
const d = new Date(iso);
@@ -397,7 +443,9 @@ describe('plans routes', () => {
expect(conflicted).toHaveLength(1);
const after = await app.get('/api/v1/plans').set('Authorization', `Bearer ${token}`);
const activeCount = after.body.plans.filter((p: { status: string }) => p.status === 'ACTIVE').length;
const activeCount = after.body.plans.filter(
(p: { status: string }) => p.status === 'ACTIVE',
).length;
expect(activeCount).toBe(1);
});
});
@@ -415,7 +463,9 @@ describe('plans: gym and week alias', () => {
{
index: 0,
customName: 'Deload',
days: [{ dayOfWeek: 1, exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10 }] }] }],
days: [
{ dayOfWeek: 1, exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10 }] }] },
],
},
],
});
@@ -428,7 +478,9 @@ describe('plans: gym and week alias', () => {
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 } });
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 () => {
@@ -441,7 +493,9 @@ describe('plans: gym and week alias', () => {
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}`);
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');
});
@@ -483,7 +537,10 @@ describe('plans: gym and week alias', () => {
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 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}`)
@@ -536,8 +593,8 @@ describe('plans: direct seeding sanity', () => {
expect(day.exercises).toHaveLength(1);
expect(day.exercises[0].sets).toHaveLength(1);
expect(day.exercises[0].sets[0].reps).toBe(10);
expect(day.exercises[0].exercise.id).toBe(exercise.id);
await prisma.trainingPlan.delete({ where: { id: plan.id } });
expect(day.exercises[0].exercise.id).toBe(exercise.id);
await prisma.trainingPlan.delete({ where: { id: plan.id } });
});
});
@@ -558,7 +615,10 @@ describe('plans: trainer-created plan (creator + owner access)', () => {
{
index: 0,
days: [
{ dayOfWeek: 1, exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60 }] }] },
{
dayOfWeek: 1,
exercises: [{ exerciseId, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60 }] }],
},
],
},
],
@@ -572,9 +632,15 @@ describe('plans: trainer-created plan (creator + owner access)', () => {
trainerId = t.user.id;
traineeId = tr.user.id;
const gym = await prisma.gym.create({ data: { name: `Vis Gym ${rand()}` } });
await prisma.gymMembership.create({ data: { userId: trainerId, gymId: gym.id, roles: ['TRAINER'] as never } });
await prisma.gymMembership.create({ data: { userId: traineeId, gymId: gym.id, roles: ['MEMBER'] as never } });
await prisma.gymMembership.create({ data: { userId: p.user.id, gymId: gym.id, roles: ['MEMBER'] as never } });
await prisma.gymMembership.create({
data: { userId: trainerId, gymId: gym.id, roles: ['TRAINER'] as never },
});
await prisma.gymMembership.create({
data: { userId: traineeId, gymId: gym.id, roles: ['MEMBER'] as never },
});
await prisma.gymMembership.create({
data: { userId: p.user.id, gymId: gym.id, roles: ['MEMBER'] as never },
});
trainerToken = (await loginViaApi(t.user.email, PASSWORD)).token;
traineeToken = (await loginViaApi(tr.user.email, PASSWORD)).token;
plainToken = (await loginViaApi(p.user.email, PASSWORD)).token;
@@ -615,13 +681,17 @@ describe('plans: trainer-created plan (creator + owner access)', () => {
});
it('a third user gets 404 for the plan', async () => {
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${plainToken}`);
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 } });
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}`)
@@ -632,7 +702,9 @@ describe('plans: trainer-created plan (creator + owner access)', () => {
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 } });
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}`)
@@ -681,7 +753,10 @@ describe('plans: sharing (PlanShare)', () => {
const ex = await createExercise();
exerciseId = ex.id;
// plan należy do podopiecznego (właściciel = podopieczny)
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${trainee.token}`).send(body('Plan do udostępnienia'));
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${trainee.token}`)
.send(body('Plan do udostępnienia'));
expect(res.status).toBe(201);
planId = res.body.plan.id;
});
@@ -732,16 +807,23 @@ describe('plans: sharing (PlanShare)', () => {
});
it('the owner sees the recipients in the plan detail', async () => {
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainee.token}`);
const res = await app
.get(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${trainee.token}`);
expect(res.status).toBe(200);
expect(res.body.plan.shares).toEqual([
{ userId: trainer.user.id, user: { id: trainer.user.id, firstName: 'ShareTrainer', lastName: null } },
{
userId: trainer.user.id,
user: { id: trainer.user.id, firstName: 'ShareTrainer', lastName: null },
},
]);
expect(res.body.plan.canManage).toBe(true);
});
it('the recipient sees the plan in /plans/shared-with-me', async () => {
const res = await app.get('/api/v1/plans/shared-with-me').set('Authorization', `Bearer ${trainer.token}`);
const res = await app
.get('/api/v1/plans/shared-with-me')
.set('Authorization', `Bearer ${trainer.token}`);
expect(res.status).toBe(200);
const found = res.body.plans.find((p: { id: string }) => p.id === planId);
expect(found).toBeTruthy();
@@ -750,7 +832,9 @@ describe('plans: sharing (PlanShare)', () => {
});
it('the trainee sees the trainer in /me/trainers', async () => {
const res = await app.get('/api/v1/me/trainers').set('Authorization', `Bearer ${trainee.token}`);
const res = await app
.get('/api/v1/me/trainers')
.set('Authorization', `Bearer ${trainee.token}`);
expect(res.status).toBe(200);
expect(res.body.trainers).toEqual([
{ id: trainer.user.id, firstName: 'ShareTrainer', lastName: null, nickname: null },
@@ -770,14 +854,18 @@ describe('plans: sharing (PlanShare)', () => {
where: { trainerId: trainer.user.id, userId: trainee.user.id },
data: { status: 'ARCHIVED' },
});
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainer.token}`);
const res = await app
.get(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(res.status).toBe(200);
expect(res.body.plan.canManage).toBe(false);
expect(res.body.plan.shares).toBeUndefined();
});
it('a stranger gets 404 for the shared plan', async () => {
const res = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${stranger.token}`);
const res = await app
.get(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${stranger.token}`);
expect(res.status).toBe(404);
});
@@ -787,10 +875,14 @@ describe('plans: sharing (PlanShare)', () => {
.set('Authorization', `Bearer ${trainee.token}`);
expect(res.status).toBe(204);
const detail = await app.get(`/api/v1/plans/${planId}`).set('Authorization', `Bearer ${trainer.token}`);
const detail = await app
.get(`/api/v1/plans/${planId}`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(detail.status).toBe(404);
const list = await app.get('/api/v1/plans/shared-with-me').set('Authorization', `Bearer ${trainer.token}`);
const list = await app
.get('/api/v1/plans/shared-with-me')
.set('Authorization', `Bearer ${trainer.token}`);
expect(list.body.plans.find((p: { id: string }) => p.id === planId)).toBeUndefined();
});
@@ -829,7 +921,10 @@ describe('plans: PATCH with startDate (update + activate)', () => {
userId = reg.user.id;
const ex = await createExercise();
exerciseId = ex.id;
const res = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body('Draft do aktywacji'));
const res = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(body('Draft do aktywacji'));
expect(res.status).toBe(201);
planId = res.body.plan.id;
});
@@ -882,14 +977,17 @@ describe('plans: PATCH with startDate (update + activate)', () => {
return [d.getFullYear(), d.getMonth() + 1, d.getDate()];
};
const [sy, sm, sd] = mondayIso(1).split('-').map(Number);
const day1 = detail.body.plan.weeks.find((w: { index: number }) => w.index === 0).days.find(
(d: { dayOfWeek: number }) => d.dayOfWeek === 1,
);
const day1 = detail.body.plan.weeks
.find((w: { index: number }) => w.index === 0)
.days.find((d: { dayOfWeek: number }) => d.dayOfWeek === 1);
expect(localDate(day1.date)).toEqual([sy, sm, sd]);
});
it('PATCH with startDate while another plan is active saves the structure as a DRAFT (activationSkipped)', async () => {
const created = await app.post('/api/v1/plans').set('Authorization', `Bearer ${token}`).send(body('Konflikt'));
const created = await app
.post('/api/v1/plans')
.set('Authorization', `Bearer ${token}`)
.send(body('Konflikt'));
const res = await app
.patch(`/api/v1/plans/${created.body.plan.id}`)
.set('Authorization', `Bearer ${token}`)
@@ -897,7 +995,9 @@ describe('plans: PATCH with startDate (update + activate)', () => {
expect(res.status).toBe(200);
expect(res.body).toMatchObject({ ok: true, activationSkipped: true });
const detail = await app.get(`/api/v1/plans/${created.body.plan.id}`).set('Authorization', `Bearer ${token}`);
const detail = await app
.get(`/api/v1/plans/${created.body.plan.id}`)
.set('Authorization', `Bearer ${token}`);
expect(detail.body.plan).toMatchObject({ status: 'DRAFT' });
expect(detail.body.plan.startDate).toBeNull();
});