Files
property-management-network/app/api/team/[id]/route.ts
T
Leon SerfatyandClaude Opus 4.8 c9968531e4 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>
2026-07-02 13:42:34 -04:00

67 lines
2.2 KiB
TypeScript

import { NextResponse } from "next/server"
import { and, eq } from "drizzle-orm"
import { z } from "zod"
import { db } from "@/lib/db"
import { account_members } from "@/lib/db/schema"
import { getSessionUser } from "@/lib/session"
const patchSchema = z.object({
role: z.enum(["member", "viewer"]),
})
/**
* PATCH — change a member's role (member <-> viewer). Owner-only; scoped to
* rows the session user owns so a member can't edit someone else's team.
*/
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 { id } = await params
const body = await request.json().catch(() => null)
const parsed = patchSchema.safeParse(body)
if (!parsed.success) {
return NextResponse.json(
{ error: parsed.error.issues[0]?.message ?? "Invalid input" },
{ status: 400 }
)
}
const [updated] = await db
.update(account_members)
.set({ role: parsed.data.role })
.where(and(eq(account_members.id, id), eq(account_members.owner_id, user.id)))
.returning({
id: account_members.id,
email: account_members.email,
role: account_members.role,
status: account_members.status,
})
if (!updated) return NextResponse.json({ error: "Not found" }, { status: 404 })
return NextResponse.json(updated)
}
/**
* DELETE — revoke a member's access. Sets status='revoked' and clears
* member_id so getAccountContext immediately stops resolving them to this
* owner. Owner-only; scoped to rows the session user owns.
*/
export async function DELETE(_: Request, { params }: { params: Promise<{ id: string }> }) {
const user = await getSessionUser()
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
const { id } = await params
const [revoked] = await db
.update(account_members)
.set({ status: "revoked", member_id: null })
.where(and(eq(account_members.id, id), eq(account_members.owner_id, user.id)))
.returning({ id: account_members.id })
if (!revoked) return NextResponse.json({ error: "Not found" }, { status: 404 })
return NextResponse.json({ ok: true })
}