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
@@ -1,10 +1,12 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { and, eq } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { maintenance_requests } from "@/lib/db/schema"
|
||||
import { maintenance_requests, notifications, profiles } from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { maintenanceSchema } from "@/lib/validations"
|
||||
import { ownsProperty, ownsUnit, ownsTenant } from "@/lib/db/ownership"
|
||||
import { getEffectiveOwnerId, getAccountContext } from "@/lib/account"
|
||||
import { emitWebhookEvent } from "@/lib/webhooks/emit"
|
||||
import { z } from "zod"
|
||||
|
||||
const maintenancePatchSchema = maintenanceSchema.partial().extend({
|
||||
@@ -18,9 +20,11 @@ export async function GET(_: Request, { params }: { params: Promise<{ id: string
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const ownerId = await getEffectiveOwnerId(user.id)
|
||||
|
||||
const { id } = await params
|
||||
const data = await db.query.maintenance_requests.findFirst({
|
||||
where: and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, user.id)),
|
||||
where: and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, ownerId)),
|
||||
with: {
|
||||
property: { columns: { name: true, address_line1: true, city: true } },
|
||||
unit: { columns: { unit_number: true } },
|
||||
@@ -36,19 +40,29 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const ctx = await getAccountContext(user.id)
|
||||
const ownerId = ctx.ownerId
|
||||
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
||||
|
||||
const { id } = await params
|
||||
const body = await request.json()
|
||||
const parsed = maintenancePatchSchema.safeParse(body)
|
||||
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
|
||||
if (
|
||||
!(await ownsProperty(user.id, parsed.data.property_id)) ||
|
||||
!(await ownsUnit(user.id, parsed.data.unit_id)) ||
|
||||
!(await ownsTenant(user.id, parsed.data.tenant_id))
|
||||
!(await ownsProperty(ownerId, parsed.data.property_id)) ||
|
||||
!(await ownsUnit(ownerId, parsed.data.unit_id)) ||
|
||||
!(await ownsTenant(ownerId, parsed.data.tenant_id))
|
||||
) {
|
||||
return NextResponse.json({ error: "Invalid reference" }, { status: 403 })
|
||||
}
|
||||
|
||||
// Read the current row so we can tell whether the status actually changed.
|
||||
const existing = await db.query.maintenance_requests.findFirst({
|
||||
where: and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, ownerId)),
|
||||
columns: { status: true, title: true },
|
||||
})
|
||||
|
||||
const updateData: Record<string, unknown> = { ...parsed.data }
|
||||
|
||||
// Auto-set resolved_at when status → resolved
|
||||
@@ -59,10 +73,40 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
||||
const [data] = await db
|
||||
.update(maintenance_requests)
|
||||
.set(updateData)
|
||||
.where(and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, user.id)))
|
||||
.where(and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, ownerId)))
|
||||
.returning()
|
||||
|
||||
if (!data) return NextResponse.json({ error: "Not found" }, { status: 404 })
|
||||
|
||||
// If the status actually changed, drop a dashboard notification row for the
|
||||
// owner and fire the maintenance.updated webhook.
|
||||
if (parsed.data.status && existing && parsed.data.status !== existing.status) {
|
||||
const owner = await db.query.profiles.findFirst({
|
||||
where: eq(profiles.id, ownerId),
|
||||
columns: { email: true },
|
||||
})
|
||||
if (owner?.email) {
|
||||
await db.insert(notifications).values({
|
||||
user_id: ownerId,
|
||||
type: "maintenance_update",
|
||||
recipient_email: owner.email,
|
||||
subject: `Maintenance "${data.title}" marked ${data.status}`,
|
||||
status: "sent",
|
||||
metadata: {
|
||||
maintenance_id: id,
|
||||
status: data.status,
|
||||
body: `Status changed from ${existing.status} to ${data.status}.`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
await emitWebhookEvent({
|
||||
ownerId,
|
||||
event: "maintenance.updated",
|
||||
data: { maintenance: data, previous_status: existing.status },
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json(data)
|
||||
}
|
||||
|
||||
@@ -70,10 +114,14 @@ export async function DELETE(_: Request, { params }: { params: Promise<{ id: str
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const ctx = await getAccountContext(user.id)
|
||||
const ownerId = ctx.ownerId
|
||||
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
||||
|
||||
const { id } = await params
|
||||
await db
|
||||
.delete(maintenance_requests)
|
||||
.where(and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, user.id)))
|
||||
.where(and(eq(maintenance_requests.id, id), eq(maintenance_requests.user_id, ownerId)))
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import { getSessionUser } from "@/lib/session"
|
||||
import { maintenanceSchema } from "@/lib/validations"
|
||||
import { ownsProperty, ownsUnit, ownsTenant } from "@/lib/db/ownership"
|
||||
import { logActivity } from "@/lib/activity"
|
||||
import { getEffectiveOwnerId, getAccountContext } from "@/lib/account"
|
||||
import { emitWebhookEvent } from "@/lib/webhooks/emit"
|
||||
|
||||
const VALID_STATUSES = ["open", "in_progress", "resolved", "closed"]
|
||||
const VALID_PRIORITIES = ["low", "medium", "high", "emergency"]
|
||||
@@ -14,6 +16,8 @@ export async function GET(request: Request) {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const ownerId = await getEffectiveOwnerId(user.id)
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const status = searchParams.get("status")
|
||||
const priority = searchParams.get("priority")
|
||||
@@ -29,7 +33,7 @@ export async function GET(request: Request) {
|
||||
|
||||
const data = await db.query.maintenance_requests.findMany({
|
||||
where: and(
|
||||
eq(maintenance_requests.user_id, user.id),
|
||||
eq(maintenance_requests.user_id, 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
|
||||
@@ -53,8 +57,10 @@ export async function POST(request: Request) {
|
||||
let userId: string
|
||||
|
||||
if (user) {
|
||||
// Authenticated landlord
|
||||
userId = user.id
|
||||
// Authenticated landlord — scope to the account owner and enforce write access.
|
||||
const ctx = await getAccountContext(user.id)
|
||||
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
||||
userId = ctx.ownerId
|
||||
} else {
|
||||
// Tenant portal submission — verify portal_token
|
||||
const portalToken = body.portal_token as string | undefined
|
||||
@@ -90,9 +96,9 @@ export async function POST(request: Request) {
|
||||
// (The portal-token path already validates these against the tenant above.)
|
||||
if (
|
||||
user &&
|
||||
(!(await ownsProperty(user.id, parsed.data.property_id)) ||
|
||||
!(await ownsUnit(user.id, parsed.data.unit_id)) ||
|
||||
!(await ownsTenant(user.id, parsed.data.tenant_id)))
|
||||
(!(await ownsProperty(userId, parsed.data.property_id)) ||
|
||||
!(await ownsUnit(userId, parsed.data.unit_id)) ||
|
||||
!(await ownsTenant(userId, parsed.data.tenant_id)))
|
||||
) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
||||
}
|
||||
@@ -111,5 +117,11 @@ export async function POST(request: Request) {
|
||||
entityId: data.id,
|
||||
})
|
||||
|
||||
await emitWebhookEvent({
|
||||
ownerId: userId,
|
||||
event: "maintenance.created",
|
||||
data: { maintenance: data },
|
||||
})
|
||||
|
||||
return NextResponse.json(data, { status: 201 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user