Weekly hours per project report
Database · PostgreSQL · intermediate · modification
Adds a weekly hours report for the reports page. `migrations/0014_project_hours_weekly.sql` (new) creates the view `project_hours_weekly`, which joins projects with their time entries and sums hours and counts entries per project per week, and grants `SELECT` on it to `app_api`. `app/reports.py` (new) adds `GET /api/reports/project-hours`, which checks that `week` is a Monday and reads that week's rows from the view on the request's tenant connection; `app/main.py` registers the new router. `tests/test_project_hours.py` (new) tests the endpoint against seeded entries.
Time-tracking app for agencies, about 600 tenants: PostgreSQL 16, Python 3.12, FastAPI 0.115, psycopg 3.2 with dict rows and psycopg_pool 3.2 (plain SQL, no ORM). Existing and unchanged schema (migration `0001`): `projects` (`id bigint GENERATED ALWAYS AS IDENTITY` primary key, `tenant_id bigint NOT NULL`, `name text NOT NULL`, `UNIQUE (tenant_id, id)`) and `time_entries` (`id bigint GENERATED ALWAYS AS IDENTITY` primary key, `tenant_id bigint NOT NULL`, `project_id bigint NOT NULL`, `user_id bigint NOT NULL`, `started_on date NOT NULL`, `hours numeric(4, 2) NOT NULL CHECK (hours > 0 AND hours <= 24)`, foreign key `(tenant_id, project_id)` referencing `projects (tenant_id, id)`, index on `(tenant_id, started_on)`; about 300,000 rows over all tenants; the report's query performance is out of scope for this PR). Both tables have `ENABLE ROW LEVEL SECURITY` and one policy each for all commands and roles, `USING` and `WITH CHECK` `tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::bigint`. They are deliberately not `FORCE`d: migrations and the nightly payroll and invoicing jobs run as the owner and must read every tenant. Roles: `app_owner` owns the database, the schema and every table, and is used only by the migration runner and the nightly jobs; the API's pool connects as `app_api` (`DATABASE_URL`; LOGIN, not a superuser, no BYPASSRLS, owns nothing), which has USAGE on the schema and SELECT, INSERT, UPDATE and DELETE on both tables. Tenant isolation in the API rests on the policies alone: by convention API queries carry no `tenant_id` condition (for example `GET /api/projects` in `app/projects.py`, unchanged, runs `SELECT id, name FROM projects ORDER BY name, id`). `app.db.get_tenant_conn` (unchanged) takes a pooled connection and runs `set_config('app.tenant_id', <tenant>, true)` for the request's transaction; `pool.connection()` commits the open transaction when the block ends (rolls it back on an exception) and returns the connection to the pool. `app.auth.current_tenant` answers 401 itself unless the request has a signed-in user and returns that user's tenant id, a positive `int`. Migrations are plain SQL files in `migrations/`, applied in order as `app_owner` by the existing runner, each file in its own transaction; a deploy stops the API, runs the pending migrations and then starts the new release. Tests run with pytest against a fresh test database built from `migrations/` as `app_owner`, with the API's pool connected as `app_api` like production; `tests/conftest.py` provides `db` (an `app_owner` connection, with both tables emptied before each test) and `client_for(tenant_id)` (a FastAPI `TestClient` signed in as a user of that tenant).
Requirements
- `GET /api/reports/project-hours?week=YYYY-MM-DD` (new) returns one row per project that has time logged in that week: `project_id`, `project_name`, `hours` (the sum of the week's entries as a decimal string with two places, e.g. `"7.50"`) and `entries` (how many entries were summed), ordered by project name, then id. Projects without entries that week are not listed; a week with no entries answers `[]`.
- Weeks start on Monday: `week` must be a Monday date, otherwise the endpoint answers 422. An entry belongs to the Monday-to-Sunday week that contains its `started_on` date.
- A user's report covers only their own tenant's projects and time entries, like every other read in the API. As everywhere in this API, that separation comes from the row-level security policies on `projects` and `time_entries`; the report's query carries no tenant condition of its own.
- Unchanged: the tables, their policies, the roles, and the nightly jobs that run as `app_owner` across all tenants. A request without a signed-in user answers 401.
Files touched
- migrations/0014_project_hours_weekly.sql
- app/reports.py
- app/main.py
- tests/test_project_hours.py