Initial import: property management SaaS + security hardening + admin dashboard
Property Management Network — Next.js 16 (App Router), Better Auth, Drizzle ORM over PostgreSQL, Stripe, OpenAI, Resend. Includes: - Security hardening: access-control/IDOR fixes, TLS-by-default DB layer, constant-time cron auth, strict security headers, atomic AI quota gating, HTML/email output encoding, demo-backdoor disabled in production. - Superadmin dashboard at /admin (overview/MRR, server-paginated users with ban/impersonate/plan/delete, billing, platform activity + admin audit log, AI usage, system health) via the Better Auth admin plugin. - Seed/migration utility scripts under scripts/. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { and, desc, eq } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { documents, properties } from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { saveFile } from "@/lib/storage"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const propertyId = searchParams.get("property_id")
|
||||
|
||||
let propertyName = ""
|
||||
if (propertyId) {
|
||||
const prop = await db.query.properties.findFirst({
|
||||
where: and(eq(properties.id, propertyId), eq(properties.user_id, user.id)),
|
||||
columns: { name: true },
|
||||
})
|
||||
propertyName = prop?.name ?? ""
|
||||
}
|
||||
|
||||
const data = await db.query.documents.findMany({
|
||||
where: and(
|
||||
eq(documents.user_id, user.id),
|
||||
propertyId ? eq(documents.property_id, propertyId) : undefined
|
||||
),
|
||||
orderBy: desc(documents.created_at),
|
||||
})
|
||||
|
||||
return NextResponse.json({ documents: data, propertyName })
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const contentType = request.headers.get("content-type") ?? ""
|
||||
|
||||
if (contentType.includes("multipart/form-data")) {
|
||||
const fd = await request.formData()
|
||||
const file = fd.get("file") as File | null
|
||||
const propertyId = fd.get("property_id") as string
|
||||
const name = fd.get("name") as string
|
||||
const category = ((fd.get("category") as string) || "other") as typeof documents.$inferInsert.category
|
||||
|
||||
if (!file) return NextResponse.json({ error: "No file provided" }, { status: 400 })
|
||||
if (file.size > 20 * 1024 * 1024) return NextResponse.json({ error: "File too large (max 20 MB)" }, { status: 400 })
|
||||
|
||||
// Verify the property belongs to the user before attaching a document to it.
|
||||
const prop = await db.query.properties.findFirst({
|
||||
where: and(eq(properties.id, propertyId), eq(properties.user_id, user.id)),
|
||||
columns: { id: true },
|
||||
})
|
||||
if (!prop) return NextResponse.json({ error: "Property not found" }, { status: 404 })
|
||||
|
||||
const { key, size, type } = await saveFile(file, { userId: user.id, scope: "documents" })
|
||||
|
||||
const [data] = await db
|
||||
.insert(documents)
|
||||
.values({
|
||||
user_id: user.id,
|
||||
property_id: propertyId,
|
||||
name: name || file.name,
|
||||
category,
|
||||
file_url: `/api/files/${key}`,
|
||||
storage_path: key,
|
||||
file_type: type,
|
||||
file_size: size,
|
||||
})
|
||||
.returning()
|
||||
|
||||
return NextResponse.json(data, { status: 201 })
|
||||
}
|
||||
|
||||
// JSON fallback (metadata only)
|
||||
const body = (await request.json()) as Record<string, unknown>
|
||||
const [data] = await db
|
||||
.insert(documents)
|
||||
.values({ ...(body as typeof documents.$inferInsert), user_id: user.id })
|
||||
.returning()
|
||||
|
||||
return NextResponse.json(data, { status: 201 })
|
||||
}
|
||||
Reference in New Issue
Block a user