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
@@ -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_predictions)
|
||||
.where(eq(ai_predictions.user_id, user.id))
|
||||
.where(eq(ai_predictions.user_id, ownerId))
|
||||
.orderBy(desc(ai_predictions.created_at))
|
||||
.limit(30)
|
||||
|
||||
@@ -38,13 +41,17 @@ export async function POST() {
|
||||
const quota = await enforceAiQuota(user.id, "ai_predictions")
|
||||
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
|
||||
|
||||
const now = new Date()
|
||||
const sixMonthsAgo = new Date(now)
|
||||
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6)
|
||||
const sixMonthsAgoDate = sixMonthsAgo.toISOString().slice(0, 10)
|
||||
|
||||
const [propertiesData, unitsData, tenantsData, payments, maintenance, leasesData, expensesData] = await Promise.all([
|
||||
db.select({ id: properties.id, name: properties.name }).from(properties).where(eq(properties.user_id, user.id)),
|
||||
db.select({ id: properties.id, name: properties.name }).from(properties).where(eq(properties.user_id, ownerId)),
|
||||
db
|
||||
.select({
|
||||
id: units.id,
|
||||
@@ -54,7 +61,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,
|
||||
@@ -64,7 +71,7 @@ export async function POST() {
|
||||
property_id: tenants.property_id,
|
||||
})
|
||||
.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({
|
||||
amount: rent_payments.amount,
|
||||
@@ -73,7 +80,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, sixMonthsAgoDate)))
|
||||
.where(and(eq(rent_payments.user_id, ownerId), gte(rent_payments.due_date, sixMonthsAgoDate)))
|
||||
.orderBy(rent_payments.due_date),
|
||||
db
|
||||
.select({
|
||||
@@ -84,7 +91,7 @@ export async function POST() {
|
||||
property_id: maintenance_requests.property_id,
|
||||
})
|
||||
.from(maintenance_requests)
|
||||
.where(eq(maintenance_requests.user_id, user.id)),
|
||||
.where(eq(maintenance_requests.user_id, ownerId)),
|
||||
db
|
||||
.select({
|
||||
tenant_id: leases.tenant_id,
|
||||
@@ -94,7 +101,7 @@ export async function POST() {
|
||||
status: leases.status,
|
||||
})
|
||||
.from(leases)
|
||||
.where(eq(leases.user_id, user.id)),
|
||||
.where(eq(leases.user_id, ownerId)),
|
||||
db
|
||||
.select({
|
||||
amount: expenses.amount,
|
||||
@@ -103,7 +110,7 @@ export async function POST() {
|
||||
property_id: expenses.property_id,
|
||||
})
|
||||
.from(expenses)
|
||||
.where(and(eq(expenses.user_id, user.id), gte(expenses.expense_date, sixMonthsAgoDate))),
|
||||
.where(and(eq(expenses.user_id, ownerId), gte(expenses.expense_date, sixMonthsAgoDate))),
|
||||
])
|
||||
|
||||
// Build monthly revenue trend
|
||||
@@ -183,10 +190,10 @@ Only return valid JSON, no other text.`
|
||||
}
|
||||
|
||||
// Replace old predictions
|
||||
await db.delete(ai_predictions).where(eq(ai_predictions.user_id, user.id))
|
||||
await db.delete(ai_predictions).where(eq(ai_predictions.user_id, ownerId))
|
||||
|
||||
const toInsert = predictions.map((p: any) => ({
|
||||
user_id: user.id,
|
||||
user_id: ownerId,
|
||||
type: p.type ?? "growth_opportunity",
|
||||
title: p.title,
|
||||
prediction: p.prediction,
|
||||
@@ -199,7 +206,7 @@ Only return valid JSON, no other text.`
|
||||
const inserted = toInsert.length > 0 ? await db.insert(ai_predictions).values(toInsert).returning() : []
|
||||
|
||||
await logActivity({
|
||||
userId: user.id,
|
||||
userId: ownerId,
|
||||
type: "ai_action",
|
||||
title: `AI generated ${inserted.length} predictions and risk alerts`,
|
||||
entityType: "ai_predictions",
|
||||
|
||||
Reference in New Issue
Block a user