various fixes

raw sql to drizzle
This commit is contained in:
dimitar committed 2026-03-19 00:57:04 +01:00
1 parent ffd3aabc55
commit d6a77fcd23
5 files changed
+57 -30

No files matched your search

+27 -13
View File
@@ -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);