Consolidate audit-fixes branch: webhooks, integrations, and deploy hardening
Batch commit of the pending working tree on security/audit-fixes-2026-07. Major areas: - Outbound webhooks / Zapier: schema + signed delivery with retries, public v1 API (REST-hook subscribe/unsubscribe), settings UI, cron drain. - Deploy hardening: email via SMTP2GO (Resend fully removed), verified DB TLS (DATABASE_SSL=require + DATABASE_CA), storage fails loud in production when Spaces is unconfigured instead of silently using ephemeral disk. - Integrations & features (concurrent work): accounting (QuickBooks/Xero), e-signature (DocuSign/Dropbox Sign), PayPal, geocoding/maps, onboarding, expanded legal pages. - DB migrations 0006–0009. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
969d5d4c8a
commit
c9968531e4
@@ -7,7 +7,7 @@ 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)
|
||||
// Runs daily at 9am UTC (scheduled via DigitalOcean Functions — see DIGITALOCEAN.md)
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { follow_up_rules } from "@/lib/db/schema"
|
||||
import { isAuthorizedCron } from "@/lib/cron-auth"
|
||||
import { runFollowUpsForUser } from "@/lib/follow-ups"
|
||||
|
||||
// Automated follow-ups cron: runs every user's active follow-up rules.
|
||||
// Runs daily at 10:00 UTC (see functions/project.yml → follow-ups trigger).
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
// Distinct user ids that have at least one active follow-up rule.
|
||||
const rows = await db
|
||||
.selectDistinct({ user_id: follow_up_rules.user_id })
|
||||
.from(follow_up_rules)
|
||||
.where(eq(follow_up_rules.is_active, true))
|
||||
|
||||
let processed = 0
|
||||
let total = 0
|
||||
|
||||
for (const { user_id } of rows) {
|
||||
try {
|
||||
const result = await runFollowUpsForUser(user_id)
|
||||
total += result.sent
|
||||
processed++
|
||||
} catch (err) {
|
||||
console.error(`follow-ups cron failed for user ${user_id}:`, err)
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ processed, sent: total })
|
||||
}
|
||||
@@ -4,7 +4,7 @@ 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)
|
||||
// Scheduled task: runs daily at 8am UTC (scheduled via DigitalOcean Functions — see DIGITALOCEAN.md)
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
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 })
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
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,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { isAuthorizedCron } from "@/lib/cron-auth"
|
||||
import { processDueDeliveries } from "@/lib/webhooks/deliver"
|
||||
|
||||
// Webhook delivery retry drain. The emitter attempts an immediate delivery when
|
||||
// an event fires; this cron re-attempts anything still pending whose backoff
|
||||
// window has elapsed (and covers deliveries orphaned by a process restart).
|
||||
// Scheduled every 5 minutes via DigitalOcean Functions — see functions/project.yml.
|
||||
export async function GET(request: Request) {
|
||||
if (!isAuthorizedCron(request)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
const { processed, delivered } = await processDueDeliveries(200)
|
||||
return NextResponse.json({ processed, delivered })
|
||||
}
|
||||
Reference in New Issue
Block a user