now gym in UI displays properly

This commit is contained in:
dimitar committed 2025-12-18 19:14:40 +01:00
1 parent 6580564767
commit 339d798a88
9 files changed
+841 -465

No files matched your search

+14
View File
@@ -12,6 +12,7 @@ import { db, users as usersTable, eq, sql } from "@fitai/database";
export async function PATCH(req: Request) {
try {
const { userId } = await auth();
console.log("PATCH /api/users/gym auth userId:", userId);
if (!userId) return new NextResponse("Unauthorized", { status: 401 });
const body = await req.json().catch(() => null);
@@ -23,25 +24,32 @@ export async function PATCH(req: Request) {
}
const gymId = body.gymId === null ? null : String(body.gymId);
console.log("PATCH /api/users/gym parsed gymId from body:", gymId);
// Ensure user exists
console.log("PATCH /api/users/gym fetching user by id:", userId);
const user = await db
.select()
.from(usersTable)
.where(eq(usersTable.id, userId))
.get();
console.log("PATCH /api/users/gym fetched user:", user);
if (!user) return new NextResponse("User not found", { status: 404 });
// Validate gym when provided
if (gymId) {
console.log("PATCH /api/users/gym validating gym:", gymId);
const rows = await db.all(
sql`SELECT status FROM gyms WHERE id = ${gymId} LIMIT 1`,
);
console.log("PATCH /api/users/gym validation query result rows:", rows);
const gym = rows?.[0] as { status?: string } | undefined;
if (!gym) {
console.log("PATCH /api/users/gym validation: gym not found");
return NextResponse.json({ error: "Gym not found" }, { status: 404 });
}
if (gym.status !== "active") {
console.log("PATCH /api/users/gym validation: gym not active", gym);
return NextResponse.json(
{ error: "Gym is not active" },
{ status: 400 },
@@ -50,15 +58,21 @@ export async function PATCH(req: Request) {
}
// Update user's gym selection
console.log("PATCH /api/users/gym updating user gym_id:", {
userId,
gymId,
});
await db.run(
sql`UPDATE users SET gym_id = ${gymId ?? null}, updated_at = ${new Date()} WHERE id = ${userId}`,
);
console.log("PATCH /api/users/gym update completed");
const updated = await db
.select()
.from(usersTable)
.where(eq(usersTable.id, userId))
.get();
console.log("PATCH /api/users/gym returning updated user:", updated);
return NextResponse.json(updated);
} catch (error) {
console.error("PATCH /users/gym error:", error);