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:
@@ -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 },
|
||||
|
||||
Reference in New Issue
Block a user