2026-06-23 20:36:07 -04:00
|
|
|
import { NextResponse } from "next/server"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { desc, eq, sql } from "drizzle-orm"
|
2026-06-23 20:36:07 -04:00
|
|
|
import { db } from "@/lib/db"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { properties } from "@/lib/db/schema"
|
2026-06-23 20:36:07 -04:00
|
|
|
import { getSessionUser } from "@/lib/session"
|
|
|
|
|
import { propertySchema } from "@/lib/validations"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { getUserPlan } from "@/lib/plan-limits"
|
|
|
|
|
import { checkLimit } from "@/lib/stripe/plans"
|
|
|
|
|
import { getEffectiveOwnerId, getAccountContext } from "@/lib/account"
|
|
|
|
|
import { emitWebhookEvent } from "@/lib/webhooks/emit"
|
|
|
|
|
import { geocodeAddress } from "@/lib/geocoding"
|
2026-06-23 20:36:07 -04:00
|
|
|
|
|
|
|
|
export async function GET() {
|
|
|
|
|
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 data = await db.query.properties.findMany({
|
2026-07-02 13:42:34 -04:00
|
|
|
where: eq(properties.user_id, ownerId),
|
2026-06-23 20:36:07 -04:00
|
|
|
with: { units: { columns: { id: true, status: true } } },
|
|
|
|
|
orderBy: desc(properties.created_at),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return NextResponse.json(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function POST(request: Request) {
|
|
|
|
|
const user = await getSessionUser()
|
|
|
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const ctx = await getAccountContext(user.id)
|
|
|
|
|
const ownerId = ctx.ownerId
|
|
|
|
|
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
const body = await request.json()
|
|
|
|
|
const parsed = propertySchema.safeParse(body)
|
|
|
|
|
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
|
|
|
|
|
|
|
|
|
// Check plan limit
|
|
|
|
|
const [{ count }] = await db
|
|
|
|
|
.select({ count: sql<number>`count(*)::int` })
|
|
|
|
|
.from(properties)
|
2026-07-02 13:42:34 -04:00
|
|
|
.where(eq(properties.user_id, ownerId))
|
2026-06-23 20:36:07 -04:00
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const plan = await getUserPlan(ownerId)
|
|
|
|
|
if (!checkLimit(plan, "maxProperties", count)) {
|
2026-06-23 20:36:07 -04:00
|
|
|
return NextResponse.json({ error: "Plan limit reached. Upgrade to add more properties." }, { status: 403 })
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
// Best-effort geocode so the property shows up on the map (never blocks save).
|
|
|
|
|
const coords = await geocodeAddress(parsed.data)
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
const [data] = await db
|
|
|
|
|
.insert(properties)
|
2026-07-02 13:42:34 -04:00
|
|
|
.values({ ...parsed.data, user_id: ownerId, ...(coords ?? {}) })
|
2026-06-23 20:36:07 -04:00
|
|
|
.returning()
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
await emitWebhookEvent({ ownerId, event: "property.created", data: { property: data } })
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
return NextResponse.json(data, { status: 201 })
|
|
|
|
|
}
|