Skip to content

The open alternative to Salesforce, designed for AI.

feat(usage-limit): consume quota on app charges (#26461)

Etienne608855c5a583
diff --git a/packages/twenty-docs/developers/extend/apps/logic/credits.mdx b/packages/twenty-docs/developers/extend/apps/logic/credits.mdx
index fdd24039bbf..11fe2bf958c 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/credits.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/credits.mdx
@@ -146,7 +146,7 @@ Notes:
- It answers whether the workspace may spend, not how much it has left. Your app does not see the balance or the plan.
- It fails open. If the check times out or the server is unreachable it reports credits available, so an infrastructure hiccup does not stop your app. Treat it as a hint worth honouring, not as an authorization decision.
-- It is a snapshot. Credits can run out between the check and the work.
+- It is a snapshot. Credits can run out between the check and the work — including because of your own charges, since every charge you make moves the same counter this reads.
- The gate is not applied uniformly. Not every outbound email path is credit-gated, so a `false` here tells you the workspace is out of credits rather than predicting exactly which calls will fail.
## Telling people what it costs
diff --git a/packages/twenty-server/src/engine/core-modules/billing/app-billing/app-billing.service.ts b/packages/twenty-server/src/engine/core-modules/billing/app-billing/app-billing.service.ts
index 056aa1dfe43..a81b2b78e35 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/app-billing/app-billing.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/app-billing/app-billing.service.ts
@@ -9,6 +9,7 @@ import { type Repository } from 'typeorm';
import { findActiveFlatApplicationById } from 'src/engine/core-modules/application/utils/find-active-flat-application-by-id.util';
import { type ChargeDto } from 'src/engine/core-modules/billing/app-billing/dtos/charge.dto';
+import { BillingUsageService } from 'src/engine/core-modules/billing/services/billing-usage.service';
import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';
@@ -47,6 +48,7 @@ export class AppBillingService {
constructor(
private readonly usageRecorderService: UsageRecorderService,
+ private readonly billingUsageService: BillingUsageService,
private readonly workspaceCacheService: WorkspaceCacheService,
@InjectRepository(UserWorkspaceEntity)
private readonly userWorkspaceRepository: Repository<UserWorkspaceEntity>,
@@ -76,6 +78,22 @@ export class AppBillingService {
`${charge.creditsUsedMicro} micro-credits (${charge.quantity} ${unit}, ${operationType})`,
);
+ const spenders = {
+ userWorkspaceId: attributedUserWorkspaceId,
+ applicationId,
+ };
+
+ await this.billingUsageService.consumeUsageQuota({
+ workspaceId,
+ resourceType: UsageResourceType.APP,
+ operationType,
+ spenders,
+ cost: {
+ creditsUsedMicro: charge.creditsUsedMicro,
+ quantity: charge.quantity,
+ },
+ });
+
await this.usageRecorderService.record(workspaceId, [
{
resourceType: UsageResourceType.APP,
@@ -85,7 +103,7 @@ export class AppBillingService {
unit,
resourceId: applicationId,
resourceContext: charge.operation ?? charge.resourceContext ?? null,
- spenders: { userWorkspaceId: attributedUserWorkspaceId, applicationId },
+ spenders,
},
]);
}