You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB

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 }
);
}
}