2026-06-23 20:36:07 -04:00
|
|
|
import { NextResponse } from "next/server"
|
|
|
|
|
import { desc, eq } from "drizzle-orm"
|
|
|
|
|
import { db } from "@/lib/db"
|
|
|
|
|
import { activity_log } from "@/lib/db/schema"
|
|
|
|
|
import { getSessionUser } from "@/lib/session"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { getEffectiveOwnerId } from "@/lib/account"
|
2026-06-23 20:36:07 -04:00
|
|
|
|
|
|
|
|
export async function GET(request: Request) {
|
|
|
|
|
const user = await getSessionUser()
|
|
|
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const ownerId = await getEffectiveOwnerId(user.id)
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
const { searchParams } = new URL(request.url)
|
|
|
|
|
const limit = Math.min(100, parseInt(searchParams.get("limit") ?? "50", 10))
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const data = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(activity_log)
|
2026-07-02 13:42:34 -04:00
|
|
|
.where(eq(activity_log.user_id, ownerId))
|
2026-06-23 20:36:07 -04:00
|
|
|
.orderBy(desc(activity_log.created_at))
|
|
|
|
|
.limit(limit)
|
|
|
|
|
|
|
|
|
|
return NextResponse.json(data)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return NextResponse.json({ error: (e as Error).message }, { status: 500 })
|
|
|
|
|
}
|
|
|
|
|
}
|