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

@@ -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:

View File

@@ -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({