/** * Scheduled messages: a send with a time, and optionally a condition checked * immediately before it goes out. * * The condition is what makes a drip safe to set and forget. A sequence of five * messages over two weeks is five rows; each one re-reads the contact a moment * before sending and stands down if the contact no longer matches. So "stop * messaging a lead once they convert" is a field on the contact changing, not a * job anybody has to remember to cancel. * * Declared here rather than imported from `@crm/contracts` so the published * package has no private dependencies; kept in step with the zod schemas. */ import type { ContactFilter } from './contact-filter'; import type { IsoDate, JsonObject, ListQuery, ObjectId } from './common'; import type { MessageContent } from './inbox'; /** Units a schedule's spacing is expressed in. */ export type ScheduleUnit = 'minutes' | 'hours' | 'days' | 'weeks' | 'months'; /** * Where one occurrence ended up. * * | Status | Meaning | * | --- | --- | * | `pending` | Waiting for its moment. The only status that can be rescheduled. | * | `processing` | Claimed by the scheduler, mid-send. | * | `sent` | Handed to the channel. Delivery is then a message status, not this. | * | `skipped` | Its condition did not match. `result` says what was expected and what was found. | * | `cancelled` | Stood down -- by a caller, or by an earlier occurrence failing its condition under `cancel_series`. | * | `failed` | Could not be sent: the contact is gone, trashed, blocked for outbound, or has no channel identity. `result.reason` says which. | */ export type ScheduledMessageStatus = 'pending' | 'processing' | 'sent' | 'skipped' | 'failed' | 'cancelled'; /** * What a failed condition does to the rest of the series. * * - `skip` -- this occurrence only. Later ones still run and are checked again, * so a contact who leaves and re-enters the condition resumes receiving. * - `cancel_series` -- this occurrence and every later one. The default for a * series, because a converted lead should drop out of the sequence for good * rather than re-enter it if someone edits their stage back. */ export type OnConditionFail = 'skip' | 'cancel_series'; /** A step's distance from the series start. Cumulative, never relative to the previous step. */ export interface ScheduleOffset { value: number; unit: ScheduleUnit; } /** * One message in a sequence, with its own copy. * * `after` is measured from the series' `starts_at`, so editing one step's delay * does not shift the steps behind it. */ export interface ScheduledMessageStep { after: ScheduleOffset; content: MessageContent; context?: JsonObject; /** * Narrows this step beyond the series condition. Both must pass. Use it for a * step that needs an extra guard -- a final discount offer only if the contact * has not already bought -- and leave it out to inherit the series condition * alone. */ conditions?: ContactFilter | null; } /** Expands into evenly spaced occurrences, all carrying the same message. */ export interface ScheduleRepeat { /** Spacing between occurrences. Must be at least 1. */ every: number; unit: ScheduleUnit; /** * Inclusive end. The last occurrence is the final one at or before this * instant, so `starts_at` + `every: 3, unit: 'days'` + `until` two weeks out * produces five sends, not four or six. */ until: IsoDate; } /** * Why an occurrence did not send, or how it did. * * Only the first failing condition is named: listing every one of them would be * noise when the first already explains the skip. */ export interface ScheduledMessageResult { /** * `queued` | `condition_not_met` | `series_cancelled_by_condition` | * `cancelled_by_user` | `contact_missing` | `contact_trashed` | * `outgoing_blocked` | `no_channel_identity` */ reason?: string; /** For `condition_not_met`: which condition refused -- the series' or the step's. */ scope?: 'series' | 'step'; /** For `condition_not_met`: the first condition that failed, and what it wanted. */ field_id?: string; condition?: string; expected?: unknown; /** For `series_cancelled_by_condition`: the occurrence whose condition failed. */ at_occurrence?: number; /** Anything else the server recorded. */ error?: string; } export interface ScheduledMessage { id: ObjectId; /** The series this belongs to, or `null` for a one-off. */ series: ObjectId | null; /** 1-based position in the series. `1` for a one-off. */ occurrence: number; title: string; contact: ObjectId; /** Channel it will go out on. Lives in the channels service, so an id only. */ channel: ObjectId; content: MessageContent; context: JsonObject; /** IANA zone the schedule was expressed in, e.g. `Asia/Kolkata`. */ timezone: string; /** The resolved instant, always UTC. This is what the scheduler fires on. */ scheduled_at: IsoDate; /** The same moment as wall-clock in `timezone`, with no offset -- what the user chose. */ scheduled_at_local: string; /** The series condition. `null` means send unconditionally. */ conditions: ContactFilter | null; /** * This occurrence's own extra narrowing, from its step. Both this and * `conditions` have to pass. */ step_conditions: ContactFilter | null; on_condition_fail: OnConditionFail; status: ScheduledMessageStatus; result: ScheduledMessageResult | null; /** When the scheduler claimed it. */ processed_at: IsoDate | null; /** When it reached a terminal status. */ completed_at: IsoDate | null; createdAt: IsoDate; updatedAt: IsoDate; } /** A series and the occurrences it expanded into. */ export interface ScheduledMessageSeries { series: ObjectId; title: string; contact: ObjectId; channel: ObjectId; timezone: string; conditions: ContactFilter | null; on_condition_fail: OnConditionFail; /** In send order. One row per message, each independently cancellable. */ occurrences: ScheduledMessage[]; } /** What every create shares. */ interface ScheduledMessageBase { /** Shown in the schedule list. Defaults to `'Untitled'`. */ title?: string; channel: ObjectId; contact: { id: ObjectId; }; /** * IANA zone name -- `Asia/Kolkata`, `Europe/London`. Not an abbreviation and * not an offset: `IST` and `+05:30` are rejected. * * Required. When the time below carries no offset it is read as wall-clock in * this zone; when it does, the instant is already pinned and this is kept for * display and for later edits. */ timezone: string; /** * Checked against the contact immediately before each send. Omit to always * send. * * Same DSL as `contacts.list({ filter })` and a saved `Segment` -- see the * contact filter page. A custom field is addressed by its **slug**. */ conditions?: ContactFilter | null; /** Default `skip` for a one-off, `cancel_series` for a series. */ on_condition_fail?: OnConditionFail; } /** One message, one time. */ export interface CreateScheduledMessageInput extends ScheduledMessageBase { /** * `2026-10-15T10:00:00` is 10:00 in `timezone`. `2026-10-15T04:30:00Z` is that * instant regardless of it. Must be in the future. */ scheduled_at: IsoDate; content: MessageContent; context?: JsonObject; } /** A sequence with different copy per step. */ export interface CreateScheduledSeriesInput extends ScheduledMessageBase { /** The first step's moment; every `after` is measured from here. */ starts_at: IsoDate; /** 1..365 steps, in any order -- they are sorted by `after` before expansion. */ steps: ScheduledMessageStep[]; } /** A sequence repeating one message on a fixed cadence. */ export interface CreateRepeatingScheduleInput extends ScheduledMessageBase { starts_at: IsoDate; repeat: ScheduleRepeat; content: MessageContent; context?: JsonObject; } /** Moves a `pending` occurrence. Anything else is a 409. */ export interface RescheduleMessageInput { scheduled_at: IsoDate; /** Re-reads `scheduled_at` in a different zone. Defaults to the one already stored. */ timezone?: string; title?: string; } export interface ListScheduledMessagesQuery extends ListQuery { /** Everything queued for one contact. */ contact?: ObjectId; /** One drip's occurrences. */ series?: ObjectId; status?: ScheduledMessageStatus; channel?: ObjectId; } /** What `cancelSeries` answers with. */ export interface CancelSeriesResult { series: ObjectId; /** How many `pending` occurrences were stood down. Already-sent ones are untouched. */ cancelled: number; } export {};