|
| 1 | +/** |
| 2 | + * Debug API endpoint to verify subscription environment configuration |
| 3 | + * This helps ensure the Current Subscription card is using the correct environment-aware collections |
| 4 | + */ |
| 5 | + |
| 6 | +import { NextRequest, NextResponse } from 'next/server'; |
| 7 | +import { getUserIdFromRequest } from '../../auth-helper'; |
| 8 | +import { |
| 9 | + getSubCollectionPath, |
| 10 | + PAYMENT_COLLECTIONS, |
| 11 | + getEnvironmentType, |
| 12 | + getSubscriptionEnvironmentType, |
| 13 | + getSubscriptionEnvironmentPrefix |
| 14 | +} from '../../../utils/environmentConfig'; |
| 15 | +import { getFirebaseAdmin } from '../../../firebase/admin'; |
| 16 | + |
| 17 | +export async function GET(request: NextRequest) { |
| 18 | + try { |
| 19 | + // Get authenticated user |
| 20 | + const authenticatedUserId = await getUserIdFromRequest(request); |
| 21 | + if (!authenticatedUserId) { |
| 22 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); |
| 23 | + } |
| 24 | + |
| 25 | + // Check if a specific userId is requested via query parameter |
| 26 | + const url = new URL(request.url); |
| 27 | + const targetUserId = url.searchParams.get('userId') || authenticatedUserId; |
| 28 | + |
| 29 | + console.log(`[SUBSCRIPTION ENV DEBUG] Checking environment config for user: ${targetUserId}`); |
| 30 | + |
| 31 | + // Get environment information |
| 32 | + const envType = getEnvironmentType(); |
| 33 | + const subscriptionEnvType = getSubscriptionEnvironmentType(); |
| 34 | + const subscriptionPrefix = getSubscriptionEnvironmentPrefix(); |
| 35 | + |
| 36 | + // Get collection paths |
| 37 | + const { parentPath, subCollectionName } = getSubCollectionPath( |
| 38 | + PAYMENT_COLLECTIONS.USERS, |
| 39 | + targetUserId, |
| 40 | + PAYMENT_COLLECTIONS.SUBSCRIPTIONS |
| 41 | + ); |
| 42 | + |
| 43 | + // Check if subscription document exists in the expected path |
| 44 | + const admin = getFirebaseAdmin(); |
| 45 | + const db = admin.firestore(); |
| 46 | + |
| 47 | + let subscriptionExists = false; |
| 48 | + let subscriptionData = null; |
| 49 | + let rawData = null; |
| 50 | + |
| 51 | + try { |
| 52 | + const subscriptionRef = db.doc(parentPath).collection(subCollectionName).doc('current'); |
| 53 | + const subscriptionSnap = await subscriptionRef.get(); |
| 54 | + subscriptionExists = subscriptionSnap.exists; |
| 55 | + |
| 56 | + if (subscriptionExists) { |
| 57 | + rawData = subscriptionSnap.data(); |
| 58 | + subscriptionData = { |
| 59 | + id: subscriptionSnap.id, |
| 60 | + status: rawData?.status, |
| 61 | + amount: rawData?.amount, |
| 62 | + tier: rawData?.tier, |
| 63 | + stripeSubscriptionId: rawData?.stripeSubscriptionId, |
| 64 | + createdAt: rawData?.createdAt, |
| 65 | + updatedAt: rawData?.updatedAt |
| 66 | + }; |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + console.error('[SUBSCRIPTION ENV DEBUG] Error checking subscription:', error); |
| 70 | + } |
| 71 | + |
| 72 | + // Also check if there's data in the wrong environment (common issue) |
| 73 | + const wrongPaths = []; |
| 74 | + |
| 75 | + // Check production path if we're in dev |
| 76 | + if (envType === 'development') { |
| 77 | + try { |
| 78 | + const prodRef = db.doc(`users/${targetUserId}`).collection('subscriptions').doc('current'); |
| 79 | + const prodSnap = await prodRef.get(); |
| 80 | + if (prodSnap.exists) { |
| 81 | + wrongPaths.push({ |
| 82 | + path: `users/${targetUserId}/subscriptions/current`, |
| 83 | + exists: true, |
| 84 | + data: prodSnap.data() |
| 85 | + }); |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + // Ignore errors checking wrong paths |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Check dev path if we're in production |
| 93 | + if (envType === 'production' || envType === 'preview') { |
| 94 | + try { |
| 95 | + const devRef = db.doc(`DEV_users/${targetUserId}`).collection('DEV_subscriptions').doc('current'); |
| 96 | + const devSnap = await devRef.get(); |
| 97 | + if (devSnap.exists) { |
| 98 | + wrongPaths.push({ |
| 99 | + path: `DEV_users/${targetUserId}/DEV_subscriptions/current`, |
| 100 | + exists: true, |
| 101 | + data: devSnap.data() |
| 102 | + }); |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + // Ignore errors checking wrong paths |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + const debugInfo = { |
| 110 | + environment: { |
| 111 | + type: envType, |
| 112 | + subscriptionType: subscriptionEnvType, |
| 113 | + subscriptionPrefix, |
| 114 | + nodeEnv: process.env.NODE_ENV, |
| 115 | + vercelEnv: process.env.VERCEL_ENV, |
| 116 | + subscriptionEnv: process.env.SUBSCRIPTION_ENV |
| 117 | + }, |
| 118 | + paths: { |
| 119 | + expected: { |
| 120 | + parentPath, |
| 121 | + subCollectionName, |
| 122 | + fullPath: `${parentPath}/${subCollectionName}/current` |
| 123 | + }, |
| 124 | + wrongEnvironmentPaths: wrongPaths |
| 125 | + }, |
| 126 | + subscription: { |
| 127 | + exists: subscriptionExists, |
| 128 | + data: subscriptionData, |
| 129 | + rawData: rawData |
| 130 | + }, |
| 131 | + issues: [] |
| 132 | + }; |
| 133 | + |
| 134 | + // Identify potential issues |
| 135 | + if (!subscriptionExists && wrongPaths.length > 0) { |
| 136 | + debugInfo.issues.push('Subscription data found in wrong environment collections'); |
| 137 | + } |
| 138 | + |
| 139 | + if (subscriptionExists && !rawData?.status) { |
| 140 | + debugInfo.issues.push('Subscription document exists but missing status field (data corruption)'); |
| 141 | + } |
| 142 | + |
| 143 | + if (subscriptionExists && rawData?.status === 'cancelled' && !rawData?.canceledAt) { |
| 144 | + debugInfo.issues.push('Subscription marked as cancelled but no canceledAt timestamp'); |
| 145 | + } |
| 146 | + |
| 147 | + return NextResponse.json(debugInfo); |
| 148 | + |
| 149 | + } catch (error) { |
| 150 | + console.error('[SUBSCRIPTION ENV DEBUG] Error:', error); |
| 151 | + return NextResponse.json({ |
| 152 | + error: 'Internal server error', |
| 153 | + details: error.message |
| 154 | + }, { status: 500 }); |
| 155 | + } |
| 156 | +} |
0 commit comments