Skip to content

The open alternative to Salesforce, designed for AI.

784 B
Raw Blame
import { MessageCampaignStatus } from 'twenty-shared/types';
import { parseCanonicalEmailDocument, parseJson } from 'twenty-shared/utils';
import { z } from 'zod';

const isSendableBodyTemplate = (bodyTemplate: string): boolean =>
  parseCanonicalEmailDocument(parseJson<unknown>(bodyTemplate)).success;

export const sendableCampaignSchema = z.object({
  status: z.enum([
    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(),
  unsubscribeTopicId: z.string().nullish(),
});