diff --git a/chat/server.js b/chat/server.js index 22b46fe..a314a6a 100644 --- a/chat/server.js +++ b/chat/server.js @@ -6766,15 +6766,35 @@ async function ensureDodoCustomer(user) { // First, try to find existing customer by email try { + log('DEBUG: Looking up Dodo customer by email', { + userId: user.id, + email: email + }); + const existingCustomers = await dodoRequest('/customers', { method: 'GET', query: { email: email } }); + log('DEBUG: Customer lookup response', { + userId: user.id, + email: email, + responseKeys: Object.keys(existingCustomers || {}), + hasItems: !!existingCustomers?.items, + itemsCount: existingCustomers?.items?.length || 0, + rawResponse: JSON.stringify(existingCustomers).substring(0, 500) + }); + // Check if any customers were returned if (existingCustomers?.items && existingCustomers.items.length > 0) { const existingCustomer = existingCustomers.items[0]; const customerId = existingCustomer?.customer_id || existingCustomer?.id || ''; + log('DEBUG: Found customer in items', { + userId: user.id, + customerId: customerId, + customerKeys: Object.keys(existingCustomer || {}), + rawCustomer: JSON.stringify(existingCustomer).substring(0, 500) + }); if (customerId) { user.dodoCustomerId = customerId; await persistUsersDb(); @@ -6785,13 +6805,20 @@ async function ensureDodoCustomer(user) { }); return customerId; } + } else { + log('DEBUG: No customers found in Dodo for email', { + userId: user.id, + email: email, + fullResponse: JSON.stringify(existingCustomers) + }); } } catch (error) { // Log the lookup failure but continue to create new customer log('Failed to lookup existing Dodo customer by email', { userId: user.id, email: email, - error: String(error) + error: String(error), + errorStack: error.stack }); } @@ -12156,18 +12183,45 @@ async function handleAccountSettingsUpdate(req, res) { // Helper function to lookup subscription by customer async function lookupSubscriptionByCustomer(user) { try { + log('DEBUG: Starting subscription lookup', { + userId: user.id, + email: user.email, + hasDodoCustomerId: !!user.dodoCustomerId + }); + const customerId = await ensureDodoCustomer(user); + + log('DEBUG: After ensureDodoCustomer', { + userId: user.id, + customerId: customerId, + hasCustomerId: !!customerId + }); + if (!customerId) return null; // Check multiple subscription statuses: active, pending, on_hold const statusesToCheck = ['active', 'pending', 'on_hold']; for (const status of statusesToCheck) { + log('DEBUG: Querying subscriptions', { + userId: user.id, + customerId: customerId, + status: status + }); + const subscriptions = await dodoRequest('/subscriptions', { method: 'GET', query: { customer_id: customerId, status: status } }); + log('DEBUG: Subscription query response', { + userId: user.id, + status: status, + responseKeys: Object.keys(subscriptions || {}), + hasItems: !!subscriptions?.items, + itemsCount: subscriptions?.items?.length || 0 + }); + if (subscriptions?.items && subscriptions.items.length > 0) { const foundSub = subscriptions.items[0]; log('Found missing subscription ID via Dodo lookup', { @@ -12181,6 +12235,12 @@ async function handleAccountSettingsUpdate(req, res) { } } + log('DEBUG: No subscriptions found for customer', { + userId: user.id, + customerId: customerId, + checkedStatuses: statusesToCheck + }); + return null; } catch (error) { log('Failed to lookup subscription by customer', { @@ -12194,6 +12254,17 @@ async function handleAccountSettingsUpdate(req, res) { // If paid user is missing dodoSubscriptionId, try to look it up if ((isPaidToFree || isPaidToPaid) && !user.dodoSubscriptionId && PAID_PLANS.has(user.plan)) { + log('DEBUG: About to lookup subscription for paid user', { + userId: user.id, + email: user.email, + currentPlan: user.plan, + requestedPlan: requestedPlan, + isPaidToFree: isPaidToFree, + isPaidToPaid: isPaidToPaid, + hasDodoCustomerId: !!user.dodoCustomerId, + dodoCustomerId: user.dodoCustomerId + }); + const foundSubscriptionId = await lookupSubscriptionByCustomer(user); if (foundSubscriptionId) { user.dodoSubscriptionId = foundSubscriptionId;