Payslip details for the employee app
Ruby · Rails · beginner · modification
Adds the payslip details endpoint the employee app's payslip screen calls. The routes gain `show` next to the existing `index` under `namespace :api`, and `Api::PayslipsController#show` loads the payslip by its id and renders the list's summary fields plus the period and the gross, tax and pension amounts; an id with no payslip raises `ActiveRecord::RecordNotFound`, which Rails answers with 404. `payslips_controller_test.rb` adds integration tests for the show action next to the existing index tests.
Ruby 4.0, Rails 8.1 API endpoints (SQLite in development and test, PostgreSQL 16 in production; nothing here depends on the database). `Api::BaseController` (existing, unchanged) inherits `ActionController::API`, reads the `Authorization: Bearer <token>` header with `authenticate_with_http_token`, sets `Current.user` (an `ActiveSupport::CurrentAttributes` attribute) to the `User` whose `api_token` matches and answers 401 otherwise. `User has_many :payslips`; `Payslip belongs_to :user` and has `id` (integer, sequential), `user_id`, `period_start`, `period_end` and `pay_date` (dates) and `gross_cents`, `tax_cents`, `pension_cents` and `net_cents` (integers), all `NOT NULL`. Payslips are written by the existing payroll import and never edited through this API. Every employee may see all of their own payslips and nobody else's; payroll staff use a separate admin app, and this API has no roles. In production `ActiveRecord::RecordNotFound` is answered with Rails' default 404 response. Tests are Minitest integration tests with inline records (`auth_headers(user)` and `create_payslip(user, pay_date)` are helpers in the existing test file). The payslip screen itself ships separately in the employee app.
Requirements
- `GET /api/payslips/:id` (new) returns one payslip for the employee app's payslip screen: its `id`, `pay_date`, `net_cents`, `period_start`, `period_end`, `gross_cents`, `tax_cents` and `pension_cents`, every amount in integer cents as stored and every date as `YYYY-MM-DD`.
- An employee sees only their own payslips, as on the existing list: a payslip that belongs to another employee answers 404, exactly like an id that does not exist, so the API does not reveal which payslip ids exist.
- Unchanged: every `/api` request needs a valid bearer token (401 otherwise), and `GET /api/payslips` keeps its behaviour (the signed-in employee's 24 newest payslips, newest pay date first, as `id`, `pay_date` and `net_cents`).
Files touched
- config/routes.rb
- app/controllers/api/payslips_controller.rb
- test/controllers/api/payslips_controller_test.rb