import { NextRequest, NextResponse } from "next/server"; import { getPool } from "@/lib/database"; export async function POST(request: NextRequest) { try { // Get user ID from auth token or session const userId = request.headers.get("user-id"); if (!userId) { return NextResponse.json( { error: "User not authenticated" }, { status: 401 } ); } const pool = getPool(); const client = await pool.connect(); try { // Cancel the subscription await client.query( `UPDATE subscriptions SET status = 'CANCELLED', updated_at = CURRENT_TIMESTAMP WHERE user_id = $1 AND status = 'ACTIVE'`, [userId] ); return NextResponse.json({ success: true, message: "Subscription cancelled", }); } finally { client.release(); } } catch (error) { console.error("Error cancelling subscription:", error); return NextResponse.json( { error: "Failed to cancel subscription" }, { status: 500 } ); } }