The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(billing): serialize stripe subscription webhook per customer (#26431)
Etienne00747d59ecca
diff --git a/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts b/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.tsindex 31c06b7c85b..5a28e518664 100644--- a/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts+++ b/packages/twenty-server/src/engine/core-modules/billing-webhook/services/billing-webhook-subscription.service.ts@@ -18,6 +18,7 @@ import {BillingException,BillingExceptionCode,} from 'src/engine/core-modules/billing/billing.exception';+import { BILLING_SUBSCRIPTION_STATE_LOCK_OPTIONS } from 'src/engine/core-modules/billing/constants/billing-subscription-state-lock-options.constant';import { WORKSPACE_ACTIVATING_SUBSCRIPTION_STATUSES } from 'src/engine/core-modules/billing/constants/workspace-activating-subscription-statuses.constant';import { BillingCustomerEntity } from 'src/engine/core-modules/billing/entities/billing-customer.entity';import { BillingSubscriptionItemEntity } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';@@ -28,7 +29,9 @@ import { UsageLimitQuotaService } from 'src/engine/core-modules/usage-limit/servimport { StripeCustomerService } from 'src/engine/core-modules/billing/stripe/services/stripe-customer.service';import { StripeSubscriptionScheduleService } from 'src/engine/core-modules/billing/stripe/services/stripe-subscription-schedule.service';import { type SubscriptionWithSchedule } from 'src/engine/core-modules/billing/types/billing-subscription-with-schedule.type';+import { buildBillingSubscriptionStateLockKey } from 'src/engine/core-modules/billing/utils/build-billing-subscription-state-lock-key.util';import { resolveBillingPeriodBoundaryUpdate } from 'src/engine/core-modules/billing/utils/resolve-billing-period-boundary-update.util';+import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';@@ -66,6 +69,7 @@ export class BillingWebhookSubscriptionService {private readonly stripeSubscriptionScheduleService: StripeSubscriptionScheduleService,private readonly usageLimitQuotaService: UsageLimitQuotaService,private readonly workspaceCacheService: WorkspaceCacheService,+ private readonly cacheLockService: CacheLockService,) {}async processStripeEvent(@@ -74,6 +78,20 @@ export class BillingWebhookSubscriptionService {| Stripe.CustomerSubscriptionUpdatedEvent| Stripe.CustomerSubscriptionCreatedEvent| Stripe.CustomerSubscriptionDeletedEvent,+ ) {+ return await this.cacheLockService.withLock(+ () => this.applySubscriptionEvent(workspaceId, event),+ buildBillingSubscriptionStateLockKey(String(event.data.object.customer)),+ BILLING_SUBSCRIPTION_STATE_LOCK_OPTIONS,+ );+ }++ private async applySubscriptionEvent(+ workspaceId: string,+ event:+ | Stripe.CustomerSubscriptionUpdatedEvent+ | Stripe.CustomerSubscriptionCreatedEvent+ | Stripe.CustomerSubscriptionDeletedEvent,) {const { data, type } = event;diff --git a/packages/twenty-server/src/engine/core-modules/billing/constants/billing-subscription-state-lock-options.constant.ts b/packages/twenty-server/src/engine/core-modules/billing/constants/billing-subscription-state-lock-options.constant.tsnew file mode 100644index 00000000000..9473cdd57ee--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/billing/constants/billing-subscription-state-lock-options.constant.ts@@ -0,0 +1,9 @@+/* @license Enterprise */++import { type CacheLockOptions } from 'src/engine/core-modules/cache-lock/cache-lock.service';++export const BILLING_SUBSCRIPTION_STATE_LOCK_OPTIONS = {+ ttl: 30_000,+ ms: 200,+ maxRetries: 50,+} satisfies CacheLockOptions;diff --git a/packages/twenty-server/src/engine/core-modules/billing/utils/build-billing-subscription-state-lock-key.util.ts b/packages/twenty-server/src/engine/core-modules/billing/utils/build-billing-subscription-state-lock-key.util.tsnew file mode 100644index 00000000000..03313773c8e--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/billing/utils/build-billing-subscription-state-lock-key.util.ts@@ -0,0 +1,5 @@+/* @license Enterprise */++export const buildBillingSubscriptionStateLockKey = (+ stripeCustomerId: string,+): string => `billing-subscription-state:${stripeCustomerId}`;