Skip to content

Visitor incident drafts

Status: internal backend with an authenticated Visitor staff interface. A draft belongs to Visitor and records a message, selected Visitor locations, creator and timestamps. It does not activate an incident, select recipients, create a simulation snapshot or dispatch, send an SMS or schedule a job. The development prototype remains separate and uses only fictional data.

The caller authenticates the user, rechecks membership and establishes TenantContext for that tenant before calling Visitor::IncidentDrafts. The service checks the active tenant context and reloads membership to verify an active admin or manager role for each operation. member is not allowed. PostgreSQL RLS and composite foreign keys separately enforce the tenant boundary for incidents and selected Visitor locations. Per-location permissions and MFA are not implemented; establish those requirements before production use.

After authentication and authorization, and only inside the tenant context:

# The example IDs come from previously authorized Visitor locations.
draft = TenantContext.with(membership.tenant_id) do
Visitor::IncidentDrafts.create(
membership: membership,
message: "Example message",
location_ids: authorized_location_ids
)
end
# A subsequent authenticated request must recheck membership first.
updated = TenantContext.with(membership.tenant_id) do
Visitor::IncidentDrafts.update(
membership: membership,
id: draft.id,
message: "Updated example message",
location_ids: authorized_location_ids,
lock_version: draft.lock_version
)
end
read_back = TenantContext.with(membership.tenant_id) do
Visitor::IncidentDrafts.find(membership: membership, id: updated.id)
end

A result includes id, status, message, location_ids, created_by_id, created_at, updated_at and lock_version. The message and location list may be empty because the record is a draft; a future activation or sending flow must validate its own requirements. Messages are limited to 1,000 characters. IDs are positive integers; repeated locations are collapsed and returned in ID order. A missing, foreign-tenant or non-Visitor location causes the entire write to fail with ActiveRecord::RecordNotFound. Invalid arguments raise ArgumentError; unauthorized or mismatched context raises SecurityError.

update requires the last observed lock_version. Concurrent or stale writes raise ActiveRecord::StaleObjectError; callers should reload and let the user resolve the conflict instead of silently discarding either edit. Location-only changes increment the version as well. A short row lock on reads prevents the message and location list from being assembled across updates made through this service; writers must follow the same locking protocol.

The caller’s outer TenantContext owns the transaction. An object returned inside that block is not confirmation of a committed write. Only report success after the block has returned successfully; if commit cannot be confirmed, follow status outcomes. Create/update use a savepoint so message and location associations roll back together on failure.

The staff interface lists up to 25 drafts per page and provides explicit create and edit forms for managers and admins. Conflicting edits display the submitted and saved text and location choices; the user must preserve the unsaved text before opening the saved draft. A failed or unconfirmed write is not reported as success. HTTP caching and the Turbo snapshot cache are disabled for these pages.

Draft message content can be sensitive; do not log service results or include real incident details in tests. No retention, deletion, public API, draft-creation idempotency, autosave or production permission model is implemented. Status is currently constrained to draft; later lifecycle states require an explicit migration and product decisions.

The confirmation button creates a simulation, never a real dispatch or an SMS job. Visitor locks the draft and the simulation uses Core to reselect active links in one SQL statement at confirmation time. In the same tenant transaction it stores a snapshot of the message, resource names, selected links and contact assessments, including excluded entries and normalization versions, together with the per_destination grouping. Empty messages, empty selections and zero valid destinations are rejected. Invalid numbers do not prevent valid numbers from being included. The confirmation view reports the saved result, not the earlier preview. A person arriving after the selection is not added automatically.

One confirmation is allowed per draft. A repeat request returns its existing snapshot; the draft can no longer be changed after confirmation. A database failure with an unknown commit outcome is reported as unconfirmed. Snapshot records include personal contact data; retention and deletion are undecided. There is no delivery queue, external provider call, or claim of delivery. This simulation is not a production dispatch API or an implementation of a durable outbox.

Changes to the saved message or selected locations after review require a new review; changes in visitor presence alone never do.

An explicit queue_test: true confirmation option is available only in the Rails test environment. It creates a simulation snapshot, links one test-only handoff per normalized destination and records the confirmation within the same outer tenant transaction. The snapshot preserves all selected links and excluded invalid entries. Repeated confirmation reuses the result; an already confirmed ordinary simulation cannot be converted into queued work. The test handoff is processed exclusively by the fake adapter. This option is not exposed by the UI and does not authorize or send a real SMS. The development database does not need the test link table until its migration is deliberately applied; no real worker, provider or delivery report is implemented here.