Skip to content
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

Merged
merged 2 commits into from
Dec 13, 2024

Conversation

anamarn
Copy link
Contributor

@anamarn anamarn commented Dec 13, 2024

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.

Copy link
Contributor

@greptile-apps greptile-apps bot left a 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 process PRODUCT_CREATED and PRODUCT_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

Comment on lines +7 to +8
PRODUCT_CREATED = 'product.created',
PRODUCT_UPDATED = 'product.updated',
Copy link
Contributor

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);
Copy link
Contributor

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);
Copy link
Contributor

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

Comment on lines +42 to +44
if (Object.keys(metadata).length === 0) {
return true;
}
Copy link
Contributor

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

Comment on lines +6 to +8
planKey: BillingPlanKey;
priceUsageBased: BillingUsageType;
[key: string]: string;
Copy link
Contributor

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.

@charlesBochet charlesBochet merged commit abaf265 into main Dec 13, 2024
19 checks passed
@charlesBochet charlesBochet deleted the feat/add-real-time-billing-product branch December 13, 2024 17:15
Copy link

Thanks @anamarn for your contribution!
This marks your 14th PR on the repo. You're top 3% of all our contributors 🎉
See contributor page - Share on LinkedIn - Share on Twitter

Contributions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants