import { NextRequest, NextResponse } from "next/server"; import { getCurrentUser } from "@/lib/auth"; import { getTripById, updateTrip, deleteTrip } from "@/lib/queries"; export async function GET(req: NextRequest, { params }: { params: Promise<{ id: string }> }) { const user = await getCurrentUser(req); if (!user) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); const { id } = await params; const trip = await getTripById(Number(id), user.id); if (!trip) return NextResponse.json({ error: "Not found" }, { status: 404 }); return NextResponse.json(trip); } export async function PATCH(req: NextRequest, { params }: { params: Promise<{ id: string }> }) { const user = await getCurrentUser(req); if (!user) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); const { id } = await params; const body = await req.json(); const trip = await updateTrip(Number(id), user.id, body); if (!trip) return NextResponse.json({ error: "Not found" }, { status: 404 }); return NextResponse.json(trip); } // Everything else about a trip is shared; delete is not. Both trip foreign keys // are ON DELETE SET NULL, so this untags every transaction on the trip and drops // the trip scope from its payments — including the hand-derived Europe-first // allocation, which nothing recomputes. A participant gets a 403 that says so // rather than a 404 that pretends the trip is not there. export async function DELETE(req: NextRequest, { params }: { params: Promise<{ id: string }> }) { const user = await getCurrentUser(req); if (!user) return NextResponse.json({ error: "Forbidden" }, { status: 403 }); const { id } = await params; const trip = await getTripById(Number(id), user.id); if (!trip) return NextResponse.json({ error: "Not found" }, { status: 404 }); if (trip.owner_id !== user.id) { return NextResponse.json( { error: "Only the trip owner can delete a trip. Deleting it would untag every transaction on it and unscope its payments." }, { status: 403 } ); } await deleteTrip(Number(id), user.id); return new NextResponse(null, { status: 204 }); }