Team-shared saved replies for the support desk
Ruby · Rails · intermediate · modification
Lets support agents share saved replies with their team. A migration adds a `shared` boolean column to `saved_replies` (`NOT NULL`, default `false`), and `SavedReply` validates that it is `true` or `false`, so a `null` from the client is a 422 rather than a database error. In `Api::SavedRepliesController`, `index` now lists the agent's own replies together with the shared ones, newest first, and each item gains `shared` and `mine` so the ticket composer can label them; `shared` is added to the permitted parameters of `create` and `update`, which keep looking replies up as before. `saved_replies_controller_test.rb` extends the integration tests for sharing, unsharing and the `null` flag.
Ruby 4.0, Rails 8.1 JSON API (SQLite in development and test, PostgreSQL 16 in production; nothing here depends on the database: the query's `WHERE` clause is built by Active Record). `Api::BaseController` (existing, unchanged) inherits `ActionController::API`, reads the `Authorization: Bearer <token>` header with `authenticate_with_http_token`, answers 401 when no `User` has that `api_token`, and otherwise sets `Current.user` and `Current.account` (`ActiveSupport::CurrentAttributes`) to the agent and the agent's account. Every `User` belongs to exactly one `Account` and never moves. `Account has_many :saved_replies`; `User has_many :saved_replies` through `author_id`; `SavedReply belongs_to :account` and `belongs_to :author` (a `User`), with `title` (string) and `body` (text), all `NOT NULL` with foreign keys. A reply's account is always its author's account: `create` builds it on `Current.account` and neither action lets a client change `account_id` or `author_id`. An account has at most 300 saved replies, so the list is not paginated. Clients send `shared` as a JSON boolean. In production `ActiveRecord::RecordNotFound` gets Rails' default 404 response. Tests are Minitest integration tests with inline records (`auth_headers(user)` and `create_reply(author, title, **attrs)` are helpers in the existing test file). The ticket composer's UI ships separately.
Requirements
- Saved replies are canned answers a support agent inserts into a ticket. Until now each agent saw only their own. An agent can now share a reply with their team: a saved reply gains a `shared` flag (boolean, `false` for new replies unless the request sets it).
- `GET /api/saved_replies` (existing) lists the replies the signed-in agent can use: every reply they wrote themselves, shared or not, plus the replies other agents of the same account have shared. A teammate's reply that is not shared is not listed.
- An account's saved replies are confidential to that account (they quote customer names, internal links and refund policies): replies of another account are never listed, shared or not.
- Each listed item is `{ "id", "title", "body", "shared", "mine" }`, where `mine` is `true` for a reply the caller wrote. Items are ordered newest first (highest `id` first).
- `POST /api/saved_replies` and `PATCH /api/saved_replies/:id` (existing) accept `shared` next to `title` and `body`. A `null` `shared` answers 422 with the errors, like a blank title.
- Unchanged: only the author can edit a reply, so `PATCH` on anyone else's reply answers 404 like an unknown id; every `/api` request without a valid bearer token answers 401.
Files touched
- db/migrate/20261103090000_add_shared_to_saved_replies.rb
- app/models/saved_reply.rb
- app/controllers/api/saved_replies_controller.rb
- test/controllers/api/saved_replies_controller_test.rb