Ongoing workout session data is persistent now
This commit is contained in:
@@ -133,6 +133,121 @@ router.post('/', async (req: any, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Get active session (session without endTime)
|
||||
router.get('/active', async (req: any, res) => {
|
||||
try {
|
||||
const userId = req.user.userId;
|
||||
const activeSession = await prisma.workoutSession.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
endTime: null
|
||||
},
|
||||
include: { sets: { include: { exercise: true }, orderBy: { order: 'asc' } } }
|
||||
});
|
||||
|
||||
if (!activeSession) {
|
||||
return res.json({ success: true, session: null });
|
||||
}
|
||||
|
||||
res.json({ success: true, session: activeSession });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Update active session (for real-time set updates)
|
||||
router.put('/active', async (req: any, res) => {
|
||||
try {
|
||||
const userId = req.user.userId;
|
||||
const { id, startTime, endTime, userBodyWeight, note, planId, planName, sets } = req.body;
|
||||
|
||||
// Convert timestamps to Date objects if they are numbers
|
||||
const start = new Date(startTime);
|
||||
const end = endTime ? new Date(endTime) : null;
|
||||
const weight = userBodyWeight ? parseFloat(userBodyWeight) : null;
|
||||
|
||||
// Check if session exists and belongs to user
|
||||
const existing = await prisma.workoutSession.findFirst({
|
||||
where: { id, userId }
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return res.status(404).json({ error: 'Session not found' });
|
||||
}
|
||||
|
||||
// Delete existing sets to replace them
|
||||
await prisma.workoutSet.deleteMany({ where: { sessionId: id } });
|
||||
|
||||
const updated = await prisma.workoutSession.update({
|
||||
where: { id },
|
||||
data: {
|
||||
startTime: start,
|
||||
endTime: end,
|
||||
userBodyWeight: weight,
|
||||
note,
|
||||
planId,
|
||||
planName,
|
||||
sets: {
|
||||
create: sets.map((s: any, idx: number) => ({
|
||||
exerciseId: s.exerciseId,
|
||||
order: idx,
|
||||
weight: s.weight,
|
||||
reps: s.reps,
|
||||
distanceMeters: s.distanceMeters,
|
||||
durationSeconds: s.durationSeconds,
|
||||
completed: s.completed !== undefined ? s.completed : true
|
||||
}))
|
||||
}
|
||||
},
|
||||
include: { sets: { include: { exercise: true } } }
|
||||
});
|
||||
|
||||
// Update user profile weight if session has weight and is finished
|
||||
if (weight && end) {
|
||||
await prisma.userProfile.upsert({
|
||||
where: { userId },
|
||||
create: { userId, weight },
|
||||
update: { weight }
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ success: true, session: updated });
|
||||
} 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 {
|
||||
const userId = req.user.userId;
|
||||
|
||||
// Find active session
|
||||
const activeSession = await prisma.workoutSession.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
endTime: null
|
||||
}
|
||||
});
|
||||
|
||||
if (!activeSession) {
|
||||
return res.json({ success: true, message: 'No active session found' });
|
||||
}
|
||||
|
||||
// Delete the session (cascade will delete sets)
|
||||
await prisma.workoutSession.delete({
|
||||
where: { id: activeSession.id }
|
||||
});
|
||||
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// Delete session
|
||||
router.delete('/:id', async (req: any, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user