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:
Leon Serfaty
2026-07-02 13:42:34 -04:00
co-authored by Claude Opus 4.8
parent 969d5d4c8a
commit c9968531e4
282 changed files with 41530 additions and 4013 deletions
+76
View File
@@ -0,0 +1,76 @@
import { NextResponse } from "next/server"
import { and, eq } from "drizzle-orm"
import { z } from "zod"
import { db } from "@/lib/db"
import { maintenance_requests } from "@/lib/db/schema"
import { maintenanceSchema } from "@/lib/validations"
import { ownsProperty, ownsUnit, ownsTenant } from "@/lib/db/ownership"
import { resolveApiRequest } from "@/lib/api-auth"
import { emitWebhookEvent } from "@/lib/webhooks/emit"
// Public REST API (v1) — update a single maintenance request. Bearer API-key
// auth. Owner-scoped by id; mirrors the internal PATCH schema.
const unauthorized = () =>
NextResponse.json({ error: { code: 401, message: "Unauthorized" } }, { status: 401 })
const forbidden = () =>
NextResponse.json({ error: { code: 403, message: "Forbidden" } }, { status: 403 })
const maintenancePatchSchema = maintenanceSchema.partial().extend({
status: z.enum(["open", "in_progress", "resolved", "closed"]).optional(),
resolution_notes: z.string().optional(),
actual_cost: z.number().positive().optional(),
resolved_at: z.string().optional(),
})
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
const ctx = await resolveApiRequest(request)
if (!ctx) return unauthorized()
if (!ctx.canWrite) return forbidden()
const { id } = await params
const body = await request.json().catch(() => null)
const parsed = maintenancePatchSchema.safeParse(body)
if (!parsed.success) {
return NextResponse.json(
{ error: { code: 400, message: parsed.error.flatten() } },
{ status: 400 }
)
}
// If any FK is being changed, ensure the caller owns the referenced rows.
if (
!(await ownsProperty(ctx.ownerId, parsed.data.property_id)) ||
!(await ownsUnit(ctx.ownerId, parsed.data.unit_id)) ||
!(await ownsTenant(ctx.ownerId, parsed.data.tenant_id))
) {
return forbidden()
}
const updateData: Record<string, unknown> = { ...parsed.data }
// Auto-stamp resolved_at when transitioning to resolved.
if (parsed.data.status === "resolved" && !parsed.data.resolved_at) {
updateData.resolved_at = new Date().toISOString()
}
const [data] = await db
.update(maintenance_requests)
.set(updateData)
.where(and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, ctx.ownerId)))
.returning()
if (!data) {
return NextResponse.json({ error: { code: 404, message: "Not found" } }, { status: 404 })
}
if (parsed.data.status) {
await emitWebhookEvent({
ownerId: ctx.ownerId,
event: "maintenance.updated",
data: { maintenance: data },
})
}
return NextResponse.json({ data })
}
+89
View File
@@ -0,0 +1,89 @@
import { NextResponse } from "next/server"
import { and, desc, eq } from "drizzle-orm"
import { db } from "@/lib/db"
import { maintenance_requests } from "@/lib/db/schema"
import { maintenanceSchema } from "@/lib/validations"
import { ownsProperty, ownsUnit, ownsTenant } from "@/lib/db/ownership"
import { resolveApiRequest } from "@/lib/api-auth"
import { emitWebhookEvent } from "@/lib/webhooks/emit"
// Public REST API (v1) — maintenance requests. Bearer API-key auth.
// Scoped by the resolved owner id.
const unauthorized = () =>
NextResponse.json({ error: { code: 401, message: "Unauthorized" } }, { status: 401 })
const forbidden = () =>
NextResponse.json({ error: { code: 403, message: "Forbidden" } }, { status: 403 })
const VALID_STATUSES = ["open", "in_progress", "resolved", "closed"]
const VALID_PRIORITIES = ["low", "medium", "high", "emergency"]
export async function GET(request: Request) {
const ctx = await resolveApiRequest(request)
if (!ctx) return unauthorized()
const { searchParams } = new URL(request.url)
const status = searchParams.get("status")
const priority = searchParams.get("priority")
const propertyId = searchParams.get("property_id")
if (status && !VALID_STATUSES.includes(status)) {
return NextResponse.json({ error: { code: 400, message: "Invalid status" } }, { status: 400 })
}
if (priority && !VALID_PRIORITIES.includes(priority)) {
return NextResponse.json({ error: { code: 400, message: "Invalid priority" } }, { status: 400 })
}
const data = await db.query.maintenance_requests.findMany({
where: and(
eq(maintenance_requests.user_id, ctx.ownerId),
status ? eq(maintenance_requests.status, status as typeof maintenance_requests.$inferSelect.status) : undefined,
priority ? eq(maintenance_requests.priority, priority as typeof maintenance_requests.$inferSelect.priority) : undefined,
propertyId ? eq(maintenance_requests.property_id, propertyId) : undefined
),
with: {
property: { columns: { name: true } },
unit: { columns: { unit_number: true } },
tenant: { columns: { first_name: true, last_name: true } },
},
orderBy: desc(maintenance_requests.created_at),
})
return NextResponse.json({ data, count: data.length })
}
export async function POST(request: Request) {
const ctx = await resolveApiRequest(request)
if (!ctx) return unauthorized()
if (!ctx.canWrite) return forbidden()
const body = await request.json().catch(() => null)
const parsed = maintenanceSchema.safeParse(body)
if (!parsed.success) {
return NextResponse.json(
{ error: { code: 400, message: parsed.error.flatten() } },
{ status: 400 }
)
}
if (
!(await ownsProperty(ctx.ownerId, parsed.data.property_id)) ||
!(await ownsUnit(ctx.ownerId, parsed.data.unit_id)) ||
!(await ownsTenant(ctx.ownerId, parsed.data.tenant_id))
) {
return forbidden()
}
const [data] = await db
.insert(maintenance_requests)
.values({ ...parsed.data, user_id: ctx.ownerId, status: "open" })
.returning()
await emitWebhookEvent({
ownerId: ctx.ownerId,
event: "maintenance.created",
data: { maintenance: data },
})
return NextResponse.json({ data }, { status: 201 })
}