build errors fixed
This commit is contained in:
1 parent
d3a36b6103
commit
6580564767
8 files changed
+132
-93
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@clerk/nextjs/server'
|
||||
import { db, users as usersTable, gyms as gymsTable, eq } from '@fitai/database'
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { db, users as usersTable, eq, sql } from "@fitai/database";
|
||||
|
||||
/**
|
||||
* PATCH /api/users/gym
|
||||
@@ -11,45 +11,57 @@ import { db, users as usersTable, gyms as gymsTable, eq } from '@fitai/database'
|
||||
*/
|
||||
export async function PATCH(req: Request) {
|
||||
try {
|
||||
const { userId } = await auth()
|
||||
if (!userId) return new NextResponse('Unauthorized', { status: 401 })
|
||||
const { userId } = await auth();
|
||||
if (!userId) return new NextResponse("Unauthorized", { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => null)
|
||||
if (!body || typeof body !== 'object' || !('gymId' in body)) {
|
||||
return NextResponse.json({ error: 'gymId is required in body (can be null)' }, { status: 400 })
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body || typeof body !== "object" || !("gymId" in body)) {
|
||||
return NextResponse.json(
|
||||
{ error: "gymId is required in body (can be null)" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const gymId = body.gymId === null ? null : String(body.gymId)
|
||||
const gymId = body.gymId === null ? null : String(body.gymId);
|
||||
|
||||
// Ensure user exists
|
||||
const user = await db.select().from(usersTable).where(eq(usersTable.id, userId)).get()
|
||||
if (!user) return new NextResponse('User not found', { status: 404 })
|
||||
const user = await db
|
||||
.select()
|
||||
.from(usersTable)
|
||||
.where(eq(usersTable.id, userId))
|
||||
.get();
|
||||
if (!user) return new NextResponse("User not found", { status: 404 });
|
||||
|
||||
// Validate gym when provided
|
||||
if (gymId) {
|
||||
const gym = await db.select().from(gymsTable).where(eq(gymsTable.id, gymId)).get()
|
||||
const rows = await db.all(
|
||||
sql`SELECT status FROM gyms WHERE id = ${gymId} LIMIT 1`,
|
||||
);
|
||||
const gym = rows?.[0] as { status?: string } | undefined;
|
||||
if (!gym) {
|
||||
return NextResponse.json({ error: 'Gym not found' }, { status: 404 })
|
||||
return NextResponse.json({ error: "Gym not found" }, { status: 404 });
|
||||
}
|
||||
if (gym.status !== 'active') {
|
||||
return NextResponse.json({ error: 'Gym is not active' }, { status: 400 })
|
||||
if (gym.status !== "active") {
|
||||
return NextResponse.json(
|
||||
{ error: "Gym is not active" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Update user's gym selection
|
||||
await db
|
||||
.update(usersTable)
|
||||
.set({
|
||||
gymId: gymId ?? null,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(usersTable.id, userId))
|
||||
.run()
|
||||
await db.run(
|
||||
sql`UPDATE users SET gym_id = ${gymId ?? null}, updated_at = ${new Date()} WHERE id = ${userId}`,
|
||||
);
|
||||
|
||||
const updated = await db.select().from(usersTable).where(eq(usersTable.id, userId)).get()
|
||||
return NextResponse.json(updated)
|
||||
const updated = await db
|
||||
.select()
|
||||
.from(usersTable)
|
||||
.where(eq(usersTable.id, userId))
|
||||
.get();
|
||||
return NextResponse.json(updated);
|
||||
} catch (error) {
|
||||
console.error('PATCH /users/gym error:', error)
|
||||
return new NextResponse('Internal Server Error', { status: 500 })
|
||||
console.error("PATCH /users/gym error:", error);
|
||||
return new NextResponse("Internal Server Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -304,7 +304,7 @@ export async function PUT(request: NextRequest) {
|
||||
role: role ?? existingUser.role,
|
||||
phone: phone !== undefined ? phone : existingUser.phone,
|
||||
gymId: gymId !== undefined ? gymId : existingUser.gymId,
|
||||
} as any);
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user