Delivery status callbacks from the SMS provider

Ruby · Rails · intermediate · modification

Adds the endpoint the SMS provider calls when an appointment reminder is delivered or fails. `config/routes.rb` gains `resource :sms_status, only: :create` under `namespace :webhooks`. `Webhooks::SmsStatusesController` is an `ActionController::API` controller: a `before_action` checks the provider's bearer token against the configured webhook secret with `secure_compare` (401 otherwise), and `create` logs the callback, rejects a status that is not final with 422, finds the message by the provider's id (`find_by!`, so an unknown id is a 404) and stores the status, the error code and the update time. `sms_statuses_controller_test.rb` adds integration tests for delivered and failed callbacks, a wrong or missing token, an unknown message id and a non-final status.

Ruby 4.0, Rails 8.1 (SQLite in development and test, PostgreSQL 16 in production; nothing here depends on the database). `SmsMessage` (existing, unchanged) has `provider_message_id` (string, `NOT NULL`, unique index), `to_number`, `body`, `status` (string, `NOT NULL`, default `queued`, validated to be `queued`, `delivered` or `failed`), `error_code` (nullable string) and `status_updated_at`; the existing `SendReminderJob` (not shown) creates it with the id the provider returns. `config/initializers/sms_provider.rb` (existing, unchanged) sets `Rails.configuration.x.sms_provider.webhook_secret` from the `SMS_WEBHOOK_SECRET` environment variable in production (set and non-empty there) and to a fixed value in development and test. The provider sends one final callback per message (`delivered` or `failed`) and re-sends the same body until it gets a 2xx, so applying a callback twice stores the same status and error code (the update time moves to the retry); it sends no other fields. `ActionController::API` has no CSRF protection, which is right for a server-to-server callback. Production (`config/environments/production.rb`) logs to STDOUT at `info` level with `log_tags = [:request_id]`, and the log collector ships STDOUT to the log platform. `config/initializers/filter_parameter_logging.rb` is the Rails 8.1 default (`:passw, :email, :secret, :token, :_key`, ...). In production `ActiveRecord::RecordNotFound` answers 404 and `ActionController::ParameterMissing` answers 400 through Rails' default exception handling. Tests are Minitest integration tests with inline records.

Requirements

Files touched

Review this PR

Ruby practice