-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add product table data in real time #9055
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR adds real-time synchronization of Stripe product data with the billing product table through webhook events, ensuring consistent product information across the platform.
- Added new
BillingWebhookProductService
in/billing/services/billing-webhook-product.service.ts
to handle Stripe product webhooks with metadata validation - Modified
/billing/billing.controller.ts
to processPRODUCT_CREATED
andPRODUCT_UPDATED
webhook events - Added utility function
transformStripeProductEventToProductRepositoryData
in/billing/utils/
for standardized product data transformation - Defined strict
BillingProductMetadata
type in/billing/types/billing-product-metadata.type.ts
to ensure valid product metadata - Missing error handling for product webhook processing in
billing.controller.ts
compared to other webhook handlers
💡 (4/5) You can add custom instructions or style guidelines for the bot here!
7 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings | Greptile
PRODUCT_CREATED = 'product.created', | ||
PRODUCT_UPDATED = 'product.updated', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing PRODUCT_DELETED event which could lead to stale data if products are deleted in Stripe
event.type === WebhookEvent.PRODUCT_CREATED || | ||
event.type === WebhookEvent.PRODUCT_UPDATED | ||
) { | ||
await this.billingWebhookProductService.processStripeEvent(event.data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing error handling for product webhook processing. Other webhook handlers have error handling but this one doesn't.
import { transformStripeProductEventToProductRepositoryData } from 'src/engine/core-modules/billing/utils/transform-stripe-product-event-to-product-repository-data.util'; | ||
@Injectable() | ||
export class BillingWebhookProductService { | ||
protected readonly logger = new Logger(BillingWebhookProductService.name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Logger is initialized but never used. Consider adding error logging in processStripeEvent
if (Object.keys(metadata).length === 0) { | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Empty metadata object is considered valid, but this might allow invalid products into the system. Consider requiring mandatory fields
planKey: BillingPlanKey; | ||
priceUsageBased: BillingUsageType; | ||
[key: string]: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Type mismatch between enum types and string index signature. planKey and priceUsageBased will be enums but [key: string]: string forces all values to be strings.
Thanks @anamarn for your contribution! |
Solves (https://github.com/twentyhq/private-issues/issues/198)
TLDR
Updates the billingProduct table data using stripe webhooks event. It saves all the updates/creates of the products, but ensuring that it has the lastest version of the correct metadata attributes (typeof BillingProductMetadata)
In order to test
Billing:
Set IS_BILLING_ENABLED to true
Add your BILLING_STRIPE_SECRET and BILLING_STRIPE_API_KEY
Add your BILLING_STRIPE_BASE_PLAN_PRODUCT_ID (use the one in testMode > Base Plan)
Authenticate with your account in the stripe CLI
Run the command: stripe listen --forward-to http://localhost:3000/billing/webhooks
Go to Stripe In test mode and update or create a product using a metadata of type of BillingProductMetadata, you can also update it using a different values for metadata.