Ticket summary for the support dashboard
Ruby · Rails · intermediate · modification
Adds the ticket summary behind the support dashboard's header counters. `config/routes.rb` routes `GET /ticket_summary` to the new `TicketSummariesController#show`, which accepts a 7- or 30-day period, counts the account's opened, solved and unsolved tickets and keeps the result in `Rails.cache` for ten minutes, since the dashboard polls it every minute. `app/models/ticket.rb` gains two scopes, `opened_since` and `solved_since`, for the period counts (`not_solved` comes from the existing enum). `test/controllers/ticket_summaries_controller_test.rb` adds integration tests for the default and 30-day counts, an unsupported period and the missing-token case.
Rails 8.1 API app on Ruby 4.0, Minitest integration tests with inline records (`ActiveSupport::Testing::TimeHelpers` available). SQLite in development and test, PostgreSQL 16 in production; nothing in this PR depends on which one. `config.time_zone` is the default UTC. The production `config.cache_store` is Solid Cache, one store shared by every app server and worker; the test environment keeps the default `:null_store`. The app hosts many customer accounts (a multi-tenant helpdesk). `ApplicationController` (existing, unchanged) is an `ActionController::API` that includes the `Authentication` concern: a `before_action` looks up `User.find_by(api_token:)` from the bearer token, answers 401 itself when there is none, and sets `Current.user`; `Current.account` delegates to `Current.user.account`. Models (existing, unchanged apart from the new scopes): `Account` `has_many :users, :tickets`; `User` (`name`, `api_token`) `belongs_to :account`; `Ticket` (`subject` `NOT NULL`, `status` `NOT NULL DEFAULT 0` (`open`), nullable `solved_at`) `belongs_to :account` with `enum :status, { open: 0, pending: 1, solved: 2 }`; the existing ticket workflow sets `solved_at` when a ticket is solved and clears it when the ticket is reopened. `tickets` has indexes on `[account_id, created_at]` and `[account_id, solved_at]`. The dashboard itself ships separately in the web app.
Requirements
- `GET /ticket_summary` (new) returns the signed-in user's account's ticket summary as JSON `{ "days", "opened", "solved", "unsolved" }`. The optional `days` query parameter picks the period and must be exactly `7` or `30` (default `7`); any other value answers 400.
- `opened` counts the account's tickets created at or after the current time minus `days` days; `solved` counts the account's tickets whose `solved_at` is at or after that instant; `unsolved` counts the account's tickets whose status is not `solved`, whatever their age. Tickets of other accounts are never counted, and every number an account sees must come from its own tickets only.
- The support dashboard polls this endpoint every minute for every signed-in agent, so a computed summary may be served from `Rails.cache` for up to 10 minutes: an account may see its own numbers up to 10 minutes old.
- Unchanged: every request needs a valid API token (`Authorization: Bearer ...`, 401 otherwise), and every user of an account may see all of its tickets. `GET /tickets/:id` keeps its behaviour.
Files touched
- config/routes.rb
- app/models/ticket.rb
- app/controllers/ticket_summaries_controller.rb
- test/controllers/ticket_summaries_controller_test.rb