38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { auth } from "@clerk/nextjs/server";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { getDatabase } from "@/lib/database";
|
|
import { ensureUserSynced } from "@/lib/sync-user";
|
|
import log from "@/lib/logger";
|
|
import { checkInSchema } from "@/lib/validation/schemas";
|
|
import {
|
|
validateRequestBody,
|
|
validationErrorResponse,
|
|
} from "@/lib/validation/helpers";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const { userId } = await auth();
|
|
if (!userId) return new NextResponse("Unauthorized", { status: 401 });
|
|
|
|
const db = await getDatabase();
|
|
|
|
// Ensure user exists in DB (sync from Clerk if needed)
|
|
await ensureUserSynced(userId, db);
|
|
|
|
// Check if already checked in
|
|
const activeCheckIn = await db.getActiveCheckIn(userId);
|
|
if (activeCheckIn) {
|
|
return new NextResponse("Already checked in", { status: 400 });
|
|
}
|
|
|
|
const body = await req.json();
|
|
const { type = "gym", notes } = body;
|
|
|
|
const attendance = await db.checkIn(userId, type, notes);
|
|
return NextResponse.json(attendance);
|
|
} catch (error) {
|
|
log.error("Failed to check in", error);
|
|
return new NextResponse("Internal Server Error", { status: 500 });
|
|
}
|
|
}
|