From 8eba440e04f4dffacf34921f8bc93c191147deed Mon Sep 17 00:00:00 2001 From: Krish Parekh Date: Wed, 14 Aug 2024 23:52:57 +0530 Subject: [PATCH] chore: Webhook events handling (#27) --- .../services/api/clients.service.ts | 59 ++++++++++- .../services/stripe/stripe-webhook.service.ts | 97 ++++++------------- packages/connections/package.json | 2 +- 3 files changed, 90 insertions(+), 68 deletions(-) diff --git a/apps/event-system/services/api/clients.service.ts b/apps/event-system/services/api/clients.service.ts index 7bc13ef..e636905 100644 --- a/apps/event-system/services/api/clients.service.ts +++ b/apps/event-system/services/api/clients.service.ts @@ -164,19 +164,74 @@ module.exports = { }, }, - getByCustomerId: { + updateOnInvoicePaymentSuccess: { params: { customerId: 'string', + endDate: 'number', }, + async handler(ctx: any) { try { const client = await this.adapter.findOne({ 'billing.customerId': ctx.params.customerId, }); - return client; + const updateDoc = await this.adapter.updateById( + client._id, + { + $set: { + 'billing.subscription.valid': true, + 'billing.subscription.reason': null, + 'billing.subscription.endDate': ctx.params.endDate, + }, + }, + (doc: any) => { + return doc; + } + ); + + if (!updateDoc) { + return await ctx.call('error.404'); + } + + return updateDoc; } catch (error) { + return await ctx.call('error.404'); + } + }, + }, + + updateOnInvoicePaymentFailed: { + params: { + customerId: 'string', + }, + + async handler(ctx: any) { + try { + const client = await this.adapter.findOne({ + 'billing.customerId': ctx.params.customerId, + }); + + const updateDoc = await this.adapter.updateById( + client._id, + { + $set: { + 'billing.subscription.valid': false, + 'billing.subscription.reason': 'payment-failed', + }, + }, + (doc: any) => { + return doc; + } + ); + + if (!updateDoc) { return await ctx.call('error.404'); + } + + return updateDoc; + } catch (error) { + return await ctx.call('error.404'); } }, }, diff --git a/apps/event-system/services/stripe/stripe-webhook.service.ts b/apps/event-system/services/stripe/stripe-webhook.service.ts index 2c678ae..7d03bb3 100644 --- a/apps/event-system/services/stripe/stripe-webhook.service.ts +++ b/apps/event-system/services/stripe/stripe-webhook.service.ts @@ -124,81 +124,48 @@ export default { const invoicePaymentFailed = event.data.object; const failedInvoiceClient = await ctx.broker.call( - 'v1.clients.getByCustomerId', + 'v1.clients.updateOnInvoicePaymentFailed', { customerId: invoicePaymentFailed?.customer, } ); - if (failedInvoiceClient) { - const failedBilling = { - ...failedInvoiceClient?.billing, - subscription: { - ...failedInvoiceClient?.billing?.subscription, - valid: false, - reason: 'payment_failed', - }, - }; - - const updatedFailedClient = await ctx.broker.call( - 'v1.clients.updateBillingByCustomerId', - { - customerId: invoicePaymentFailed?.customer, - billing: failedBilling, - } - ); - - await ctx.broker.call('v1.tracking.public.track', { - path: 't', - data: { - event: 'Failed Invoice Payment', - properties: invoicePaymentFailed, - userId: updatedFailedClient?.author?._id, - }, - }); - } - + await ctx.broker.call('v1.tracking.public.track', { + path: 't', + data: { + event: 'Failed Invoice Payment', + properties: invoicePaymentFailed, + userId: failedInvoiceClient?.author?._id, + }, + }); break; case 'invoice.payment_succeeded': - setTimeout(async () => { - const invoicePaymentSucceeded = event.data.object; + + const invoicePaymentSucceeded = event.data.object; - const succeededInvoiceClient = await ctx.broker.call( - 'v1.clients.getByCustomerId', - { - customerId: invoicePaymentSucceeded?.customer, - } - ); - - if (succeededInvoiceClient) { - const succeededBilling = { - ...succeededInvoiceClient?.billing, - subscription: { - ...succeededInvoiceClient?.billing?.subscription, - valid: true, - reason: null, - }, - }; - - const updatedSucceededClient = await ctx.broker.call( - 'v1.clients.updateBillingByCustomerId', - { - customerId: invoicePaymentSucceeded?.customer, - billing: succeededBilling, - } - ); - - await ctx.broker.call('v1.tracking.public.track', { - path: 't', - data: { - event: 'Successful Invoice Payment', - properties: invoicePaymentSucceeded, - userId: updatedSucceededClient?.author?._id, - }, - }); + const subscription = await stripe.subscriptions.retrieve( + invoicePaymentSucceeded?.subscription as string + ); + + const succeededInvoiceClient = await ctx.broker.call( + 'v1.clients.updateOnInvoicePaymentSuccess', + { + customerId: invoicePaymentSucceeded?.customer, + endDate: subscription?.current_period_end, } - }, 5000); + ); + + await ctx.broker.call('v1.tracking.public.track', { + path: 't', + data: { + event: 'Successful Invoice Payment', + properties: invoicePaymentSucceeded, + userId: succeededInvoiceClient?.author?._id, + }, + }); + + break; } diff --git a/packages/connections/package.json b/packages/connections/package.json index c4137ef..a69cf31 100644 --- a/packages/connections/package.json +++ b/packages/connections/package.json @@ -1,6 +1,6 @@ { "name": "@integrationos/authkit-node", - "version": "1.0.7", + "version": "1.0.9", "description": "Secure token generation for IntegrationOS AuthKit", "main": "dist/index.js", "scripts": {