The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Give message campaigns their own send rate limit counters (#26331)
neo7739be8034c1269
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql b/packages/twenty-client-sdk/src/metadata/generated/schema.graphqlindex dbed6541ebf..b19738c9094 100644--- a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql@@ -1608,6 +1608,7 @@ enum UsageOperationType {WEB_SEARCHCALL_RECORDINGEMAIL_SEND+ MESSAGE_CAMPAIGN_SENDAPI_REQUESTWEBHOOK_CALLSTORAGE_FILEdiff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.ts b/packages/twenty-client-sdk/src/metadata/generated/schema.tsindex 596f37c63a2..9c044f48a6a 100644--- a/packages/twenty-client-sdk/src/metadata/generated/schema.ts+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.ts@@ -1238,7 +1238,7 @@ export interface UsageQuotaDefinition {export type UsageResourceType = 'AI' | 'WORKFLOW' | 'APP' | 'STORAGE' | 'API' | 'LOGIC_FUNCTION' | 'EMAIL' | 'WEBHOOK' | 'RECORD'-export type UsageOperationType = 'ALL' | 'AI_CHAT_TOKEN' | 'AI_WORKFLOW_TOKEN' | 'WORKFLOW_EXECUTION' | 'CODE_EXECUTION' | 'WEB_SEARCH' | 'CALL_RECORDING' | 'EMAIL_SEND' | 'API_REQUEST' | 'WEBHOOK_CALL' | 'STORAGE_FILE' | 'RECORD_WRITE' | 'SUBSCRIPTION'+export type UsageOperationType = 'ALL' | 'AI_CHAT_TOKEN' | 'AI_WORKFLOW_TOKEN' | 'WORKFLOW_EXECUTION' | 'CODE_EXECUTION' | 'WEB_SEARCH' | 'CALL_RECORDING' | 'EMAIL_SEND' | 'MESSAGE_CAMPAIGN_SEND' | 'API_REQUEST' | 'WEBHOOK_CALL' | 'STORAGE_FILE' | 'RECORD_WRITE' | 'SUBSCRIPTION'export interface UsageQuotaDefinitions {definitions: UsageQuotaDefinition[]@@ -10787,6 +10787,7 @@ export const enumUsageOperationType = {WEB_SEARCH: 'WEB_SEARCH' as const,CALL_RECORDING: 'CALL_RECORDING' as const,EMAIL_SEND: 'EMAIL_SEND' as const,+ MESSAGE_CAMPAIGN_SEND: 'MESSAGE_CAMPAIGN_SEND' as const,API_REQUEST: 'API_REQUEST' as const,WEBHOOK_CALL: 'WEBHOOK_CALL' as const,STORAGE_FILE: 'STORAGE_FILE' as const,diff --git a/packages/twenty-front/src/generated-metadata/graphql.ts b/packages/twenty-front/src/generated-metadata/graphql.tsindex 35a49b6f622..91814defbfd 100644--- a/packages/twenty-front/src/generated-metadata/graphql.ts+++ b/packages/twenty-front/src/generated-metadata/graphql.ts@@ -6885,6 +6885,7 @@ export enum UsageOperationType {CALL_RECORDING = 'CALL_RECORDING',CODE_EXECUTION = 'CODE_EXECUTION',EMAIL_SEND = 'EMAIL_SEND',+ MESSAGE_CAMPAIGN_SEND = 'MESSAGE_CAMPAIGN_SEND',RECORD_WRITE = 'RECORD_WRITE',STORAGE_FILE = 'STORAGE_FILE',SUBSCRIPTION = 'SUBSCRIPTION',diff --git a/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitOperationTypeIcons.ts b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitOperationTypeIcons.tsindex a1ea199d371..6176c874290 100644--- a/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitOperationTypeIcons.ts+++ b/packages/twenty-front/src/modules/settings/billing/constants/UsageLimitOperationTypeIcons.ts@@ -11,6 +11,7 @@ import {IconRepeat,IconRobot,IconSearch,+ IconSend,IconSettingsAutomation,IconWebhook,} from 'twenty-ui/icon';@@ -29,6 +30,7 @@ export const USAGE_LIMIT_OPERATION_TYPE_ICONS: Record<[UsageOperationType.WEB_SEARCH]: IconSearch,[UsageOperationType.CALL_RECORDING]: IconPhone,[UsageOperationType.EMAIL_SEND]: IconMail,+ [UsageOperationType.MESSAGE_CAMPAIGN_SEND]: IconSend,[UsageOperationType.API_REQUEST]: IconApi,[UsageOperationType.WEBHOOK_CALL]: IconWebhook,[UsageOperationType.STORAGE_FILE]: IconFiles,diff --git a/packages/twenty-front/src/modules/settings/usage/constants/UsageOperationTypeLabels.ts b/packages/twenty-front/src/modules/settings/usage/constants/UsageOperationTypeLabels.tsindex 9a1fa0f97e0..45eb5b7d7a6 100644--- a/packages/twenty-front/src/modules/settings/usage/constants/UsageOperationTypeLabels.ts+++ b/packages/twenty-front/src/modules/settings/usage/constants/UsageOperationTypeLabels.ts@@ -17,6 +17,7 @@ export const USAGE_OPERATION_TYPE_LABELS: Record<[UsageOperationType.WEB_SEARCH]: msg`Web Search`,[UsageOperationType.CALL_RECORDING]: msg`Call Recording`,[UsageOperationType.EMAIL_SEND]: msg`Email Send`,+ [UsageOperationType.MESSAGE_CAMPAIGN_SEND]: msg`Campaign Email Send`,[UsageOperationType.API_REQUEST]: msg`API Request`,[UsageOperationType.WEBHOOK_CALL]: msg`Webhook Call`,[UsageOperationType.STORAGE_FILE]: msg`File Storage`,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.tsindex 33f721eb37c..056aa1dfe43 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@@ -29,6 +29,7 @@ const USAGE_UNIT_BY_OPERATION_TYPE: Record<[UsageOperationType.WEB_SEARCH]: UsageUnit.INVOCATION,[UsageOperationType.CALL_RECORDING]: UsageUnit.MINUTE,[UsageOperationType.EMAIL_SEND]: UsageUnit.INVOCATION,+ [UsageOperationType.MESSAGE_CAMPAIGN_SEND]: UsageUnit.INVOCATION,[UsageOperationType.API_REQUEST]: UsageUnit.REQUEST,[UsageOperationType.WEBHOOK_CALL]: UsageUnit.REQUEST,[UsageOperationType.STORAGE_FILE]: UsageUnit.BYTE,diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.tsindex 3260b1f60cf..b20f0d79bb3 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/usage-limit-definitions.constant.ts@@ -68,7 +68,10 @@ export const USAGE_LIMIT_DEFINITIONS = {[UsageResourceType.LOGIC_FUNCTION]: {},[UsageResourceType.EMAIL]: {speed: {- allowedOperationTypes: [UsageOperationType.EMAIL_SEND],+ allowedOperationTypes: [+ UsageOperationType.EMAIL_SEND,+ UsageOperationType.MESSAGE_CAMPAIGN_SEND,+ ],allowedSpenderTypes: ['workspace'],// Two buckets, and a send has to fit both. The workspace one keeps a// single tenant's campaign from spending the whole instance budget; thediff --git a/packages/twenty-server/src/engine/core-modules/usage/enums/usage-operation-type.enum.ts b/packages/twenty-server/src/engine/core-modules/usage/enums/usage-operation-type.enum.tsindex e590949bf55..4e13286e38b 100644--- a/packages/twenty-server/src/engine/core-modules/usage/enums/usage-operation-type.enum.ts+++ b/packages/twenty-server/src/engine/core-modules/usage/enums/usage-operation-type.enum.ts@@ -11,6 +11,7 @@ export enum UsageOperationType {WEB_SEARCH = 'WEB_SEARCH',CALL_RECORDING = 'CALL_RECORDING',EMAIL_SEND = 'EMAIL_SEND',+ MESSAGE_CAMPAIGN_SEND = 'MESSAGE_CAMPAIGN_SEND',// Raised by the query runner and read back by the API rate limit, so it is// out of twenty-shared's USAGE_OPERATION_TYPES: an app billing under it// would spend the workspace's request budget.diff --git a/packages/twenty-server/src/modules/emailing/services/campaign-send-slot.service.ts b/packages/twenty-server/src/modules/emailing/services/campaign-send-slot.service.tsindex 36f2b0b4503..04b0986e92c 100644--- a/packages/twenty-server/src/modules/emailing/services/campaign-send-slot.service.ts+++ b/packages/twenty-server/src/modules/emailing/services/campaign-send-slot.service.ts@@ -29,7 +29,7 @@ export class CampaignSendSlotService {try {await this.usageLimitSpeedService.consumeOrThrow({resourceType: UsageResourceType.EMAIL,- operationType: UsageOperationType.EMAIL_SEND,+ operationType: UsageOperationType.MESSAGE_CAMPAIGN_SEND,authContext: buildSystemAuthContext(workspaceId),cost: requestedSlotCount,});diff --git a/packages/twenty-shared/src/application/usageOperationTypesType.ts b/packages/twenty-shared/src/application/usageOperationTypesType.tsindex 65352d5665c..702a6ee9e73 100644--- a/packages/twenty-shared/src/application/usageOperationTypesType.ts+++ b/packages/twenty-shared/src/application/usageOperationTypesType.ts@@ -1,8 +1,8 @@// The subset of the server's UsageOperationType an app may bill under, so a// manifest can name a category without importing server code. API_REQUEST,-// STORAGE_FILE, RECORD_WRITE and SUBSCRIPTION are left out on purpose: the-// platform raises all four itself, and API_REQUEST rows feed the workspace's-// API rate limit.+// STORAGE_FILE, RECORD_WRITE, SUBSCRIPTION and MESSAGE_CAMPAIGN_SEND are left+// out on purpose: the platform raises all five itself, and API_REQUEST rows+// feed the workspace's API rate limit.export const USAGE_OPERATION_TYPES = ['AI_CHAT_TOKEN','AI_WORKFLOW_TOKEN',