Skip to content

Commit 501ec10

Browse files
fix(billing): update JSDoc, wrap emit in try-catch, use null for unknown plan ranks
1 parent 9b6e060 commit 501ec10

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

modules/billing/services/billing.webhook.service.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const handleCheckoutCompleted = async (session) => {
8181
/**
8282
* @desc Handle customer.subscription.updated event — sync subscription state
8383
* @param {Object} subscription - Stripe subscription object
84+
* @param {Object} event - Full Stripe event (with data.previous_attributes for plan change detection)
8485
* @returns {Promise<void>}
8586
*/
8687
const handleSubscriptionUpdated = async (subscription, event) => {
@@ -106,14 +107,18 @@ const handleSubscriptionUpdated = async (subscription, event) => {
106107
|| previousItems[0]?.plan?.metadata?.planId
107108
|| null;
108109
if (previousPlan && previousPlan !== newPlan) {
109-
const isDowngrade = (planRanks[previousPlan] ?? -1) > (planRanks[newPlan] ?? -1);
110-
billingEvents.emit('plan.changed', {
111-
organizationId,
112-
previousPlan,
113-
newPlan,
114-
subscription,
115-
isDowngrade,
116-
});
110+
const prevRank = planRanks[previousPlan];
111+
const newRank = planRanks[newPlan];
112+
const isDowngrade = prevRank != null && newRank != null ? prevRank > newRank : null;
113+
try {
114+
billingEvents.emit('plan.changed', {
115+
organizationId,
116+
previousPlan,
117+
newPlan,
118+
subscription,
119+
isDowngrade,
120+
});
121+
} catch { /* listener errors must not disrupt webhook processing */ }
117122
}
118123
}
119124
};

0 commit comments

Comments
 (0)