Skip to content

Commit d04fb5c

Browse files
committed
fix subscirption handling
1 parent 275ed4f commit d04fb5c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

repos/License.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ const deleteLicense = async (userId: string): Promise<boolean> => {
118118

119119

120120

121+
const getLicenseByStripePaymentId = async (stripePaymentId: string): Promise<License | null> => {
122+
const result = await db(tableName)
123+
.where({ stripe_payment_id: stripePaymentId })
124+
.first('*')
125+
126+
return result || null
127+
}
128+
121129
const updateLicenseByStripePaymentId = async (stripePaymentId: string, status: LicenseStatus): Promise<License | null> => {
122130
const [license] = await db(tableName)
123131
.where({ stripe_payment_id: stripePaymentId })
@@ -126,7 +134,7 @@ const updateLicenseByStripePaymentId = async (stripePaymentId: string, status: L
126134
updated_at: new Date()
127135
})
128136
.returning('*')
129-
137+
130138
return license || null
131139
}
132140

@@ -135,6 +143,7 @@ export const LicenseRepo = {
135143
getFreeTrialLicenseByUserId,
136144
getExistingSubscriptionLicenseByUserId,
137145
getLicenseByUserId,
146+
getLicenseByStripePaymentId,
138147
createLicense,
139148
updateLicense,
140149
deleteLicense,

services/WebhookService.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const handleSubscriptionUpdated = async (subscription: Stripe.Subscription): Pro
117117
const license = await LicenseRepo.getLicenseByStripePaymentId(subscription.id)
118118

119119
await notificationEngine.sendNotification({
120-
type: 'subscription_cancel_requested',
120+
type: 'subscription_cancelled',
121121
user: {
122122
id: license?.user_id || 'unknown',
123123
email: 'N/A' // Email not available in subscription object
@@ -128,8 +128,9 @@ const handleSubscriptionUpdated = async (subscription: Stripe.Subscription): Pro
128128
customer_id: subscription.customer,
129129
license_id: license?.id,
130130
cancel_at: subscription.cancel_at ? new Date(subscription.cancel_at * 1000) : null,
131-
current_period_end: new Date(subscription.current_period_end * 1000),
132-
status: subscription.status
131+
cancel_at_period_end: subscription.cancel_at_period_end,
132+
status: subscription.status,
133+
message: 'User requested cancellation - will expire at period end'
133134
}
134135
}, ['discord'])
135136

@@ -228,6 +229,7 @@ const handleInvoicePaymentFailed = async (invoice: Stripe.Invoice): Promise<void
228229

229230
export const WebhookService = {
230231
handleCheckoutSessionCompleted,
232+
handleSubscriptionUpdated,
231233
handleSubscriptionDeleted,
232234
handleInvoicePaymentFailed,
233235
}

0 commit comments

Comments
 (0)