Plany: notatka o zmianach opcjonalna (brak = wpis historii bez powodu; pole tylko przy edycji planu realizowanego)

This commit is contained in:
tisachi
2026-11-05 22:57:03 +01:00
parent 41f04ff62c
commit 23c881d4f3
15 changed files with 132 additions and 108 deletions

View File

@@ -91,8 +91,14 @@ export const updatePlanSchema = planBaseSchema
.string()
.refine((v) => !Number.isNaN(new Date(v).getTime()), 'Invalid date')
.optional(),
// Plan ACTIVE: wymagana notatka audytowa, gdy struktura faktycznie się zmienia (pilnuje serwis).
changeNote: z.string().trim().min(3).max(300).optional(),
// Plan ACTIVE: notatka audytowa zmiany — opcjonalna (pusta = brak, serwis zapisuje
// wpis historii bez notatki).
changeNote: z
.string()
.trim()
.max(300)
.optional()
.transform((v) => (v ? v : undefined)),
// Plan ACTIVE: świadoma zgoda na usunięcie dnia z historią (soft-remove `removedAt`;
// historia sesji/serii zostaje). Bez flagi serwis odrzuca 400 PLAN_DAY_HAS_HISTORY.
forceRemoveDays: z.boolean().optional(),

View File

@@ -129,14 +129,14 @@ export function createPlanRoutes({ prisma }: AppDeps) {
* current or future week, activates the plan in one step). ACTIVE = differential edit
* (days with history are kept; removing a day with history returns 400
* PLAN_DAY_HAS_HISTORY unless forceRemoveDays: true soft-removes it with history
* preserved) and requires
* changeNote (min 3 chars) recorded in the plan's edit history.
* preserved) and records an optional
* changeNote in the plan's edit history.
* The update is always allowed; when the user already has an active plan the
* structure is saved and the response carries activationSkipped: true.
* tags: [Plans]
* responses:
* 200: { description: OK }
* 400: { description: Not a draft/active plan / start date not a Monday / start week in the past / change note required / PLAN_DAY_HAS_HISTORY }
* 400: { description: Not a draft/active plan / start date not a Monday / start week in the past / PLAN_DAY_HAS_HISTORY }
*/
router.patch(
'/:id',

View File

@@ -492,8 +492,9 @@ export function createTrainerRoutes({ prisma }: AppDeps) {
* /trainer/trainees/{userId}/plans/{planId}:
* patch:
* summary: Update plan structure. DRAFT = full replace (optional startDate = a Monday
* activates the plan in one step); ACTIVE = differential edit requiring changeNote
* (recorded in the plan edit history; the trainee gets a PLAN_EDITED feed notification).
* activates the plan in one step); ACTIVE = differential edit with an optional
* changeNote (recorded in the plan edit history; the trainee gets a PLAN_EDITED
* feed notification).
* Same body as POST (gymId, weeks[].customName,
* forceRemoveDays for soft-removing days that have training history).
* tags: [Trainer]

View File

@@ -49,7 +49,7 @@ export interface CreatePlanInput {
}
export interface UpdatePlanInput extends CreatePlanInput {
/** Plan ACTIVE: notatka audytowa zmiany („co i dlaczego") — wymagana, gdy coś się różni. */
/** Plan ACTIVE: notatka audytowa zmiany („co i dlaczego") — opcjonalna. */
changeNote?: string;
/**
* Plan ACTIVE: zezwolenie na usunięcie dnia z historią (sesje/COMPLETED/SKIPPED).
@@ -571,7 +571,7 @@ async function reconcileDayExercises(
/**
* Edycja planu w trakcie realizacji. Zmiany są rozliczane wiersz po wierszu:
* dni z historią zostają nietknięte (ich usunięcie = 400), nowe dni dostają datę
* z szablonu planu, a każda faktyczna zmiana trafia do `PlanEdit` z wymaganą
* z szablonu planu, a każda faktyczna zmiana trafia do `PlanEdit` z opcjonalną
* notatką („co i dlaczego”). Startu aktywnego planu nie zmienia się.
* Edycja przez kogoś innego niż właściciel (trener) generuje dodatkowo
* powiadomienie `PLAN_EDITED` w feedzie właściciela.
@@ -608,6 +608,7 @@ async function updateActivePlan(
const titleChanged = data.title.trim() !== plan.title;
let aliasesChanged = false;
let notifyOwner = false;
let editNote = '';
await prisma.$transaction(async (tx) => {
const byIndex = new Map(weeks.map((w) => [w.index, w]));
@@ -719,9 +720,9 @@ async function updateActivePlan(
});
const changed = titleChanged || aliasesChanged || Object.values(c).some((n) => n > 0);
if (changed) {
// Notatka opcjonalna: brak = wpis historii bez informacji „dlaczego”.
const note = (data.changeNote ?? '').trim();
if (note.length < 3)
throw badRequest('A change note is required when editing an active plan');
editNote = note;
await tx.planEdit.create({
data: {
planId: plan.id,
@@ -739,8 +740,8 @@ async function updateActivePlan(
await createNotification(prisma, {
userId: ownerId,
type: 'PLAN_EDITED',
title: `Trener zmienił Twój plan: ${data.title.trim()} — ${data.changeNote!.trim()}`,
payload: { planId: plan.id, planTitle: data.title.trim(), note: data.changeNote!.trim() },
title: `Trener zmienił Twój plan: ${data.title.trim()}${editNote ? ` — ${editNote}` : ''}`,
payload: { planId: plan.id, planTitle: data.title.trim(), note: editNote },
});
}
return { ok: true, activationSkipped: false, edited: true };

View File

@@ -1380,10 +1380,9 @@ export const openapi: Record<string, unknown> = {
},
changeNote: {
type: 'string',
minLength: 3,
maxLength: 300,
description:
'PATCH only, required when an ACTIVE plan actually changes — stored in the plan edit history',
'PATCH only, optional note recorded in the plan edit history for ACTIVE-plan edits (empty/absent = no note)',
},
weeks: {
type: 'array',
@@ -1475,7 +1474,7 @@ export const openapi: Record<string, unknown> = {
patch: {
tags: ['Plans'],
summary:
'Update plan structure. DRAFT = full replace (same body as POST /plans; optional startDate (a Monday of the current or a future week) updates and activates the plan in one step, and an already-active plan of the user only saves the structure with activationSkipped: true). ACTIVE = differential edit: rows matched by the ids sent in the payload are updated in place, new rows are added, the plan start date is immutable, and any real change requires changeNote (recorded in the plan edit history, exposed as plan.edits). Removing a day that has training history is rejected with code PLAN_DAY_HAS_HISTORY unless the payload sets forceRemoveDays: true — then the day is soft-removed (removedAt), its row and sessions stay in history while the day disappears from the plan, calendar, stats and killstreak. When an ACTIVE plan actually changes and the editor is not its owner (trainer edit), the owner gets a PLAN_EDITED notification in their feed',
'Update plan structure. DRAFT = full replace (same body as POST /plans; optional startDate (a Monday of the current or a future week) updates and activates the plan in one step, and an already-active plan of the user only saves the structure with activationSkipped: true). ACTIVE = differential edit: rows matched by the ids sent in the payload are updated in place, new rows are added, the plan start date is immutable, and any real change is recorded in the plan edit history (plan.edits) with an optional changeNote. Removing a day that has training history is rejected with code PLAN_DAY_HAS_HISTORY unless the payload sets forceRemoveDays: true — then the day is soft-removed (removedAt), its row and sessions stay in history while the day disappears from the plan, calendar, stats and killstreak. When an ACTIVE plan actually changes and the editor is not its owner (trainer edit), the owner gets a PLAN_EDITED notification in their feed',
security: [{ bearerAuth: [] }],
parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }],
requestBody: {