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>
103 lines
3.3 KiB
TypeScript
103 lines
3.3 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { and, desc, eq } from "drizzle-orm"
|
|
import { db } from "@/lib/db"
|
|
import { notifications, rent_payments, maintenance_requests } from "@/lib/db/schema"
|
|
import { getSessionUser } from "@/lib/session"
|
|
import { sendEmail, rentDueReminderHtml, rentOverdueHtml, maintenanceUpdateHtml } from "@/lib/email/send"
|
|
import { formatCurrency, formatDate } from "@/lib/utils"
|
|
|
|
export async function GET() {
|
|
const user = await getSessionUser()
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
try {
|
|
const data = await db
|
|
.select()
|
|
.from(notifications)
|
|
.where(eq(notifications.user_id, user.id))
|
|
.orderBy(desc(notifications.sent_at))
|
|
.limit(50)
|
|
|
|
return NextResponse.json(data)
|
|
} catch (e) {
|
|
return NextResponse.json({ error: (e as Error).message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const user = await getSessionUser()
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
const body = await request.json() as {
|
|
type: string
|
|
payment_id?: string
|
|
maintenance_id?: string
|
|
}
|
|
|
|
let result
|
|
|
|
if (body.type === "rent_reminder" && body.payment_id) {
|
|
const payment = await db.query.rent_payments.findFirst({
|
|
where: and(eq(rent_payments.id, body.payment_id), eq(rent_payments.user_id, user.id)),
|
|
with: {
|
|
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
|
property: { columns: { name: true } },
|
|
unit: { columns: { unit_number: true } },
|
|
},
|
|
})
|
|
|
|
if (!payment?.tenant?.email) {
|
|
return NextResponse.json({ error: "Tenant has no email" }, { status: 400 })
|
|
}
|
|
|
|
result = await sendEmail({
|
|
to: payment.tenant.email,
|
|
subject: `Rent Due Reminder — ${payment.property.name}`,
|
|
html: rentDueReminderHtml({
|
|
tenantName: `${payment.tenant.first_name} ${payment.tenant.last_name}`,
|
|
propertyName: payment.property.name,
|
|
unitNumber: payment.unit?.unit_number ?? "—",
|
|
amount: formatCurrency(payment.amount),
|
|
dueDate: formatDate(payment.due_date),
|
|
}),
|
|
})
|
|
|
|
if (result.success) {
|
|
await db.insert(notifications).values({
|
|
user_id: user.id,
|
|
type: "rent_reminder",
|
|
recipient_email: payment.tenant.email,
|
|
subject: `Rent Due Reminder — ${payment.property.name}`,
|
|
status: "sent",
|
|
metadata: { payment_id: body.payment_id },
|
|
})
|
|
}
|
|
}
|
|
|
|
if (body.type === "maintenance_update" && body.maintenance_id) {
|
|
const req = await db.query.maintenance_requests.findFirst({
|
|
where: and(eq(maintenance_requests.id, body.maintenance_id), eq(maintenance_requests.user_id, user.id)),
|
|
with: {
|
|
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
|
},
|
|
})
|
|
|
|
if (!req?.tenant?.email) {
|
|
return NextResponse.json({ error: "Tenant has no email" }, { status: 400 })
|
|
}
|
|
|
|
result = await sendEmail({
|
|
to: req.tenant.email,
|
|
subject: `Maintenance Update — ${req.title}`,
|
|
html: maintenanceUpdateHtml({
|
|
tenantName: `${req.tenant.first_name} ${req.tenant.last_name}`,
|
|
title: req.title,
|
|
status: req.status,
|
|
resolutionNotes: req.resolution_notes ?? undefined,
|
|
}),
|
|
})
|
|
}
|
|
|
|
return NextResponse.json({ success: true })
|
|
}
|