diff --git a/apps/admin/data/fitai.db b/apps/admin/data/fitai.db index 26a48f5..858699d 100644 Binary files a/apps/admin/data/fitai.db and b/apps/admin/data/fitai.db differ diff --git a/apps/admin/src/app/api/gyms/[id]/route.ts b/apps/admin/src/app/api/gyms/[id]/route.ts index 259c1e0..095772e 100644 --- a/apps/admin/src/app/api/gyms/[id]/route.ts +++ b/apps/admin/src/app/api/gyms/[id]/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { auth } from "@clerk/nextjs/server"; import { eq, sql } from "@fitai/database"; -import { db, users as usersTable } from "@fitai/database"; +import { db, users as usersTable, gyms as gymsTable } from "@fitai/database"; import { ensureUserSynced } from "@/lib/sync-user"; import log from "@/lib/logger"; @@ -68,16 +68,22 @@ export async function DELETE( await ensureGymsTable(); - // Check if gym exists - const gymRows = await db.all(sql`SELECT * FROM gyms WHERE id = ${gymId}`); - if (gymRows.length === 0) { + // Check if gym exists using Drizzle ORM + const existingGym = await db + .select() + .from(gymsTable) + .where(eq(gymsTable.id, gymId)) + .get(); + + if (!existingGym) { return NextResponse.json({ error: "Gym not found" }, { status: 404 }); } - // Soft delete - mark as inactive - await db.run( - sql`UPDATE gyms SET status = 'inactive', updated_at = ${Date.now()} WHERE id = ${gymId}`, - ); + // Soft delete - mark as inactive using Drizzle ORM + await db + .update(gymsTable) + .set({ status: "inactive", updatedAt: new Date() }) + .where(eq(gymsTable.id, gymId)); return NextResponse.json({ success: true, diff --git a/apps/admin/src/app/api/gyms/[id]/stats/route.ts b/apps/admin/src/app/api/gyms/[id]/stats/route.ts index e4b0938..8a8b7c1 100644 --- a/apps/admin/src/app/api/gyms/[id]/stats/route.ts +++ b/apps/admin/src/app/api/gyms/[id]/stats/route.ts @@ -1,6 +1,6 @@ import { NextResponse } from "next/server"; import { eq, sql } from "@fitai/database"; -import { db } from "@fitai/database"; +import { db, gyms as gymsTable } from "@fitai/database"; import log from "@/lib/logger"; async function ensureGymsTable() { @@ -27,12 +27,16 @@ export async function GET( const { id: gymId } = await params; await ensureGymsTable(); - // Get gym info - const gymRows = await db.all(sql`SELECT * FROM gyms WHERE id = ${gymId}`); - if (gymRows.length === 0) { - return new NextResponse("Gym not found", { status: 404 }); + // Get gym info using Drizzle ORM + const gym = await db + .select() + .from(gymsTable) + .where(eq(gymsTable.id, gymId)) + .get(); + + if (!gym) { + return NextResponse.json({ error: "Gym not found" }, { status: 404 }); } - const gym = gymRows[0]; // Get user counts const usersResult = await db.all( @@ -100,6 +104,9 @@ export async function GET( return NextResponse.json({ gym, stats }); } catch (error) { log.error("Failed to get gym stats", error); - return new NextResponse("Internal Server Error", { status: 500 }); + return NextResponse.json( + { error: "Internal Server Error" }, + { status: 500 }, + ); } } diff --git a/apps/admin/src/app/api/gyms/route.ts b/apps/admin/src/app/api/gyms/route.ts index ae1c8df..728e340 100644 --- a/apps/admin/src/app/api/gyms/route.ts +++ b/apps/admin/src/app/api/gyms/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { auth } from "@clerk/nextjs/server"; import { eq, sql } from "@fitai/database"; -import { db, users as usersTable } from "@fitai/database"; +import { db, users as usersTable, gyms as gymsTable } from "@fitai/database"; import { ensureUserSynced } from "@/lib/sync-user"; import log from "@/lib/logger"; @@ -24,9 +24,12 @@ async function ensureGymsTable() { export async function GET() { try { await ensureGymsTable(); - const rows = await db.all( - sql`SELECT * FROM gyms WHERE status = 'active' ORDER BY created_at DESC`, - ); + const rows = await db + .select() + .from(gymsTable) + .where(eq(gymsTable.status, "active")) + .orderBy(sql`created_at DESC`) + .all(); return NextResponse.json(rows); } catch (error) { @@ -146,19 +149,30 @@ export async function POST(req: Request) { } const id = generateId(); - const nowTs = Date.now(); + const nowTs = new Date(); - await db.run( - sql`INSERT INTO gyms (id, name, location, status, admin_user_id, created_at, updated_at) - VALUES (${id}, ${name}, ${location ?? null}, 'active', ${adminUserId!}, ${nowTs}, ${nowTs})`, - ); + // Use Drizzle's insert method instead of raw SQL + await db.insert(gymsTable).values({ + id, + name, + location: location ?? null, + status: "active", + adminUserId: adminUserId!, + createdAt: nowTs, + updatedAt: nowTs, + }); // Assign the admin to this gym immediately after creation - await db.run( - sql`UPDATE users SET gym_id = ${id}, updated_at = ${nowTs} WHERE id = ${adminUserId!}`, - ); + await db + .update(usersTable) + .set({ gymId: id, updatedAt: nowTs }) + .where(eq(usersTable.id, adminUserId!)); - const created = await db.get(sql`SELECT * FROM gyms WHERE id = ${id}`); + const created = await db + .select() + .from(gymsTable) + .where(eq(gymsTable.id, id)) + .get(); return NextResponse.json(created, { status: 201 }); } catch (error) { log.error("Failed to create gym", error); diff --git a/packages/database/src/schema.ts b/packages/database/src/schema.ts index 3924ee6..f9c5a71 100644 --- a/packages/database/src/schema.ts +++ b/packages/database/src/schema.ts @@ -53,10 +53,10 @@ export const gyms = sqliteTable( adminUserId: text("admin_user_id") .notNull() .references(() => users.id, { onDelete: "cascade" }), - createdAt: integer("created_at", { mode: "timestamp" }) + createdAt: integer("created_at", { mode: "timestamp_ms" }) .notNull() .$defaultFn(() => new Date()), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .notNull() .$defaultFn(() => new Date()), },