invite and saving fitness profile

added, need polishing
This commit is contained in:
dimitar committed 2025-11-20 18:36:40 +01:00
1 parent daaf5aa2dd
commit 118efad70f
10 files changed
+202 -72

No files matched your search

+36 -20
View File
@@ -16,10 +16,20 @@ export async function GET(request: NextRequest) {
const profile = db
.prepare(
`SELECT * FROM fitness_profiles WHERE user_id = ?`
`SELECT * FROM fitness_profiles WHERE userId = ?`
)
.get(userId);
if (profile) {
const p = profile as any;
// Parse JSON fields
try {
p.fitnessGoals = JSON.parse(p.fitnessGoals || '[]');
} catch (e) {
p.fitnessGoals = [];
}
}
return NextResponse.json({ profile: profile || null });
} catch (error) {
console.error("Error fetching fitness profile:", error);
@@ -45,19 +55,22 @@ export async function POST(request: NextRequest) {
weight,
age,
gender,
fitnessGoal,
fitnessGoals, // Changed from fitnessGoal
activityLevel,
medicalConditions,
allergies,
injuries,
exerciseHabits,
dietHabits
} = body;
// Check if profile exists
const existingProfile = db
.prepare(`SELECT id FROM fitness_profiles WHERE user_id = ?`)
.get(userId) as { id: string } | undefined;
.prepare(`SELECT userId FROM fitness_profiles WHERE userId = ?`)
.get(userId) as { userId: string } | undefined;
const now = Date.now();
const now = new Date().toISOString();
const fitnessGoalsJson = JSON.stringify(fitnessGoals || []);
if (existingProfile) {
// Update existing profile
@@ -67,52 +80,55 @@ export async function POST(request: NextRequest) {
weight = ?,
age = ?,
gender = ?,
fitness_goal = ?,
activity_level = ?,
medical_conditions = ?,
fitnessGoals = ?,
activityLevel = ?,
medicalConditions = ?,
allergies = ?,
injuries = ?,
updated_at = ?
WHERE user_id = ?`
exerciseHabits = ?,
dietHabits = ?,
updatedAt = ?
WHERE userId = ?`
).run(
height || null,
weight || null,
age || null,
gender || null,
fitnessGoal || null,
fitnessGoalsJson,
activityLevel || null,
medicalConditions || null,
allergies || null,
injuries || null,
exerciseHabits || null,
dietHabits || null,
now,
userId
);
return NextResponse.json({
message: "Fitness profile updated successfully",
profileId: existingProfile.id,
userId: userId,
});
} else {
// Create new profile
const profileId = `fp_${randomBytes(16).toString("hex")}`;
db.prepare(
`INSERT INTO fitness_profiles
(id, user_id, height, weight, age, gender, fitness_goal, activity_level,
medical_conditions, allergies, injuries, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
(userId, height, weight, age, gender, fitnessGoals, activityLevel,
medicalConditions, allergies, injuries, exerciseHabits, dietHabits, createdAt, updatedAt)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(
profileId,
userId,
height || null,
weight || null,
age || null,
gender || null,
fitnessGoal || null,
fitnessGoalsJson,
activityLevel || null,
medicalConditions || null,
allergies || null,
injuries || null,
exerciseHabits || null,
dietHabits || null,
now,
now
);
@@ -120,7 +136,7 @@ export async function POST(request: NextRequest) {
return NextResponse.json(
{
message: "Fitness profile created successfully",
profileId,
userId,
},
{ status: 201 }
);