Sporadic set logging added

This commit is contained in:
AG
2025-11-29 19:03:42 +02:00
parent d86abd6b1b
commit b5c8e8ac43
15 changed files with 1491 additions and 396 deletions

View File

@@ -0,0 +1,182 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import jwt from 'jsonwebtoken';
const router = express.Router();
const prisma = new PrismaClient();
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
const authenticate = (req: any, res: any, next: any) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'Unauthorized' });
try {
const decoded = jwt.verify(token, JWT_SECRET) as any;
req.user = decoded;
next();
} catch {
res.status(401).json({ error: 'Invalid token' });
}
};
router.use(authenticate);
// Get all sporadic sets for the authenticated user
router.get('/', async (req: any, res) => {
try {
const userId = req.user.userId;
const sporadicSets = await prisma.sporadicSet.findMany({
where: { userId },
include: { exercise: true },
orderBy: { timestamp: 'desc' }
});
// Map to include exercise name and type
const mappedSets = sporadicSets.map(set => ({
id: set.id,
exerciseId: set.exerciseId,
exerciseName: set.exercise.name,
type: set.exercise.type,
weight: set.weight,
reps: set.reps,
distanceMeters: set.distanceMeters,
durationSeconds: set.durationSeconds,
height: set.height,
bodyWeightPercentage: set.bodyWeightPercentage,
timestamp: set.timestamp.getTime(),
note: set.note
}));
res.json({ success: true, sporadicSets: mappedSets });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Server error' });
}
});
// Create a new sporadic set
router.post('/', async (req: any, res) => {
try {
const userId = req.user.userId;
const { exerciseId, weight, reps, distanceMeters, durationSeconds, height, bodyWeightPercentage, note } = req.body;
if (!exerciseId) {
return res.status(400).json({ error: 'Exercise ID is required' });
}
const sporadicSet = await prisma.sporadicSet.create({
data: {
userId,
exerciseId,
weight: weight ? parseFloat(weight) : null,
reps: reps ? parseInt(reps) : null,
distanceMeters: distanceMeters ? parseFloat(distanceMeters) : null,
durationSeconds: durationSeconds ? parseInt(durationSeconds) : null,
height: height ? parseFloat(height) : null,
bodyWeightPercentage: bodyWeightPercentage ? parseFloat(bodyWeightPercentage) : null,
note: note || null
},
include: { exercise: true }
});
const mappedSet = {
id: sporadicSet.id,
exerciseId: sporadicSet.exerciseId,
exerciseName: sporadicSet.exercise.name,
type: sporadicSet.exercise.type,
weight: sporadicSet.weight,
reps: sporadicSet.reps,
distanceMeters: sporadicSet.distanceMeters,
durationSeconds: sporadicSet.durationSeconds,
height: sporadicSet.height,
bodyWeightPercentage: sporadicSet.bodyWeightPercentage,
timestamp: sporadicSet.timestamp.getTime(),
note: sporadicSet.note
};
res.json({ success: true, sporadicSet: mappedSet });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Server error' });
}
});
// Update a sporadic set
router.put('/:id', async (req: any, res) => {
try {
const userId = req.user.userId;
const { id } = req.params;
const { weight, reps, distanceMeters, durationSeconds, height, bodyWeightPercentage, note } = req.body;
// Verify ownership
const existing = await prisma.sporadicSet.findFirst({
where: { id, userId }
});
if (!existing) {
return res.status(404).json({ error: 'Sporadic set not found' });
}
const updated = await prisma.sporadicSet.update({
where: { id },
data: {
weight: weight !== undefined ? (weight ? parseFloat(weight) : null) : undefined,
reps: reps !== undefined ? (reps ? parseInt(reps) : null) : undefined,
distanceMeters: distanceMeters !== undefined ? (distanceMeters ? parseFloat(distanceMeters) : null) : undefined,
durationSeconds: durationSeconds !== undefined ? (durationSeconds ? parseInt(durationSeconds) : null) : undefined,
height: height !== undefined ? (height ? parseFloat(height) : null) : undefined,
bodyWeightPercentage: bodyWeightPercentage !== undefined ? (bodyWeightPercentage ? parseFloat(bodyWeightPercentage) : null) : undefined,
note: note !== undefined ? note : undefined
},
include: { exercise: true }
});
const mappedSet = {
id: updated.id,
exerciseId: updated.exerciseId,
exerciseName: updated.exercise.name,
type: updated.exercise.type,
weight: updated.weight,
reps: updated.reps,
distanceMeters: updated.distanceMeters,
durationSeconds: updated.durationSeconds,
height: updated.height,
bodyWeightPercentage: updated.bodyWeightPercentage,
timestamp: updated.timestamp.getTime(),
note: updated.note
};
res.json({ success: true, sporadicSet: mappedSet });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Server error' });
}
});
// Delete a sporadic set
router.delete('/:id', async (req: any, res) => {
try {
const userId = req.user.userId;
const { id } = req.params;
// Verify ownership
const existing = await prisma.sporadicSet.findFirst({
where: { id, userId }
});
if (!existing) {
return res.status(404).json({ error: 'Sporadic set not found' });
}
await prisma.sporadicSet.delete({
where: { id }
});
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Server error' });
}
});
export default router;