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,5 +1,5 @@
PORT=3002
DATABASE_URL="file:./dev.db"
DATABASE_URL="file:D:/Coding/gymflow/server/prisma/dev.db"
JWT_SECRET="supersecretkey_change_in_production"
API_KEY="AIzaSyCiu9gD-BcsbyIT1qpPIJrKvz_2sVyZE9A"
ADMIN_EMAIL=admin@gymflow.ai

0
server/dev.db Normal file
View File

721
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,17 +5,21 @@
"main": "src/index.ts",
"scripts": {
"start": "node dist/index.js",
"dev": "nodemon src/index.ts",
"dev": "ts-node-dev -r dotenv/config --respawn --transpile-only src/index.ts",
"build": "tsc"
},
"dependencies": {
"@google/generative-ai": "^0.24.1",
"@prisma/adapter-better-sqlite3": "^7.1.0",
"@prisma/client": "*",
"@types/better-sqlite3": "^7.6.13",
"bcryptjs": "*",
"better-sqlite3": "^12.5.0",
"cors": "*",
"dotenv": "*",
"express": "*",
"jsonwebtoken": "*"
"jsonwebtoken": "*",
"ts-node-dev": "^2.0.0"
},
"devDependencies": {
"@types/bcryptjs": "*",

Binary file not shown.

View File

@@ -0,0 +1,116 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL PRIMARY KEY,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"role" TEXT NOT NULL DEFAULT 'USER',
"isFirstLogin" BOOLEAN NOT NULL DEFAULT true,
"isBlocked" BOOLEAN NOT NULL DEFAULT false,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
-- CreateTable
CREATE TABLE "BodyWeightRecord" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"weight" REAL NOT NULL,
"date" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"dateStr" TEXT NOT NULL,
CONSTRAINT "BodyWeightRecord_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "UserProfile" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"weight" REAL,
"height" REAL,
"gender" TEXT,
"birthDate" DATETIME,
"language" TEXT DEFAULT 'en',
CONSTRAINT "UserProfile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Exercise" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT,
"name" TEXT NOT NULL,
"type" TEXT NOT NULL,
"bodyWeightPercentage" REAL DEFAULT 0,
"isArchived" BOOLEAN NOT NULL DEFAULT false,
"isUnilateral" BOOLEAN NOT NULL DEFAULT false,
CONSTRAINT "Exercise_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "WorkoutSession" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"startTime" DATETIME NOT NULL,
"endTime" DATETIME,
"userBodyWeight" REAL,
"note" TEXT,
"planId" TEXT,
"planName" TEXT,
CONSTRAINT "WorkoutSession_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "WorkoutSet" (
"id" TEXT NOT NULL PRIMARY KEY,
"sessionId" TEXT NOT NULL,
"exerciseId" TEXT NOT NULL,
"order" INTEGER NOT NULL,
"weight" REAL,
"reps" INTEGER,
"distanceMeters" REAL,
"durationSeconds" INTEGER,
"completed" BOOLEAN NOT NULL DEFAULT true,
"side" TEXT,
CONSTRAINT "WorkoutSet_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "WorkoutSession" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "WorkoutSet_exerciseId_fkey" FOREIGN KEY ("exerciseId") REFERENCES "Exercise" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "WorkoutPlan" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"exercises" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "WorkoutPlan_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "SporadicSet" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"exerciseId" TEXT NOT NULL,
"weight" REAL,
"reps" INTEGER,
"distanceMeters" REAL,
"durationSeconds" INTEGER,
"height" REAL,
"bodyWeightPercentage" REAL,
"side" TEXT,
"timestamp" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"note" TEXT,
CONSTRAINT "SporadicSet_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "SporadicSet_exerciseId_fkey" FOREIGN KEY ("exerciseId") REFERENCES "Exercise" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE UNIQUE INDEX "BodyWeightRecord_userId_dateStr_key" ON "BodyWeightRecord"("userId", "dateStr");
-- CreateIndex
CREATE UNIQUE INDEX "UserProfile_userId_key" ON "UserProfile"("userId");
-- CreateIndex
CREATE INDEX "SporadicSet_userId_timestamp_idx" ON "SporadicSet"("userId", "timestamp");

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "sqlite"

View File

@@ -58,6 +58,7 @@ model Exercise {
type String // STRENGTH, CARDIO, BODYWEIGHT, STATIC
bodyWeightPercentage Float? @default(0)
isArchived Boolean @default(false)
isUnilateral Boolean @default(false)
sets WorkoutSet[]
sporadicSets SporadicSet[]
@@ -90,6 +91,7 @@ model WorkoutSet {
distanceMeters Float?
durationSeconds Int?
completed Boolean @default(true)
side String? // LEFT, RIGHT, or null for bilateral
}
model WorkoutPlan {
@@ -116,6 +118,7 @@ model SporadicSet {
durationSeconds Int?
height Float?
bodyWeightPercentage Float?
side String? // LEFT, RIGHT, or null for bilateral
timestamp DateTime @default(now())
note String?

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) => {