Vetspire. Reminders. Autoconfirm
(vetspire v3.0.0)
View Source
Sends the nightly appointment-confirmation texts and resolves the replies they get back.
Only one request per (phone, org_id) stays live at a time: creating a new
request supersedes any row still pending for that pair, so an inbound
reply always resolves against the newest one. Requests for different orgs on
the same phone coexist, and a reply resolves all of them.
A confirm, cancel or reschedule reply closes the request. A free-text reply
gets the no_match text and leaves the request pending, so a Y or N
after it still resolves it. The expiry sweep closes a request that never
gets a matching reply.
Summary
Functions
Marks any still-pending request for the same (phone, org_id) as
superseded, then inserts the new pending request.
Deletes resolved requests older than the retention window, and reports how many rows went.
Expires every request whose wait window has closed, and sends each one the lapsed body.
Resolves an inbound reply against every pending request for one phone, and performs every side effect: confirming or cancelling the request's appointments, and sending the matching auto-reply. Free text that matches no token gets the same reply as a reply from an unknown phone.
All pending, unexpired requests for one phone, newest first.
Moves a request to new_status, but only while it is still pending.
Sends the initial confirmation text for a client's appointments.
Functions
@spec create_request(map()) :: {:ok, Vetspire.Reminders.Autoconfirm.Request.t()} | {:error, Ecto.Changeset.t()}
Marks any still-pending request for the same (phone, org_id) as
superseded, then inserts the new pending request.
The supersede runs first because a partial unique index allows one
pending row per pair. Both statements share one transaction, so a failed
insert puts the older row back.
The phone is normalized here, so no caller can insert a row the inbound lookup cannot find.
@spec delete_expired_requests() :: non_neg_integer()
Deletes resolved requests older than the retention window, and reports how many rows went.
A request is dead within a day of its creation: it holds no history anyone
reads, only the state one confirmation text needed while it waited for a
reply. The window keeps enough rows to answer "what did we text this client
last month" during a support call. A row still pending is never deleted,
however old it is, because the expiry sweep still owes it a lapsed text.
@spec expire_lapsed_requests(pos_integer()) :: :ok
Expires every request whose wait window has closed, and sends each one the lapsed body.
Safe to run at any time and as often as wanted. Only a row still pending
moves, so a request answered between the read and the update keeps the status
its reply gave it, and a row that never got its initial text is not pending
and stays untouched. When nothing has lapsed this costs one indexed query.
The lapsed text itself is best effort: a send that Twilio rejects is logged
and not retried, and the row stays expired.
One sweep sends at most limit texts. The sends are sequential message
creates, so they cannot trip Twilio's concurrent-request limit, but a whole
night of unanswered requests lapses inside the same hour and every one of
those texts would otherwise land in the number's outbound queue at once. The
cap drains that backlog over the following sweeps instead, oldest first.
@spec handle_reply(String.t(), term()) :: Vetspire.Subscriptions.MessagingUsage.Metering.t()
Resolves an inbound reply against every pending request for one phone, and performs every side effect: confirming or cancelling the request's appointments, and sending the matching auto-reply. Free text that matches no token gets the same reply as a reply from an unknown phone.
Returns the billing context to attribute the inbound usage event to. The newest pending request supplies the org and location, and a phone matching no pending request meters against a nil org.
A phone with no pending request gets the no_match reply only when it belongs to a client. Anyone can text a public toll-free number, and a reply to every stray text is billable outbound that one sender could drive without limit.
Twilio's keywords (START, UNSTOP, HELP, STOP and the other opt-out
words) get no reply and resolve nothing. Twilio has already answered them
itself.
@spec pending_requests_for_phone(String.t()) :: [ Vetspire.Reminders.Autoconfirm.Request.t() ]
All pending, unexpired requests for one phone, newest first.
Callers that pick one request out of the list take the newest one, so the order matters.
Twilio sends From in E.164 and phone_numbers.value holds whatever a
member of staff typed, so both sides go through
PhoneNumber.sanitise_phone_number/1 before they are compared.
@spec resolve_request( Vetspire.Reminders.Autoconfirm.Request.t(), Vetspire.Reminders.Autoconfirm.Request.status() ) :: {:ok, Vetspire.Reminders.Autoconfirm.Request.t()} | {:error, :already_resolved}
Moves a request to new_status, but only while it is still pending.
Returns {:ok, request} when this call won the transition, or
{:error, :already_resolved} when the row had already moved on (resolved,
expired, or superseded by a newer request).
@spec send_confirmation_request(Vetspire.Accounts.Client.t(), [ Vetspire.Schedule.Appointment.t() ]) :: {:ok, [Vetspire.Events.Event.t()]} | {:error, term()} | nil
Sends the initial confirmation text for a client's appointments.
The text goes to the first phone from Clients.sendable_phone/1. A client
with no such phone sends nothing and writes no row. The row lands before the
text goes out, so a reply always finds a row to resolve. The
appointment.sent_autoconfirm events, one per appointment, are written last
and only for a text that went out. expire_lapsed_requests/0 closes the row
out later if nobody replies.
Every appointment must carry a location: the body needs each one's timezone,
and the first one's location names the clinic. run_autoconfirm filters
location-less appointments out of its query, so none reach this function.
Returns nil for a client with no phone that can take an SMS, and for a call
that matches neither clause's shape.