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
@@ -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