Plany: wizard 3-etapowy tworzenia/edycji, silownia i alias tygodnia (UX + API + migracja)

This commit is contained in:
tisachi
2026-09-12 15:16:13 +02:00
parent a2d31ee46b
commit ef2fa726bb
26 changed files with 1340 additions and 361 deletions

View File

@@ -27,12 +27,16 @@ export interface PlanDayInput {
export interface PlanWeekInput {
index: number;
/** Opcjonalny alias tygodnia (np. „Deload"). */
customName?: string | null;
days: PlanDayInput[];
}
export interface CreatePlanInput {
title: string;
weeksCount: number;
/** Informacyjna — siłownia, w której realizowany jest plan (właściciel musi być jej członkiem). */
gymId?: string | null;
weeks: PlanWeekInput[];
/** Opcjonalna data startu (poniedziałek) — plan tworzony i aktywowany w jednym kroku. */
startDate?: string;
@@ -63,9 +67,28 @@ async function validateExercises(prisma: PrismaClient, weeks: PlanWeekInput[]) {
if (exercises.length !== exerciseIds.size) throw badRequest('Unknown exerciseId in plan');
}
/**
* Siłownia planu jest informacyjna, ale musi istnieć i właściciel planu
* (podopieczny w trybie trenera) musi być jej członkiem.
*/
async function validateGymMembership(
prisma: PrismaClient,
gymId: string | null | undefined,
ownerId: string,
) {
if (!gymId) return;
const gym = await prisma.gym.findUnique({ where: { id: gymId } });
if (!gym) throw badRequest('Unknown gymId in plan');
const membership = await prisma.gymMembership.findUnique({
where: { userId_gymId: { userId: ownerId, gymId } },
});
if (!membership) throw badRequest('Plan owner is not a member of the gym');
}
function weekData(weeks: PlanWeekInput[]) {
return weeks.map((w) => ({
index: w.index,
customName: w.customName ?? null,
days: {
create: w.days.map((d) => ({
dayOfWeek: d.dayOfWeek,
@@ -87,6 +110,7 @@ export async function listPlans(prisma: PrismaClient, userId: string) {
orderBy: { createdAt: 'desc' },
include: {
creator: { select: { id: true, firstName: true, lastName: true } },
gym: { select: { id: true, name: true } },
weeks: { select: { days: { select: { status: true } } } },
},
});
@@ -115,6 +139,7 @@ export async function createPlan(
creatorId?: string,
) {
await validateExercises(prisma, data.weeks);
await validateGymMembership(prisma, data.gymId, userId);
// Data startu: walidacja (poniedziałek) przed sprawdzeniem aktywnego planu.
const startOfStart = data.startDate ? parseStartMonday(data.startDate) : null;
if (startOfStart) await assertNoActivePlan(prisma, userId);
@@ -125,11 +150,16 @@ export async function createPlan(
creatorId: creatorId ?? userId,
status: 'DRAFT',
weeksCount: data.weeksCount,
gymId: data.gymId ?? null,
weeks: { create: weekData(data.weeks) },
},
include: { gym: { select: { id: true, name: true } } },
});
if (startOfStart) await applyStartDate(prisma, plan.id, userId, startOfStart);
const updated = await prisma.trainingPlan.findUnique({ where: { id: plan.id } });
const updated = await prisma.trainingPlan.findUnique({
where: { id: plan.id },
include: { gym: { select: { id: true, name: true } } },
});
return {
plan: {
id: updated!.id,
@@ -137,6 +167,7 @@ export async function createPlan(
status: updated!.status,
weeksCount: updated!.weeksCount,
startDate: updated!.startDate,
gym: updated!.gym,
},
};
}
@@ -146,6 +177,7 @@ export async function getPlan(prisma: PrismaClient, planId: string, userId: stri
const full = await prisma.trainingPlan.findUnique({
where: { id: planId },
include: {
gym: { select: { id: true, name: true } },
weeks: {
orderBy: { index: 'asc' },
include: {
@@ -175,11 +207,17 @@ export async function updatePlan(
const plan = await assertPlanAccess(prisma, planId, userId);
if (plan.status !== 'DRAFT') throw badRequest('Only draft plans can be edited');
await validateExercises(prisma, data.weeks);
await validateGymMembership(prisma, data.gymId, plan.userId);
await prisma.$transaction(async (tx) => {
await tx.planWeek.deleteMany({ where: { planId: plan.id } });
await tx.trainingPlan.update({
where: { id: plan.id },
data: { title: data.title, weeksCount: data.weeksCount, weeks: { create: weekData(data.weeks) } },
data: {
title: data.title,
weeksCount: data.weeksCount,
gymId: data.gymId ?? null,
weeks: { create: weekData(data.weeks) },
},
});
});
return { ok: true };