enhancment complete with phase 10

This commit is contained in:
dimitar committed 2026-03-10 05:37:22 +01:00
1 parent 2c4927478a
commit 6adb541967
44 files changed
+7820 -845

No files matched your search

+29 -19
View File
@@ -3,6 +3,11 @@ import { auth } from "@clerk/nextjs/server";
import Database from "better-sqlite3";
import { randomBytes } from "crypto";
import log from "@/lib/logger";
import { fitnessProfileSchema } from "@/lib/validation/schemas";
import {
validateRequestBody,
validationErrorResponse,
} from "@/lib/validation/helpers";
const db = new Database("./data/fitai.db");
@@ -48,20 +53,28 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body = await request.json();
// Validate request body
const validation = await validateRequestBody(request, fitnessProfileSchema);
if (!validation.success) {
log.warn("Fitness profile validation failed", {
errors: validation.errors,
});
return validationErrorResponse(validation.errors);
}
const {
height,
weight,
age,
gender,
fitnessGoals, // Changed from fitnessGoal
fitnessGoals,
activityLevel,
medicalConditions,
allergies,
injuries,
exerciseHabits,
dietHabits,
} = body;
preferences,
} = validation.data;
// Check if profile exists
const existingProfile = db
@@ -84,22 +97,20 @@ export async function POST(request: NextRequest) {
medicalConditions = ?,
allergies = ?,
injuries = ?,
exerciseHabits = ?,
dietHabits = ?,
preferences = ?,
updatedAt = ?
WHERE userId = ?`,
).run(
height || null,
weight || null,
age || null,
height,
weight,
age,
gender || null,
fitnessGoalsJson,
activityLevel || null,
medicalConditions || null,
allergies || null,
injuries || null,
exerciseHabits || null,
dietHabits || null,
preferences || null,
now,
userId,
);
@@ -113,21 +124,20 @@ export async function POST(request: NextRequest) {
db.prepare(
`INSERT INTO fitness_profiles
(userId, height, weight, age, gender, fitnessGoals, activityLevel,
medicalConditions, allergies, injuries, exerciseHabits, dietHabits, createdAt, updatedAt)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
medicalConditions, allergies, injuries, preferences, createdAt, updatedAt)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
).run(
userId,
height || null,
weight || null,
age || null,
height,
weight,
age,
gender || null,
fitnessGoalsJson,
activityLevel || null,
medicalConditions || null,
allergies || null,
injuries || null,
exerciseHabits || null,
dietHabits || null,
preferences || null,
now,
now,
);