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;
|
if (user.dodoCustomerId) return user.dodoCustomerId;
|
||||||
const email = user.billingEmail || user.email || '';
|
const email = user.billingEmail || user.email || '';
|
||||||
if (!email) return '';
|
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', {
|
const customers = await dodoRequest('/customers', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: {
|
body: {
|
||||||
@@ -6775,6 +6808,11 @@ async function ensureDodoCustomer(user) {
|
|||||||
if (customerId) {
|
if (customerId) {
|
||||||
user.dodoCustomerId = customerId;
|
user.dodoCustomerId = customerId;
|
||||||
await persistUsersDb();
|
await persistUsersDb();
|
||||||
|
log('Created new Dodo customer', {
|
||||||
|
userId: user.id,
|
||||||
|
email: email,
|
||||||
|
customerId: customerId
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return customerId;
|
return customerId;
|
||||||
}
|
}
|
||||||
@@ -12121,21 +12159,28 @@ async function handleAccountSettingsUpdate(req, res) {
|
|||||||
const customerId = await ensureDodoCustomer(user);
|
const customerId = await ensureDodoCustomer(user);
|
||||||
if (!customerId) return null;
|
if (!customerId) return null;
|
||||||
|
|
||||||
const subscriptions = await dodoRequest('/subscriptions', {
|
// Check multiple subscription statuses: active, pending, on_hold
|
||||||
method: 'GET',
|
const statusesToCheck = ['active', 'pending', 'on_hold'];
|
||||||
query: { customer_id: customerId, status: 'active' }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (subscriptions?.items && subscriptions.items.length > 0) {
|
for (const status of statusesToCheck) {
|
||||||
const activeSub = subscriptions.items[0];
|
const subscriptions = await dodoRequest('/subscriptions', {
|
||||||
log('Found missing subscription ID via Dodo lookup', {
|
method: 'GET',
|
||||||
userId: user.id,
|
query: { customer_id: customerId, status: status }
|
||||||
email: user.email,
|
|
||||||
subscriptionId: activeSub.subscription_id || activeSub.id,
|
|
||||||
customerId
|
|
||||||
});
|
});
|
||||||
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;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log('Failed to lookup subscription by customer', {
|
log('Failed to lookup subscription by customer', {
|
||||||
|
|||||||
Reference in New Issue
Block a user