Skip to content

Contact destinations: first internal slice

Status: additive, internal Rails API. A destination belongs to one tenant and contains a channel identifier, original value, normalized value, normalization version and active/inactive status. Recipients and destinations have a many-to-many relation; a destination can be linked to more than one recipient, and a recipient can have several destinations. Identical values can exist in separate records: no global deduplication or ownership equivalence is assumed. A later dispatch chooses its own deduplication policy.

The caller authenticates and authorizes the operation and each resource, then establishes TenantContext for the same tenant. Core checks that context; it does not determine product permissions. RLS and composite foreign keys enforce isolation independently. Returns inside TenantContext.with are provisional until the surrounding transaction commits. Avoid logging contact values.

# `tenant_id` and `recipient_id` are already authorized by the caller.
TenantContext.with(tenant_id) do
destination = Core::ContactDestinations.create!(
tenant_id: tenant_id,
channel: "email",
original_value: "[email protected]",
normalizer: Channels::Email::AddressNormalizer
)
Core::ContactDestinations.link!(tenant_id: tenant_id,
recipient_id: recipient_id, destination_id: destination.id)
end

The normalizer is supplied by the caller, not selected by Core. Its return type is Core::ContactDestinations::Assessment, containing normalized_value and version. The email implementation accepts a deliberately conservative ASCII subset, lowercases the domain, preserves the local part and rejects malformed inputs. It does not prove a mailbox exists, belongs to a person or is reachable; it is not a complete implementation of every valid international email format. Normalizers must handle their own channel’s semantics. Invalid input raises ArgumentError; Core does not store a destination for that call.

for_recipient returns linked destinations including inactive ones. An explicitly authorized caller can use set_status! to activate or deactivate a destination; the destination may be shared, so the change affects all linked recipients. Status does not yet drive any dispatch. link! is idempotent for a particular tenant/recipient/destination tuple. It does not infer a shared identity from equal values. No automatic migration or synchronization with legacy phone fields exists.

The generic Core::Recipients.create! can create a recipient without a phone number. Current Visitor check-in still requires a phone number, and its existing selection and simulation still use the legacy phone fields, not these destination records. There is no email dispatch, delivery snapshot, adapter transport, duplicate-policy selection, UI, public API, deletion, retention policy or data backfill. These require separate design and work.

Core::DestinationSelection.call(tenant_id:, recipient_ids:, channel:) accepts an explicit list of 1–1,000 already-authorized recipient IDs and a channel identifier. It returns one entry per distinct recipient, ordered by ID; each entry contains that recipient’s active destinations for that channel. An empty destination list (missing?) means no active destination for the selected channel; inactive destinations are not returned. It does not distinguish an inactive record from a recipient who never had that channel configured. A shared destination appears in each linked recipient’s result, and duplicate recipient IDs are collapsed. It never groups deliveries or determines a duplicate policy.

The caller authenticates and authorizes the tenant and all recipients, then establishes TenantContext. Missing or foreign recipient IDs reject the entire selection with ActiveRecord::RecordNotFound. A single SQL statement validates the recipient set and reads the active destinations from the same database snapshot. The result is a point-in-time read, not a reservation, revalidation, mailbox reachability check or frozen dispatch snapshot. Destination values and names are sensitive; do not log them. Channel validation, further product rules, authorization and final dispatch behavior remain separate responsibilities.

The migration allowing null legacy phone fields is deliberately irreversible: if rollback is needed, an operator must first decide how to handle recipients that have no phone. It must not invent or silently remove data.