Trening: dodanie cwiczenia tworzy automatycznie pierwsza serie; limit niezapisanego bonusu liczony na cwiczenie (wczesniej na sesje)

This commit is contained in:
Tilumca
2026-10-14 22:11:49 +02:00
parent 1bed564bfc
commit 7d89f5bcca
8 changed files with 95 additions and 24 deletions

View File

@@ -139,8 +139,8 @@ export function createSessionRoutes({ prisma }: AppDeps) {
* /sessions/exercises/{id}/sets:
* post:
* summary: Add an extra (bonus) set. Only when all planned sets of this exercise are
* recorded (added exercises have no norm) and no extra set is still pending
* (at most one pending extra per session).
* recorded (added exercises have no norm) and this exercise has no extra set
* still pending (at most one pending extra per exercise).
* tags: [Sessions]
* responses:
* 201: { description: Created }

View File

@@ -241,8 +241,8 @@ export async function updateExercise(
/**
* Add an extra (bonus) set to a session exercise.
* Allowed once every planned (norm) set of THIS exercise is recorded (exercises
* added to the session have no norm — vacuously satisfied), and no extra set is
* still pending — at most one pending extra set per session.
* added to the session have no norm — vacuously satisfied), and this exercise has
* no extra set still pending — at most one pending extra set per exercise.
* Norm of other exercises must not block exceeding one that is already done:
* "nie dowożę / dowożę / przekraczam" is an option per exercise, in order.
*/
@@ -259,13 +259,7 @@ export async function addSet(prisma: PrismaClient, exerciseId: string, userId: s
'All planned sets of this exercise must be recorded before adding an extra set',
);
const pendingExtra = await tx.sessionSet.count({
where: {
// usunięte ćwiczenia nie liczą się do limitu — ich seria „w toku" jest nietykalna
exercise: { sessionId: exercise.sessionId, removed: false },
planSetId: null,
reps: null,
removed: false,
},
where: { exerciseId: exercise.id, planSetId: null, reps: null, removed: false },
});
if (pendingExtra > 0) throw badRequest('An extra set is already in progress');
const maxIndex = await tx.sessionSet.aggregate({

View File

@@ -831,7 +831,7 @@ export const openapi: Record<string, unknown> = {
post: {
tags: ['Sessions'],
summary:
"Add extra (bonus) set to session exercise (requires this exercise's planned sets recorded; at most one pending extra per session)",
"Add extra (bonus) set to session exercise (requires this exercise's planned sets recorded; at most one pending extra per exercise)",
security: [{ bearerAuth: [] }],
parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }],
},

View File

@@ -424,6 +424,46 @@ describe('sessions routes', () => {
expect(cleanup.status).toBe(200);
});
it('allows one pending extra per exercise (równolegle w różnych ćwiczeniach, nie dwa w tym samym)', async () => {
const app = buildApp();
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);
const sessionId = session.body.day.sessions[0].id;
const detail = await app
.get(`/api/v1/sessions/${sessionId}`)
.set('Authorization', `Bearer ${token}`);
const ex = detail.body.session.exercises[0];
const a = await app
.post(`/api/v1/sessions/exercises/${ex.id}/sets`)
.set('Authorization', `Bearer ${token}`);
expect(a.status).toBe(201);
// inne ćwiczenie może mieć własną niezapisaną serię bonusową w tym samym czasie
const added = await app
.post(`/api/v1/sessions/${sessionId}/exercises`)
.set('Authorization', `Bearer ${token}`)
.send({ exerciseId });
expect(added.status).toBe(201);
const b = await app
.post(`/api/v1/sessions/exercises/${added.body.exerciseId}/sets`)
.set('Authorization', `Bearer ${token}`);
expect(b.status).toBe(201);
// ale nie druga w tym samym ćwiczeniu
const dup = await app
.post(`/api/v1/sessions/exercises/${added.body.exerciseId}/sets`)
.set('Authorization', `Bearer ${token}`);
expect(dup.status).toBe(400);
expect(dup.body.error.message).toBe('An extra set is already in progress');
for (const setId of [a.body.setId, b.body.setId]) {
const del = await app
.delete(`/api/v1/sessions/sets/${setId}`)
.set('Authorization', `Bearer ${token}`);
expect(del.status).toBe(200);
}
});
it('records actual set results', async () => {
const app = buildApp();
const session = await app.get(`/api/v1/days/${dayId}`).set('Authorization', `Bearer ${token}`);

View File

@@ -503,10 +503,10 @@ describe('addSet', () => {
removed: false,
},
});
// limit bonusu pomija serie nalezace do usunietych cwiczen
// limit bonusu jest liczony dla tego cwiczenia (nie dla sesji)
expect(prisma.sessionSet.count).toHaveBeenNthCalledWith(2, {
where: {
exercise: { sessionId: 's1', removed: false },
exerciseId: 'se1',
planSetId: null,
reps: null,
removed: false,