Skip to content

The open alternative to Salesforce, designed for AI.

clean code and normalize billing eventNames (#9928)

Ana Sofia Marin Alexandred777f6265121
diff --git a/packages/twenty-server/src/engine/core-modules/billing/billing.module.ts b/packages/twenty-server/src/engine/core-modules/billing/billing.module.ts
index bce60ac24fa..66daabc2a2d 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/billing.module.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/billing.module.ts
@@ -13,7 +13,7 @@ import { BillingProduct } from 'src/engine/core-modules/billing/entities/billing
import { BillingSubscriptionItem } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';
import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { BillingRestApiExceptionFilter } from 'src/engine/core-modules/billing/filters/billing-api-exception.filter';
-import { BillingExecuteBilledFunctionListener } from 'src/engine/core-modules/billing/listeners/billing-execute-billed-function.listener';
+import { BillingFeatureUsedListener } from 'src/engine/core-modules/billing/listeners/billing-feature-used.listener';
import { BillingWorkspaceMemberListener } from 'src/engine/core-modules/billing/listeners/billing-workspace-member.listener';
import { BillingPlanService } from 'src/engine/core-modules/billing/services/billing-plan.service';
import { BillingPortalWorkspaceService } from 'src/engine/core-modules/billing/services/billing-portal.workspace-service';
@@ -65,7 +65,7 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
BillingResolver,
BillingPlanService,
BillingWorkspaceMemberListener,
- BillingExecuteBilledFunctionListener,
+ BillingFeatureUsedListener,
BillingService,
BillingWebhookProductService,
BillingWebhookPriceService,
diff --git a/packages/twenty-server/src/engine/core-modules/billing/constants/billing-execute-billed-function.constant.ts b/packages/twenty-server/src/engine/core-modules/billing/constants/billing-execute-billed-function.constant.ts
deleted file mode 100644
index cf6a04d2a2b..00000000000
--- a/packages/twenty-server/src/engine/core-modules/billing/constants/billing-execute-billed-function.constant.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export const BILLING_EXECUTE_BILLED_FUNCTION =
- 'billing_execute_billed_function';
diff --git a/packages/twenty-server/src/engine/core-modules/billing/constants/billing-feature-used.constant.ts b/packages/twenty-server/src/engine/core-modules/billing/constants/billing-feature-used.constant.ts
new file mode 100644
index 00000000000..73fc368f739
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/billing/constants/billing-feature-used.constant.ts
@@ -0,0 +1 @@
+export const BILLING_FEATURE_USED = 'BILLING_FEATURE_USED';
diff --git a/packages/twenty-server/src/engine/core-modules/billing/enums/billing-meter-event-names.ts b/packages/twenty-server/src/engine/core-modules/billing/enums/billing-meter-event-names.ts
index 1bd781ab396..16edfb8121c 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/enums/billing-meter-event-names.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/enums/billing-meter-event-names.ts
@@ -1,5 +1,3 @@
export enum BillingMeterEventName {
- WORKFLOW_NODE_RUN = 'creditexecutiontest1',
+ WORKFLOW_NODE_RUN = 'WORKFLOW_NODE_RUN',
}
-//this is a test event name (no conventions) would you want camel case?, snake case, or all caps?
-//Something like workflowNodeRunBillingMeterEvent ?
diff --git a/packages/twenty-server/src/engine/core-modules/billing/listeners/billing-execute-billed-function.listener.ts b/packages/twenty-server/src/engine/core-modules/billing/listeners/billing-feature-used.listener.ts
similarity index 69%
rename from packages/twenty-server/src/engine/core-modules/billing/listeners/billing-execute-billed-function.listener.ts
rename to packages/twenty-server/src/engine/core-modules/billing/listeners/billing-feature-used.listener.ts
index 522692f9dc2..99567d6c1d9 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/listeners/billing-execute-billed-function.listener.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/listeners/billing-feature-used.listener.ts
@@ -1,33 +1,32 @@
import { Injectable } from '@nestjs/common';
import { OnCustomBatchEvent } from 'src/engine/api/graphql/graphql-query-runner/decorators/on-custom-batch-event.decorator';
-import { BILLING_EXECUTE_BILLED_FUNCTION } from 'src/engine/core-modules/billing/constants/billing-execute-billed-function.constant';
+import { BILLING_FEATURE_USED } from 'src/engine/core-modules/billing/constants/billing-feature-used.constant';
import { BillingUsageService } from 'src/engine/core-modules/billing/services/billing-usage.service';
import { BillingUsageEvent } from 'src/engine/core-modules/billing/types/billing-usage-event.type';
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event.type';
@Injectable()
-export class BillingExecuteBilledFunctionListener {
+export class BillingFeatureUsedListener {
constructor(
private readonly billingUsageService: BillingUsageService,
private readonly environmentService: EnvironmentService,
) {}
- @OnCustomBatchEvent(BILLING_EXECUTE_BILLED_FUNCTION)
- async handleExecuteBilledFunctionEvent(
+ @OnCustomBatchEvent(BILLING_FEATURE_USED)
+ async handleBillingFeatureUsedEvent(
payload: WorkspaceEventBatch<BillingUsageEvent>,
) {
if (!this.environmentService.get('IS_BILLING_ENABLED')) {
return;
}
- const canExecuteBilledFunction =
- await this.billingUsageService.canExecuteBilledFunction(
- payload.workspaceId,
- );
+ const canFeatureBeUsed = await this.billingUsageService.canFeatureBeUsed(
+ payload.workspaceId,
+ );
- if (!canExecuteBilledFunction) {
+ if (!canFeatureBeUsed) {
return;
}
diff --git a/packages/twenty-server/src/engine/core-modules/billing/services/billing-usage.service.ts b/packages/twenty-server/src/engine/core-modules/billing/services/billing-usage.service.ts
index 429191f3919..33ce6385ebf 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/services/billing-usage.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/services/billing-usage.service.ts
@@ -27,7 +27,7 @@ export class BillingUsageService {
private readonly stripeBillingMeterEventService: StripeBillingMeterEventService,
) {}
- async canExecuteBilledFunction(workspaceId: string): Promise<boolean> {
+ async canFeatureBeUsed(workspaceId: string): Promise<boolean> {
const isBillingEnabled = this.environmentService.get('IS_BILLING_ENABLED');
const isBillingPlansEnabled =
await this.featureFlagService.isFeatureEnabled(
@@ -82,7 +82,7 @@ export class BillingUsageService {
});
} catch (error) {
throw new BillingException(
- 'Failed to send billing meter events to Cache Service',
+ `Failed to send billing meter events to Stripe: ${error}`,
BillingExceptionCode.BILLING_METER_EVENT_FAILED,
);
}
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
index aaaf231ced1..7481b5abdc4 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
-import { BILLING_EXECUTE_BILLED_FUNCTION } from 'src/engine/core-modules/billing/constants/billing-execute-billed-function.constant';
+import { BILLING_FEATURE_USED } from 'src/engine/core-modules/billing/constants/billing-feature-used.constant';
import { BillingMeterEventName } from 'src/engine/core-modules/billing/enums/billing-meter-event-names';
import { BillingUsageEvent } from 'src/engine/core-modules/billing/types/billing-usage-event.type';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
@@ -74,7 +74,7 @@ export class WorkflowExecutorWorkspaceService {
(result.result ? undefined : 'Execution result error, no data or error');
if (!error) {
- this.sendUsageEvent();
+ this.sendWorkflowNodeRunEvent();
}
const updatedStepOutput = {
@@ -136,12 +136,12 @@ export class WorkflowExecutorWorkspaceService {
return { ...updatedOutput, status: WorkflowRunStatus.FAILED };
}
- async sendUsageEvent() {
+ private sendWorkflowNodeRunEvent() {
const workspaceId =
this.scopedWorkspaceContextFactory.create().workspaceId ?? '';
this.workspaceEventEmitter.emitCustomBatchEvent<BillingUsageEvent>(
- BILLING_EXECUTE_BILLED_FUNCTION,
+ BILLING_FEATURE_USED,
[
{
eventName: BillingMeterEventName.WORKFLOW_NODE_RUN,
diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts
index 8db64b38cc2..3a4af236eb9 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts
@@ -27,10 +27,10 @@ export class WorkflowRunnerWorkspaceService {
payload: object,
source: ActorMetadata,
) {
- const canExecuteBilledFunction =
- await this.billingUsageService.canExecuteBilledFunction(workspaceId);
+ const canFeatureBeUsed =
+ await this.billingUsageService.canFeatureBeUsed(workspaceId);
- if (!canExecuteBilledFunction) {
+ if (!canFeatureBeUsed) {
this.logger.log(
'Cannot execute billed function, there is no subscription for this workspace',
);