build errors fixed
This commit is contained in:
1 parent
d3a36b6103
commit
6580564767
8 files changed
+132
-93
No files matched your search
@@ -38,7 +38,7 @@ export async function GET(req: Request) {
|
||||
if (!user.gymId) {
|
||||
return new NextResponse("Admin gymId not set", { status: 400 });
|
||||
}
|
||||
targetGymId = user.gymId;
|
||||
targetGymId = user.gymId as string;
|
||||
} else if (user.role === "superAdmin") {
|
||||
targetGymId = requestedGymId;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -86,7 +86,7 @@ export async function POST(req: Request) {
|
||||
| "trainer"
|
||||
| "client"
|
||||
| "generalUser") || "client";
|
||||
const gymId = (public_metadata?.gymId as string | null) ?? null;
|
||||
let gymId = (public_metadata?.gymId as string | null) ?? null;
|
||||
const inviterUserId =
|
||||
(public_metadata?.inviterUserId as string | undefined) ?? undefined;
|
||||
const roleAssigned =
|
||||
@@ -147,8 +147,8 @@ export async function POST(req: Request) {
|
||||
inviterUserId
|
||||
) {
|
||||
const inviterGymRow = db
|
||||
.prepare("SELECT gymId FROM users WHERE id = ?")
|
||||
.get(inviterUserId) as { gymId?: string } | undefined;
|
||||
.prepare("SELECT gym_id FROM users WHERE id = ?")
|
||||
.get(inviterUserId) as { gym_id?: string } | undefined;
|
||||
|
||||
if (inviterGymRow?.gym_id) {
|
||||
const inheritStmt = db.prepare(`
|
||||
@@ -194,7 +194,7 @@ export async function POST(req: Request) {
|
||||
| "trainer"
|
||||
| "client"
|
||||
| "generalUser") || "client";
|
||||
const gymId = (public_metadata?.gymId as string | null) ?? null;
|
||||
let gymId = (public_metadata?.gymId as string | null) ?? null;
|
||||
|
||||
// Update user in database
|
||||
const now = new Date().toISOString();
|
||||
@@ -225,7 +225,7 @@ export async function POST(req: Request) {
|
||||
.prepare("SELECT gym_id FROM users WHERE id = ?")
|
||||
.get(inviterUserId) as { gym_id?: string } | undefined;
|
||||
|
||||
if (inviterGymRow?.gymId) {
|
||||
if (inviterGymRow?.gym_id) {
|
||||
const inheritStmt = db.prepare(`
|
||||
UPDATE users
|
||||
SET gym_id = ?, updated_at = ?
|
||||
|
||||
Reference in new issue
Block a user