fix billingCustomer stripeId fetching (#12116)
### Context Several 'Customer not found' errors arrived in Sentry, all coming from webhook-entitlement.service, at subscription creation (coinciding with customer creation 99% of the time). Stripe sends many events to update/create customer, subscription, entitlement, ... All these events are handle in parallel but customer.created stripe event arrived first and few seconds after subscription.created and entitlements.active_entitlement_summary.updated Issue happens at entitlements.active_entitlement_summary.updated handling. It checks for customer existence through subscription. But subscription can be not created yet at this moment. ### Solution Check directly for customer existence in billingCustomer table. Not sure it will fix the error because of the parallel handling of Stripe event, but should still be better. ### Tested - Workspace creation - Subscription upgrade (check for entitlement update) closes https://github.com/twentyhq/twenty/issues/11960
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
/* @license Enterprise */
|
/* @license Enterprise */
|
||||||
|
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import Stripe from 'stripe';
|
import Stripe from 'stripe';
|
||||||
@ -10,15 +10,14 @@ import {
|
|||||||
BillingException,
|
BillingException,
|
||||||
BillingExceptionCode,
|
BillingExceptionCode,
|
||||||
} from 'src/engine/core-modules/billing/billing.exception';
|
} from 'src/engine/core-modules/billing/billing.exception';
|
||||||
|
import { BillingCustomer } from 'src/engine/core-modules/billing/entities/billing-customer.entity';
|
||||||
import { BillingEntitlement } from 'src/engine/core-modules/billing/entities/billing-entitlement.entity';
|
import { BillingEntitlement } from 'src/engine/core-modules/billing/entities/billing-entitlement.entity';
|
||||||
import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
|
||||||
import { transformStripeEntitlementUpdatedEventToDatabaseEntitlement } from 'src/engine/core-modules/billing/webhooks/utils/transform-stripe-entitlement-updated-event-to-database-entitlement.util';
|
import { transformStripeEntitlementUpdatedEventToDatabaseEntitlement } from 'src/engine/core-modules/billing/webhooks/utils/transform-stripe-entitlement-updated-event-to-database-entitlement.util';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BillingWebhookEntitlementService {
|
export class BillingWebhookEntitlementService {
|
||||||
protected readonly logger = new Logger(BillingWebhookEntitlementService.name);
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(BillingSubscription, 'core')
|
@InjectRepository(BillingCustomer, 'core')
|
||||||
private readonly billingSubscriptionRepository: Repository<BillingSubscription>,
|
private readonly billingCustomerRepository: Repository<BillingCustomer>,
|
||||||
@InjectRepository(BillingEntitlement, 'core')
|
@InjectRepository(BillingEntitlement, 'core')
|
||||||
private readonly billingEntitlementRepository: Repository<BillingEntitlement>,
|
private readonly billingEntitlementRepository: Repository<BillingEntitlement>,
|
||||||
) {}
|
) {}
|
||||||
@ -26,19 +25,18 @@ export class BillingWebhookEntitlementService {
|
|||||||
async processStripeEvent(
|
async processStripeEvent(
|
||||||
data: Stripe.EntitlementsActiveEntitlementSummaryUpdatedEvent.Data,
|
data: Stripe.EntitlementsActiveEntitlementSummaryUpdatedEvent.Data,
|
||||||
) {
|
) {
|
||||||
const billingSubscription =
|
const billingCustomer = await this.billingCustomerRepository.findOne({
|
||||||
await this.billingSubscriptionRepository.findOne({
|
where: { stripeCustomerId: data.object.customer },
|
||||||
where: { stripeCustomerId: data.object.customer },
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (!billingSubscription) {
|
if (!billingCustomer) {
|
||||||
throw new BillingException(
|
throw new BillingException(
|
||||||
'Billing customer not found',
|
'Billing customer not found',
|
||||||
BillingExceptionCode.BILLING_CUSTOMER_NOT_FOUND,
|
BillingExceptionCode.BILLING_CUSTOMER_NOT_FOUND,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const workspaceId = billingSubscription.workspaceId;
|
const workspaceId = billingCustomer.workspaceId;
|
||||||
|
|
||||||
await this.billingEntitlementRepository.upsert(
|
await this.billingEntitlementRepository.upsert(
|
||||||
transformStripeEntitlementUpdatedEventToDatabaseEntitlement(
|
transformStripeEntitlementUpdatedEventToDatabaseEntitlement(
|
||||||
|
|||||||
Reference in New Issue
Block a user