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:
Leon Serfaty
2026-06-23 20:36:07 -04:00
co-authored by Claude Opus 4.8
commit 857b9a7811
291 changed files with 38996 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
import { NextResponse } from "next/server"
import { and, eq } from "drizzle-orm"
import { db } from "@/lib/db"
import { documents } from "@/lib/db/schema"
import { getSessionUser } from "@/lib/session"
import { deleteFile } from "@/lib/storage"
export async function GET(_: Request, { params }: { params: Promise<{ id: string }> }) {
const user = await getSessionUser()
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
const { id } = await params
const doc = await db.query.documents.findFirst({
where: and(eq(documents.id, id), eq(documents.user_id, user.id)),
})
if (!doc) return NextResponse.json({ error: "Not found" }, { status: 404 })
// file_url already points at the auth-gated /api/files route; expose it as
// signed_url for compatibility with the existing client.
return NextResponse.json({ ...doc, signed_url: doc.file_url })
}
export async function DELETE(_: Request, { params }: { params: Promise<{ id: string }> }) {
const user = await getSessionUser()
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
const { id } = await params
const doc = await db.query.documents.findFirst({
where: and(eq(documents.id, id), eq(documents.user_id, user.id)),
columns: { storage_path: true },
})
if (!doc) return NextResponse.json({ error: "Not found" }, { status: 404 })
await db.delete(documents).where(and(eq(documents.id, id), eq(documents.user_id, user.id)))
if (doc.storage_path) {
await deleteFile(doc.storage_path)
}
return NextResponse.json({ success: true })
}
+85
View File
@@ -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 })
}