fix: query all subscriptions without status filter for customers with multiple subs
- Query ALL subscriptions for customer without status filter first - This handles customers with multiple subscriptions across different statuses - Returns first active subscription found, or most recent if none active - Added detailed logging to show all subscription IDs and statuses found - Fallback to check individual statuses including cancelled/expired
This commit is contained in:
108
chat/server.js
108
chat/server.js
@@ -12199,11 +12199,51 @@ async function handleAccountSettingsUpdate(req, res) {
|
|||||||
|
|
||||||
if (!customerId) return null;
|
if (!customerId) return null;
|
||||||
|
|
||||||
// Check multiple subscription statuses: active, pending, on_hold
|
// First try: Query ALL subscriptions for customer without status filter
|
||||||
const statusesToCheck = ['active', 'pending', 'on_hold'];
|
log('DEBUG: Querying ALL subscriptions for customer (no status filter)', {
|
||||||
|
userId: user.id,
|
||||||
|
customerId: customerId
|
||||||
|
});
|
||||||
|
|
||||||
|
const allSubscriptions = await dodoRequest('/subscriptions', {
|
||||||
|
method: 'GET',
|
||||||
|
query: { customer_id: customerId, page_size: 100 }
|
||||||
|
});
|
||||||
|
|
||||||
|
log('DEBUG: All subscriptions query response', {
|
||||||
|
userId: user.id,
|
||||||
|
customerId: customerId,
|
||||||
|
responseKeys: Object.keys(allSubscriptions || {}),
|
||||||
|
hasItems: !!allSubscriptions?.items,
|
||||||
|
itemsCount: allSubscriptions?.items?.length || 0,
|
||||||
|
itemsDetails: allSubscriptions?.items?.map(s => ({
|
||||||
|
id: s.subscription_id || s.id,
|
||||||
|
status: s.status,
|
||||||
|
productId: s.product_id
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
|
||||||
|
if (allSubscriptions?.items && allSubscriptions.items.length > 0) {
|
||||||
|
// Find the most recent active subscription, or any subscription if no active one
|
||||||
|
const activeSub = allSubscriptions.items.find(s => s.status === 'active');
|
||||||
|
const foundSub = activeSub || allSubscriptions.items[0];
|
||||||
|
|
||||||
|
log('Found subscription via customer lookup', {
|
||||||
|
userId: user.id,
|
||||||
|
email: user.email,
|
||||||
|
subscriptionId: foundSub.subscription_id || foundSub.id,
|
||||||
|
status: foundSub.status,
|
||||||
|
totalSubs: allSubscriptions.items.length,
|
||||||
|
customerId
|
||||||
|
});
|
||||||
|
return foundSub.subscription_id || foundSub.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: Check specific statuses if no results without filter
|
||||||
|
const statusesToCheck = ['active', 'pending', 'on_hold', 'cancelled', 'expired'];
|
||||||
|
|
||||||
for (const status of statusesToCheck) {
|
for (const status of statusesToCheck) {
|
||||||
log('DEBUG: Querying subscriptions', {
|
log('DEBUG: Querying subscriptions with status filter', {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
customerId: customerId,
|
customerId: customerId,
|
||||||
status: status
|
status: status
|
||||||
@@ -12217,8 +12257,6 @@ async function handleAccountSettingsUpdate(req, res) {
|
|||||||
log('DEBUG: Subscription query response', {
|
log('DEBUG: Subscription query response', {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
status: status,
|
status: status,
|
||||||
responseKeys: Object.keys(subscriptions || {}),
|
|
||||||
hasItems: !!subscriptions?.items,
|
|
||||||
itemsCount: subscriptions?.items?.length || 0
|
itemsCount: subscriptions?.items?.length || 0
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -12241,6 +12279,66 @@ async function handleAccountSettingsUpdate(req, res) {
|
|||||||
checkedStatuses: statusesToCheck
|
checkedStatuses: statusesToCheck
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Fallback: Search ALL subscriptions without customer filter to find any with matching metadata
|
||||||
|
try {
|
||||||
|
log('DEBUG: Trying fallback - searching all subscriptions', {
|
||||||
|
userId: user.id,
|
||||||
|
email: user.email
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const status of statusesToCheck) {
|
||||||
|
const allSubscriptions = await dodoRequest('/subscriptions', {
|
||||||
|
method: 'GET',
|
||||||
|
query: { status: status, page_size: 100 }
|
||||||
|
});
|
||||||
|
|
||||||
|
log('DEBUG: All subscriptions query', {
|
||||||
|
userId: user.id,
|
||||||
|
status: status,
|
||||||
|
totalItems: allSubscriptions?.items?.length || 0
|
||||||
|
});
|
||||||
|
|
||||||
|
if (allSubscriptions?.items && allSubscriptions.items.length > 0) {
|
||||||
|
// Look for subscription with matching email in customer data
|
||||||
|
const matchingSub = allSubscriptions.items.find(sub => {
|
||||||
|
const subEmail = sub.customer?.email || '';
|
||||||
|
const subMetadata = sub.metadata || {};
|
||||||
|
return subEmail.toLowerCase() === user.email.toLowerCase() ||
|
||||||
|
subMetadata.userId === String(user.id) ||
|
||||||
|
subMetadata.email === user.email;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (matchingSub) {
|
||||||
|
log('Found subscription via fallback search', {
|
||||||
|
userId: user.id,
|
||||||
|
email: user.email,
|
||||||
|
subscriptionId: matchingSub.subscription_id || matchingSub.id,
|
||||||
|
foundCustomerId: matchingSub.customer?.customer_id,
|
||||||
|
status: status
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update the customer ID if different
|
||||||
|
if (matchingSub.customer?.customer_id && matchingSub.customer.customer_id !== customerId) {
|
||||||
|
user.dodoCustomerId = matchingSub.customer.customer_id;
|
||||||
|
await persistUsersDb();
|
||||||
|
log('Updated dodoCustomerId from fallback search', {
|
||||||
|
userId: user.id,
|
||||||
|
oldCustomerId: customerId,
|
||||||
|
newCustomerId: matchingSub.customer.customer_id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return matchingSub.subscription_id || matchingSub.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (fallbackError) {
|
||||||
|
log('DEBUG: Fallback search failed', {
|
||||||
|
userId: user.id,
|
||||||
|
error: String(fallbackError)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
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