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.

54 lines
1.5 KiB

import { NextRequest, NextResponse } from "next/server";
import { getPool } from "@/lib/database";
export async function GET(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 {
// Try to get active subscription first, otherwise get the most recent one
const result = await client.query(
`SELECT id, tier, status, storage_limit_gb, current_usage_gb, created_at, updated_at
FROM subscriptions
WHERE user_id = $1
ORDER BY
CASE WHEN status = 'ACTIVE' THEN 0 ELSE 1 END,
created_at DESC
LIMIT 1`,
[userId]
);
console.log(
`Found ${result.rows.length} subscriptions for user ${userId}`
);
if (result.rows.length === 0) {
console.log("No subscriptions found");
return NextResponse.json({ subscription: null });
}
console.log("Returning subscription:", result.rows[0]);
return NextResponse.json({ subscription: result.rows[0] });
} finally {
client.release();
}
} catch (error) {
console.error("Error fetching subscription:", error);
return NextResponse.json(
{ error: "Failed to fetch subscription" },
{ status: 500 }
);
}
}