Plan state is persistent during session
This commit is contained in:
@@ -219,6 +219,163 @@ router.put('/active', async (req: any, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Log a set to the active session
|
||||
router.post('/active/log-set', async (req: any, res) => {
|
||||
try {
|
||||
const userId = req.user.userId;
|
||||
const { exerciseId, reps, weight, distanceMeters, durationSeconds } = req.body;
|
||||
|
||||
// Find active session
|
||||
const activeSession = await prisma.workoutSession.findFirst({
|
||||
where: { userId, endTime: null },
|
||||
include: { sets: true }
|
||||
});
|
||||
|
||||
if (!activeSession) {
|
||||
return res.status(404).json({ error: 'No active session found' });
|
||||
}
|
||||
|
||||
// Get the highest order value from the existing sets
|
||||
const maxOrder = activeSession.sets.reduce((max, set) => Math.max(max, set.order), -1);
|
||||
|
||||
// Create the new set
|
||||
const newSet = await prisma.workoutSet.create({
|
||||
data: {
|
||||
sessionId: activeSession.id,
|
||||
exerciseId,
|
||||
order: maxOrder + 1,
|
||||
reps: reps ? parseInt(reps) : null,
|
||||
weight: weight ? parseFloat(weight) : null,
|
||||
distanceMeters: distanceMeters ? parseFloat(distanceMeters) : null,
|
||||
durationSeconds: durationSeconds ? parseInt(durationSeconds) : null,
|
||||
completed: true
|
||||
},
|
||||
include: { exercise: true }
|
||||
});
|
||||
|
||||
// Recalculate active step
|
||||
if (activeSession.planId) {
|
||||
const plan = await prisma.workoutPlan.findUnique({
|
||||
where: { id: activeSession.planId }
|
||||
});
|
||||
|
||||
if (plan) {
|
||||
const planExercises: { id: string }[] = JSON.parse(plan.exercises);
|
||||
const allPerformedSets = await prisma.workoutSet.findMany({
|
||||
where: { sessionId: activeSession.id }
|
||||
});
|
||||
|
||||
const performedCounts = new Map<string, number>();
|
||||
for (const set of allPerformedSets) {
|
||||
performedCounts.set(set.exerciseId, (performedCounts.get(set.exerciseId) || 0) + 1);
|
||||
}
|
||||
|
||||
let activeExerciseId = null;
|
||||
const plannedCounts = new Map<string, number>();
|
||||
for (const planExercise of planExercises) {
|
||||
const exerciseId = planExercise.id;
|
||||
plannedCounts.set(exerciseId, (plannedCounts.get(exerciseId) || 0) + 1);
|
||||
const performedCount = performedCounts.get(exerciseId) || 0;
|
||||
|
||||
if (performedCount < plannedCounts.get(exerciseId)!) {
|
||||
activeExerciseId = exerciseId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const mappedNewSet = {
|
||||
...newSet,
|
||||
exerciseName: newSet.exercise.name,
|
||||
type: newSet.exercise.type
|
||||
};
|
||||
|
||||
return res.json({ success: true, newSet: mappedNewSet, activeExerciseId });
|
||||
}
|
||||
}
|
||||
|
||||
// If no plan or plan not found, just return the new set
|
||||
const mappedNewSet = {
|
||||
...newSet,
|
||||
exerciseName: newSet.exercise.name,
|
||||
type: newSet.exercise.type
|
||||
};
|
||||
|
||||
res.json({ success: true, newSet: mappedNewSet, activeExerciseId: null });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Update a set in the active session
|
||||
router.put('/active/set/:setId', async (req: any, res) => {
|
||||
try {
|
||||
const userId = req.user.userId;
|
||||
const { setId } = req.params;
|
||||
const { reps, weight, distanceMeters, durationSeconds } = req.body;
|
||||
|
||||
// Find active session
|
||||
const activeSession = await prisma.workoutSession.findFirst({
|
||||
where: { userId, endTime: null },
|
||||
});
|
||||
|
||||
if (!activeSession) {
|
||||
return res.status(404).json({ error: 'No active session found' });
|
||||
}
|
||||
|
||||
const updatedSet = await prisma.workoutSet.update({
|
||||
where: { id: setId },
|
||||
data: {
|
||||
reps: reps ? parseInt(reps) : null,
|
||||
weight: weight ? parseFloat(weight) : null,
|
||||
distanceMeters: distanceMeters ? parseFloat(distanceMeters) : null,
|
||||
durationSeconds: durationSeconds ? parseInt(durationSeconds) : null,
|
||||
},
|
||||
include: { exercise: true }
|
||||
});
|
||||
|
||||
const mappedUpdatedSet = {
|
||||
...updatedSet,
|
||||
exerciseName: updatedSet.exercise.name,
|
||||
type: updatedSet.exercise.type
|
||||
};
|
||||
|
||||
res.json({ success: true, updatedSet: mappedUpdatedSet });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Delete a set from the active session
|
||||
router.delete('/active/set/:setId', async (req: any, res) => {
|
||||
try {
|
||||
const userId = req.user.userId;
|
||||
const { setId } = req.params;
|
||||
|
||||
// Find active session
|
||||
const activeSession = await prisma.workoutSession.findFirst({
|
||||
where: { userId, endTime: null },
|
||||
});
|
||||
|
||||
if (!activeSession) {
|
||||
return res.status(404).json({ error: 'No active session found' });
|
||||
}
|
||||
|
||||
await prisma.workoutSet.delete({
|
||||
where: { id: setId }
|
||||
});
|
||||
|
||||
res.json({ success: true });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Delete active session (quit without saving)
|
||||
router.delete('/active', async (req: any, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user