Let volunteers sign up for food bank shifts in the app
Ruby · Rails · intermediate · modification
Volunteers can now pick a shift in the mobile app instead of emailing the coordinators. `config/routes.rb` nests a singular `signup` resource under `shifts`, so `POST /api/shifts/:shift_id/signup` reaches the new `Api::SignupsController#create`. The rules live in a new `Shift#sign_up!`, which raises `Shift::Closed`, `Shift::AlreadySignedUp` or `Shift::Full` and otherwise creates the sign-up; the controller turns each of those into its 409 body, and also answers "already signed up" if the unique index on sign-ups rejects the insert. `shift_signups_test.rb` adds integration tests for the endpoint's responses.
Ruby 4.0, Rails 8.1 (`config.load_defaults 8.1`, default UTC time zone, so `starts_at` is a UTC instant and encodes as ISO 8601 in UTC). SQLite in development, test and production: the app runs the Rails 8 default SQLite setup with `timeout: 5000` in `database.yml`, on one server, as Puma with 2 worker processes of 3 threads each, all on the same database file. Tests are Minitest integration tests (`ActionDispatch::IntegrationTest`). `Api::BaseController` (existing, unchanged) is an `ActionController::API` (no CSRF, no cookies): a `before_action` reads the `Authorization: Bearer <token>` header, sets `Current.user` to the `User` with that `api_token` and answers 401 otherwise. `Current` is an `ActiveSupport::CurrentAttributes` with `attribute :user`. The schema comes from earlier migrations of this feature: `shifts` (`title`, `starts_at` and `ends_at` datetimes, `spots` integer NOT NULL with a check constraint 1 to 30), `signups` (`shift_id` and `user_id`, both NOT NULL with foreign keys, a unique index on `[shift_id, user_id]`), and `users` (`name`, `api_token` from `has_secure_token`). `Signup` (`belongs_to :shift`, `belongs_to :user`) and `User` (`has_many :signups`) are unchanged. The food bank has one location and every volunteer may join any shift. Coordinators publish shifts in the admin tool and do not change `spots` or delete a shift after publishing; they can remove a sign-up there, and this endpoint is the only code that creates sign-ups. Next month's shifts are published on the last Tuesday of the month at 18:00, and the Saturday shifts before the holidays usually fill within minutes. `ActiveRecord::RecordNotFound` answers 404 (Rails' default exception mapping). The app screen ships separately.
Requirements
- New endpoint `POST /api/shifts/:shift_id/signup` (no body) signs the signed-in volunteer up for that shift and answers 201 with `{ "id", "shift_id", "starts_at" }`, where `id` is the new sign-up's id.
- A shift takes at most `spots` volunteers, the number of people the warehouse can work with on that shift. A sign-up that would go over it answers 409 `{ "error": "shift full" }`, and a shift never holds more sign-ups than its `spots`.
- A shift that has started (`starts_at` at or before the current time) answers 409 `{ "error": "shift closed" }`. A volunteer who is already on the shift answers 409 `{ "error": "already signed up" }`. When several of these apply, the first one in this order wins: closed, already signed up, full. An unknown shift id answers 404.
- Unchanged: every `/api` endpoint requires a volunteer's API token (401 otherwise), and `GET /api/shifts` keeps listing the upcoming shifts with `spots_left`.
Files touched
- config/routes.rb
- app/models/shift.rb
- app/controllers/api/signups_controller.rb
- test/integration/shift_signups_test.rb