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
This commit is contained in:
@@ -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', {
|
||||
|
||||
Reference in New Issue
Block a user