Skip to content

The open alternative to Salesforce, designed for AI.

Refuse a campaign send upfront when the list is over the daily email limit (#28685)

neo77342407deb2a0e
diff --git a/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts b/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts
index e437aed541f..aeb1469de1e 100644
--- a/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts
+++ b/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts
@@ -268,6 +268,7 @@ export class MessageCampaignService {
const sendRefusal = await this.emailBillingService.findEmailSendRefusal({
workspaceId,
spenders: { userWorkspaceId },
+ emailCount: sendableRecipients.length,
});
if (sendableRecipients.length > 0 && isDefined(sendRefusal)) {
diff --git a/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-send-refusal-exception.util.spec.ts b/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-send-refusal-exception.util.spec.ts
index 2958d0576fb..fc35f7a0007 100644
--- a/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-send-refusal-exception.util.spec.ts
+++ b/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-send-refusal-exception.util.spec.ts
@@ -13,7 +13,10 @@ const buildException = (sendRefusal: UsageRefusal) =>
sendRefusal,
});
-const buildQuotaRefusal = (exhaustedKind: ExhaustedKind): UsageRefusal => ({
+const buildQuotaRefusal = (
+ exhaustedKind: ExhaustedKind,
+ { isDefault }: { isDefault?: boolean } = {},
+): UsageRefusal => ({
kind: 'quotaExhausted',
exhaustedScope: {
resourceType: UsageResourceType.EMAIL,
@@ -27,6 +30,7 @@ const buildQuotaRefusal = (exhaustedKind: ExhaustedKind): UsageRefusal => ({
periodCount: 1,
periodUnit: 'month',
retryAfterMs: 0,
+ isDefault,
} satisfies ExhaustedScope,
});
@@ -53,6 +57,21 @@ describe('buildCampaignSendRefusalException', () => {
);
});
+ it('tells the sender to contact us when the default daily limit is what the list exceeds', () => {
+ const exception = buildException(
+ buildQuotaRefusal('limit', { isDefault: true }),
+ );
+
+ expect(exception.code).toBe(
+ EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_USAGE_LIMIT_REACHED,
+ );
+ expect(exception.userFriendlyMessage.message).toContain('Contact us');
+ expect(exception.userFriendlyMessage.values).toEqual({ limitValue: 1000 });
+ expect(exception.userFriendlyMessage).not.toEqual(
+ buildException(buildQuotaRefusal('limit')).userFriendlyMessage,
+ );
+ });
+
it.each(['NO_SUBSCRIPTION', 'WORKSPACE_SUSPENDED'] as const)(
'reports %s as a subscription problem rather than missing credits',
(reason) => {
diff --git a/packages/twenty-server/src/modules/emailing/utils/build-campaign-send-refusal-exception.util.ts b/packages/twenty-server/src/modules/emailing/utils/build-campaign-send-refusal-exception.util.ts
index ecd4a0e3d1b..55a0791d168 100644
--- a/packages/twenty-server/src/modules/emailing/utils/build-campaign-send-refusal-exception.util.ts
+++ b/packages/twenty-server/src/modules/emailing/utils/build-campaign-send-refusal-exception.util.ts
@@ -1,3 +1,5 @@
+import { msg, plural } from '@lingui/core/macro';
+
import { SUBSCRIPTION_INACTIVE_REASON_USER_FRIENDLY_MESSAGE } from 'src/engine/core-modules/billing/constants/subscription-inactive-reason-user-friendly-message.constant';
import { type UsageRefusal } from 'src/engine/core-modules/billing/types/usage-refusal.type';
import {
@@ -27,7 +29,8 @@ export const buildCampaignSendRefusalException = ({
);
}
- const { exhaustedKind, spenderType, limitValue } = sendRefusal.exhaustedScope;
+ const { exhaustedKind, spenderType, limitValue, isDefault } =
+ sendRefusal.exhaustedScope;
if (exhaustedKind === 'workspaceAllowance') {
return new EmailingDomainException(
@@ -36,6 +39,16 @@ export const buildCampaignSendRefusalException = ({
);
}
+ if (isDefault === true) {
+ return new EmailingDomainException(
+ `Campaign ${campaignId} cannot be sent to ${recipientCount} recipient(s): it does not fit in the default email limit of ${limitValue}`,
+ EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_USAGE_LIMIT_REACHED,
+ {
+ userFriendlyMessage: msg`Your daily limit is ${plural(limitValue, { one: '# email', other: '# emails' })}, and this campaign would go over what's left today. Contact us to raise it.`,
+ },
+ );
+ }
+
return new EmailingDomainException(
`Campaign ${campaignId} cannot be sent to ${recipientCount} recipient(s): an email usage limit of ${limitValue} for ${spenderType} is reached`,
EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_USAGE_LIMIT_REACHED,