Move budget between cost centres
Database · PostgreSQL · intermediate · modification
Finance admins can now move budget from one cost centre to another, for example when marketing hands part of its quarter to sales. `POST /api/budget-transfers` validates the request (positive amount up to 10^10 cents, two different cost centres) and calls the new `budget.transfer`, which does the whole move in one transaction: it locks both cost centre rows of the admin's company in id order, debits the source with a conditional `UPDATE` that only applies when the budget covers the amount, credits the destination and inserts the `budget_transfers` row. A missing cost centre or a short budget raises before anything is kept, and the route maps them to 404 and 409. Migration `0058_budget_transfers.sql` adds the `budget_transfers` table. Includes tests in `tests/test_budget_transfers.py`.
Spend-management API: FastAPI 0.115 on Python 3.12 with psycopg 3 (plain SQL, no ORM). `app/db.py` holds a `psycopg_pool.ConnectionPool` (at most 10 connections per pod; `pool.connection()` commits on a clean exit and rolls back on an exception), and the API runs as 4 pods against one PostgreSQL 16 primary at the default isolation level, READ COMMITTED. Tables from earlier migrations: `companies` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `name text NOT NULL`, `created_at timestamptz NOT NULL DEFAULT now()`); `users` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `company_id bigint NOT NULL REFERENCES companies (id)`, `email text NOT NULL UNIQUE`, `created_at timestamptz NOT NULL DEFAULT now()`); users are never deleted and their ids never change (a leaver is deactivated in the sign-in provider instead); `cost_centres` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `company_id bigint NOT NULL REFERENCES companies (id)` with an index, `name text NOT NULL`, `budget_cents bigint NOT NULL DEFAULT 0 CHECK (budget_cents >= 0)`, `created_at timestamptz NOT NULL DEFAULT now()`). There are a few thousand cost centres; they are never deleted, and every budget stays below 10^12 cents. Two existing writers change budgets, each in its own short autocommit statement: card spend runs `UPDATE cost_centres SET budget_cents = budget_cents - %s WHERE id = %s AND budget_cents >= %s` and declines the payment when no row comes back, and the monthly top-up runs `UPDATE cost_centres SET budget_cents = budget_cents + %s WHERE id = %s`. No transaction on these tables stays open for long. `app.auth.current_finance_admin` (existing) returns `Admin(user_id, company_id)` for a signed-in finance admin, or answers 401 when signed out and 403 for any other user. `app/main.py` already includes the `cost_centres` router and opens the pool at startup. Schema changes are plain SQL files in `migrations/`, applied in name order by the existing runner at the start of each deploy, each file in one transaction. The test suite (`tests/conftest.py`, existing) builds a fresh test database from the migrations, empties it between tests, and provides `create_company(name="Acme")` (a company and one finance admin, returns `(company_id, user_id)`), `create_cost_centre(company_id, name, budget_cents=0)` (returns the id), `fetch_one(sql, params)` and `client_for(user_id, company_id)` (a test client signed in as that admin; the app, and with it the pool, runs for the whole session).
Requirements
- New endpoint `POST /api/budget-transfers` with `{"from_cost_centre_id", "to_cost_centre_id", "amount_cents"}` lets a finance admin move budget between two of their company's cost centres. On success the source's `budget_cents` goes down and the destination's goes up by exactly `amount_cents`, a `budget_transfers` row records the move (both cost centres, the amount and the admin who made it), and the answer is 201 `{"transfer_id", "from_budget_cents", "to_budget_cents"}` with both new budgets.
- `amount_cents` is a whole number from 1 to 10,000,000,000 and both ids are positive bigint values, otherwise 422; moving budget from a cost centre to itself answers 422 `cannot transfer to the same cost centre`. A cost centre that does not exist or belongs to another company answers 404 `cost centre not found`. If the source's budget is less than the amount, the answer is 409 `insufficient budget`. In all of these cases nothing changes.
- The budget rule: a cost centre's budget is never below zero, and every change to it is kept. Transfers, the existing card-spend writer and the existing monthly top-up can touch the same cost centres at the same time (two finance admins working at once, card payments all day), and the rule holds however they interleave: no transfer, spend or top-up is lost, and when two transfers out of one cost centre each fit its budget alone but not together, one succeeds and the other answers 409.
- Two transfers in opposite directions between the same two cost centres at the same time (A to B and B to A) both complete; neither answers an error because of the other.
- Unchanged: `GET /api/cost-centres` lists the admin's company's cost centres with their budgets.
Files touched
- migrations/0058_budget_transfers.sql
- app/budget.py
- app/cost_centres.py
- tests/test_budget_transfers.py