debug: add detailed logging for customer and subscription lookup
Add comprehensive DEBUG logs to trace through: - Customer lookup by email (request and response details) - Subscription lookup by customer ID (for each status) - Entry point when plan change is initiated This will help identify exactly where the lookup is failing.
This commit is contained in:
@@ -6766,15 +6766,35 @@ async function ensureDodoCustomer(user) {
|
|||||||
|
|
||||||
// First, try to find existing customer by email
|
// First, try to find existing customer by email
|
||||||
try {
|
try {
|
||||||
|
log('DEBUG: Looking up Dodo customer by email', {
|
||||||
|
userId: user.id,
|
||||||
|
email: email
|
||||||
|
});
|
||||||
|
|
||||||
const existingCustomers = await dodoRequest('/customers', {
|
const existingCustomers = await dodoRequest('/customers', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
query: { email: email }
|
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
|
// Check if any customers were returned
|
||||||
if (existingCustomers?.items && existingCustomers.items.length > 0) {
|
if (existingCustomers?.items && existingCustomers.items.length > 0) {
|
||||||
const existingCustomer = existingCustomers.items[0];
|
const existingCustomer = existingCustomers.items[0];
|
||||||
const customerId = existingCustomer?.customer_id || existingCustomer?.id || '';
|
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) {
|
if (customerId) {
|
||||||
user.dodoCustomerId = customerId;
|
user.dodoCustomerId = customerId;
|
||||||
await persistUsersDb();
|
await persistUsersDb();
|
||||||
@@ -6785,13 +6805,20 @@ async function ensureDodoCustomer(user) {
|
|||||||
});
|
});
|
||||||
return customerId;
|
return customerId;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log('DEBUG: No customers found in Dodo for email', {
|
||||||
|
userId: user.id,
|
||||||
|
email: email,
|
||||||
|
fullResponse: JSON.stringify(existingCustomers)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Log the lookup failure but continue to create new customer
|
// Log the lookup failure but continue to create new customer
|
||||||
log('Failed to lookup existing Dodo customer by email', {
|
log('Failed to lookup existing Dodo customer by email', {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
email: email,
|
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
|
// Helper function to lookup subscription by customer
|
||||||
async function lookupSubscriptionByCustomer(user) {
|
async function lookupSubscriptionByCustomer(user) {
|
||||||
try {
|
try {
|
||||||
|
log('DEBUG: Starting subscription lookup', {
|
||||||
|
userId: user.id,
|
||||||
|
email: user.email,
|
||||||
|
hasDodoCustomerId: !!user.dodoCustomerId
|
||||||
|
});
|
||||||
|
|
||||||
const customerId = await ensureDodoCustomer(user);
|
const customerId = await ensureDodoCustomer(user);
|
||||||
|
|
||||||
|
log('DEBUG: After ensureDodoCustomer', {
|
||||||
|
userId: user.id,
|
||||||
|
customerId: customerId,
|
||||||
|
hasCustomerId: !!customerId
|
||||||
|
});
|
||||||
|
|
||||||
if (!customerId) return null;
|
if (!customerId) return null;
|
||||||
|
|
||||||
// Check multiple subscription statuses: active, pending, on_hold
|
// Check multiple subscription statuses: active, pending, on_hold
|
||||||
const statusesToCheck = ['active', 'pending', 'on_hold'];
|
const statusesToCheck = ['active', 'pending', 'on_hold'];
|
||||||
|
|
||||||
for (const status of statusesToCheck) {
|
for (const status of statusesToCheck) {
|
||||||
|
log('DEBUG: Querying subscriptions', {
|
||||||
|
userId: user.id,
|
||||||
|
customerId: customerId,
|
||||||
|
status: status
|
||||||
|
});
|
||||||
|
|
||||||
const subscriptions = await dodoRequest('/subscriptions', {
|
const subscriptions = await dodoRequest('/subscriptions', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
query: { customer_id: customerId, status: status }
|
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) {
|
if (subscriptions?.items && subscriptions.items.length > 0) {
|
||||||
const foundSub = subscriptions.items[0];
|
const foundSub = subscriptions.items[0];
|
||||||
log('Found missing subscription ID via Dodo lookup', {
|
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;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log('Failed to lookup subscription by customer', {
|
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 paid user is missing dodoSubscriptionId, try to look it up
|
||||||
if ((isPaidToFree || isPaidToPaid) && !user.dodoSubscriptionId && PAID_PLANS.has(user.plan)) {
|
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);
|
const foundSubscriptionId = await lookupSubscriptionByCustomer(user);
|
||||||
if (foundSubscriptionId) {
|
if (foundSubscriptionId) {
|
||||||
user.dodoSubscriptionId = foundSubscriptionId;
|
user.dodoSubscriptionId = foundSubscriptionId;
|
||||||
|
|||||||
Reference in New Issue
Block a user