Plany: wznowienie ukonczonego/porzuconego planu (poprawki bez tworzenia nowego, blokada przy innym aktywnym)
This commit is contained in:
@@ -414,6 +414,35 @@ export async function completePlan(prisma: PrismaClient, planId: string, userId:
|
||||
return { ok: true, title: plan.title };
|
||||
}
|
||||
|
||||
/**
|
||||
* Cofnięcie zamknięcia planu (COMPLETED/ABANDONED -> ACTIVE). Służy do naprawy
|
||||
* 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.
|
||||
*/
|
||||
export async function reopenPlan(prisma: PrismaClient, planId: string, userId: string) {
|
||||
const plan = await assertPlanAccess(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).
|
||||
// Pre-check + częściowy indeks unikalny (TrainingPlan_userId_active_key)
|
||||
// rozstrzyga równoległe wznowienia/aktywacje.
|
||||
const otherActive = await prisma.trainingPlan.findFirst({
|
||||
where: { userId: plan.userId, status: 'ACTIVE', id: { not: plan.id } },
|
||||
});
|
||||
if (otherActive) throw conflict('User already has an active plan');
|
||||
try {
|
||||
await prisma.trainingPlan.update({
|
||||
where: { id: plan.id },
|
||||
data: { status: 'ACTIVE' },
|
||||
});
|
||||
} catch (e) {
|
||||
if (isPrismaUnique(e)) throw conflict('User already has an active plan');
|
||||
throw e;
|
||||
}
|
||||
return { ok: true, title: plan.title };
|
||||
}
|
||||
|
||||
/**
|
||||
* The owner shares the plan with a user in an ACTIVE coaching relation
|
||||
* (trainer <-> trainee, either direction). Recipients get read-only access.
|
||||
|
||||
Reference in New Issue
Block a user