Plans: wznawianie planu tylko dla wlasciciela (usuniete akcje trenera)

This commit is contained in:
tisachi
2026-10-22 23:53:49 +02:00
parent 50df81dbf0
commit 83ee38dc3a
13 changed files with 54 additions and 175 deletions

View File

@@ -845,31 +845,11 @@ describe('trainer — managing a trainee plan', () => {
expect(res.status).toBe(200);
});
it('rejects reopening an active plan with 400', async () => {
it('rejects reopening a trainee plan — only the owner can resume (404)', async () => {
const created = await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans`)
.set('Authorization', `Bearer ${trainer.token}`)
.send(planBody('Reopen active'));
await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${created.body.plan.id}/activate`)
.set('Authorization', `Bearer ${trainer.token}`)
.send({ startDate: mondayIso(1) });
const res = await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${created.body.plan.id}/reopen`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(res.status).toBe(400);
expect(res.body.error.message).toBe('Only a completed or abandoned plan can be reopened');
// sprzątanie — zwolnij slot aktywnego planu
await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${created.body.plan.id}/abandon`)
.set('Authorization', `Bearer ${trainer.token}`);
});
it('reopens a trainee plan abandoned by the trainer', async () => {
const created = await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans`)
.set('Authorization', `Bearer ${trainer.token}`)
.send(planBody('Trainer reopen'));
.send(planBody('Trainer reopen denied'));
const id = created.body.plan.id;
await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${id}/activate`)
@@ -881,13 +861,9 @@ describe('trainer — managing a trainee plan', () => {
const res = await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${id}/reopen`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(res.status).toBe(200);
expect(res.status).toBe(404);
const stored = await prisma.trainingPlan.findUnique({ where: { id } });
expect(stored!.status).toBe('ACTIVE');
// sprzątanie
await app
.post(`/api/v1/trainer/trainees/${trainee.id}/plans/${id}/abandon`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(stored!.status).toBe('ABANDONED');
});
it('deletes a draft plan', async () => {

View File

@@ -167,6 +167,16 @@ describe('draft-only rules', () => {
});
});
it('reopenPlan: rejects reopening a plan owned by another user with 404', async () => {
const { prisma } = mockPrisma();
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'COMPLETED' });
await expect(reopenPlan(prisma, 'p1', 'trainer1')).rejects.toMatchObject({
status: 404,
message: 'Plan not found',
});
expect(prisma.trainingPlan.update).not.toHaveBeenCalled();
});
it('reopenPlan: rejects when the owner has another active plan with 409', async () => {
const { prisma } = mockPrisma();
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'COMPLETED' });