From d82dcfd5e41d50b70a39b0a60d5aaaaad7e147d9 Mon Sep 17 00:00:00 2001 From: southseact-3d Date: Thu, 12 Feb 2026 11:30:52 +0000 Subject: [PATCH] fix: improve customer lookup to prevent duplicate customers in Dodo - Modified ensureDodoCustomer() to query existing customers by email before creating new ones - This prevents duplicate customer records when dodoCustomerId is missing from database - Added logging for both found existing customers and new customer creation - Updated subscription lookup to check multiple statuses (active, pending, on_hold) - Should resolve paid-to-paid and paid-to-free plan change issues for users missing subscription IDs --- chat/server.js | 69 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/chat/server.js b/chat/server.js index 8e8f46a..22b46fe 100644 --- a/chat/server.js +++ b/chat/server.js @@ -6763,6 +6763,39 @@ async function ensureDodoCustomer(user) { if (user.dodoCustomerId) return user.dodoCustomerId; const email = user.billingEmail || user.email || ''; if (!email) return ''; + + // First, try to find existing customer by email + try { + const existingCustomers = await dodoRequest('/customers', { + method: 'GET', + query: { email: email } + }); + + // 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 || ''; + if (customerId) { + user.dodoCustomerId = customerId; + await persistUsersDb(); + log('Found existing Dodo customer by email', { + userId: user.id, + email: email, + customerId: customerId + }); + return customerId; + } + } + } 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) + }); + } + + // If no existing customer found, create a new one const customers = await dodoRequest('/customers', { method: 'POST', body: { @@ -6775,6 +6808,11 @@ async function ensureDodoCustomer(user) { if (customerId) { user.dodoCustomerId = customerId; await persistUsersDb(); + log('Created new Dodo customer', { + userId: user.id, + email: email, + customerId: customerId + }); } return customerId; } @@ -12121,21 +12159,28 @@ async function handleAccountSettingsUpdate(req, res) { const customerId = await ensureDodoCustomer(user); if (!customerId) return null; - const subscriptions = await dodoRequest('/subscriptions', { - method: 'GET', - query: { customer_id: customerId, status: 'active' } - }); + // Check multiple subscription statuses: active, pending, on_hold + const statusesToCheck = ['active', 'pending', 'on_hold']; - if (subscriptions?.items && subscriptions.items.length > 0) { - const activeSub = subscriptions.items[0]; - log('Found missing subscription ID via Dodo lookup', { - userId: user.id, - email: user.email, - subscriptionId: activeSub.subscription_id || activeSub.id, - customerId + for (const status of statusesToCheck) { + const subscriptions = await dodoRequest('/subscriptions', { + method: 'GET', + query: { customer_id: customerId, status: status } }); - return activeSub.subscription_id || activeSub.id; + + if (subscriptions?.items && subscriptions.items.length > 0) { + const foundSub = subscriptions.items[0]; + log('Found missing subscription ID via Dodo lookup', { + userId: user.id, + email: user.email, + subscriptionId: foundSub.subscription_id || foundSub.id, + status: status, + customerId + }); + return foundSub.subscription_id || foundSub.id; + } } + return null; } catch (error) { log('Failed to lookup subscription by customer', {