80 lines
2.8 KiB
TypeScript
80 lines
2.8 KiB
TypeScript
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,
|
||
|
|
})
|
||
|
|
}
|