The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Only advertise campaign variables the resolver accepts (#29070)
neo7731b905a0ead02
diff --git a/packages/twenty-server/src/modules/emailing/tools/save-campaign-tool.schema.ts b/packages/twenty-server/src/modules/emailing/tools/save-campaign-tool.schema.tsindex e4d79690983..f7a8a7ba736 100644--- a/packages/twenty-server/src/modules/emailing/tools/save-campaign-tool.schema.ts+++ b/packages/twenty-server/src/modules/emailing/tools/save-campaign-tool.schema.ts@@ -26,10 +26,10 @@ export const SaveCampaignToolInputZodSchema = z.object({.optional().describe('The full email document that replaces the campaign body. ' +- 'A document is {type: "doc", content: [...blocks]}. Blocks: paragraph and heading (level 1-3) hold inline text, variableTag chips ({attrs: {variable: "{{firstName}}"}}) and hardBreak; section wraps blocks in a styled band; columns holds 2-4 column children; button is a call-to-action with an href; image, divider, bulletList/orderedList and html (raw HTML) complete the set. ' ++ 'A document is {type: "doc", content: [...blocks]}. Blocks: paragraph and heading (level 1-3) hold inline text, variableTag chips ({attrs: {variable: "{{name.firstName}}"}}) and hardBreak; section wraps blocks in a styled band; columns holds 2-4 column children; button is a call-to-action with an href; image, divider, bulletList/orderedList and html (raw HTML) complete the set. ' +'Style attributes are objects of camelCase CSS properties, e.g. {"paddingTop": "12px", "backgroundColor": "#f4f4f5"}. Use longhand box properties (paddingTop/Right/Bottom/Left). ' +- 'Per-recipient variables reference person fields by path, e.g. {{name.firstName}}, {{emails.primaryEmail}}, {{city}} or any custom person field; {{firstName}}, {{lastName}}, {{fullName}}, {{email}} and {{personId}} also work. They apply in text, button and link URLs and raw HTML, and unknown names are rejected with the available list. ' +- 'Give a variable a fallback for recipients whose value is empty with the Liquid default filter, e.g. {{name.firstName | default: "there"}}; the fallback is double-quoted and a double quote inside it is written \\". Any other {{...}} is rejected as malformed. ' ++ 'Per-recipient variables reference person fields by path, e.g. {{name.firstName}}, {{name.lastName}}, {{jobTitle}} or any custom person text, number, boolean, date, select or rating field; {{fullName}} and {{personId}} also work. Email and phone fields are not variables. They apply in text, button and link URLs and raw HTML, and unknown names are rejected with the available list. ' ++ 'Give a variable a fallback for recipients whose value is empty with the Liquid default filter, e.g. {{name.firstName | default: "there"}}; the fallback is double-quoted and a double quote inside it is written \\". Any other double-brace syntax is rejected as malformed. ' +'To modify an existing body, read the record first, edit the parsed document and send the whole result back.',),});diff --git a/packages/twenty-server/test/integration/emailing/save-campaign-variable-validation.integration-spec.ts b/packages/twenty-server/test/integration/emailing/save-campaign-variable-validation.integration-spec.tsindex d65cb841e8e..5a6a4423bf0 100644--- a/packages/twenty-server/test/integration/emailing/save-campaign-variable-validation.integration-spec.ts+++ b/packages/twenty-server/test/integration/emailing/save-campaign-variable-validation.integration-spec.ts@@ -1,40 +1,76 @@+import { executeToolThroughMcp } from 'test/integration/ai/utils/execute-tool-through-mcp.util';import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';import { USER_WORKSPACE_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/core/utils/seed-user-workspaces.util';+import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';import { type MessageCampaignDraftService } from 'src/modules/emailing/services/message-campaign-draft.service';+import { SaveCampaignToolInputZodSchema } from 'src/modules/emailing/tools/save-campaign-tool.schema';-const saveDraftWithVariable = (variable: string) =>+const MESSAGE_CAMPAIGN_TABLE = `"${getWorkspaceSchemaName(SEED_APPLE_WORKSPACE_ID)}"."messageCampaign"`;++const buildBodyWithVariables = (variables: string[]) => ({+ type: 'doc' as const,+ content: [+ {+ type: 'paragraph' as const,+ content: [+ { type: 'text' as const, text: 'Hi ' },+ ...variables.map((variable) => ({+ type: 'variableTag' as const,+ attrs: { variable },+ })),+ ],+ },+ ],+});++const saveDraftWithVariables = (...variables: string[]) =>getAppProviderByClassName<MessageCampaignDraftService>('MessageCampaignDraftService',).saveDraft({workspaceId: SEED_APPLE_WORKSPACE_ID,userWorkspaceId: USER_WORKSPACE_DATA_SEED_IDS.JANE,subject: 'Hello',- body: {- type: 'doc',- content: [- {- type: 'paragraph',- content: [- { type: 'text', text: 'Hi ' },- { type: 'variableTag', attrs: { variable } },- ],- },- ],- },+ body: buildBodyWithVariables(variables),});describe('Saving a campaign draft with variables', () => {+ it('should accept every variable the save_campaign tool advertises', async () => {+ const advertisedVariables =+ SaveCampaignToolInputZodSchema.shape.body.description?.match(+ /\{\{.*?\}\}/g,+ ) ?? [];++ expect(advertisedVariables).toContain('{{name.firstName}}');++ const output = await executeToolThroughMcp<{ campaignId: string }>({+ toolName: 'save_campaign',+ toolArguments: {+ subject: 'Hello',+ body: buildBodyWithVariables(advertisedVariables),+ },+ token: APPLE_JANE_ADMIN_ACCESS_TOKEN,+ });++ expect(output.error).toBeUndefined();+ expect(output.success).toBe(true);++ await testDataSource.query(+ `DELETE FROM ${MESSAGE_CAMPAIGN_TABLE} WHERE "id" = $1`,+ [output.result?.campaignId],+ );+ });+it('should reject a fallback written without the default filter', async () => {await expect(- saveDraftWithVariable('{{jobTitle | "there"}}'),+ saveDraftWithVariables('{{jobTitle | "there"}}'),).rejects.toThrow('Malformed campaign variable: {{jobTitle | "there"}}');});it('should reject an unknown variable even when it has a fallback', async () => {await expect(- saveDraftWithVariable('{{notAPersonField | default: "there"}}'),+ saveDraftWithVariables('{{notAPersonField | default: "there"}}'),).rejects.toThrow('Unknown campaign variables: notAPersonField');});});