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
@@ -3,12 +3,17 @@ import { and, eq } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { ai_recommendations } from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { getAccountContext } from "@/lib/account"
|
||||
import { logActivity } from "@/lib/activity"
|
||||
|
||||
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const ctx = await getAccountContext(user.id)
|
||||
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
||||
const ownerId = ctx.ownerId
|
||||
|
||||
const { id } = await params
|
||||
const { status } = await request.json() as { status: "approved" | "dismissed" }
|
||||
|
||||
@@ -23,13 +28,13 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
||||
const [data] = await db
|
||||
.update(ai_recommendations)
|
||||
.set(updateData)
|
||||
.where(and(eq(ai_recommendations.id, id), eq(ai_recommendations.user_id, user.id)))
|
||||
.where(and(eq(ai_recommendations.id, id), eq(ai_recommendations.user_id, ownerId)))
|
||||
.returning()
|
||||
|
||||
if (!data) return NextResponse.json({ error: "Not found" }, { status: 404 })
|
||||
|
||||
await logActivity({
|
||||
userId: user.id,
|
||||
userId: ownerId,
|
||||
type: "ai_action",
|
||||
title: status === "approved"
|
||||
? `AI recommendation approved: ${data.title}`
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
expenses,
|
||||
} from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { getEffectiveOwnerId, getAccountContext } from "@/lib/account"
|
||||
import { openai } from "@/lib/ai/client"
|
||||
import { logActivity } from "@/lib/activity"
|
||||
import { enforceAiQuota } from "@/lib/ai/usage"
|
||||
@@ -21,10 +22,12 @@ export async function GET() {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const ownerId = await getEffectiveOwnerId(user.id)
|
||||
|
||||
const data = await db
|
||||
.select()
|
||||
.from(ai_recommendations)
|
||||
.where(eq(ai_recommendations.user_id, user.id))
|
||||
.where(eq(ai_recommendations.user_id, ownerId))
|
||||
.orderBy(desc(ai_recommendations.created_at))
|
||||
|
||||
return NextResponse.json(data)
|
||||
@@ -37,6 +40,10 @@ export async function POST() {
|
||||
const quota = await enforceAiQuota(user.id, "ai_recommendations")
|
||||
if (!quota.ok) return NextResponse.json({ error: quota.error }, { status: quota.status })
|
||||
|
||||
const ctx = await getAccountContext(user.id)
|
||||
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
||||
const ownerId = ctx.ownerId
|
||||
|
||||
// Fetch portfolio data
|
||||
const now = new Date()
|
||||
const threeMonthsAgo = new Date(now)
|
||||
@@ -47,7 +54,7 @@ export async function POST() {
|
||||
db
|
||||
.select({ id: properties.id, name: properties.name, address_line1: properties.address_line1, city: properties.city })
|
||||
.from(properties)
|
||||
.where(eq(properties.user_id, user.id)),
|
||||
.where(eq(properties.user_id, ownerId)),
|
||||
db
|
||||
.select({
|
||||
id: units.id,
|
||||
@@ -57,7 +64,7 @@ export async function POST() {
|
||||
status: units.status,
|
||||
})
|
||||
.from(units)
|
||||
.where(eq(units.user_id, user.id)),
|
||||
.where(eq(units.user_id, ownerId)),
|
||||
db
|
||||
.select({
|
||||
id: tenants.id,
|
||||
@@ -69,7 +76,7 @@ export async function POST() {
|
||||
move_in_date: tenants.move_in_date,
|
||||
})
|
||||
.from(tenants)
|
||||
.where(and(eq(tenants.user_id, user.id), eq(tenants.status, "active"))),
|
||||
.where(and(eq(tenants.user_id, ownerId), eq(tenants.status, "active"))),
|
||||
db
|
||||
.select({
|
||||
id: rent_payments.id,
|
||||
@@ -80,7 +87,7 @@ export async function POST() {
|
||||
property_id: rent_payments.property_id,
|
||||
})
|
||||
.from(rent_payments)
|
||||
.where(and(eq(rent_payments.user_id, user.id), gte(rent_payments.due_date, threeMonthsAgoDate))),
|
||||
.where(and(eq(rent_payments.user_id, ownerId), gte(rent_payments.due_date, threeMonthsAgoDate))),
|
||||
db
|
||||
.select({
|
||||
id: maintenance_requests.id,
|
||||
@@ -91,7 +98,7 @@ export async function POST() {
|
||||
created_at: maintenance_requests.created_at,
|
||||
})
|
||||
.from(maintenance_requests)
|
||||
.where(and(eq(maintenance_requests.user_id, user.id), inArray(maintenance_requests.status, ["open", "in_progress"]))),
|
||||
.where(and(eq(maintenance_requests.user_id, ownerId), inArray(maintenance_requests.status, ["open", "in_progress"]))),
|
||||
db
|
||||
.select({
|
||||
id: leases.id,
|
||||
@@ -102,7 +109,7 @@ export async function POST() {
|
||||
status: leases.status,
|
||||
})
|
||||
.from(leases)
|
||||
.where(and(eq(leases.user_id, user.id), eq(leases.status, "active"))),
|
||||
.where(and(eq(leases.user_id, ownerId), eq(leases.status, "active"))),
|
||||
db
|
||||
.select({
|
||||
amount: expenses.amount,
|
||||
@@ -111,7 +118,7 @@ export async function POST() {
|
||||
expense_date: expenses.expense_date,
|
||||
})
|
||||
.from(expenses)
|
||||
.where(and(eq(expenses.user_id, user.id), gte(expenses.expense_date, threeMonthsAgoDate))),
|
||||
.where(and(eq(expenses.user_id, ownerId), gte(expenses.expense_date, threeMonthsAgoDate))),
|
||||
])
|
||||
|
||||
const totalRevenue = payments.filter((p) => p.status === "paid").reduce((s, p) => s + Number(p.amount), 0)
|
||||
@@ -175,10 +182,10 @@ Only return valid JSON, no other text.`
|
||||
// Delete old pending recommendations and insert new ones
|
||||
await db
|
||||
.delete(ai_recommendations)
|
||||
.where(and(eq(ai_recommendations.user_id, user.id), eq(ai_recommendations.status, "pending")))
|
||||
.where(and(eq(ai_recommendations.user_id, ownerId), eq(ai_recommendations.status, "pending")))
|
||||
|
||||
const toInsert = recommendations.map((r: any) => ({
|
||||
user_id: user.id,
|
||||
user_id: ownerId,
|
||||
type: r.type ?? "opportunity",
|
||||
title: r.title,
|
||||
description: r.description,
|
||||
@@ -192,7 +199,7 @@ Only return valid JSON, no other text.`
|
||||
const inserted = toInsert.length > 0 ? await db.insert(ai_recommendations).values(toInsert).returning() : []
|
||||
|
||||
await logActivity({
|
||||
userId: user.id,
|
||||
userId: ownerId,
|
||||
type: "ai_action",
|
||||
title: `AI generated ${inserted.length} new recommendations`,
|
||||
entityType: "ai_recommendations",
|
||||
|
||||
Reference in New Issue
Block a user