Skip to content

The open alternative to Salesforce, designed for AI.

Say which campaign detail is missing when a send is refused (#28111)

neo7735728652edcce
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 0171c6a36ea..a956b832040 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
@@ -28,6 +28,7 @@ import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace
import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';
import { CampaignVariableService } from 'src/modules/emailing/services/campaign-variable.service';
import { EmailBillingService } from 'src/modules/emailing/services/email-billing.service';
+import { buildCampaignNotSendableExceptions } from 'src/modules/emailing/utils/build-campaign-not-sendable-exceptions.util';
import { buildCampaignSendRefusalException } from 'src/modules/emailing/utils/build-campaign-send-refusal-exception.util';
import { EmailingDomainSenderService } from 'src/modules/emailing/services/emailing-domain-sender.service';
import { MessageCampaignAudienceService } from 'src/modules/emailing/services/message-campaign-audience.service';
@@ -355,12 +356,12 @@ export class MessageCampaignService {
const sendableCampaign = sendableCampaignSchema.safeParse(campaign);
if (!sendableCampaign.success) {
- throw new EmailingDomainException(
- `Campaign ${campaignId} is not sendable: ${sendableCampaign.error.issues
- .map((issue) => `${issue.path.join('.')} ${issue.message}`)
- .join(', ')}`,
- EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_NOT_SENDABLE,
- );
+ const [firstNotSendableException] = buildCampaignNotSendableExceptions({
+ campaignId,
+ issues: sendableCampaign.error.issues,
+ });
+
+ throw firstNotSendableException;
}
await this.campaignVariableService.assertKnownVariables(
diff --git a/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-not-sendable-exceptions.util.spec.ts b/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-not-sendable-exceptions.util.spec.ts
new file mode 100644
index 00000000000..eaed69bd7f9
--- /dev/null
+++ b/packages/twenty-server/src/modules/emailing/utils/__tests__/build-campaign-not-sendable-exceptions.util.spec.ts
@@ -0,0 +1,91 @@
+import { MessageCampaignStatus } from 'twenty-shared/types';
+import { EMAIL_DOCUMENT_SCHEMA_VERSION } from 'twenty-shared/utils';
+
+import { EmailingDomainExceptionCode } from 'src/engine/core-modules/emailing-domain/exceptions/emailing-domain.exception';
+import { buildCampaignNotSendableExceptions } from 'src/modules/emailing/utils/build-campaign-not-sendable-exceptions.util';
+import { sendableCampaignSchema } from 'src/modules/emailing/zod-schemas/sendable-campaign.zod-schema';
+
+const sendableCampaign = {
+ status: MessageCampaignStatus.DRAFT,
+ subject: 'Monthly newsletter',
+ bodyTemplate: JSON.stringify({
+ type: 'doc',
+ attrs: { schemaVersion: EMAIL_DOCUMENT_SCHEMA_VERSION },
+ content: [
+ { type: 'paragraph', content: [{ type: 'text', text: 'Hello' }] },
+ ],
+ }),
+ fromAddress: { primaryEmail: '[email protected]' },
+ listId: '20202020-0000-4000-8000-000000000001',
+};
+
+const buildExceptionsFor = (campaign: Record<string, unknown>) => {
+ const parsed = sendableCampaignSchema.safeParse(campaign);
+
+ return buildCampaignNotSendableExceptions({
+ campaignId: 'campaign-1',
+ issues: parsed.error?.issues ?? [],
+ });
+};
+
+describe('buildCampaignNotSendableExceptions', () => {
+ it('lists every missing field of an empty draft in the order the user fixes them', () => {
+ const exceptions = buildExceptionsFor({
+ status: MessageCampaignStatus.DRAFT,
+ subject: null,
+ bodyTemplate: null,
+ fromAddress: null,
+ listId: null,
+ });
+
+ expect(
+ exceptions.map((exception) => exception.userFriendlyMessage.message),
+ ).toEqual([
+ 'Pick a list in the To field first.',
+ 'Add a sender to this campaign first.',
+ 'Add a subject to this campaign first.',
+ 'Write the email content first.',
+ ]);
+ });
+
+ it('keeps the not sendable code the send path throws', () => {
+ const [exception] = buildExceptionsFor({
+ ...sendableCampaign,
+ subject: '',
+ });
+
+ expect(exception.code).toBe(
+ EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_NOT_SENDABLE,
+ );
+ expect(exception.message).toContain(
+ 'Campaign campaign-1 is not sendable: subject',
+ );
+ });
+
+ it('reports an empty body once even though it also fails the document check', () => {
+ const exceptions = buildExceptionsFor({
+ ...sendableCampaign,
+ bodyTemplate: '',
+ });
+
+ expect(exceptions).toHaveLength(1);
+ expect(exceptions[0].userFriendlyMessage.message).toBe(
+ 'Write the email content first.',
+ );
+ });
+
+ it('puts an already sent campaign ahead of its content problems', () => {
+ const exceptions = buildExceptionsFor({
+ ...sendableCampaign,
+ status: MessageCampaignStatus.SENT,
+ subject: '',
+ });
+
+ expect(
+ exceptions.map((exception) => exception.userFriendlyMessage.message),
+ ).toEqual([
+ 'Only a draft or scheduled campaign can be sent.',
+ 'Add a subject to this campaign first.',
+ ]);
+ });
+});
diff --git a/packages/twenty-server/src/modules/emailing/utils/build-campaign-not-sendable-exceptions.util.ts b/packages/twenty-server/src/modules/emailing/utils/build-campaign-not-sendable-exceptions.util.ts
new file mode 100644
index 00000000000..0640f411372
--- /dev/null
+++ b/packages/twenty-server/src/modules/emailing/utils/build-campaign-not-sendable-exceptions.util.ts
@@ -0,0 +1,54 @@
+import { type MessageDescriptor } from '@lingui/core';
+import { msg } from '@lingui/core/macro';
+import { type z } from 'zod';
+
+import {
+ EmailingDomainException,
+ EmailingDomainExceptionCode,
+} from 'src/engine/core-modules/emailing-domain/exceptions/emailing-domain.exception';
+
+const getNotSendableFieldUserFriendlyMessage = (
+ field: PropertyKey | undefined,
+): MessageDescriptor | undefined => {
+ switch (field) {
+ case 'status':
+ return msg`Only a draft or scheduled campaign can be sent.`;
+ case 'listId':
+ return msg`Pick a list in the To field first.`;
+ case 'fromAddress':
+ return msg`Add a sender to this campaign first.`;
+ case 'subject':
+ return msg`Add a subject to this campaign first.`;
+ case 'bodyTemplate':
+ return msg`Write the email content first.`;
+ default:
+ return undefined;
+ }
+};
+
+export const buildCampaignNotSendableExceptions = ({
+ campaignId,
+ issues,
+}: {
+ campaignId: string;
+ issues: z.ZodError['issues'];
+}): EmailingDomainException[] => {
+ const firstIssuePerField = issues.filter(
+ (issue, issueIndex) =>
+ issues.findIndex((otherIssue) => otherIssue.path[0] === issue.path[0]) ===
+ issueIndex,
+ );
+
+ return firstIssuePerField.map(
+ (issue) =>
+ new EmailingDomainException(
+ `Campaign ${campaignId} is not sendable: ${issue.path.join('.')} ${issue.message}`,
+ EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_NOT_SENDABLE,
+ {
+ userFriendlyMessage: getNotSendableFieldUserFriendlyMessage(
+ issue.path[0],
+ ),
+ },
+ ),
+ );
+};
diff --git a/packages/twenty-server/src/modules/emailing/zod-schemas/sendable-campaign.zod-schema.ts b/packages/twenty-server/src/modules/emailing/zod-schemas/sendable-campaign.zod-schema.ts
index a56b604aaba..2bb1e3ec8db 100644
--- a/packages/twenty-server/src/modules/emailing/zod-schemas/sendable-campaign.zod-schema.ts
+++ b/packages/twenty-server/src/modules/emailing/zod-schemas/sendable-campaign.zod-schema.ts
@@ -10,12 +10,12 @@ export const sendableCampaignSchema = z.object({
MessageCampaignStatus.DRAFT,
MessageCampaignStatus.SCHEDULED,
]),
+ listId: z.string().min(1),
+ fromAddress: z.object({ primaryEmail: z.email() }),
subject: z.string().min(1),
bodyTemplate: z.string().min(1).refine(isSendableBodyTemplate, {
message: 'bodyTemplate is not a valid email document',
}),
scheduledAt: z.coerce.date().nullish(),
- fromAddress: z.object({ primaryEmail: z.email() }),
- listId: z.string().min(1),
unsubscribeTopicId: z.string().nullish(),
});