Upcoming events for the organisers' dashboard
Ruby · Rails · intermediate · modification
Adds the upcoming events list for the organisers' team dashboard. `config/routes.rb` routes `GET /upcoming_events` to the new `UpcomingEventsController#index`, which reads the account's events through a new `Event.upcoming` scope (`app/models/event.rb`: events starting now or later, in start order) and renders each one with its host's name and its count of `going` RSVPs. `test/controllers/upcoming_events_controller_test.rb` adds integration tests for the list and for 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 on a managed instance in the same region; nothing in this PR depends on which one. `config.time_zone` is the default UTC. `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` (one users query during authentication, then one accounts query the first time `Current.account` is read). Models (existing, unchanged apart from the new scope): `Account` `has_many :users, :events`; `User` (`name`, `api_token`) `belongs_to :account`; `Event` (`title`, `starts_at`, both `NOT NULL`) `belongs_to :account` and `belongs_to :host, class_name: "User"` with `host_id NOT NULL` and a foreign key to `users`, `has_many :rsvps`; a host is always a user of the event's account. `Rsvp` `belongs_to :event, :user` with `enum :status, { going: 0, maybe: 1, declined: 2 }` (`NOT NULL`), one RSVP per user and event (unique index on `[event_id, user_id]`). `events` has an index on `[account_id, starts_at]`. Rails' per-request query cache is on (the default). The dashboard itself ships separately in the web app.
Requirements
- `GET /upcoming_events` (new) returns the signed-in user's account's upcoming events as a JSON array of `{ "id", "title", "starts_at", "host_name", "going_count" }`. An event is upcoming when its `starts_at` is at or after the current time; events of other accounts are never included. Rows are ordered by `starts_at`, ties by `id`; an account with no upcoming events gets `[]`.
- `host_name` is the name of the event's host. `going_count` is the number of the event's RSVPs whose status is `going` (`maybe` and `declined` do not count); an event with no such RSVPs gets `0`. `starts_at` is written as Rails' JSON encoding writes a UTC time (ISO 8601, for example `2026-11-03T12:00:00.000Z`).
- Scale: an account has up to 300 upcoming events, hosted by up to 300 different users, and the endpoint returns all of them (no pagination). It backs the organisers' team dashboard, which every organiser keeps open and which reloads it every minute, so after authentication and the account lookup (`Current.account`), the action must run a fixed number of further SQL queries (at most 3), however many events the account has.
- Unchanged: every request needs a valid API token (`Authorization: Bearer ...`, 401 otherwise), and every user of an account may see all of its events. `GET /events/:id` keeps its behaviour.
Files touched
- config/routes.rb
- app/models/event.rb
- app/controllers/upcoming_events_controller.rb
- test/controllers/upcoming_events_controller_test.rb