phase 1-6 implemented

This commit is contained in:
dimitar committed 2026-03-10 04:14:03 +01:00
1 parent 22c274bb83
commit 2c4927478a
73 files changed
+9702 -2354

No files matched your search

@@ -1,31 +1,32 @@
import { auth } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
import { getDatabase } from '@/lib/database'
import { ensureUserSynced } from '@/lib/sync-user'
import { auth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { getDatabase } from "@/lib/database";
import { ensureUserSynced } from "@/lib/sync-user";
import log from "@/lib/logger";
export async function POST(req: Request) {
try {
const { userId } = await auth()
if (!userId) return new NextResponse('Unauthorized', { status: 401 })
try {
const { userId } = await auth();
if (!userId) return new NextResponse("Unauthorized", { status: 401 });
const db = await getDatabase()
const db = await getDatabase();
// Ensure user exists in DB (sync from Clerk if needed)
await ensureUserSynced(userId, db)
// 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) {
console.error('Check-in error:', error)
return new NextResponse('Internal Server Error', { status: 500 })
// 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 });
}
}
@@ -1,23 +1,24 @@
import { auth } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
import { getDatabase } from '@/lib/database'
import { auth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { getDatabase } from "@/lib/database";
import log from "@/lib/logger";
export async function POST(req: Request) {
try {
const { userId } = await auth()
if (!userId) return new NextResponse('Unauthorized', { status: 401 })
try {
const { userId } = await auth();
if (!userId) return new NextResponse("Unauthorized", { status: 401 });
const db = await getDatabase()
const db = await getDatabase();
const activeCheckIn = await db.getActiveCheckIn(userId)
if (!activeCheckIn) {
return new NextResponse('No active check-in found', { status: 404 })
}
const attendance = await db.checkOut(activeCheckIn.id)
return NextResponse.json(attendance)
} catch (error) {
console.error('Check-out error:', error)
return new NextResponse('Internal Server Error', { status: 500 })
const activeCheckIn = await db.getActiveCheckIn(userId);
if (!activeCheckIn) {
return new NextResponse("No active check-in found", { status: 404 });
}
const attendance = await db.checkOut(activeCheckIn.id);
return NextResponse.json(attendance);
} catch (error) {
log.error("Failed to check out", error);
return new NextResponse("Internal Server Error", { status: 500 });
}
}
@@ -1,31 +1,33 @@
import { auth } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
import { getDatabase } from '@/lib/database'
import { ensureUserSynced } from '@/lib/sync-user'
import { auth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { getDatabase } from "@/lib/database";
import { ensureUserSynced } from "@/lib/sync-user";
import log from "@/lib/logger";
export async function GET(req: Request) {
console.log('GET /api/attendance/history called')
console.log('Headers:', Object.fromEntries(req.headers.entries()))
log.apiRequest("GET", "/api/attendance/history", {
headers: Object.fromEntries(req.headers.entries()),
});
try {
const authResult = await auth()
const { userId } = authResult
console.log('Auth result:', JSON.stringify(authResult, null, 2))
try {
const authResult = await auth();
const { userId } = authResult;
log.debug("Auth result received", { authResult });
if (!userId) {
console.log('No userId found in auth result')
return new NextResponse('Unauthorized', { status: 401 })
}
const db = await getDatabase()
// Ensure user exists in DB (sync from Clerk if needed)
await ensureUserSynced(userId, db)
const history = await db.getAttendanceHistory(userId)
return NextResponse.json(history)
} catch (error) {
console.error('History error:', error)
return new NextResponse('Internal Server Error', { status: 500 })
if (!userId) {
log.warn("No userId found in auth result");
return new NextResponse("Unauthorized", { status: 401 });
}
const db = await getDatabase();
// Ensure user exists in DB (sync from Clerk if needed)
await ensureUserSynced(userId, db);
const history = await db.getAttendanceHistory(userId);
return NextResponse.json(history);
} catch (error) {
log.error("Failed to fetch attendance history", error);
return new NextResponse("Internal Server Error", { status: 500 });
}
}