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,141 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { and, eq, lt, lte } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { rent_payments, leases } from "@/lib/db/schema"
|
||||
import { sendEmail, rentDueReminderHtml, rentOverdueHtml, leaseExpiryHtml } from "@/lib/email/send"
|
||||
import { formatCurrency, formatDate, daysUntil } from "@/lib/utils"
|
||||
import { isAuthorizedCron } from "@/lib/cron-auth"
|
||||
|
||||
// Combined daily cron: rent reminders + overdue marking + lease expiry emails
|
||||
// Runs daily at 9am (see vercel.json)
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10)
|
||||
|
||||
// ──────────────────────────────────────
|
||||
// 1. RENT REMINDERS (due in 3 days)
|
||||
// ──────────────────────────────────────
|
||||
const in3Days = new Date()
|
||||
in3Days.setDate(in3Days.getDate() + 3)
|
||||
const in3DaysStr = in3Days.toISOString().slice(0, 10)
|
||||
|
||||
const upcoming = await db.query.rent_payments.findMany({
|
||||
where: and(eq(rent_payments.status, "pending"), eq(rent_payments.due_date, in3DaysStr)),
|
||||
with: {
|
||||
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
||||
property: { columns: { name: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
},
|
||||
})
|
||||
|
||||
for (const payment of upcoming) {
|
||||
if (!payment.tenant?.email) continue
|
||||
await sendEmail({
|
||||
to: payment.tenant.email,
|
||||
subject: `Rent Due in 3 Days — ${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),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────
|
||||
// 2. MARK OVERDUE + SEND NOTICE
|
||||
// ──────────────────────────────────────
|
||||
const pastDue = await db.query.rent_payments.findMany({
|
||||
where: and(eq(rent_payments.status, "pending"), lt(rent_payments.due_date, today)),
|
||||
with: {
|
||||
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
||||
property: { columns: { name: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
},
|
||||
})
|
||||
|
||||
for (const payment of pastDue) {
|
||||
await db
|
||||
.update(rent_payments)
|
||||
.set({ status: "overdue" })
|
||||
.where(eq(rent_payments.id, payment.id))
|
||||
|
||||
if (!payment.tenant?.email) continue
|
||||
await sendEmail({
|
||||
to: payment.tenant.email,
|
||||
subject: `Rent Overdue — ${payment.property?.name}`,
|
||||
html: rentOverdueHtml({
|
||||
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),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────
|
||||
// 3. LEASE EXPIRY REMINDERS (60/30/7 days)
|
||||
// ──────────────────────────────────────
|
||||
const checkpoints = [
|
||||
{ days: 60, field: "reminder_60_sent" as const },
|
||||
{ days: 30, field: "reminder_30_sent" as const },
|
||||
{ days: 7, field: "reminder_7_sent" as const },
|
||||
]
|
||||
|
||||
let leaseRemindersSent = 0
|
||||
|
||||
for (const { days, field } of checkpoints) {
|
||||
const target = new Date()
|
||||
target.setDate(target.getDate() + days)
|
||||
const targetStr = target.toISOString().slice(0, 10)
|
||||
|
||||
const expiringLeases = await db.query.leases.findMany({
|
||||
where: and(
|
||||
eq(leases.status, "active"),
|
||||
eq(leases[field], false),
|
||||
lte(leases.lease_end, targetStr)
|
||||
),
|
||||
with: {
|
||||
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
||||
property: { columns: { name: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
},
|
||||
})
|
||||
|
||||
for (const lease of expiringLeases) {
|
||||
if (!lease.tenant?.email) continue
|
||||
|
||||
const daysLeft = daysUntil(lease.lease_end)
|
||||
|
||||
await sendEmail({
|
||||
to: lease.tenant.email,
|
||||
subject: `Your lease expires in ${daysLeft} days — ${lease.property?.name}`,
|
||||
html: leaseExpiryHtml({
|
||||
tenantName: `${lease.tenant.first_name} ${lease.tenant.last_name}`,
|
||||
propertyName: lease.property?.name ?? "",
|
||||
unitNumber: lease.unit?.unit_number ?? "—",
|
||||
leaseEnd: formatDate(lease.lease_end),
|
||||
daysLeft,
|
||||
}),
|
||||
})
|
||||
|
||||
await db
|
||||
.update(leases)
|
||||
.set({ [field]: true })
|
||||
.where(eq(leases.id, lease.id))
|
||||
|
||||
leaseRemindersSent++
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
rent_reminders_sent: upcoming.length,
|
||||
marked_overdue: pastDue.length,
|
||||
lease_reminders_sent: leaseRemindersSent,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { and, eq, lte } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { rent_payments, expenses } from "@/lib/db/schema"
|
||||
import { isAuthorizedCron } from "@/lib/cron-auth"
|
||||
|
||||
// Vercel Cron: runs daily at 8am (see vercel.json)
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
// Find overdue payments older than grace period (default 5 days) that don't yet have a late fee
|
||||
const graceDays = 5
|
||||
const cutoff = new Date()
|
||||
cutoff.setDate(cutoff.getDate() - graceDays)
|
||||
const cutoffDate = cutoff.toISOString().slice(0, 10)
|
||||
|
||||
const overduePayments = await db
|
||||
.select({
|
||||
id: rent_payments.id,
|
||||
user_id: rent_payments.user_id,
|
||||
tenant_id: rent_payments.tenant_id,
|
||||
property_id: rent_payments.property_id,
|
||||
unit_id: rent_payments.unit_id,
|
||||
amount: rent_payments.amount,
|
||||
due_date: rent_payments.due_date,
|
||||
})
|
||||
.from(rent_payments)
|
||||
.where(
|
||||
and(
|
||||
eq(rent_payments.status, "overdue"),
|
||||
lte(rent_payments.due_date, cutoffDate),
|
||||
eq(rent_payments.late_fee_applied, false)
|
||||
)
|
||||
)
|
||||
|
||||
if (!overduePayments.length) {
|
||||
return NextResponse.json({ processed: 0 })
|
||||
}
|
||||
|
||||
let processed = 0
|
||||
|
||||
for (const payment of overduePayments) {
|
||||
const lateFeeAmount = Math.round(Number(payment.amount) * 0.05 * 100) / 100 // 5% late fee
|
||||
|
||||
// Insert late fee as a separate expense
|
||||
await db.insert(expenses).values({
|
||||
user_id: payment.user_id,
|
||||
property_id: payment.property_id,
|
||||
unit_id: payment.unit_id ?? null,
|
||||
category: "other",
|
||||
description: `Late fee — rent due ${payment.due_date}`,
|
||||
amount: lateFeeAmount,
|
||||
expense_date: new Date().toISOString().slice(0, 10),
|
||||
vendor: "Auto-generated",
|
||||
})
|
||||
|
||||
// Mark late fee applied
|
||||
await db
|
||||
.update(rent_payments)
|
||||
.set({ late_fee_applied: true })
|
||||
.where(eq(rent_payments.id, payment.id))
|
||||
|
||||
processed++
|
||||
}
|
||||
|
||||
return NextResponse.json({ processed, message: `Applied late fees to ${processed} overdue payment(s).` })
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { and, eq, lte } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { leases } from "@/lib/db/schema"
|
||||
import { sendEmail, leaseExpiryHtml } from "@/lib/email/send"
|
||||
import { formatDate, daysUntil } from "@/lib/utils"
|
||||
import { isAuthorizedCron } from "@/lib/cron-auth"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
const checkpoints = [
|
||||
{ days: 60, field: "reminder_60_sent" as const },
|
||||
{ days: 30, field: "reminder_30_sent" as const },
|
||||
{ days: 7, field: "reminder_7_sent" as const },
|
||||
]
|
||||
|
||||
let sent = 0
|
||||
|
||||
for (const { days, field } of checkpoints) {
|
||||
const target = new Date()
|
||||
target.setDate(target.getDate() + days)
|
||||
const targetStr = target.toISOString().slice(0, 10)
|
||||
|
||||
const expiringLeases = await db.query.leases.findMany({
|
||||
where: and(
|
||||
eq(leases.status, "active"),
|
||||
eq(leases[field], false),
|
||||
lte(leases.lease_end, targetStr)
|
||||
),
|
||||
with: {
|
||||
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
||||
property: { columns: { name: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
},
|
||||
})
|
||||
|
||||
for (const lease of expiringLeases) {
|
||||
if (!lease.tenant?.email) continue
|
||||
|
||||
const daysLeft = daysUntil(lease.lease_end)
|
||||
|
||||
await sendEmail({
|
||||
to: lease.tenant.email,
|
||||
subject: `Your lease expires in ${daysLeft} days — ${lease.property?.name}`,
|
||||
html: leaseExpiryHtml({
|
||||
tenantName: `${lease.tenant.first_name} ${lease.tenant.last_name}`,
|
||||
propertyName: lease.property?.name ?? "",
|
||||
unitNumber: lease.unit?.unit_number ?? "—",
|
||||
leaseEnd: formatDate(lease.lease_end),
|
||||
daysLeft,
|
||||
}),
|
||||
})
|
||||
|
||||
await db
|
||||
.update(leases)
|
||||
.set({ [field]: true })
|
||||
.where(eq(leases.id, lease.id))
|
||||
|
||||
sent++
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ reminders_sent: sent })
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { and, eq, lt } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { rent_payments } from "@/lib/db/schema"
|
||||
import { sendEmail, rentDueReminderHtml, rentOverdueHtml } from "@/lib/email/send"
|
||||
import { formatCurrency, formatDate } from "@/lib/utils"
|
||||
import { isAuthorizedCron } from "@/lib/cron-auth"
|
||||
|
||||
// Called by Vercel Cron — runs daily
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10)
|
||||
const in3Days = new Date()
|
||||
in3Days.setDate(in3Days.getDate() + 3)
|
||||
const in3DaysStr = in3Days.toISOString().slice(0, 10)
|
||||
|
||||
// Payments due in 3 days → send reminder
|
||||
const upcoming = await db.query.rent_payments.findMany({
|
||||
where: and(eq(rent_payments.status, "pending"), eq(rent_payments.due_date, in3DaysStr)),
|
||||
with: {
|
||||
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
||||
property: { columns: { name: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
},
|
||||
})
|
||||
|
||||
for (const payment of upcoming) {
|
||||
if (!payment.tenant?.email) continue
|
||||
await sendEmail({
|
||||
to: payment.tenant.email,
|
||||
subject: `Rent Due in 3 Days — ${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),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
// Payments past due date → mark overdue + send notice
|
||||
const pastDue = await db.query.rent_payments.findMany({
|
||||
where: and(eq(rent_payments.status, "pending"), lt(rent_payments.due_date, today)),
|
||||
with: {
|
||||
tenant: { columns: { first_name: true, last_name: true, email: true } },
|
||||
property: { columns: { name: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
},
|
||||
})
|
||||
|
||||
for (const payment of pastDue) {
|
||||
await db
|
||||
.update(rent_payments)
|
||||
.set({ status: "overdue" })
|
||||
.where(eq(rent_payments.id, payment.id))
|
||||
|
||||
if (!payment.tenant?.email) continue
|
||||
await sendEmail({
|
||||
to: payment.tenant.email,
|
||||
subject: `Rent Overdue — ${payment.property?.name}`,
|
||||
html: rentOverdueHtml({
|
||||
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),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
reminders_sent: upcoming.length,
|
||||
marked_overdue: pastDue.length,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user