/** * Campaign shapes. Declared here rather than imported from `@crm/contracts` so * the published package has no private dependencies; they are kept in step with * the zod schemas in `packages/contracts/src/campaign/`. */ import type { IsoDate, JsonObject } from './common'; import type { ContactFilterEntry } from './contact-filter'; export type CampaignStatus = 'pending' | 'processing' | 'completed' | 'failed' | 'cancelled'; /** What the campaign charges for. `payment` is the collect-a-payment flow. */ export type CampaignType = 'general' | 'payment'; export type CampaignTrigger = 'immediate' | 'schedule'; export type CampaignRepeatUnit = 'minutes' | 'hours' | 'days' | 'weeks' | 'months'; export interface CampaignRepeat { every: number; unit: CampaignRepeatUnit; /** Inclusive: the last occurrence is the final one at or before this moment. */ until?: IsoDate; } /** * A bulk recipient list held in an uploaded file. * * This is what `campaigns.createBulk()` produces, and it is the only audience * mode whose contents are not in the campaign document: the numbers live in an * encrypted file and the campaign holds a pointer to it. */ export interface CampaignNumbersFilter { /** * HTTPS URL of the encrypted receiver file. Must be a public host -- the * service that sends the campaign fetches it. */ numbers: string; /** * The secret the file was encrypted under, 32-128 hex characters. * * Named `hash` for historical reasons; it is a key, not a digest. Whatever * reads the file derives the AES key from it. Treat it as a credential: anyone * holding this and the URL can read the recipient list. */ hash: string; } /** Re-target by what happened to a previous campaign's messages. */ export interface CampaignRetargetStatusFilter { type: 'status'; /** The campaign to re-target. */ cid: string; /** Its numeric message id. A number, not a string. */ cmid: number; status: 'all' | 'sent' | 'delivered' | 'read' | 'replied' | 'failed'; delivered?: boolean | null; read?: boolean | null; replied?: boolean | null; /** Hours within which a reply counts. */ replied_in?: number | null; reply_type?: string | null; reply?: string[] | null; } /** Re-target an explicit set of previous recipients. */ export interface CampaignRetargetSelectionFilter { type: 'selection'; records: string[]; } /** * One entry in a campaign's `filters`. * * The audience is one field with four modes, and the mode is decided by the * **shape of the array** rather than by a selector: * * | `filters` | Audience | * |---|---| * | `[]` | every contact in the account | * | `ContactFilterEntry[]` | a segment, built from contact conditions | * | `[{numbers, hash}]` | a bulk list from an uploaded file | * | `[{type: 'status' \| 'selection', …}]` | re-target of an earlier campaign | * * The modes cannot be mixed, and the three non-segment modes take exactly one * entry -- everything downstream reads `filters[0]` and nothing else. The server * rejects a mixture rather than guessing. */ export type CampaignFilter = CampaignNumbersFilter | CampaignRetargetStatusFilter | CampaignRetargetSelectionFilter | ContactFilterEntry; /** Which of the five modes a `filters` array expresses. */ export type CampaignAudienceKind = 'all' | 'segment' | 'numbers' | 'group' | 'retarget'; export interface CampaignMessage { id?: string | number; channel: { id: string; version?: string; category?: string; }; category?: string; /** The provider payload. Shape is per channel type, so it is left open. */ message: JsonObject; /** Save a reply to a contact field. */ replyStatus?: { save: boolean; fieldId: string; }; polls?: string[]; /** Anything else the panel put here is carried through. */ [key: string]: unknown; } /** Who launched a campaign and from where. Stamped by the server; never sent. */ export interface CampaignTriggerInfo { userIp?: string; userType?: string; psuid?: string; userRefId?: string; [key: string]: unknown; } export interface Campaign { id: string; /** * The team member who created the campaign, by psuid. Absent when the account * holder created it, and on campaigns written before this was recorded. * * Who *wrote* it. Who armed it is `triggerInfo.psuid`, which can be someone * else and is set a step later. */ member?: string; version: string; crmVersion: string; name: string; /** * The dispatch route, as the sending service understands it -- `general`, * `bulk_campaign`, `retarget`, and others. Distinct from `campaignType`. */ type: string; campaignType: CampaignType; trigger: CampaignTrigger; triggerInfo: CampaignTriggerInfo; channelType: string; messagingPattern: string; status: CampaignStatus; filters: CampaignFilter[]; messages: CampaignMessage[]; showMessageInPanel: boolean; isRepeating: boolean; repeat?: CampaignRepeat; parentBroadcastId?: string; scheduleId?: string; hidden: boolean; /** * When the campaign is armed to go out -- and whether it is armed at all. * * Absent on a campaign nobody has triggered yet, which is the state every * campaign starts in. Set by `trigger()` or `schedule()`, never by `create()` * or `update()`. */ scheduledAt?: IsoDate; processedAt?: IsoDate; completedAt?: IsoDate; /** * Recipients resolved at send time, not at creation. * * `0` until the campaign is processed. The audience is a stored query -- even a * bulk list is a file the sender reads later -- so a scheduled campaign's final * count can differ from what was shown when it was built. */ receivers: number; totalMessages: number; error?: string; metaInfo: JsonObject[]; createdAt: IsoDate; updatedAt: IsoDate; } /** * A campaign to write. Writing one does not send it: see `campaigns.trigger()`. * * There is no `scheduledAt` here on purpose, and the API rejects one rather than * ignoring it -- a send time belongs to the act of arming the campaign, which is * also what records who armed it. */ export interface CreateCampaignInput { name: string; /** Defaults to `general`. `createBulk()` sets `bulk_campaign` itself. */ type?: string; campaignType?: CampaignType; /** * How you expect it to go out. Advisory: `trigger()` rewrites it from what it * is actually given, so this only matters to your own reading of the record. */ trigger?: CampaignTrigger; channelType?: string; messagingPattern?: string; /** The audience. `[]` means every contact. See {@link CampaignFilter}. */ filters?: CampaignFilter[]; messages: CampaignMessage[]; showMessageInPanel?: boolean; isRepeating?: boolean; repeat?: CampaignRepeat; parentBroadcastId?: string; hidden?: boolean; } export type UpdateCampaignInput = Partial; /** * When to send. An empty object -- or no argument at all -- means now. * * "Now" is not instant: the campaign is armed a short way out (a minute by * default) so there is a window in which `cancel()` still stops it. */ export interface TriggerCampaignInput { /** ISO-8601 with an offset, e.g. `2026-11-01T04:30:00.000Z`. */ scheduledAt?: IsoDate; } /** * Declared as a type alias rather than an interface, and that is load-bearing: * only an alias of an object literal gets TypeScript's implicit index signature, * which is what lets it be passed as a request's query record. */ export type ListCampaignsQuery = { limit: number; cursor?: string; status?: CampaignStatus; campaignType?: CampaignType; type?: string; hidden?: boolean; }; /** * How far one message got. * * A funnel held in **one field**, not a set of flags: a row carries the furthest * state it has reached, so `read` also means delivered and sent. There is no * `queued` -- a row only exists once the send has been attempted. * * `failed` sits off the funnel rather than at the end of it. A failed message * was never delivered, so it is counted on its own and never inside `sent`. */ export type CampaignMessageStatus = 'sent' | 'delivered' | 'read' | 'replied' | 'failed'; /** * One attempted send: one campaign message, to one recipient. * * **Not a {@link CampaignMessage}**, which is the confusingly similar * thing: that is one of the messages a campaign is *defined* to send. This is a * record of such a message having gone to one person, and there is one per * recipient per message. * * Written by the campaign manager at send time; its `status` is moved forward by * the channel's webhooks, which are processed outside the CRM. So a row is * append-only from this SDK's side -- there is nothing here that writes one. */ export interface CampaignMessageRecord { /** * The row's own id. * * This is the handle a `selection` retarget takes: hand a list of these back * and the campaign manager resolves them to recipients, which is how * "re-send to everyone who did not read it" is built. */ id: string; campaignId: string; /** Which message of the campaign this was, zero-based. */ messageIndex: number; /** The provider's id for the message. `''` when the send failed. */ providerMessageId: string; channelId: string; /** The recipient's contact id. */ contactId: string; /** The destination itself -- a WhatsApp number, a Telegram id, and so on. */ identifier: string; /** The recipient's name at send time; `''` when the contact had none. */ name: string; status: CampaignMessageStatus; /** Set for each stage the row has reached, absent for the ones it has not. */ sentAt?: IsoDate; deliveredAt?: IsoDate; readAt?: IsoDate; repliedAt?: IsoDate; failedAt?: IsoDate; /** What the recipient replied; `''` unless `status` is `replied`. */ reply: string; /** The reply's kind -- `text`, `button`, … `''` unless replied. */ replyType: string; /** Seconds between the send and the reply. `0` unless replied. */ repliedIn: number; /** The provider's error, on failed rows only. */ error?: string; /** When the send was attempted. */ createdAt: IsoDate; } /** * The campaign report: the numbers behind the tiles. * * Every stage is **cumulative** -- `delivered` counts everything that got at * least that far -- so `read` is a subset of `delivered`. Subtract for a stage's * exclusive count: `delivered - read` is "delivered but unread". * * `total` is the only number that counts every row, failures included, and it is * the number of attempted sends. */ export interface CampaignMessageStats { total: number; /** Reached the provider: sent, delivered, read or replied. */ sent: number; /** Delivered, read or replied. */ delivered: number; /** Read or replied. */ read: number; replied: number; /** Never part of `sent`. */ failed: number; } /** * What narrows `crm.campaignMessages.stats()`. * * The campaign itself is not in here: it is the first argument, because the * report is addressed as the campaign's sub-resource (`/campaigns/:id/report`) * rather than filtered out of a collection of rows. * * A type alias, for the same reason as {@link ListCampaignsQuery}. */ export type CampaignMessageStatsQuery = { /** One message of a multi-message campaign, by zero-based index. */ message?: number; }; /** * `crm.campaignMessages.list()` -- offset paginated. * * `status` filters **cumulatively** by default, so it agrees with the stats: * `status: 'delivered'` returns the same rows the `delivered` number counted. * `exact: true` instead returns only rows sitting at that stage -- "delivered * but never read" -- which is the drill-down for `delivered - read`. */ export type ListCampaignMessagesQuery = { /** The campaign is the method's first argument, not a query field. */ message?: number; status?: CampaignMessageStatus; /** Only rows at `status` right now. No effect without `status`, or on `failed`. */ exact?: boolean; /** Exact match on one recipient, e.g. a single WhatsApp number. Not a search. */ identifier?: string; /** Replied rows only; the API refuses it otherwise. */ reply_type?: string; /** Replied rows only; the API refuses it otherwise. */ replied_within_hours?: number; page?: number; /** 1..100, default 25. */ rows?: number; }; /** One distinct reply, and how many people sent it. */ export interface CampaignReplyGroup { reply: string; replyType: string; count: number; } /** What a poll or button message was answered with. */ export interface CampaignReplyBreakdown { /** Most common first, capped by the query's `limit`. */ groups: CampaignReplyGroup[]; /** Replied rows in total, whether or not their group is listed. */ replied: number; /** Distinct replies in total -- compare with `groups.length` to detect truncation. */ distinct: number; } /** A type alias, for the same reason as {@link ListCampaignsQuery}. */ export type CampaignRepliesQuery = { /** The campaign is the method's first argument, not a query field. */ message?: number; /** Distinct replies to return, 1..200, default 50. */ limit?: number; };