Plans: wznawianie planu tylko dla wlasciciela (usuniete akcje trenera)
This commit is contained in:
@@ -33,7 +33,6 @@ import {
|
||||
deletePlan,
|
||||
getPlan,
|
||||
listPlans,
|
||||
reopenPlan,
|
||||
updatePlan,
|
||||
} from '../services/plans.js';
|
||||
import { activateSchema, createPlanSchema, updatePlanSchema } from './plans-schemas.js';
|
||||
@@ -640,36 +639,6 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /trainer/trainees/{userId}/plans/{planId}/reopen:
|
||||
* post:
|
||||
* summary: Reopen the trainee's completed/abandoned plan (back to ACTIVE)
|
||||
* tags: [Trainer]
|
||||
* responses:
|
||||
* 200: { description: OK }
|
||||
* 400: { description: Only a completed or abandoned plan can be reopened }
|
||||
* 403: { description: Trainer role required }
|
||||
* 404: { description: Trainee or plan not found }
|
||||
* 409: { description: Trainee already has an active plan }
|
||||
*/
|
||||
router.post(
|
||||
'/trainees/:userId/plans/:planId/reopen',
|
||||
trainerOnly,
|
||||
asyncHandler(async (req, res) => {
|
||||
await assertActiveTrainee(prisma, req.user!.id, req.params.userId);
|
||||
const result = await reopenPlan(prisma, req.params.planId, req.user!.id);
|
||||
await logActivity(prisma, {
|
||||
actorId: req.user!.id,
|
||||
action: 'plan.reopened',
|
||||
entityType: 'plan',
|
||||
entityId: req.params.planId,
|
||||
details: { title: result.title, traineeId: req.params.userId },
|
||||
});
|
||||
res.json(result);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /trainer/trainees/{userId}/comments:
|
||||
|
||||
@@ -429,12 +429,13 @@ export async function completePlan(prisma: PrismaClient, planId: string, userId:
|
||||
* pomyłek (np. błędnie oznaczony plan) — dni SKIPPED nie są wskrzeszane, a plan
|
||||
* bez dni zaplanowanych zostanie ponownie ukończony przez scheduler, gdy tylko
|
||||
* wszystkie dni pozostaną domknięte.
|
||||
* Wznowić może wyłącznie właściciel planu (trener nie ma takiej akcji).
|
||||
*/
|
||||
export async function reopenPlan(prisma: PrismaClient, planId: string, userId: string) {
|
||||
const plan = await assertPlanAccess(prisma, planId, userId);
|
||||
const plan = await assertPlanOwner(prisma, planId, userId);
|
||||
if (plan.status !== 'COMPLETED' && plan.status !== 'ABANDONED')
|
||||
throw badRequest('Only a completed or abandoned plan can be reopened');
|
||||
// Jeden aktywny plan dotyczy właściciela (inicjatorem może być trener).
|
||||
// Jeden aktywny plan dotyczy właściciela.
|
||||
// Pre-check + częściowy indeks unikalny (TrainingPlan_userId_active_key)
|
||||
// rozstrzyga równoległe wznowienia/aktywacje.
|
||||
const otherActive = await prisma.trainingPlan.findFirst({
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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' });
|
||||
|
||||
Reference in New Issue
Block a user