From 3539f5cb7ac609e44c8e7df7e9df010b54e37866 Mon Sep 17 00:00:00 2001 From: Etienne <45695613+etiennejouan@users.noreply.github.com> Date: Tue, 6 Jan 2026 13:25:28 +0100 Subject: [PATCH] Billing - Update alert when meter price is updated (#16952) --- .../billing-subscription-update.service.ts | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/billing/services/billing-subscription-update.service.ts b/packages/twenty-server/src/engine/core-modules/billing/services/billing-subscription-update.service.ts index 50ec6f10cc..76b826bca2 100644 --- a/packages/twenty-server/src/engine/core-modules/billing/services/billing-subscription-update.service.ts +++ b/packages/twenty-server/src/engine/core-modules/billing/services/billing-subscription-update.service.ts @@ -79,37 +79,7 @@ export class BillingSubscriptionUpdateService { newMeteredPriceId: meteredPriceId, } as const; - const isScheduledForPeriodEnd = - await this.shouldUpdateAtSubscriptionPeriodEnd( - billingSubscription, - subscriptionUpdate, - ); - await this.updateSubscription(billingSubscription.id, subscriptionUpdate); - - if (!isScheduledForPeriodEnd) { - await this.billingSubscriptionItemRepository.update( - { stripeSubscriptionId: billingSubscription.stripeSubscriptionId }, - { hasReachedCurrentPeriodCap: false }, - ); - - const meteredPricingInfo = - await this.meteredCreditService.getMeteredPricingInfoFromPriceId( - meteredPriceId, - ); - - const creditBalance = await this.meteredCreditService.getCreditBalance( - billingSubscription.stripeCustomerId, - meteredPricingInfo.unitPriceCents, - ); - - await this.stripeBillingAlertService.createUsageThresholdAlertForCustomerMeter( - billingSubscription.stripeCustomerId, - meteredPricingInfo.tierCap, - creditBalance, - billingSubscription.currentPeriodStart, - ); - } } async cancelSwitchMeteredPrice(workspace: WorkspaceEntity): Promise { @@ -299,6 +269,30 @@ export class BillingSubscriptionUpdateService { ...subscriptionOptions, }); + if (subscriptionUpdate.type !== SubscriptionUpdateType.SEATS) { + await this.billingSubscriptionItemRepository.update( + { stripeSubscriptionId: subscription.stripeSubscriptionId }, + { hasReachedCurrentPeriodCap: false }, + ); + + const meteredPricingInfo = + await this.meteredCreditService.getMeteredPricingInfoFromPriceId( + toUpdateCurrentPrices.meteredPriceId, + ); + + const creditBalance = await this.meteredCreditService.getCreditBalance( + subscription.stripeCustomerId, + meteredPricingInfo.unitPriceCents, + ); + + await this.stripeBillingAlertService.createUsageThresholdAlertForCustomerMeter( + subscription.stripeCustomerId, + meteredPricingInfo.tierCap, + creditBalance, + subscription.currentPeriodStart, + ); + } + if (isDefined(nextPhase)) { assertIsDefinedOrThrow(schedule); const { currentPhase: refreshedCurrentPhase } =