Refactor: separera user_measurements och user_strength tabeller

- Ny databasstruktur för historik/progress tracking
- Nya endpoints: POST/GET measurements och strength
- Onboarding sparar till rätt tabeller
- Beräknar och sparar body_fat_pct
- Fixar tomma numeriska fält (null istället för '')
- Döljer 1RM för nybörjare
This commit is contained in:
2026-02-01 00:10:48 +01:00
parent 04d74469c7
commit 613f1666e1
3 changed files with 157 additions and 16 deletions
+95 -6
View File
@@ -69,9 +69,31 @@ app.post('/api/auth/login', async (req, res) => {
app.get('/api/user/profile', authMiddleware, async (req, res) => {
try {
const result = await pool.query('SELECT id, email, gender, age, weight, neck_cm, waist_cm, hip_cm, experience_level, bench_1rm, squat_1rm, deadlift_1rm, goal, workouts_per_week, onboarding_complete FROM users WHERE id = $1', [req.user.id]);
if (!result.rows.length) return res.status(404).json({ error: 'User not found' });
res.json(result.rows[0]);
const userResult = await pool.query(
'SELECT id, email, gender, age, height_cm, experience_level, goal, workouts_per_week, onboarding_complete FROM users WHERE id = $1',
[req.user.id]
);
if (!userResult.rows.length) return res.status(404).json({ error: 'User not found' });
const user = userResult.rows[0];
// Get latest measurements
const measResult = await pool.query(
'SELECT weight, neck_cm, waist_cm, hip_cm, body_fat_pct, measured_at FROM user_measurements WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 1',
[req.user.id]
);
// Get latest strength
const strResult = await pool.query(
'SELECT bench_1rm, squat_1rm, deadlift_1rm, measured_at FROM user_strength WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 1',
[req.user.id]
);
res.json({
...user,
measurements: measResult.rows[0] || null,
strength: strResult.rows[0] || null
});
} catch (err) {
console.error('Profile error:', err);
res.status(500).json({ error: 'Server error' });
@@ -80,10 +102,13 @@ app.get('/api/user/profile', authMiddleware, async (req, res) => {
app.put('/api/user/profile', authMiddleware, async (req, res) => {
try {
const { gender, age, weight, neck_cm, waist_cm, hip_cm, experience_level, bench_1rm, squat_1rm, deadlift_1rm, goal, workouts_per_week, onboarding_complete } = req.body;
const { gender, age, height_cm, experience_level, goal, workouts_per_week, onboarding_complete } = req.body;
const num = v => (v === '' || v === undefined) ? null : v;
const result = await pool.query(
`UPDATE users SET gender=$1, age=$2, weight=$3, neck_cm=$4, waist_cm=$5, hip_cm=$6, experience_level=$7, bench_1rm=$8, squat_1rm=$9, deadlift_1rm=$10, goal=$11, workouts_per_week=$12, onboarding_complete=$13 WHERE id=$14 RETURNING id, email, gender, age, weight, neck_cm, waist_cm, hip_cm, experience_level, bench_1rm, squat_1rm, deadlift_1rm, goal, workouts_per_week, onboarding_complete`,
[gender, age, weight, neck_cm, waist_cm, hip_cm, experience_level, bench_1rm, squat_1rm, deadlift_1rm, goal, workouts_per_week, onboarding_complete, req.user.id]
`UPDATE users SET gender=$1, age=$2, height_cm=$3, experience_level=$4, goal=$5, workouts_per_week=$6, onboarding_complete=$7
WHERE id=$8 RETURNING id, email, gender, age, height_cm, experience_level, goal, workouts_per_week, onboarding_complete`,
[gender, num(age), num(height_cm), experience_level, goal, num(workouts_per_week), onboarding_complete, req.user.id]
);
res.json(result.rows[0]);
} catch (err) {
@@ -92,6 +117,70 @@ app.put('/api/user/profile', authMiddleware, async (req, res) => {
}
});
// Add measurements
app.post('/api/user/measurements', authMiddleware, async (req, res) => {
try {
const { weight, neck_cm, waist_cm, hip_cm, body_fat_pct } = req.body;
const num = v => (v === '' || v === undefined) ? null : v;
const result = await pool.query(
`INSERT INTO user_measurements (user_id, weight, neck_cm, waist_cm, hip_cm, body_fat_pct)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
[req.user.id, num(weight), num(neck_cm), num(waist_cm), num(hip_cm), num(body_fat_pct)]
);
res.json(result.rows[0]);
} catch (err) {
console.error('Add measurements error:', err);
res.status(500).json({ error: 'Server error' });
}
});
// Get measurements history
app.get('/api/user/measurements', authMiddleware, async (req, res) => {
try {
const result = await pool.query(
'SELECT * FROM user_measurements WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 100',
[req.user.id]
);
res.json(result.rows);
} catch (err) {
console.error('Get measurements error:', err);
res.status(500).json({ error: 'Server error' });
}
});
// Add strength record
app.post('/api/user/strength', authMiddleware, async (req, res) => {
try {
const { bench_1rm, squat_1rm, deadlift_1rm } = req.body;
const num = v => (v === '' || v === undefined) ? null : v;
const result = await pool.query(
`INSERT INTO user_strength (user_id, bench_1rm, squat_1rm, deadlift_1rm)
VALUES ($1, $2, $3, $4) RETURNING *`,
[req.user.id, num(bench_1rm), num(squat_1rm), num(deadlift_1rm)]
);
res.json(result.rows[0]);
} catch (err) {
console.error('Add strength error:', err);
res.status(500).json({ error: 'Server error' });
}
});
// Get strength history
app.get('/api/user/strength', authMiddleware, async (req, res) => {
try {
const result = await pool.query(
'SELECT * FROM user_strength WHERE user_id = $1 ORDER BY measured_at DESC LIMIT 100',
[req.user.id]
);
res.json(result.rows);
} catch (err) {
console.error('Get strength error:', err);
res.status(500).json({ error: 'Server error' });
}
});
// Get all programs
app.get('/api/programs', async (req, res) => {
try {