import { z } from 'zod'; import { User, user, userRequest, userResponse } from '../../common/user.js'; import { BroadcastStatus, broadcastStatus, broadcastStatusRequest, broadcastStatusResponse, } from './broadcast-status.js'; import { Overrides, overrides, overridesRequest, overridesResponse } from './overrides.js'; /** * Zod schema for the Broadcast model. * Defines the structure and validation rules for this data type. * This is the shape used in application code - what developers interact with. */ export const broadcast = z.lazy(() => { return z.object({ actionUrl: z.string().max(2048).optional().nullable(), category: z .string() .max(255) .regex(/^[A-Za-z0-9_\.\-\/:]+$/) .optional() .nullable(), content: z.string().max(10485760).optional().nullable(), createdAt: z.string().optional(), customAttributes: z.any().optional().nullable(), id: z.string().optional(), overrides: overrides.optional().nullable(), recipients: z.array(user).min(1).max(1000).nullable(), status: broadcastStatus.optional(), title: z.string().min(1).max(255), topic: z .string() .max(255) .regex(/^[A-Za-z0-9_\.\-\/:]+$/) .optional() .nullable(), }); }); /** * * @typedef {Broadcast} broadcast * @property {string} - The URL recipients will be directed to when interacting with the broadcast. * @property {string} - The label used to group broadcasts. * @property {string} - The body content delivered with the broadcast. * @property {string} - The timestamp when the broadcast was created. * @property {any} - Arbitrary custom data associated with the broadcast. * @property {string} - The unique id for this broadcast. * @property {Overrides} - Channel- or provider-specific values that override the defaults. * @property {User[]} - A collection of users or filters that determine who receives the broadcast. * @property {BroadcastStatus} - The runtime state of the broadcast execution. * @property {string} - The subject or headline that will be shown to recipients. * @property {string} - The topic that further classifies the broadcast. */ export type Broadcast = z.infer; /** * Zod schema for mapping API responses to the Broadcast application shape. * Handles any property name transformations from the API schema. * If property names match the API schema exactly, this is identical to the application shape. */ export const broadcastResponse = z.lazy(() => { return z .object({ action_url: z.string().max(2048).optional().nullable(), category: z .string() .max(255) .regex(/^[A-Za-z0-9_\.\-\/:]+$/) .optional() .nullable(), content: z.string().max(10485760).optional().nullable(), created_at: z.string().optional(), custom_attributes: z.any().optional().nullable(), id: z.string().optional(), overrides: overridesResponse.optional().nullable(), recipients: z.array(userResponse).min(1).max(1000).nullable(), status: broadcastStatusResponse.optional(), title: z.string().min(1).max(255), topic: z .string() .max(255) .regex(/^[A-Za-z0-9_\.\-\/:]+$/) .optional() .nullable(), }) .transform((data) => ({ actionUrl: data['action_url'], category: data['category'], content: data['content'], createdAt: data['created_at'], customAttributes: data['custom_attributes'], id: data['id'], overrides: data['overrides'], recipients: data['recipients'], status: data['status'], title: data['title'], topic: data['topic'], })); }); /** * Zod schema for mapping the Broadcast application shape to API requests. * Handles any property name transformations required by the API schema. * If property names match the API schema exactly, this is identical to the application shape. */ export const broadcastRequest = z.lazy(() => { return z .object({ actionUrl: z.string().max(2048).optional().nullable(), category: z .string() .max(255) .regex(/^[A-Za-z0-9_\.\-\/:]+$/) .optional() .nullable(), content: z.string().max(10485760).optional().nullable(), createdAt: z.string().optional(), customAttributes: z.any().optional().nullable(), id: z.string().optional(), overrides: overridesRequest.optional().nullable(), recipients: z.array(userRequest).min(1).max(1000).nullable(), status: broadcastStatusRequest.optional(), title: z.string().min(1).max(255), topic: z .string() .max(255) .regex(/^[A-Za-z0-9_\.\-\/:]+$/) .optional() .nullable(), }) .transform((data) => ({ action_url: data['actionUrl'], category: data['category'], content: data['content'], created_at: data['createdAt'], custom_attributes: data['customAttributes'], id: data['id'], overrides: data['overrides'], recipients: data['recipients'], status: data['status'], title: data['title'], topic: data['topic'], })); });