Unilateral exercises logging

This commit is contained in:
AG
2025-12-03 23:30:32 +02:00
parent 50f3d4d49b
commit a632de65ea
24 changed files with 1656 additions and 244 deletions

View File

@@ -1,6 +1,5 @@
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import authRoutes from './routes/auth';
import exerciseRoutes from './routes/exercises';
import sessionRoutes from './routes/sessions';
@@ -10,8 +9,9 @@ import weightRoutes from './routes/weight';
import sporadicSetsRoutes from './routes/sporadic-sets';
import bcrypt from 'bcryptjs';
import { PrismaClient } from '@prisma/client';
dotenv.config();
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import BetterSqlite3 from 'better-sqlite3';
import path from 'path';
const app = express();
@@ -22,7 +22,8 @@ async function ensureAdminUser() {
const adminEmail = process.env.ADMIN_EMAIL || 'admin@gymflow.ai';
const adminPassword = process.env.ADMIN_PASSWORD || 'admin1234';
const prisma = new PrismaClient();
// Use the singleton prisma client
const prisma = (await import('./lib/prisma')).default;
// Check for existing admin
const existingAdmin = await prisma.user.findFirst({

33
server/src/lib/prisma.ts Normal file
View File

@@ -0,0 +1,33 @@
import { PrismaClient } from '@prisma/client';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import BetterSqlite3 from 'better-sqlite3';
import path from 'path';
// Ensure env vars are loaded
import 'dotenv/config';
declare global {
// allow global `var` declarations
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined;
}
const dbUrl = process.env.DATABASE_URL;
if (!dbUrl) {
throw new Error("DATABASE_URL environment variable is not set. Please check your .env file.");
}
const adapter = new PrismaBetterSqlite3({ url: dbUrl });
const prisma =
global.prisma ||
new PrismaClient({
adapter,
});
if (process.env.NODE_ENV !== 'production') {
global.prisma = prisma;
}
export default prisma;

View File

@@ -1,10 +1,9 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import prisma from '../lib/prisma';
const router = express.Router();
const prisma = new PrismaClient();
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
// Get Current User

View File

@@ -1,9 +1,8 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import jwt from 'jsonwebtoken';
import prisma from '../lib/prisma';
const router = express.Router();
const prisma = new PrismaClient();
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
// Middleware to check auth
@@ -46,9 +45,15 @@ router.get('/', async (req: any, res) => {
router.post('/', async (req: any, res) => {
try {
const userId = req.user.userId;
const { id, name, type, bodyWeightPercentage, isArchived } = req.body;
const { id, name, type, bodyWeightPercentage, isArchived, isUnilateral } = req.body;
const data = {
name,
type,
bodyWeightPercentage: bodyWeightPercentage ? parseFloat(bodyWeightPercentage) : undefined,
isArchived: !!isArchived,
isUnilateral: !!isUnilateral
};
// If id exists and belongs to user, update. Else create.
// Note: We can't update system exercises directly. If user edits a system exercise,
@@ -62,7 +67,7 @@ router.post('/', async (req: any, res) => {
const updated = await prisma.exercise.update({
where: { id },
data: { name, type, bodyWeightPercentage, isArchived }
data: data
});
return res.json(updated);
@@ -74,10 +79,11 @@ router.post('/', async (req: any, res) => {
data: {
id: id || undefined, // Use provided ID if available
userId,
name,
type,
bodyWeightPercentage,
isArchived: isArchived || false
name: data.name,
type: data.type,
bodyWeightPercentage: data.bodyWeightPercentage,
isArchived: data.isArchived,
isUnilateral: data.isUnilateral,
}
});
res.json(newExercise);

View File

@@ -1,9 +1,8 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import jwt from 'jsonwebtoken';
import prisma from '../lib/prisma';
const router = express.Router();
const prisma = new PrismaClient();
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
const authenticate = (req: any, res: any, next: any) => {

View File

@@ -1,9 +1,8 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import jwt from 'jsonwebtoken';
import prisma from '../lib/prisma';
const router = express.Router();
const prisma = new PrismaClient();
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
const authenticate = (req: any, res: any, next: any) => {

View File

@@ -1,9 +1,8 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import jwt from 'jsonwebtoken';
import prisma from '../lib/prisma';
const router = express.Router();
const prisma = new PrismaClient();
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
const authenticate = (req: any, res: any, next: any) => {
@@ -58,12 +57,22 @@ router.get('/', async (req: any, res) => {
router.post('/', async (req: any, res) => {
try {
const userId = req.user.userId;
const { exerciseId, weight, reps, distanceMeters, durationSeconds, height, bodyWeightPercentage, note } = req.body;
const { exerciseId, weight, reps, distanceMeters, durationSeconds, height, bodyWeightPercentage, note, side } = req.body;
if (!exerciseId) {
return res.status(400).json({ error: 'Exercise ID is required' });
}
// Verify that the exercise exists
const exercise = await prisma.exercise.findUnique({
where: { id: exerciseId }
});
if (!exercise) {
return res.status(400).json({ error: `Exercise with ID ${exerciseId} not found` });
}
const sporadicSet = await prisma.sporadicSet.create({
data: {
userId,
@@ -74,7 +83,8 @@ router.post('/', async (req: any, res) => {
durationSeconds: durationSeconds ? parseInt(durationSeconds) : null,
height: height ? parseFloat(height) : null,
bodyWeightPercentage: bodyWeightPercentage ? parseFloat(bodyWeightPercentage) : null,
note: note || null
note: note || null,
side: side || null
},
include: { exercise: true }
});
@@ -91,7 +101,8 @@ router.post('/', async (req: any, res) => {
height: sporadicSet.height,
bodyWeightPercentage: sporadicSet.bodyWeightPercentage,
timestamp: sporadicSet.timestamp.getTime(),
note: sporadicSet.note
note: sporadicSet.note,
side: sporadicSet.side
};
res.json({ success: true, sporadicSet: mappedSet });
@@ -106,7 +117,7 @@ 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;
const { weight, reps, distanceMeters, durationSeconds, height, bodyWeightPercentage, note, side } = req.body;
// Verify ownership
const existing = await prisma.sporadicSet.findFirst({
@@ -126,7 +137,8 @@ router.put('/:id', async (req: any, res) => {
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
note: note !== undefined ? note : undefined,
side: side !== undefined ? side : undefined
},
include: { exercise: true }
});
@@ -143,7 +155,8 @@ router.put('/:id', async (req: any, res) => {
height: updated.height,
bodyWeightPercentage: updated.bodyWeightPercentage,
timestamp: updated.timestamp.getTime(),
note: updated.note
note: updated.note,
side: updated.side
};
res.json({ success: true, sporadicSet: mappedSet });

View File

@@ -1,9 +1,8 @@
import express from 'express';
import { PrismaClient } from '@prisma/client';
import { authenticateToken } from '../middleware/auth';
import prisma from '../lib/prisma';
const router = express.Router();
const prisma = new PrismaClient();
// Get weight history
router.get('/', authenticateToken, async (req, res) => {