Trening: seria nadmiarowa dostepna po dowiezieniu wlasnej normy cwiczenia (norma innych cwiczen wiecej nie blokuje; cwiczenia dodane od razu; limit 1 bonusu na sesje bez zmian)
This commit is contained in:
@@ -138,12 +138,13 @@ export function createSessionRoutes({ prisma }: AppDeps) {
|
||||
* @openapi
|
||||
* /sessions/exercises/{id}/sets:
|
||||
* post:
|
||||
* summary: Add an extra (bonus) set. Only when all planned sets of the session are
|
||||
* recorded and no extra set is still pending (at most one pending extra per session).
|
||||
* 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).
|
||||
* tags: [Sessions]
|
||||
* responses:
|
||||
* 201: { description: Created }
|
||||
* 400: { description: Norm not done or an extra set is already in progress }
|
||||
* 400: { description: Norm of this exercise not done or an extra set is already in progress }
|
||||
*/
|
||||
router.post(
|
||||
'/sessions/exercises/:id/sets',
|
||||
|
||||
@@ -240,21 +240,32 @@ export async function updateExercise(
|
||||
|
||||
/**
|
||||
* Add an extra (bonus) set to a session exercise.
|
||||
* Allowed only when every planned (norm) set of the session is recorded and no
|
||||
* extra set is still pending — at most one pending extra set per session.
|
||||
* 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.
|
||||
* Norm of other exercises must not block exceeding one that is already done:
|
||||
* "nie dowożę / dowożę / przekraczam" is an option per exercise, in order.
|
||||
*/
|
||||
export async function addSet(prisma: PrismaClient, exerciseId: string, userId: string) {
|
||||
const exercise = await getExerciseOwned(prisma, exerciseId, userId);
|
||||
// Cała kontrola + wstawienie pod blokadą sesji: dwa równoległe żądania nie mogą
|
||||
// przejść obiecyjnie warunku „brak serii dodatkowej w toku".
|
||||
const set = await inSessionTransaction(prisma, exercise.sessionId, async (tx) => {
|
||||
const sets = await tx.sessionSet.findMany({
|
||||
where: { exercise: { sessionId: exercise.sessionId }, removed: false },
|
||||
const pendingNorm = await tx.sessionSet.count({
|
||||
where: { exerciseId: exercise.id, planSetId: { not: null }, reps: null, removed: false },
|
||||
});
|
||||
const pendingNorm = sets.filter((s) => s.planSetId != null && s.reps == null).length;
|
||||
if (pendingNorm > 0)
|
||||
throw badRequest('All planned sets must be recorded before adding an extra set');
|
||||
const pendingExtra = sets.filter((s) => s.planSetId == null && s.reps == null).length;
|
||||
throw badRequest(
|
||||
'All planned sets of this exercise must be recorded before adding an extra set',
|
||||
);
|
||||
const pendingExtra = await tx.sessionSet.count({
|
||||
where: {
|
||||
exercise: { sessionId: exercise.sessionId },
|
||||
planSetId: null,
|
||||
reps: null,
|
||||
removed: false,
|
||||
},
|
||||
});
|
||||
if (pendingExtra > 0) throw badRequest('An extra set is already in progress');
|
||||
const maxIndex = await tx.sessionSet.aggregate({
|
||||
where: { exerciseId: exercise.id },
|
||||
|
||||
@@ -830,7 +830,8 @@ export const openapi: Record<string, unknown> = {
|
||||
'/sessions/exercises/{id}/sets': {
|
||||
post: {
|
||||
tags: ['Sessions'],
|
||||
summary: 'Add set to session exercise',
|
||||
summary:
|
||||
"Add extra (bonus) set to session exercise (requires this exercise's planned sets recorded; at most one pending extra per session)",
|
||||
security: [{ bearerAuth: [] }],
|
||||
parameters: [{ name: 'id', in: 'path', required: true, schema: { type: 'string' } }],
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user