Skip to content

API Contract

agent edited this page Nov 25, 2026 · 248 revisions

API Contract

The API contract evolves incrementally as each epic is implemented. This page documents endpoint specifications, request/response shapes, and error patterns for all routes.

Current Endpoints

Health Check

GET /api/health

Auth required: No

Response (200 OK):

{
  "status": "ok",
  "timestamp": "2026-02-07T12:00:00.000Z"
}

GET /api/health is a liveness check only: it touches neither the database nor password hashing.

Readiness Check

GET /api/health/ready

Auth required: No

Response (200 OK):

{
  "status": "ready",
  "timestamp": "2026-02-07T12:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
429 RATE_LIMIT_EXCEEDED The first (uncached) self-check found the password-hashing queue full; the next probe retries
500 INTERNAL_ERROR The database query or the password-hashing self-check fails

Behavior:

  • Every call runs SELECT 1 against the database.
  • The password-hashing self-check (hash a fixed string with the current scrypt parameters, then verify it) runs once per process, on the first call, and only a success is cached. A failure clears the cache, so the next call retries it. Concurrent first calls share one self-check. One hash costs about 128 MiB (ADR-010, Amendments), which is why it is not repeated on every probe.
  • The Docker image's HEALTHCHECK calls this endpoint every 30 seconds (Dockerfile). The sample docker-compose.yml healthcheck calls GET /api/health instead.

Application Configuration

GET /api/config

Auth required: No

Returns deployment-level configuration that the client needs for rendering. This endpoint is public because the client needs configuration (e.g., display currency) before authentication.

Response (200 OK):

{
  "currency": "EUR",
  "vatRate": 0.19,
  "autoItemizeEnabled": false,
  "llmEnabled": false
}
Field Type Description
currency string ISO 4217 currency code configured via the CURRENCY environment variable.
vatRate number VAT/sales-tax rate as a fraction (e.g. 0.19 = 19%) configured via the VAT_RATE environment variable. Default: 0.19.
autoItemizeEnabled boolean true when LLM_BASE_URL, LLM_API_KEY, and LLM_MODEL are all set. Controls visibility of the auto-itemize UI.
llmEnabled boolean Same condition as autoItemizeEnabled, under a name that is not auto-itemize-specific. Story #1901.

Which flag to use. llmEnabled and autoItemizeEnabled are always equal — both mean "LLM_BASE_URL, LLM_API_KEY, and LLM_MODEL are all set". llmEnabled is the preferred field for any new LLM-backed feature; autoItemizeEnabled is retained only for the existing auto-itemize UI and should be treated as deprecated. New code must not introduce a third alias — if the two ever need to diverge (per-feature LLM toggles), that is a contract change requiring an ADR, not a new boolean.

Environment variables:

Variable Default Description
CURRENCY EUR ISO 4217 currency code for display formatting (e.g., EUR, USD, CHF).
VAT_RATE 0.19 VAT/sales-tax rate as a fraction for budget-line gross-up math (e.g., 0.19, 0.20).
LLM_BASE_URL — Base URL of an OpenAI-compatible LLM API (e.g., https://api.openai.com/v1). Required to enable auto-itemize.
LLM_API_KEY — Bearer token for the LLM provider. Required to enable auto-itemize.
LLM_MODEL — Model identifier (e.g., gpt-4o, claude-haiku-4-5, gemini-2.5-flash). Required to enable auto-itemize.
LLM_REQUEST_TIMEOUT_MS 30000 LLM request timeout in milliseconds.
LLM_MAX_TOKENS 16384 Maximum output tokens per LLM call. Increase if extraction returns LLM_INVALID_RESPONSE with finishReason: "length".
LLM_PROVIDER (auto) Provider hint: openai, anthropic, gemini, ollama, or generic. Auto-detected from LLM_BASE_URL when unset.

Notes:

  • This endpoint always returns 200. It cannot fail.
  • The currency value is set at deployment time and applies to all users in the instance.
  • The client fetches this once at startup and uses the value for all Intl.NumberFormat currency formatting.
  • autoItemizeEnabled is false by default. All three LLM env vars (LLM_BASE_URL, LLM_API_KEY, LLM_MODEL) must be set to enable auto-itemize. The LLM API key is never returned to the client; only the boolean flag is surfaced.
  • See ADR-031 for the full LLM integration design and security posture.

EPIC-01: Authentication & User Management

Authentication Flow Overview

Cornerstone supports two authentication methods:

  1. Local authentication -- Email/password login for the initial admin account (setup flow) and as a fallback
  2. OIDC authentication -- OpenID Connect sign-in for existing accounts. By default OIDC never creates accounts (opt-in just-in-time provisioning of member accounts via OIDC_JIT_PROVISIONING, default false; see ADR-035's #2142 amendment): a first OIDC login links the provider identity to the existing account whose email matches the provider's verified email claim (see GET /api/auth/oidc/callback and ADR-035)

All authenticated API requests use a server-side session. The session token is delivered as an HttpOnly cookie (cornerstone_session). There are no bearer tokens or API keys.

Session Cookie

Property Value
Name cornerstone_session
HttpOnly true
SameSite Strict
Secure Configurable via SECURE_COOKIES env var (default: true)
Path /
Max-Age Configurable via SESSION_DURATION env var (default: 604800 seconds = 7 days)

The cookie is set on successful login (local or OIDC) and cleared on logout.

Environment Variables (Auth)

Variable Default Description
SESSION_DURATION 604800 Session lifetime in seconds (default: 7 days)
SECURE_COOKIES true Set Secure flag on cookies; set to false for local dev without TLS
TRUST_PROXY false Trust X-Forwarded-* headers from a reverse proxy
AUTH_RATE_LIMIT_MAX 20 Login endpoint rate limit: max requests per IP per window (positive integer)
AUTH_RATE_LIMIT_WINDOW 15 minutes Login endpoint rate limit: time window (ms-library format, e.g. 15 minutes, 1h)
OIDC_ISSUER (none) OIDC provider issuer URL (e.g., https://auth.example.com/realms/main)
OIDC_CLIENT_ID (none) OIDC client ID
OIDC_CLIENT_SECRET (none) OIDC client secret
OIDC_JIT_PROVISIONING false Opt-in: create a member account when an OIDC sign-in matches no account
EXTERNAL_URL (none) Public base URL (e.g., https://cornerstone.example.com); OIDC callback origin

OIDC is enabled when the three variables OIDC_ISSUER, OIDC_CLIENT_ID, and OIDC_CLIENT_SECRET are set (server/src/plugins/config.ts, oidcEnabled). If any of the three is missing, GET /api/auth/oidc/login returns 404 OIDC_NOT_CONFIGURED, GET /api/auth/oidc/callback redirects to /login?error=oidc_not_configured, and POST /api/users with authProvider: "oidc" (an SSO-only account) returns 400 OIDC_NOT_CONFIGURED.

OIDC_JIT_PROVISIONING does not take part in enabling OIDC and has no effect while OIDC is disabled. It accepts true or false (case-insensitive); any other value fails startup. With true, an OIDC sign-in that resolves to no account creates a member account (see the callback's Behavior, step 8). Enable it only with a provider that restricts who can sign in: there is no domain or email allow-list, so with an open provider such as consumer Google anyone who finds the login URL can register.

The callback URL is not configured through a dedicated environment variable — there is no OIDC_REDIRECT_URI, and the server never reads one. The redirect URI sent to the provider is derived per request as <EXTERNAL_URL>/api/auth/oidc/callback, falling back to the incoming request's own origin (request.protocol + request.host) when EXTERNAL_URL is unset. Behind a reverse proxy, set EXTERNAL_URL: both halves of the fallback are gated on TRUST_PROXY — request.protocol reads X-Forwarded-Proto and request.host reads X-Forwarded-Host only when TRUST_PROXY=true, so with it unset the fallback yields the container's own internal scheme and host, not the public one. A correct scheme is therefore not sufficient to make the fallback safe. The redirect URI registered with the OIDC provider must match the derived value exactly.

Rate Limiting (Auth)

POST /api/auth/login is rate limited per client identity (see Rate-Limit Key below), configured via AUTH_RATE_LIMIT_MAX and AUTH_RATE_LIMIT_WINDOW. Exceeding the limit returns 429 with the standard error shape and code RATE_LIMIT_EXCEEDED, alongside the standard x-ratelimit-limit, x-ratelimit-remaining, x-ratelimit-reset, and retry-after response headers. Behind a reverse proxy, TRUST_PROXY=true is required for the limit to be keyed on the real client address rather than the proxy's.

Password-Hashing Queue

Separately from the login rate limit, every request that runs scrypt (ADR-010) goes through one in-process queue. That covers POST /api/auth/setup, POST /api/auth/login (including the dummy verification for unknown or password-less accounts), POST /api/users/me/password, POST /api/users for a local account, and the first GET /api/health/ready. At most two derivations run at once. When 50 callers are already waiting, a further request is rejected immediately with 429 RATE_LIMIT_EXCEEDED and the message Too many requests. Please try again shortly..

  • The body is the same for every account, so it reveals nothing about which accounts exist.
  • No rate-limit headers are sent, because this is a capacity limit, not a per-client quota.
  • On login, a wrong password is recorded against the account before the extra dummy verification for legacy hashes runs, so a full queue cannot be used to skip the lockout counter.
  • The best-effort rehash after a successful login swallows this error like any other, and the login still succeeds.

Rate-Limit Key

The key is not the raw client IP. It is request.ip passed through @fastify/rate-limit's normalizeIP:

Client address form Key
IPv4 (203.0.113.7) the address itself, lowercased — one bucket per address
IPv4-mapped IPv6 (::ffff:203.0.113.7) unwrapped to the dotted IPv4 form (203.0.113.7)
Any other IPv6 (2001:db8:abcd:12::1) the /64 network prefix in canonical form (2001:db8:abcd:12::)

The /64 truncation means every address within a client's IPv6 prefix shares a single bucket. This is required, not incidental: residential and hosting IPv6 allocations hand out a /64 (or larger) to a single subscriber, so a per-address key lets an attacker rotate through 2^64 addresses and defeat the limit entirely. A keyGenerator returning request.ip verbatim reintroduces exactly that bypass — this was CVE-2026-15144 (issue #1995).

The invariant: the rate-limit key must always be normalizeIP-normalized, at the same subnet width the plugin is configured with. rateLimitPlugin satisfies this with an explicit, exported key generator rather than the library's default:

const IPV6_SUBNET = 64;

export function rateLimitKeyGenerator(request: Pick<FastifyRequest, 'ip'>): string {
  return normalizeIP(request.ip ?? 'unknown', IPV6_SUBNET);
}

The same IPV6_SUBNET constant is passed to normalizeIP and to the plugin's own ipv6Subnet option, so the two cannot drift. Two hazards make this explicit generator necessary rather than merely equivalent to the library default:

  • request.ip is typed string but is genuinely null when the socket carries no address metadata. normalizeIP dereferences its argument (ip.toLowerCase()), so a generator that forwards request.ip unguarded throws and turns every rate-limited endpoint into a 500. The ?? 'unknown' fallback puts such requests in one shared bucket instead.
  • @fastify/rate-limit gates its own normalization on generator identity. It applies normalizeIP only when keyGenerator is the exact defaultKeyGenerator reference; every other generator is invoked as keyGenerator(req) with no subnet argument and no normalization wrapper. So any custom generator — including one that just wraps the default — silently loses the /64 truncation unless it calls normalizeIP itself. That gate is what made CVE-2026-15144 possible.

Invariants any change to rateLimitKeyGenerator MUST preserve:

  • Call normalizeIP on every return path. Returning request.ip verbatim, or falling through to a raw address on a guard/error path, is exactly CVE-2026-15144.
  • Pass the subnet width explicitly, from the same constant given to ipv6Subnet. Relying on normalizeIP's own parameter default makes the ipv6Subnet option silently ineffective and lets the two values drift apart.
  • Keep the null guard. request.ip's string type is a lie at runtime; removing the fallback reintroduces the 500.
  • Never parse X-Forwarded-For / X-Real-IP by hand. Redundant — TRUST_PROXY already makes request.ip reflect those headers — and such code typically bypasses normalizeIP on the fallback path, reintroducing the bypass for proxied requests only, which is harder to spot.

POST /api/auth/setup has a separate, fixed limit of 5 requests per 15 minutes (not configurable).


Auth Endpoints

GET /api/auth/me

Returns the current authentication state. This is the first endpoint the client calls on app load to determine what UI to show.

Auth required: No (returns different shapes based on auth state)

Response (200 OK -- authenticated):

{
  "user": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "email": "admin@example.com",
    "displayName": "Admin User",
    "role": "admin",
    "authProvider": "local",
    "oidcLinked": false,
    "createdAt": "2026-02-08T10:00:00.000Z"
  },
  "oidcEnabled": true
}

Response (200 OK -- not authenticated, setup required):

{
  "user": null,
  "setupRequired": true,
  "oidcEnabled": false
}

Response (200 OK -- not authenticated, setup complete):

{
  "user": null,
  "setupRequired": false,
  "oidcEnabled": true
}

Notes:

  • This endpoint never returns 401. It always returns 200 with the current state.
  • setupRequired: true means zero users exist in the database; the client should show the setup form.
  • oidcEnabled tells the client whether to show the "Login with SSO" button.

POST /api/auth/setup

Creates the first admin user. Only works when zero users exist in the database.

Auth required: No

Request body:

{
  "email": "admin@example.com",
  "displayName": "Admin User",
  "password": "securepassword123"
}
Field Type Validation
email string Required. Valid email format.
displayName string Required. 1-100 characters.
password string Required. Minimum 12 characters.

Response (201 Created):

{
  "user": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "email": "admin@example.com",
    "displayName": "Admin User",
    "role": "admin",
    "authProvider": "local",
    "oidcLinked": false,
    "createdAt": "2026-02-08T10:00:00.000Z"
  }
}

The response also sets the cornerstone_session cookie (the admin is logged in immediately after setup).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid email, password too short, or missing fields
403 SETUP_COMPLETE At least one user already exists
429 RATE_LIMIT_EXCEEDED The password-hashing queue is full (see Password-Hashing Queue); same body for every account

POST /api/auth/login

Authenticates a local user with email and password.

Auth required: No

Request body:

{
  "email": "admin@example.com",
  "password": "securepassword123"
}
Field Type Validation
email string Required.
password string Required.

Response (200 OK):

{
  "user": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "email": "admin@example.com",
    "displayName": "Admin User",
    "role": "admin",
    "authProvider": "local",
    "oidcLinked": false,
    "createdAt": "2026-02-08T10:00:00.000Z"
  }
}

The response also sets the cornerstone_session cookie.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing email or password
401 INVALID_CREDENTIALS Wrong email/password, or the account has no password, deactivated or not (no information leakage about which)
401 ACCOUNT_DEACTIVATED Account with a password has been deactivated
423 ACCOUNT_LOCKED Too many failed attempts; details.lockedUntil carries the unlock timestamp
429 RATE_LIMIT_EXCEEDED The login rate limit is exceeded (see Rate Limiting (Auth)), or the password-hashing queue is full (see Password-Hashing Queue); same body for every account

Security notes:

  • The INVALID_CREDENTIALS error uses a generic message ("Invalid email or password") that does not reveal whether the email exists or the password was wrong.
  • Deactivated accounts that have a password receive ACCOUNT_DEACTIVATED to distinguish from invalid credentials (the user should contact an admin).
  • Accounts without a password_hash (authProvider: "oidc": OIDC just-in-time provisioned, or admin-created SSO-only via POST /api/users) attempting local login receive INVALID_CREDENTIALS, including when they are deactivated. The no-password branch runs a dummy scrypt verify and returns before the deactivated and lockout checks, so the response (status, code, message and timing) is the same as for an unknown email and does not reveal that the address has an account.
  • Unknown emails and accounts without a password are checked against a dummy scrypt hash made once per process with the current parameters, so their timing matches a real verification after the first such request.
  • After a successful login, a password hash stored with the legacy scrypt parameters is rehashed with the current ones and saved (best-effort: a failure is logged and the login still succeeds). The response is unaffected. See ADR-010, Amendments.
  • Local accounts that have been linked to OIDC (oidcLinked: true) keep their password and can still sign in here. Linking never changes an account's id, role, authProvider, or password_hash.

POST /api/auth/logout

Destroys the current session and clears the session cookie.

Auth required: Yes

Request body: None

Response (204 No Content): Empty body. The cornerstone_session cookie is cleared.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

GET /api/auth/oidc/login

Initiates the OIDC Authorization Code flow by redirecting the user to the OIDC provider.

Auth required: No

Query parameters:

Parameter Type Description
redirect string Optional. In-app URL (pathname, search and hash) to land on after a successful sign-in. Default: / (the Home path)

redirect is accepted only when safeAppPath (@cornerstone/shared, shared/src/routes/safePath.ts) returns it unchanged. The value must be a string of 1 to 2048 characters (MAX_APP_PATH_LENGTH) that starts with /, does not start with //, and contains no \, no :// and no ASCII control character (U+0000 to U+001F, U+007F). Browsers read \ as / and strip tab and newline inside URLs, so /\evil.example or /<tab>/evil.example would otherwise become the off-site //evil.example. The rule validates and never rewrites: there is no decoding or normalization. An absent or rejected value falls back to the Home path, /. The client's sign-in page (/login?next=) sends its next value here, so the SSO sign-in lands on the deep link that the signed-out user asked for.

Response (302 Found): Redirects to the OIDC provider's authorization endpoint with:

  • response_type=code
  • client_id from config
  • redirect_uri from config
  • scope=openid email profile
  • state = cryptographically random value (stored server-side temporarily for CSRF protection)

Error responses:

HTTP Status Error Code When
404 OIDC_NOT_CONFIGURED OIDC environment variables are not set

GET /api/auth/oidc/callback

Handles the OIDC provider's redirect after the user authenticates.

Auth required: No

Query parameters (set by the OIDC provider):

Parameter Type Description
code string Authorization code from the provider
state string CSRF state parameter to validate
error string Error code from the provider (if auth failed)
error_description string Error description from the provider (if auth failed)

Response (302 Found): On success, redirects to the app (the redirect value validated by GET /api/auth/oidc/login and stored with the state, or /). The cornerstone_session cookie is set.

Behavior:

  1. Validates the state parameter against the stored value
  2. Exchanges the code for tokens using the OIDC token endpoint. The redirect_uri sent in this exchange is derived by the same expression as the login leg (<EXTERNAL_URL> or the request origin, + /api/auth/oidc/callback, keeping the callback's query string) — see Environment Variables (Auth)
  3. Extracts sub, email, email_verified, name and preferred_username from the ID token (only a boolean true counts as verified; an absent claim is unverified). The two name claims are used only by step 8 (provisioning)
  4. Subject match. Looks up the user by oidc_subject = sub (any auth_provider). If found, that account signs in — neither an email claim nor email_verified is required on this path (the subject is already bound)
  5. Email present. No subject match: an empty or missing email claim redirects with missing_email (error code OIDC_MISSING_EMAIL)
  6. Verified-email gate. If email_verified !== true, redirects with oidc_email_unverified. This check runs before any email lookup, so an unverified token can never probe which emails have accounts
  7. Email match. Looks up the account by email, case-insensitively. An exact (case-sensitive) match is preferred. If there is no exact match and more than one account matches case-insensitively, the result is ambiguous and treated as no match. A match continues at step 9; no match continues at step 8
  8. No match: reject or provision. With OIDC_JIT_PROVISIONING unset or false (the default), redirects with oidc_no_matching_account: OIDC does not create the account, which only initial setup (POST /api/auth/setup) or an admin (POST /api/users) can do. With the flag true, the callback creates the account (outcome provisioned), except that it still redirects with oidc_no_matching_account when the email was ambiguous in step 7 (fail closed) or the users table is empty (initial setup has not run; provisioning a member first would permanently block POST /api/auth/setup). The new account has role = 'member' (never admin), authProvider = 'oidc', oidc_subject = sub, the claim's email, and no password. displayName is the first non-empty of name, preferred_username, then the email, trimmed and capped at 100 code points. The insert runs in the step 7 transaction. Transactions serialize within the process, so a second sign-in for the same sub finds the account at step 4 and never inserts. If a write from outside the process (another instance or an external tool) already bound the sub, the insert hits the unique index on oidc_subject. The violation is caught, the account is re-read by subject, and it signs in as a subject match. The server logs provisioning at info with the new userId only. Continues at step 11
  9. Deactivated. If the matched account is deactivated, redirects with account_deactivated and does not link. (A deactivated account found in step 4 is refused the same way.) A deactivated match is never replaced by a provisioned account
  10. Link. Sets the matched account's oidc_subject to sub. If the account was already linked to a different subject, the re-bind is accepted (gated by step 6) and logged at warn. id, role, authProvider, and password_hash are unchanged
  11. Creates a session and sets the cookie
  12. Redirects to the app

Error responses (all via redirect to /login?error=<code>, plus &next=<path> when the sign-in state is known):

next is the app redirect stored with the state, URL-encoded. It is appended only after a valid state was consumed (account_deactivated, missing_email, oidc_email_unverified, oidc_no_matching_account, and oidc_error from the token exchange), or on the provider-error branch when the provider's redirect carries a valid state. That branch now consumes the state, so the state can no longer be reused. next is never appended for oidc_not_configured or invalid_state (or a missing state), never when the stored value is the Home path /, and never when it fails safeAppPath. The key order is error, then next, so a redirect without next stays exactly /login?error=<code>. The login page keeps next for the next sign-in attempt; its error banner does not depend on it.

Redirect Error When
oidc_not_configured OIDC env vars not set
oidc_error OIDC provider returned an error, or the token exchange failed
invalid_state State parameter mismatch (CSRF)
missing_email No account is bound to this subject and the ID token has no (or an empty) email claim
oidc_email_unverified No account is bound to this subject and the token's email_verified is not true
oidc_no_matching_account No subject or email match, and provisioning is off, ambiguous, or blocked (step 8)
account_deactivated The matched account has been deactivated

The redirect codes are exactly the OIDC_LOGIN_ERROR_CODES tuple in @cornerstone/shared (OidcLoginErrorCode). The server builds every redirect through a helper typed against it, and the login page translates only codes in the tuple (an unknown ?error= value renders no banner). Adding a code means adding it to the tuple; the client's I18N_UNION_KEYS.oidcLoginError locale-parity test then requires its translation.

Notes:

  • OIDC callback errors redirect to the login page with a query parameter rather than returning JSON, because this endpoint is called via browser redirect (not AJAX).
  • The three OIDC account-resolution rejections map to the error codes OIDC_MISSING_EMAIL (403), OIDC_EMAIL_UNVERIFIED (403), and OIDC_NO_MATCHING_ACCOUNT (403) in the Error Codes table; the endpoint surfaces them only as the redirect codes above.
  • The earlier email_conflict redirect (and the EMAIL_CONFLICT error code) no longer exist: an OIDC identity whose verified email matches a local account is linked to it rather than rejected.

User Endpoints

GET /api/users/me

Returns the full profile of the currently authenticated user.

Auth required: Yes

Response (200 OK):

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "email": "admin@example.com",
  "displayName": "Admin User",
  "role": "admin",
  "authProvider": "local",
  "oidcLinked": false,
  "createdAt": "2026-02-08T10:00:00.000Z",
  "updatedAt": "2026-02-08T10:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

PATCH /api/users/me

Updates the current user's profile. Only displayName can be changed by the user.

Auth required: Yes

Request body:

{
  "displayName": "New Display Name"
}
Field Type Validation
displayName string Required. 1-100 characters.

Response (200 OK):

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "email": "admin@example.com",
  "displayName": "New Display Name",
  "role": "admin",
  "authProvider": "local",
  "oidcLinked": false,
  "createdAt": "2026-02-08T10:00:00.000Z",
  "updatedAt": "2026-02-08T12:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid or missing displayName
401 UNAUTHORIZED No valid session

POST /api/users/me/password

Changes the current user's password. Only available for local auth users.

Auth required: Yes

Request body:

{
  "currentPassword": "oldpassword123",
  "newPassword": "newpassword456"
}
Field Type Validation
currentPassword string Required. Must match stored hash.
newPassword string Required. Minimum 12 characters.

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing fields or newPassword too short
401 UNAUTHORIZED No valid session
401 INVALID_CREDENTIALS Current password is incorrect
403 FORBIDDEN User's authProvider is oidc (no local password)
429 RATE_LIMIT_EXCEEDED The password-hashing queue is full (see Password-Hashing Queue); same body for every account

Notes:

  • The 403 covers every authProvider: "oidc" account: OIDC just-in-time provisioned and admin-created SSO-only (POST /api/users with authProvider: "oidc"). It runs in the handler, after body schema validation, so such a user sending an invalid body gets 400, not 403.
  • This is the only password endpoint, and it requires the current password. No endpoint sets a password for an account that has none, and authProvider never changes, so an SSO-only account stays SSO-only (#2122).

GET /api/users

Returns a list of all users. Supports search filtering.

Auth required: Yes (any authenticated user; the route has no role check, so members can list users too)

Query parameters:

Parameter Type Description
q string Optional. Search filter matching email or display_name (case-insensitive LIKE)

Response (200 OK):

{
  "users": [
    {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "email": "admin@example.com",
      "displayName": "Admin User",
      "role": "admin",
      "authProvider": "local",
      "oidcLinked": false,
      "createdAt": "2026-02-08T10:00:00.000Z",
      "updatedAt": "2026-02-08T10:00:00.000Z",
      "deactivatedAt": null,
      "lockedUntil": null
    },
    {
      "id": "660e8400-e29b-41d4-a716-446655440001",
      "email": "member@example.com",
      "displayName": "Member User",
      "role": "member",
      "authProvider": "oidc",
      "oidcLinked": true,
      "createdAt": "2026-02-08T11:00:00.000Z",
      "updatedAt": "2026-02-08T11:00:00.000Z",
      "deactivatedAt": null,
      "lockedUntil": "2026-02-09T08:15:00.000Z"
    }
  ]
}

The example is an admin caller's response. For a member caller the lockedUntil key is absent from every row.

Lock state (admin callers only) — Implemented (#2219) 🔒: each row is a UserListItem (shared/src/types/user.ts), a UserResponse plus lockedUntil?: string | null.

  • lockedUntil is present only when the caller's role is admin. Its value is the ISO 8601 end of an active login lockout (10 failed local password logins lock the account for 15 minutes, see POST /api/auth/login 423 ACCOUNT_LOCKED), or null when the account is not locked or the lock has expired. It is derived on read with userService.getAccountLockStatus; the stored locked_until is not cleared when it passes.
  • For member callers the key is omitted, not null: members use this endpoint for assignee pickers and never learn another user's lock state.
  • It is independent of deactivatedAt; both may be set. The client shows "Deactivated" when both are set.
  • failed_login_attempts is never exposed. It is not reset when a lock expires, so the next wrong password after an expired lock locks the account again; POST /api/users/:id/unlock resets it.

Notes:

  • Returns both active and deactivated users (deactivated users have deactivatedAt set)
  • No pagination needed for <5 users; will be revisited if the user count model changes
  • The password_hash and oidc_subject fields are never exposed in API responses
  • lockedUntil is never part of UserResponse: GET /api/users/me, PATCH /api/users/me, PATCH /api/users/:id, POST /api/users and the auth responses never carry it

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

Unknown query parameters are silently dropped, not rejected: the querystring schema declares additionalProperties: false, and Fastify's default AJV configuration (removeAdditional: true) strips the extra keys before the handler runs.


POST /api/users (Admin only)

Creates a local account with a password, or (with authProvider: "oidc") an SSO-only account with no password. Besides initial setup (POST /api/auth/setup) and opt-in OIDC just-in-time provisioning (OIDC_JIT_PROVISIONING, see ADR-035), this is the only way an account is created.

Auth required: Yes (admin role)

Request body (email, displayName and role are required; password is required for a local account and forbidden for an SSO-only one; unknown properties are stripped, not rejected):

{
  "email": "member@example.com",
  "displayName": "Member User",
  "password": "a-long-password",
  "role": "member"
}

SSO-only account (requires OIDC to be enabled):

{
  "email": "member@example.com",
  "displayName": "Member User",
  "role": "member",
  "authProvider": "oidc"
}
Field Type Validation
email string Required. format: email, max 255 characters
displayName string Required. 1-100 characters
password string 12-255 characters. Required when authProvider is "local" or absent; forbidden when it is "oidc"
role string Required. "admin" or "member" (no default applied)
authProvider string Optional. "local" (default) or "oidc". "oidc" creates an SSO-only account and requires OIDC to be enabled

Lengths are counted in code points (AJV minLength/maxLength).

Response (201 Created): the new user wrapped in a user key (unlike PATCH /api/users/:id, which returns the bare object). The shape is the User Response Shape.

{
  "user": {
    "id": "770e8400-e29b-41d4-a716-446655440002",
    "email": "member@example.com",
    "displayName": "Member User",
    "role": "member",
    "authProvider": "local",
    "createdAt": "2026-10-01T10:00:00.000Z",
    "updatedAt": "2026-10-01T10:00:00.000Z",
    "deactivatedAt": null,
    "oidcLinked": false
  }
}

An SSO-only account is returned with "authProvider": "oidc" and "oidcLinked": false.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Schema failure: missing/invalid field, or an authProvider other than "local"/"oidc" (details.fields[] lists the failures)
400 VALIDATION_ERROR password sent with authProvider: "oidc", or password missing for a local account (details: { "field": "password" })
400 OIDC_NOT_CONFIGURED authProvider: "oidc" while OIDC is disabled (OIDC_ISSUER, OIDC_CLIENT_ID and OIDC_CLIENT_SECRET not all set)
401 UNAUTHORIZED No valid session
403 FORBIDDEN User is not an admin
409 CONFLICT email exactly matches an existing account's email (details.email echoes it)
429 RATE_LIMIT_EXCEEDED A local account (authProvider "local" or absent) while password-hashing queue is full (see Password-Hashing Queue); same body for every account

Notes:

  • Check order: the authentication hook runs at preValidation (401), then body schema validation (400), then the admin role check at preHandler (403). A non-admin sending an invalid body therefore gets 400, not 403. The handler then applies, in this order: the password rule (400 VALIDATION_ERROR), the OIDC configuration check for "oidc" (400 OIDC_NOT_CONFIGURED), and the email conflict check (409). An "oidc" request that also carries a password gets VALIDATION_ERROR even while OIDC is disabled. No row is written on any error.
  • The password rule is enforced in the handler, not in the JSON schema, so its two failures carry details.field rather than the schema's details.fields[]. Omitting password never silently creates an SSO-only account: authProvider: "oidc" must be explicit.
  • OIDC_NOT_CONFIGURED is 400 here (the request is well-formed but the instance cannot satisfy it), unlike GET /api/auth/oidc/login, where the route itself is unavailable and it is 404.
  • A local account's password is hashed with scrypt (ADR-010) before storage. No session is created for the new user and the calling admin's session is unchanged.
  • SSO-only accounts are stored with auth_provider = 'oidc', password_hash = NULL and oidc_subject = NULL. They have oidcLinked: false until the user's first OIDC sign-in with a verified email that matches the account, which binds the subject (GET /api/auth/oidc/callback, link step). Until then, and whenever OIDC is disabled, the account cannot sign in at all. Local login answers INVALID_CREDENTIALS (see POST /api/auth/login), and POST /api/users/me/password answers 403.
  • SSO-only is permanent. No endpoint sets a password for an account without one, and PATCH /api/users/:id cannot change authProvider. Because emails are never released (deactivation is a soft delete and the conflict check includes deactivated accounts), switching a person to a local account means: change the SSO-only account's email to a placeholder, deactivate it, then create a new local account with the real email. History stays on the old account. See ADR-035, Amendment (#2122).
  • The uniqueness check is an exact, case-sensitive comparison (findByEmail), and it includes deactivated accounts: an email held by a deactivated user returns 409. Case variants (Member@example.com vs member@example.com) can coexist, consistent with ADR-035.
  • The limits match the rest of the API: displayName 1-100 as on PATCH /api/users/me and PATCH /api/users/:id, and a 12-character password minimum as on POST /api/users/me/password. This endpoint has no route-specific rate limit.

PATCH /api/users/:id (Admin only)

Updates a user's role or display name. Admins can also update a user's email.

Auth required: Yes (admin role)

Path parameters:

Parameter Type Description
id string User UUID

Request body (all fields optional, at least one required):

{
  "displayName": "Updated Name",
  "email": "new-email@example.com",
  "role": "admin"
}
Field Type Validation
displayName string 1-100 characters
email string Valid email format
role string "admin" or "member"

Response (200 OK):

{
  "id": "660e8400-e29b-41d4-a716-446655440001",
  "email": "new-email@example.com",
  "displayName": "Updated Name",
  "role": "admin",
  "authProvider": "oidc",
  "oidcLinked": true,
  "createdAt": "2026-02-08T11:00:00.000Z",
  "deactivatedAt": null
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
403 FORBIDDEN User is not an admin
404 NOT_FOUND User ID does not exist
409 LAST_ADMIN Attempting to demote the last admin to member
409 CONFLICT Email already in use by another user

DELETE /api/users/:id (Admin only)

Deactivates a user account (soft delete). Sets deactivated_at and invalidates all active sessions.

Auth required: Yes (admin role)

Path parameters:

Parameter Type Description
id string User UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
403 FORBIDDEN User is not an admin
404 NOT_FOUND User ID does not exist
409 SELF_DEACTIVATION Admin attempting to deactivate themselves
409 LAST_ADMIN Attempting to deactivate the last admin

Notes:

  • This is a soft delete: the user record is retained with deactivated_at set to the current timestamp
  • All active sessions for the deactivated user are deleted immediately
  • Deactivated users cannot log in. Local login returns ACCOUNT_DEACTIVATED for an account with a password and INVALID_CREDENTIALS for one without (OIDC-provisioned or SSO-only, see POST /api/auth/login); the OIDC callback redirects with account_deactivated
  • An already-deactivated user can be "re-deleted" without error (idempotent)

POST /api/users/:id/unlock (Admin only)

Clears a login lockout. Local login locks an account for 15 minutes after 10 consecutive failed attempts (423 ACCOUNT_LOCKED, see the Error Codes table); this endpoint resets failed_login_attempts to 0 and locked_until to NULL immediately.

Settings › Users shows the action as the row menu's Unlock item while the admin view of GET /api/users reports lockedUntil (#2219). The route itself accepts any existing user, locked or not, and always answers 204.

Auth required: Yes (admin role)

Path parameters:

Parameter Type Description
id string User UUID

Request body: None (the route declares no body schema; a body, if sent, is ignored).

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
403 FORBIDDEN User is not an admin
404 NOT_FOUND User ID does not exist

Notes:

  • Idempotent: unlocking an account that is not locked also returns 204 (and bumps updated_at).
  • The endpoint does not check deactivated_at; unlocking a deactivated account returns 204 but does not reactivate it.
  • Sessions are not touched.

Route Protection

All /api/* routes are protected by a Fastify preValidation hook (server/src/plugins/auth.ts), so an unauthenticated request gets 401 before body validation runs. Role checks (requireRole) run later, at preHandler, after validation. The hook skips these routes (PUBLIC_ROUTES, matched on the route pattern):

Unprotected Routes Reason
GET /api/config Client needs currency config before auth
GET /api/health Health check for Docker/monitoring
GET /api/health/ready Readiness check for Docker/monitoring
GET /api/auth/me Must work without auth to detect setup state
POST /api/auth/setup Must work without auth (no users exist yet)
POST /api/auth/login Must work without auth (logging in)
POST /api/auth/logout Must succeed even when the session has already expired
GET /api/auth/oidc/login Must work without auth (initiating OIDC flow)
GET /api/auth/oidc/callback Must work without auth (OIDC provider callback)

Admin-only routes additionally check user.role === 'admin' via a requireRole('admin') decorator.


EPIC-03: Work Items Core CRUD & Properties

Work items are the central entity of the application. All endpoints in this section require authentication. Both admin and member roles can perform all work item operations (no role restriction).

Work Item Response Shapes

Work item summary (used in list responses):

interface WorkItemSummary {
  id: string;
  title: string;
  status: WorkItemStatus;
  startDate: string | null; // PLANNED start (stored, never floored to today); null for an undated task
  endDate: string | null; // PLANNED end
  projectedStartDate: string | null; // Forecast start (after the today floor); equals startDate unless late/held up; start of the forecast for an undated task
  projectedEndDate: string | null; // Forecast end; equals endDate unless late/held up
  isLate: boolean; // The today floor moved this item itself (never for completed or undated)
  lateDays: number | null; // >= 1 exactly when isLate
  isHeldUp: boolean; // Not late itself, but moved by an unfinished late predecessor
  actualStartDate: string | null; // Actual start date (YYYY-MM-DD)
  actualEndDate: string | null; // Actual end date (YYYY-MM-DD)
  durationDays: number | null;
  assignedUser: UserSummary | null;
  assignedVendor: VendorSummary | null;
  area: AreaSummary | null;
  createdAt: string;
  updatedAt: string;
}

Work item detail (used in single-item responses):

interface WorkItemDetail {
  id: string;
  title: string;
  description: string | null;
  status: WorkItemStatus;
  startDate: string | null; // PLANNED start (stored, never floored to today); null for an undated task
  endDate: string | null; // PLANNED end
  projectedStartDate: string | null; // Forecast start (after the today floor); equals startDate unless late/held up; start of the forecast for an undated task
  projectedEndDate: string | null; // Forecast end; equals endDate unless late/held up
  isLate: boolean; // The today floor moved this item itself (never for completed or undated)
  lateDays: number | null; // >= 1 exactly when isLate
  isHeldUp: boolean; // Not late itself, but moved by an unfinished late predecessor
  actualStartDate: string | null; // Actual start date (YYYY-MM-DD)
  actualEndDate: string | null; // Actual end date (YYYY-MM-DD)
  durationDays: number | null;
  startAfter: string | null;
  startBefore: string | null;
  assignedUser: UserSummary | null;
  assignedVendor: VendorSummary | null;
  area: AreaSummary | null;
  createdBy: UserSummary | null;
  subtasks: SubtaskResponse[];
  dependencies: {
    predecessors: DependencyResponse[];
    successors: DependencyResponse[];
  };
  budgets: WorkItemBudgetLine[];
  createdAt: string;
  updatedAt: string;
}

Task page detail (GET /api/work-items/:id only, #2248):

// shared/src/types/workItem.ts
interface WorkItemPageVendor extends VendorSummary {
  phone: string | null; // the company's phone, for the strip's Call button
}

interface WorkItemPageDetail extends Omit<WorkItemDetail, 'assignedVendor'> {
  assignedVendor: WorkItemPageVendor | null;
  expectedCost: number | null; // as WorkItemListRow.expectedCost; null without cost lines
  grants: ItemGrantChip[]; // as WorkItemListRow.grants (rejected excluded, by name then id)
  waitsFor: WorkItemWaitsFor | null; // as WorkItemListRow.waitsFor
  holdsUp: AttentionHoldsUp | null; // as WorkItemListRow.holdsUp (computeHoldsUp)
}

The four figures are computed by the same helper as the Tasks list rows (buildRowFigures in workItemService, used by listWorkItemRows and getWorkItemPageDetail), so the task page, the Tasks list and the Schedule never disagree (figures.consistency.test.ts compares detail, list and timeline). isCritical and timeWindow stay list-only. Every other response that returns a work item detail (POST, PATCH, the dependency and subtask routes) keeps WorkItemDetail.

Supporting types:

type WorkItemStatus = 'not_started' | 'in_progress' | 'completed';
// shared/src/types/dependency.ts: the runtime tuple is the source of the union (#2247; i18n key sets derive from it, #2029)
const DEPENDENCY_TYPES = ['finish_to_start', 'start_to_start', 'finish_to_finish', 'start_to_finish'] as const;
type DependencyType = (typeof DEPENDENCY_TYPES)[number];
type ConfidenceLevel = 'own_estimate' | 'professional_estimate' | 'quote' | 'invoice';

interface UserSummary {
  id: string;
  displayName: string;
  email: string;
}

interface AreaAncestor {
  id: string;
  name: string;
  color: string | null;
}

interface AreaSummary {
  id: string;
  name: string;
  color: string | null;
  /** Root-first ancestor chain. Does NOT include the area itself. Empty array for root-level areas. */
  ancestors: AreaAncestor[];
}

interface SubtaskResponse {
  id: string;
  title: string;
  isCompleted: boolean;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

interface DependencyResponse {
  workItem: WorkItemSummary;
  dependencyType: DependencyType;
  leadLagDays: number; // Lead (negative) or lag (positive) offset in days; default 0
}

interface WorkItemBudgetLine {
  id: string;
  workItemId: string;
  description: string | null;
  plannedAmount: number;
  confidence: ConfidenceLevel;
  confidenceMargin: number; // Computed: 0.20, 0.10, 0.05, or 0.00
  budgetCategory: BudgetCategoryResponse | null;
  budgetSource: BudgetSourceSummary | null;
  vendor: VendorSummary | null;
  actualCost: number; // Computed: sum of itemized amounts from ALL linked invoices, including quotations. See "Quotation handling for `actualCost`" below.
  actualCostPaid: number; // Computed: aggregates per-deposit 'paid'/'claimed' contributions (proportional split) + residual when parent invoice is 'paid'/'claimed'. Excludes quotations.
  invoiceCount: number; // Computed: count of linked invoices
  invoiceLink: BudgetLineInvoiceLink | null; // The (at most one) linked invoice; null if unlinked
  createdBy: UserSummary | null;
  createdAt: string;
  updatedAt: string;
}

interface BudgetLineInvoiceLink {
  invoiceBudgetLineId: string;
  invoiceId: string;
  invoiceNumber: string | null;
  invoiceDate: string;
  invoiceStatus: InvoiceStatus;
  itemizedAmount: number;
  vendorId: string | null; // Denormalized from the invoice's vendor
  vendorName: string | null; // Denormalized from the invoice's vendor (may be null only if the vendor row has no name)
}

interface BudgetSourceSummary {
  id: string;
  name: string;
  sourceType: string;
}

interface VendorSummary {
  id: string;
  name: string;
  trade: TradeSummary | null;
}

interface TradeSummary {
  id: string;
  name: string;
  color: string | null;
}

Notes on the detail response:

  • area is the spatial location of the work item (nullable). Only id, name, and color are embedded.
  • assignedVendor is the vendor assigned to this work item (nullable, mutually exclusive with assignedUser).
  • subtasks are always embedded, sorted by sortOrder ascending.
  • dependencies.predecessors lists work items that this item depends on. dependencies.successors lists work items that depend on this item. Both include a summary of the related work item.
  • budgets are always embedded, listing all budget lines for the work item. Each budget line includes computed fields (confidenceMargin, actualCost, actualCostPaid, invoiceCount) derived from linked invoices and the confidence level enum.
  • Notes are NOT embedded in the detail response. They are fetched separately via GET /api/work-items/:id/notes to allow independent pagination if needed in the future.
  • The work item's trade is derived transitively: work_item.assignedVendor.trade. There is no direct trade field on work items.

Work Item Endpoints

GET /api/work-items

Returns a paginated, filterable, sortable list of work items.

startDate/endDate are the planned dates. The forecast fields (projectedStartDate, projectedEndDate, isLate, lateDays, isHeldUp) come from the same read-time projection as GET /api/timeline (contract 4, #2199). start_date/end_date sorting and the four date filters use the shown date, actualStartDate ?? projectedStartDate (respectively end): the date every client surface displays. Rows without a shown date sort last in both directions; ties break on createdAt then id. A bounded date filter excludes rows without a shown date.

Auth required: Yes

Query parameters:

Parameter Type Default Description
page integer 1 Page number (1-indexed)
pageSize integer 25 Items per page (max 100)
status string (none) Filter by status. One of: not_started, in_progress, completed
assignedUserId string (none) Filter by assigned user UUID
assignedVendorId string (none) Filter by assigned vendor UUID
areaId string (none) Filter by area UUID or comma-separated list of area UUIDs. For each supplied ID the server expands descendants server-side; results are the union of all matching subtrees. Use the sentinel value __none__ to include items with no area assignment (e.g., ?areaId=__none__ returns only unassigned items; ?areaId=__none__,<uuid> returns unassigned items OR items in the specified area subtree).
q string (none) Search title and description (case-insensitive substring match)
budgetLinesMin number (none) Filter: minimum budget line count (inclusive)
budgetLinesMax number (none) Filter: maximum budget line count (inclusive)
startDateFrom string (none) Filter: shown start date >= value (YYYY-MM-DD, inclusive; 400 VALIDATION_ERROR if malformed)
startDateTo string (none) Filter: shown start date <= value (YYYY-MM-DD, inclusive)
endDateFrom string (none) Filter: shown end date >= value (YYYY-MM-DD, inclusive)
endDateTo string (none) Filter: shown end date <= value (YYYY-MM-DD, inclusive)
milestoneId integer (none) Tasks that count toward this milestone (milestone_work_items). Integer ≥ 1 (else 400). An unknown id returns no rows, not 404 (#2245)
ids string (none) Comma-separated work item ids (≤ 4000 chars). See ids rules below (#2245)
group string (none) Server-side grouping. Only time (WORK_ITEM_LIST_GROUPS). See Grouping below (#2245)
sortBy string created_at Sort field. One of: title, status, start_date, end_date, created_at, updated_at, expected_cost (#2245: rows without budget lines (expectedCost: null) last in both directions; ties break on createdAt then id)
sortOrder string desc Sort direction: asc or desc

Grant chips (#2242): every row carries grants (ItemGrantChip[]: { id, name, reductionType, rate }): the grants linked to the task, rejected grants excluded, ordered by name then id, [] when none. One batched query per response. The field is optional in the shared type (WorkItemSummary.grants?): it is always sent on GET list rows and absent from every other response that returns a work item summary.

List rows (#2245, contract 16). The route answers through workItemService.listWorkItemRows (always the in-memory path, so time windows, shown-date filters and the expected_cost sort share one pass). Rows are WorkItemListRow (shared/src/types/workItem.ts), which extends WorkItemSummary with required fields. These fields are sent only by this endpoint; every other response that returns a work item summary keeps WorkItemSummary. listWorkItems (used by the Home counts) keeps returning WorkItemSummary and runs no money computation; it shares the filter builder, so it honours milestoneId too.

Field Type Meaning
grants ItemGrantChip[] As above; required on a row
expectedCost number | null ADR-039 "Expected cost" of the task's lines in major units (getExpectedCostByWorkItem, one computeMoney run per response, no N+1); null when budgetLineCount === 0. It equals GET /api/budget/breakdown's workItems[id].expectedCost.expected. It is a part, not a registered figure
timeWindow TaskTimeWindow One of TASK_TIME_WINDOWS = late · this_week · next_two_weeks · later · done. Rules in order: completed → done; late (isTaskLate, contract 4) → late; in_progress → this_week; not_started with no planned startDate → later; else the forecast projectedStartDate ≤ today + 6 days → this_week, ≤ today + 20 days (the two weeks after this one) → next_two_weeks, else later. Same week/agenda bounds as GET /api/home/week (HOME_WEEK_DAYS, HOME_AGENDA_DAYS); "today" is one UTC date per request (Architecture › Attention › Task time windows)
isCritical boolean status !== 'completed' and the task is on the projected critical path (Home's rule)
waitsFor WorkItemWaitsFor | null { id, title, more }: the unfinished task this one waits for (real predecessors plus the contributors of unreached milestone gates). The shown one is Home's "Waits for" cause when there is one, else the candidate with the latest projectedEndDate (nulls last, ties by id); more = candidates − 1. null when there are none or the task is completed
holdsUp AttentionHoldsUp | null { count, startingWithin14Days } from computeHoldsUp (the one holds-up definition, as GET /api/attention); null when the count is 0 or the task is completed

Grouping (#2245). With group=time the rows are ordered by window (TASK_TIME_WINDOWS order) first, then by the sortBy/sortOrder comparator, so consecutive pages never interleave windows and a client can append batches without re-sorting. The response then carries groupCounts: all five window keys (zero included), counted over every row matching the query (filters, q, status), not just the page. Without group, groupCounts is absent and the order is the comparator alone.

scopeTotal (#2245, always sent). The number of work items matching only status (no search, no filters): the "of m" of the Tasks heading "(n of m)", in the same round trip.

ids rules (#2245, as GET /api/invoices ids). Values are trimmed and empty entries dropped. More than 100 usable ids → 400 VALIDATION_ERROR. With at least one usable id, the rows are the matching rows among those ids, page/pageSize are ignored and pagination is { page: 1, pageSize: 100, totalItems: n, totalPages: n > 0 ? 1 : 0 }. groupCounts, scopeTotal and filterMeta ignore ids, so a host can refresh a few rows and the counts in one request. A value with no usable id (e.g. ids=,) behaves as if absent.

Response (200 OK):

{
  "items": [
    {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "title": "Foundation Work",
      "status": "in_progress",
      "startDate": "2026-03-01",
      "endDate": "2026-03-15",
      "actualStartDate": "2026-03-02",
      "actualEndDate": null,
      "durationDays": 14,
      "assignedUser": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "assignedVendor": null,
      "area": {
        "id": "area-kitchen",
        "name": "Kitchen",
        "color": "#F59E0B",
        "ancestors": [
          { "id": "area-ground-floor", "name": "Ground Floor", "color": "#3B82F6" },
          { "id": "area-living-spaces", "name": "Living Spaces", "color": "#10B981" }
        ]
      },
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z",
      "grants": [],
      "expectedCost": 12500,
      "timeWindow": "this_week",
      "isCritical": true,
      "waitsFor": null,
      "holdsUp": { "count": 3, "startingWithin14Days": 1 }
    }
  ],
  "pagination": {
    "page": 1,
    "pageSize": 25,
    "totalItems": 1,
    "totalPages": 1
  },
  "filterMeta": {
    "budgetLines": { "min": 0, "max": 5 }
  },
  "groupCounts": { "late": 0, "this_week": 1, "next_two_weeks": 0, "later": 0, "done": 0 },
  "scopeTotal": 1
}

(The example is a group=time response; groupCounts is absent without group. The row abbreviates the WorkItemSummary fields.)

The filterMeta object contains min/max bounds for each numeric filterable column, computed from all rows matching the current non-numeric filters. Used by the frontend to set appropriate slider ranges on number filters. Returns { "min": 0, "max": 0 } per column when no rows match.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid query parameters (e.g., invalid status value, page < 1, pageSize > 100, group not time, milestoneId < 1, more than 100 usable ids)
401 UNAUTHORIZED No valid session

POST /api/work-items

Creates a new work item.

Auth required: Yes

Request body:

{
  "title": "Foundation Work",
  "description": "Excavation and concrete pouring for the foundation",
  "status": "not_started",
  "startDate": "2026-03-01",
  "endDate": "2026-03-15",
  "durationDays": 14,
  "startAfter": "2026-02-28",
  "startBefore": "2026-03-10",
  "assignedUserId": "550e8400-e29b-41d4-a716-446655440000",
  "areaId": "area-kitchen"
}
Field Type Required Validation
title string Yes 1-500 characters
description string No Max 10000 characters
status string No One of: not_started, in_progress, completed. Default: not_started
startDate string No ISO 8601 date (YYYY-MM-DD)
endDate string No ISO 8601 date (YYYY-MM-DD). Must be >= startDate if both provided
actualStartDate string No ISO 8601 date (YYYY-MM-DD) or null. Overrides auto-population on status transition.
actualEndDate string No ISO 8601 date (YYYY-MM-DD) or null. Overrides auto-population on status transition.
durationDays integer No >= 0
startAfter string No ISO 8601 date (YYYY-MM-DD)
startBefore string No ISO 8601 date (YYYY-MM-DD). Must be >= startAfter if both provided
assignedUserId string No Must be a valid, active user UUID. Mutually exclusive with assignedVendorId.
assignedVendorId string No Must be a valid vendor UUID. Mutually exclusive with assignedUserId.
areaId string No Must be a valid area UUID

The createdBy field is automatically set to the authenticated user's ID.

Response (201 Created):

Returns a WorkItemDetail object (GET /api/work-items/:id returns the superset WorkItemPageDetail).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty title, invalid status, invalid date format, startDate > endDate, startAfter > startBefore, non-existent assignedUserId/assignedVendorId/areaId, both assignedUserId and assignedVendorId set
401 UNAUTHORIZED No valid session

GET /api/work-items/:id

Returns a single work item as WorkItemPageDetail (#2248): the full WorkItemDetail (subtasks, dependencies, budget lines) plus the task page's strip figures expectedCost, grants, waitsFor, holdsUp and the company phone on assignedVendor (see Work Item Response Shapes).

Auth required: Yes

Path parameters:

Parameter Type Description
id string Work item UUID

Response (200 OK):

{
  "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "title": "Foundation Work",
  "description": "Excavation and concrete pouring for the foundation",
  "status": "in_progress",
  "startDate": "2026-03-01",
  "endDate": "2026-03-15",
  "actualStartDate": "2026-03-02",
  "actualEndDate": null,
  "durationDays": 14,
  "startAfter": "2026-02-28",
  "startBefore": "2026-03-10",
  "assignedUser": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "displayName": "Admin User",
    "email": "admin@example.com"
  },
  "assignedVendor": null,
  "area": { "id": "area-kitchen", "name": "Kitchen", "color": "#F59E0B", "ancestors": [] },
  "createdBy": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "displayName": "Admin User",
    "email": "admin@example.com"
  },
  "subtasks": [
    {
      "id": "s1",
      "title": "Excavate site",
      "isCompleted": true,
      "sortOrder": 0,
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-16T08:00:00.000Z"
    },
    {
      "id": "s2",
      "title": "Pour concrete",
      "isCompleted": false,
      "sortOrder": 1,
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z"
    }
  ],
  "dependencies": {
    "predecessors": [
      {
        "workItem": {
          "id": "prev-item-id",
          "title": "Site Survey",
          "status": "completed",
          "startDate": "2026-02-20",
          "endDate": "2026-02-22",
          "actualStartDate": "2026-02-20",
          "actualEndDate": "2026-02-22",
          "durationDays": 2,
          "assignedUser": null,
          "assignedVendor": null,
          "area": null,
          "createdAt": "2026-02-10T10:00:00.000Z",
          "updatedAt": "2026-02-22T10:00:00.000Z"
        },
        "dependencyType": "finish_to_start",
        "leadLagDays": 0
      }
    ],
    "successors": []
  },
  "budgets": [
    {
      "id": "b1",
      "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "description": "Foundation concrete materials",
      "plannedAmount": 8500.0,
      "confidence": "quote",
      "confidenceMargin": 0.05,
      "budgetCategory": {
        "id": "bc-materials",
        "name": "Materials",
        "description": "Raw materials and building supplies",
        "color": "#3B82F6",
        "sortOrder": 0,
        "createdAt": "2026-02-20T10:00:00.000Z",
        "updatedAt": "2026-02-20T10:00:00.000Z"
      },
      "budgetSource": {
        "id": "bs-1",
        "name": "ABC Bank Mortgage",
        "sourceType": "bank_loan"
      },
      "vendor": {
        "id": "v1",
        "name": "ABC Concrete",
        "trade": { "id": "trade-masonry", "name": "Masonry", "color": "#78716C" }
      },
      "actualCost": 2500.0,
      "actualCostPaid": 2500.0,
      "invoiceCount": 1,
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z"
    }
  ],
  "expectedCost": 8925.0,
  "grants": [{ "id": "sp-1", "name": "Energy Efficiency Grant", "reductionType": "percentage", "rate": 15 }],
  "waitsFor": null,
  "holdsUp": { "count": 3, "startingWithin14Days": 1 },
  "createdAt": "2026-02-15T10:00:00.000Z",
  "updatedAt": "2026-02-16T08:00:00.000Z"
}

With a company assigned, assignedVendor carries phone as well (null when the company has none).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

PATCH /api/work-items/:id

Updates a work item. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Work item UUID

Request body (all fields optional, at least one required):

{
  "title": "Updated Foundation Work",
  "description": "Updated description",
  "status": "completed",
  "startDate": "2026-03-01",
  "endDate": "2026-03-20",
  "actualStartDate": "2026-03-02",
  "actualEndDate": "2026-03-18",
  "durationDays": 19,
  "startAfter": null,
  "startBefore": null,
  "assignedUserId": "550e8400-e29b-41d4-a716-446655440000",
  "areaId": "area-kitchen"
}
Field Type Validation
title string 1-500 characters
description string | null Max 10000 characters; null clears the description
status string One of: not_started, in_progress, completed
startDate string | null ISO 8601 date or null to clear
endDate string | null ISO 8601 date or null to clear. Must be >= startDate if both are set
actualStartDate string | null ISO 8601 date or null to clear. Overrides auto-population on status transition.
actualEndDate string | null ISO 8601 date or null to clear. Overrides auto-population on status transition.
durationDays integer | null >= 0 or null to clear
startAfter string | null ISO 8601 date or null to clear
startBefore string | null ISO 8601 date or null to clear. Must be >= startAfter if both are set
assignedUserId string | null Valid active user UUID or null to unassign. Mutually exclusive with assignedVendorId.
assignedVendorId string | null Valid vendor UUID or null to unassign. Mutually exclusive with assignedUserId.
areaId string | null Valid area UUID or null to clear

Notes:

  • Nullable fields can be explicitly set to null to clear the value.
  • Actual dates auto-population: When status changes, actual dates are auto-populated as follows:
    • not_started -> in_progress: sets actualStartDate to today (if not explicitly provided)
    • in_progress -> completed: sets actualEndDate to today (if not explicitly provided)
    • not_started -> completed: sets actualEndDate to today (if not explicitly provided and not already set), and sets a missing actualStartDate to the resolved actual end date, the actualEndDate the work item has after the call (#2209; before, it was always today, which stored start > end for "Mark done" with a past date)
    • If actualStartDate or actualEndDate is explicitly included in the request body (even as null), the explicit value is used and auto-population is skipped for that field.
    • Backward moves (completed -> in_progress, in_progress -> not_started) keep both actual dates. Actual dates later than today are accepted.

Response (200 OK):

Returns the updated WorkItemDetail object. When the call changed status, actualStartDate or actualEndDate, the body also carries a top-level undo token (contract 11).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid field values, startDate > endDate, startAfter > startBefore, non-existent assignedUserId/assignedVendorId/areaId, both assignedUserId and assignedVendorId set (in the request, or one in the request and the other already stored), no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

DELETE /api/work-items/:id

Deletes a work item. Cascades to notes, subtasks, budget lines, and dependencies.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Work item UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

Tag Endpoints (REMOVED — EPIC-18)

All tag endpoints have been removed in EPIC-18. Tags are replaced by structured Area and Trade dimensions. See ADR-028.


Area Endpoints

Areas provide hierarchical spatial organization of the construction project. The area list is expected to remain small (fewer than ~100 areas) so no pagination is used. All area endpoints require authentication. Both admin and member roles can perform all area operations.

GET /api/areas

Returns all areas as a flat list sorted by sort_order ascending, then name ascending. The client builds the tree structure using parentId references.

Auth required: Yes

Query parameters:

Parameter Type Default Description
search string (none) Filter by name (case-insensitive substring match)
include string (none) Only the value counts (#2218, EPIC-21 contract 10): every area gains counts. Any other value → 400 VALIDATION_ERROR. Combines with search

Response (200 OK, without include):

{
  "areas": [
    {
      "id": "area-ground",
      "name": "Ground Floor",
      "parentId": null,
      "color": "#3B82F6",
      "description": "Main living level",
      "sortOrder": 0,
      "createdAt": "2026-03-19T10:00:00.000Z",
      "updatedAt": "2026-03-19T10:00:00.000Z"
    },
    {
      "id": "area-kitchen",
      "name": "Kitchen",
      "parentId": "area-ground",
      "color": "#F59E0B",
      "description": null,
      "sortOrder": 0,
      "createdAt": "2026-03-19T10:00:00.000Z",
      "updatedAt": "2026-03-19T10:00:00.000Z"
    }
  ]
}

Response type:

interface AreaResponse {
  id: string;
  name: string;
  parentId: string | null;
  color: string | null;
  description: string | null;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

With include=counts each element is an AreaWithCounts (AreaResponse plus counts and, from #2251, expectedCost { own, subtree }, see EPIC-21 contract 10). 🔒

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR include is a value other than counts
401 UNAUTHORIZED No valid session

GET /api/areas/:id

Returns a single area.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Area UUID

Response (200 OK):

{
  "area": {
    "id": "area-kitchen",
    "name": "Kitchen",
    "parentId": "area-ground",
    "color": "#F59E0B",
    "description": null,
    "sortOrder": 0,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Area ID does not exist

POST /api/areas

Creates a new area.

Auth required: Yes

Request body:

{
  "name": "Kitchen",
  "parentId": "area-ground",
  "color": "#F59E0B",
  "description": "Main kitchen area",
  "sortOrder": 0
}
Field Type Required Validation
name string Yes 1-200 characters. Must be unique among siblings under same parent.
parentId string | null No Must be a valid area UUID if provided. Default: null (top-level).
color string | null No Hex color code matching /^#[0-9A-Fa-f]{6}$/, or null
description string | null No Max 2000 characters
sortOrder integer No >= 0. Default (#2218): appended after its siblings, MAX(sort_order) + 1 among areas with the same parentId (IS NULL at top level), or 0 when it has none

Response (201 Created):

{
  "area": {
    "id": "area-kitchen",
    "name": "Kitchen",
    "parentId": "area-ground",
    "color": "#F59E0B",
    "description": "Main kitchen area",
    "sortOrder": 0,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty name, name too long, invalid color format, non-existent parentId
401 UNAUTHORIZED No valid session
409 CONFLICT An area with the same name already exists under the same parent

PATCH /api/areas/:id

Updates an area. All fields are optional; only provided fields are updated.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Area UUID

Request body (at least one field required):

{
  "name": "Updated Kitchen",
  "color": "#FFAA00"
}
Field Type Validation
name string 1-200 characters. Must be unique among siblings under the same parent.
parentId string | null Valid area UUID or null. Cannot create circular references (self or ancestor).
color string | null Hex color code or null to clear
description string | null Max 2000 characters; null clears the field
sortOrder integer >= 0

Rules (#2218): 🔒

  • Sibling names. Whenever name or parentId changes, the (new) name is checked against the siblings under the (new) parent, case-insensitively (LOWER(), which folds ASCII letters only), excluding the area itself → 409 CONFLICT. Before #2218 a parentId-only move skipped the check.
  • Move appends. A parentId that differs from the stored one, sent without sortOrder, sets sort_order to MAX(sort_order) + 1 among the new siblings (0 when there are none). With sortOrder the given value wins. A parentId equal to the stored one changes nothing.

Response (200 OK):

{
  "area": {
    "id": "area-kitchen",
    "name": "Updated Kitchen",
    "parentId": "area-ground",
    "color": "#FFAA00",
    "description": "Main kitchen area",
    "sortOrder": 0,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T11:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields, no fields provided, circular parentId reference, non-existent parentId
401 UNAUTHORIZED No valid session
404 NOT_FOUND Area ID does not exist
409 CONFLICT An area with the new name already exists under the same parent

DELETE /api/areas/:id

Deletes an area. Fails with 409 Conflict if the area or any of its descendant areas are referenced by work items or household items.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Area UUID

Response (200 OK, #2218; 204 before): DeleteAreaResponse, always with a token. 🔒

{ "undo": { "token": "u_6f1c0d9e2a7b4c5d8e9f0a1b2c3d4e5f", "expiresAt": "2026-10-19T09:00:30.000Z" } }

POST /api/undo/:token restores the deleted subtree and the photos' area links (EPIC-21 contract 11 › Delete snapshots).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Area ID does not exist
409 AREA_IN_USE Area or a descendant is referenced by work items or household items

Notes:

  • AREA_IN_USE: details is not sent: the error is raised with suppressDetails (#315 hardening), so its counts reach only the server log and the response carries code and message alone. Server-side counts (log only): workItemCount, householdItemCount.
  • Deletion cascades to all descendant areas (via ON DELETE CASCADE on parent_id), but only if the entire subtree has no item references.
  • The Areas index shows the blocked state before the user confirms, from the client roll-up of the include=counts totals (workItems.total + householdItems.total over the subtree, open or not, which is exactly the server's AREA_IN_USE condition). A 409 AREA_IN_USE that still arrives (another user booked an item meanwhile) makes the page refetch the tree and switch the dialog to its blocked state.

PATCH /api/areas/reorder

Saves the order of one sibling group (implemented by #2218). Specified under Reference List Reorder › Areas: one sibling group. 🔒


Trade Endpoints

Trades provide flat craft/skill classification. Trades are assigned to vendors and transitively to work items via vendor assignment. The trade list is expected to remain small (fewer than ~50 trades) so no pagination is used. All trade endpoints require authentication. Both admin and member roles can perform all trade operations.

GET /api/trades

Returns all trades sorted by sort_order ascending, then name ascending. Each item carries vendorCount (#2213), the number of companies whose trade_id is this trade; the Trades sheet shows it ("‹n› companies"), states it in the Merge dialog ("‹n› companies move to ‹kept trade›") and uses it to show Delete as blocked before the user confirms. Clients that display trades A–Z by their translated name sort client-side.

Auth required: Yes

Query parameters:

Parameter Type Default Description
search string (none) Filter by name (case-insensitive substring match)

Response (200 OK):

{
  "trades": [
    {
      "id": "trade-plumbing",
      "name": "Plumbing",
      "color": "#0EA5E9",
      "description": "Water supply, drainage, gas piping",
      "translationKey": "trades.plumbing",
      "sortOrder": 0,
      "createdAt": "2026-03-19T10:00:00.000Z",
      "updatedAt": "2026-03-19T10:00:00.000Z",
      "vendorCount": 3
    }
  ]
}

Response type:

interface TradeResponse {
  id: string;
  name: string;
  color: string | null;
  description: string | null;
  translationKey: string | null; // built-in trades only; the client shows t(translationKey) with `name` as fallback
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

// GET /api/trades items (#2213)
interface TradeListItem extends TradeResponse {
  vendorCount: number;
}

interface TradeListResponse {
  trades: TradeListItem[];
}

color, description and sortOrder stay in the data and the API, but since #2213 no view renders the colour or description and no screen edits the three (Architecture › Areas & Trades).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

GET /api/trades/:id

Returns a single trade.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Trade UUID

Response (200 OK):

{
  "trade": {
    "id": "trade-plumbing",
    "name": "Plumbing",
    "color": "#0EA5E9",
    "description": "Water supply, drainage, gas piping",
    "sortOrder": 0,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Trade ID does not exist

POST /api/trades

Creates a new trade.

Auth required: Yes

Request body:

{
  "name": "Custom Trade",
  "color": "#FF5733",
  "description": "Description of the trade",
  "sortOrder": 15
}
Field Type Required Validation
name string Yes 1-200 characters. Must be unique (case-insensitive).
color string | null No Hex color code matching /^#[0-9A-Fa-f]{6}$/, or null
description string | null No Max 2000 characters
sortOrder integer No >= 0. Default: 0

Response (201 Created):

{
  "trade": {
    "id": "trade-uuid",
    "name": "Custom Trade",
    "color": "#FF5733",
    "description": "Description of the trade",
    "sortOrder": 15,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty name, name too long, invalid color format
401 UNAUTHORIZED No valid session
409 CONFLICT A trade with the same name already exists (case-insensitive)

PATCH /api/trades/:id

Updates a trade. All fields are optional; only provided fields are updated.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Trade UUID

Request body (at least one field required):

{
  "name": "Updated Trade Name",
  "color": "#FFAA00"
}
Field Type Validation
name string 1-200 characters. Must be unique (case-insensitive).
color string | null Hex color code or null to clear
description string | null Max 2000 characters; null clears the field
sortOrder integer >= 0

Renaming a built-in trade (#2213). When name is given and its trimmed value differs from the stored name (case-sensitive), the server also sets translation_key to NULL, in the same UPDATE. Without it the client keeps showing t(translationKey) and the rename has no visible effect in any locale. The new name then shows as typed in every locale. A name equal to the stored one keeps the key. The same rule applies to PATCH /api/budget-categories/:id and PATCH /api/household-item-categories/:id.

Response (200 OK):

{
  "trade": {
    "id": "trade-plumbing",
    "name": "Updated Trade Name",
    "color": "#FFAA00",
    "description": "Water supply, drainage, gas piping",
    "sortOrder": 0,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T11:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Trade ID does not exist
409 CONFLICT A trade with the new name already exists (case-insensitive)

DELETE /api/trades/:id

Deletes a trade. Fails with 409 Conflict if the trade is referenced by any vendors.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Trade UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Trade ID does not exist
409 TRADE_IN_USE Trade is referenced by vendors

Notes:

  • TRADE_IN_USE: details is not sent: the error is raised with suppressDetails (#315 hardening), so its counts reach only the server log and the response carries code and message alone. Server-side counts (log only): vendorCount.
  • Trades with no vendor references can be freely deleted.
  • The Trades sheet shows Delete as blocked up front from the list's vendorCount; the 409 above stays the authority (another user may have assigned the trade since the list loaded). Trades have no GET /api/delete-impact entry (EPIC-21 contract 19): a delete that succeeds affects nothing else.

POST /api/trades/:id/merge

Merges the trade :id (the duplicate) into targetTradeId (the trade to keep), #2213: every company whose trade_id is :id gets targetTradeId, then :id is deleted. Nothing else references trades (vendors.trade_id is the only foreign key), so no data is lost. Not undoable (no snapshot, no undo token); the confirm dialog states the consequence with the duplicate's vendorCount.

Auth required: Yes (member or admin)

Path parameters:

Parameter Type Description
id string Trade UUID of the trade to remove

Request body:

{ "targetTradeId": "trade-electrical" }
Field Type Required Validation
targetTradeId string Yes Non-empty; an existing trade; not equal to :id

Behaviour. One transaction:

  1. :id must exist (404), targetTradeId must exist and differ from :id (400).
  2. UPDATE vendors SET trade_id = :targetTradeId, updated_at = :now WHERE trade_id = :id (the moved companies' updated_at changes, like any edit of their trade).
  3. DELETE FROM trades WHERE id = :id.

The kept trade's name, translationKey, color, description and sortOrder are unchanged.

Response (200 OK):

{
  "trade": {
    "id": "trade-electrical",
    "name": "Electrical",
    "color": null,
    "description": null,
    "translationKey": "trades.electrical",
    "sortOrder": 1,
    "createdAt": "2026-03-19T10:00:00.000Z",
    "updatedAt": "2026-03-19T10:00:00.000Z",
    "vendorCount": 5
  },
  "movedVendorCount": 2
}
interface TradeMergeRequest {
  targetTradeId: string;
}

interface TradeMergeResponse {
  trade: TradeListItem; // the kept trade, vendorCount after the merge
  movedVendorCount: number; // companies moved from :id (0 is valid)
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR targetTradeId missing or empty; equals :id; target trade does not exist
401 UNAUTHORIZED No valid session
404 NOT_FOUND Trade :id does not exist

A missing target is 400, not 404, matching how other bodies treat an unknown referenced id (e.g. tradeId on POST /api/vendors). A second merge of the same :id (double submit) returns 404.


Note Endpoints

Notes are nested under work items. All note endpoints require the parent work item to exist.

GET /api/work-items/:workItemId/notes

Returns all notes for a work item, sorted by created_at descending (newest first).

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Response (200 OK):

{
  "notes": [
    {
      "id": "n1",
      "content": "Foundation concrete has cured successfully.",
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User"
      },
      "createdAt": "2026-02-16T14:00:00.000Z",
      "updatedAt": "2026-02-16T14:00:00.000Z"
    }
  ]
}

The createdBy object contains id and displayName only (not the full user response). If the creating user has been deleted (SET NULL), createdBy is null.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

POST /api/work-items/:workItemId/notes

Adds a note to a work item. The createdBy is automatically set to the authenticated user.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Request body:

{
  "content": "Foundation concrete has cured successfully."
}
Field Type Required Validation
content string Yes 1-10000 characters

Response (201 Created):

{
  "id": "n1",
  "content": "Foundation concrete has cured successfully.",
  "createdBy": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "displayName": "Admin User"
  },
  "createdAt": "2026-02-16T14:00:00.000Z",
  "updatedAt": "2026-02-16T14:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty content
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

PATCH /api/work-items/:workItemId/notes/:noteId

Updates a note's content. Only the note's author or an admin can edit a note.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
noteId string Note UUID

Request body:

{
  "content": "Updated note content."
}
Field Type Required Validation
content string Yes 1-10000 characters

Response (200 OK): Returns the updated note object.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty content
401 UNAUTHORIZED No valid session
403 FORBIDDEN Non-admin user trying to edit another user's note
404 NOT_FOUND Work item or note does not exist

DELETE /api/work-items/:workItemId/notes/:noteId

Deletes a note. Only the note's author or an admin can delete a note.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
noteId string Note UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
403 FORBIDDEN Non-admin user trying to delete another user's note
404 NOT_FOUND Work item or note does not exist

Subtask Endpoints

Subtasks are nested under work items. All subtask endpoints require the parent work item to exist.

GET /api/work-items/:workItemId/subtasks

Returns all subtasks for a work item, sorted by sort_order ascending.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Response (200 OK):

{
  "subtasks": [
    {
      "id": "s1",
      "title": "Excavate site",
      "isCompleted": true,
      "sortOrder": 0,
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-16T08:00:00.000Z"
    },
    {
      "id": "s2",
      "title": "Pour concrete",
      "isCompleted": false,
      "sortOrder": 1,
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

POST /api/work-items/:workItemId/subtasks

Adds a subtask to a work item. If sortOrder is not provided, the subtask is appended to the end (max sort_order + 1).

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Request body:

{
  "title": "Excavate site",
  "sortOrder": 0
}
Field Type Required Validation
title string Yes 1-500 characters
sortOrder integer No >= 0

Response (201 Created):

{
  "id": "s1",
  "title": "Excavate site",
  "isCompleted": false,
  "sortOrder": 0,
  "createdAt": "2026-02-15T10:00:00.000Z",
  "updatedAt": "2026-02-15T10:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty title, non-integer sortOrder
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

PATCH /api/work-items/:workItemId/subtasks/:subtaskId

Updates a subtask's title, completion status, and/or sort order.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
subtaskId string Subtask UUID

Request body (at least one field required):

{
  "title": "Updated subtask title",
  "isCompleted": true,
  "sortOrder": 2
}
Field Type Validation
title string 1-500 characters
isCompleted boolean true or false
sortOrder integer >= 0

Response (200 OK): Returns the updated subtask object.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item or subtask does not exist

DELETE /api/work-items/:workItemId/subtasks/:subtaskId

Deletes a subtask.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
subtaskId string Subtask UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item or subtask does not exist

PATCH /api/work-items/:workItemId/subtasks/reorder

Bulk reorders all subtasks for a work item. Accepts an ordered array of subtask IDs and updates the sort_order of each subtask to match the array index.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Request body:

{
  "subtaskIds": ["s2", "s1", "s3"]
}
Field Type Required Validation
subtaskIds string[] Yes Array of subtask UUIDs. Must contain ALL subtask IDs belonging to this work item (no subset reordering).

Each subtask's sort_order is set to its index in the array (0-indexed). All subtask updatedAt timestamps are updated.

Response (200 OK):

{
  "subtasks": [
    {
      "id": "s2",
      "title": "Pour concrete",
      "isCompleted": false,
      "sortOrder": 0,
      "createdAt": "...",
      "updatedAt": "..."
    },
    {
      "id": "s1",
      "title": "Excavate site",
      "isCompleted": true,
      "sortOrder": 1,
      "createdAt": "...",
      "updatedAt": "..."
    },
    {
      "id": "s3",
      "title": "Inspect foundation",
      "isCompleted": false,
      "sortOrder": 2,
      "createdAt": "...",
      "updatedAt": "..."
    }
  ]
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty array, any subtask ID does not belong to this work item, array does not contain all subtask IDs for this work item, duplicate IDs
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

Dependency Endpoints

Dependencies define predecessor/successor relationships between work items for scheduling.

GET /api/work-items/:id/dependencies

Returns both predecessors and successors for a work item.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Work item UUID

Response (200 OK):

{
  "predecessors": [
    {
      "workItem": {
        "id": "prev-item-id",
        "title": "Site Survey",
        "status": "completed",
        "startDate": "2026-02-20",
        "endDate": "2026-02-22",
        "actualStartDate": "2026-02-20",
        "actualEndDate": "2026-02-22",
        "durationDays": 2,
        "assignedUser": null,
        "assignedVendor": null,
        "area": null,
        "createdAt": "2026-02-10T10:00:00.000Z",
        "updatedAt": "2026-02-22T10:00:00.000Z"
      },
      "dependencyType": "finish_to_start",
      "leadLagDays": 0
    }
  ],
  "successors": [
    {
      "workItem": {
        "id": "next-item-id",
        "title": "Framing",
        "status": "not_started",
        "startDate": null,
        "endDate": null,
        "actualStartDate": null,
        "actualEndDate": null,
        "durationDays": null,
        "assignedUser": null,
        "assignedVendor": null,
        "area": null,
        "createdAt": "2026-02-10T10:00:00.000Z",
        "updatedAt": "2026-02-10T10:00:00.000Z"
      },
      "dependencyType": "finish_to_start",
      "leadLagDays": 0
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

POST /api/work-items/:id/dependencies

Adds a dependency to a work item. The work item identified by :id becomes the successor (it depends on predecessorId).

Auth required: Yes

Path parameters:

Parameter Type Description
id string Successor work item UUID (the item that depends on the predecessor)

Request body:

{
  "predecessorId": "prev-item-id",
  "dependencyType": "finish_to_start",
  "leadLagDays": 3
}
Field Type Required Validation
predecessorId string Yes Must be a valid work item UUID, different from :id
dependencyType string No One of: finish_to_start, start_to_start, finish_to_finish, start_to_finish. Default: finish_to_start
leadLagDays integer No Lead (negative) or lag (positive) offset in days. Default: 0

Circular dependency detection: Before creating the dependency, the server performs a depth-first traversal of the dependency graph starting from the predecessor, following the successor direction. If the traversal reaches the current work item (:id), a cycle would be created and the request is rejected.

Response (201 Created):

{
  "predecessorId": "prev-item-id",
  "successorId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "dependencyType": "finish_to_start",
  "leadLagDays": 3
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing predecessorId, self-referencing dependency (predecessorId === :id), invalid dependencyType
401 UNAUTHORIZED No valid session
404 NOT_FOUND Either work item (successor or predecessor) does not exist
409 CIRCULAR_DEPENDENCY Adding this dependency would create a cycle. details is { cycle } (array of work item IDs in the detected cycle) and nothing else; there is no details.code
409 DUPLICATE_DEPENDENCY A dependency between these two work items already exists

PATCH /api/work-items/:id/dependencies/:predecessorId

Updates a dependency's properties (dependency type and/or lead/lag days).

Auth required: Yes

Path parameters:

Parameter Type Description
id string Successor work item UUID
predecessorId string Predecessor work item UUID

Request body (at least one field required):

{
  "dependencyType": "start_to_start",
  "leadLagDays": -2
}
Field Type Validation
dependencyType string One of: finish_to_start, start_to_start, finish_to_finish, start_to_finish
leadLagDays integer Lead (negative) or lag (positive) offset in days

Response (200 OK):

{
  "predecessorId": "prev-item-id",
  "successorId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "dependencyType": "start_to_start",
  "leadLagDays": -2
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR No fields provided, invalid dependencyType
401 UNAUTHORIZED No valid session
404 NOT_FOUND The dependency (this predecessor/successor pair) does not exist

DELETE /api/work-items/:id/dependencies/:predecessorId

Removes a dependency from a work item.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Successor work item UUID
predecessorId string Predecessor work item UUID

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204 No Content). Undo re-inserts the dependency and reschedules (contract 11 › Relation unlink snapshots).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND The dependency (this predecessor/successor pair) does not exist

Budget Line Endpoints

Budget lines are nested under work items. Each budget line represents a cost estimate or allocation with its own confidence level, optional vendor, budget category, and budget source. All budget line endpoints require the parent work item to exist. Budget lines are NOT paginated (a work item typically has fewer than ~20 budget lines).

GET /api/work-items/:workItemId/budgets

Returns all budget lines for a work item.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Response (200 OK):

{
  "budgets": [
    {
      "id": "b1",
      "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "description": "Foundation concrete materials",
      "plannedAmount": 8500.0,
      "confidence": "quote",
      "confidenceMargin": 0.05,
      "budgetCategory": {
        "id": "bc-materials",
        "name": "Materials",
        "description": "Raw materials and building supplies",
        "color": "#3B82F6",
        "sortOrder": 0,
        "createdAt": "2026-02-20T10:00:00.000Z",
        "updatedAt": "2026-02-20T10:00:00.000Z"
      },
      "budgetSource": {
        "id": "bs-1",
        "name": "ABC Bank Mortgage",
        "sourceType": "bank_loan"
      },
      "vendor": {
        "id": "v1",
        "name": "ABC Concrete",
        "trade": { "id": "trade-masonry", "name": "Masonry", "color": "#78716C" }
      },
      "actualCost": 2500.0,
      "actualCostPaid": 2500.0,
      "invoiceCount": 1,
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

POST /api/work-items/:workItemId/budgets

Creates a new budget line for a work item. The createdBy is automatically set to the authenticated user.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Request body:

{
  "description": "Foundation concrete materials",
  "plannedAmount": 8500.0,
  "confidence": "quote",
  "budgetCategoryId": "bc-materials",
  "budgetSourceId": "bs-1",
  "vendorId": "v1"
}
Field Type Required Validation
description string | null No Max 500 characters
plannedAmount number Yes Must be >= 0
confidence string No One of: own_estimate, professional_estimate, quote, invoice. Default: own_estimate
budgetCategoryId string | null No Must be a valid budget category UUID if provided
budgetSourceId string Yes Must be a valid budget source UUID
vendorId string | null No Must be a valid vendor UUID if provided
quantity number | null No >= 0
unit string | null No Max 100 characters
unitPrice number | null No >= 0
includesVat boolean No Default true (plannedAmount is gross); false = net, VAT added at VAT_RATE

Response (201 Created):

{
  "budget": {
    "id": "b1",
    "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "description": "Foundation concrete materials",
    "plannedAmount": 8500.0,
    "confidence": "quote",
    "confidenceMargin": 0.05,
    "budgetCategory": {
      "id": "bc-materials",
      "name": "Materials",
      "description": "Raw materials and building supplies",
      "color": "#3B82F6",
      "sortOrder": 0,
      "createdAt": "2026-02-20T10:00:00.000Z",
      "updatedAt": "2026-02-20T10:00:00.000Z"
    },
    "budgetSource": {
      "id": "bs-1",
      "name": "ABC Bank Mortgage",
      "sourceType": "bank_loan"
    },
    "vendor": {
      "id": "v1",
      "name": "ABC Concrete",
      "trade": { "id": "trade-masonry", "name": "Masonry", "color": "#78716C" }
    },
    "actualCost": 0,
    "actualCostPaid": 0,
    "invoiceCount": 0,
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-02-20T14:00:00.000Z",
    "updatedAt": "2026-02-20T14:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing plannedAmount or budgetSourceId, negative amount, invalid confidence value, non-existent budgetCategoryId/budgetSourceId/vendorId, description too long
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item ID does not exist

PATCH /api/work-items/:workItemId/budgets/:budgetId

Updates a budget line. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically. This endpoint supports same-table moves only (move within work items) — to move a budget line to a household item, use PATCH /api/invoices/:invoiceId/budget-lines/:id instead.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
budgetId string Budget line UUID

Request body (all fields optional, at least one required):

{
  "description": "Updated description",
  "plannedAmount": 9000.0,
  "confidence": "invoice",
  "budgetCategoryId": "bc-materials",
  "budgetSourceId": null,
  "vendorId": "v1"
}
Field Type Validation
description string | null Max 500 characters; null clears the description
plannedAmount number Must be >= 0
confidence string One of: own_estimate, professional_estimate, quote, invoice
budgetCategoryId string | null Valid budget category UUID or null to clear
budgetSourceId string | null Valid budget source UUID or null to clear
vendorId string | null Valid vendor UUID or null to clear

Response (200 OK):

Returns the updated WorkItemBudgetLine object wrapped in { "budget": ... }.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR No fields provided, negative amount, invalid confidence value, non-existent budgetCategoryId/budgetSourceId/vendorId, description too long
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item or budget line does not exist, or budget line does not belong to this work item

Notes:

  • Cross-table moves (work item to household item or vice versa) are not supported by this endpoint. Use PATCH /api/invoices/:invoiceId/budget-lines/:id to move budget lines between work items and household items.

DELETE /api/work-items/:workItemId/budgets/:budgetId

Deletes a budget line. Fails with 409 Conflict if the budget line has invoices linked to it.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
budgetId string Budget line UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item or budget line does not exist, or budget line does not belong to this work item
409 BUDGET_LINE_IN_USE Budget line has linked invoices and cannot be deleted

Notes:

  • BUDGET_LINE_IN_USE: details is not sent: the error is raised with suppressDetails (#315 hardening), so its counts reach only the server log and the response carries code and message alone. Server-side counts (log only): invoiceCount.
  • Budget lines with no linked invoices can be freely deleted.
  • The budget line must belong to the specified work item. If the budget line exists but belongs to a different work item, a 404 is returned.

GET /api/work-items/:workItemId/subsidy-payback

Returns the work item's share of every non-rejected grant linked to it, and the totals of the counted grants. Computed by the money engine (ADR-039, since #2228), so the figures equal the item's grant share on the budget overview and breakdown.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Response (200 OK):

{
  "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "minTotalPayback": 5400.0,
  "maxTotalPayback": 6600.0,
  "expectedTotalPayback": 6000.0,
  "subsidies": [
    {
      "subsidyProgramId": "sp-001",
      "name": "Grant A",
      "reductionType": "percentage",
      "reductionValue": 15,
      "minPayback": 5400.0,
      "maxPayback": 6600.0,
      "expectedPayback": 6000.0,
      "counted": true
    },
    {
      "subsidyProgramId": "sp-002",
      "name": "Grant B",
      "reductionType": "percentage",
      "reductionValue": 10,
      "minPayback": 3600.0,
      "maxPayback": 4400.0,
      "expectedPayback": 4000.0,
      "counted": false
    }
  ]
}

Response type definitions:

interface WorkItemSubsidyPaybackResponse {
  workItemId: string;
  /** Σ minPayback of the counted (approved / received) grants: best scenario. */
  minTotalPayback: number;
  /** Σ maxPayback of the counted grants: worst scenario. */
  maxTotalPayback: number;
  /** Σ expectedPayback of the counted grants: expected scenario. */
  expectedTotalPayback: number;
  /** One entry per non-rejected linked grant, ordered by name, id. Empty when none is linked. */
  subsidies: WorkItemSubsidyPaybackEntry[];
}

/** = SubsidyPaybackEntry (shared/src/types/budget.ts) */
interface WorkItemSubsidyPaybackEntry {
  subsidyProgramId: string;
  name: string;
  reductionType: 'percentage' | 'fixed';
  reductionValue: number;
  /** The item's share in the best scenario. */
  minPayback: number;
  /** The item's share in the worst scenario. */
  maxPayback: number;
  /** The item's share in the expected scenario. */
  expectedPayback: number;
  /** true for approved / received grants; the totals count only these. */
  counted: boolean;
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item does not exist

Notes (rules: ADR-039 §2–§3, EPIC-21 contract 2):

  • Every non-rejected grant linked to the item is listed. Only approved and received grants (counted: true) add to the totals; eligible and applied grants are listed with counted: false as possible grants.
  • Each entry is the item's pro-rata share of the grant's project-wide payout: the grant is computed over all its linked items' eligible lines (category filter as before), capped at maximum_amount, then spread over those lines by cost (largest remainder in cents). The share therefore depends on the other items linked to the same grant.
  • Line cost: a line linked to an invoice or offer (any status) costs its itemized amounts at face value in every scenario; an unlinked line costs its VAT-effective planned amount (net lines × 1 + VAT_RATE), at the low (best) or high (worst) end of its confidence margin (own estimate ±20%, professional estimate ±10%, quote ±5%, invoice 0%).
  • minPayback / maxPayback name the best / worst scenarios, not an order. For a capped grant the best-scenario share can be larger than the worst-scenario one, so minPayback > maxPayback (and minTotalPayback > maxTotalPayback) is possible.
  • Legacy fixed grants are shown unchanged: per linked item, reduction_value is split equally over that item's eligible lines (largest remainder, ties by line id) and each part is capped at the line's cost in the scenario; the sum is the uncapped payout; maximum_amount (if set) caps the payout, spread over the lines weighted by their capped parts (largest remainder); a linked item with no eligible line pays 0. So an uncapped fixed grant gives an item reductionValue (less where its lines cost less), and an item with no eligible line gets 0 (before #2228 this endpoint showed the flat reductionValue for it).
  • When no grant is linked to the item, all totals are 0 and subsidies is an empty array.

GET /api/work-items/:workItemId/dependent-household-items

Returns all household items that depend on this work item (via household_item_deps). This is the reverse direction of the GET /api/household-items/:id/dependencies endpoint. Not paginated.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Response (200 OK):

{
  "householdItems": [
    {
      "id": "hi-001",
      "name": "Kitchen Faucet",
      "category": "hic-fixtures",
      "status": "ordered",
      "targetDeliveryDate": "2026-05-15",
      "earliestDeliveryDate": "2026-05-10",
      "latestDeliveryDate": "2026-05-20"
    }
  ]
}

Response type definitions:

interface WorkItemDependentHouseholdItemsResponse {
  householdItems: WorkItemLinkedHouseholdItemSummary[];
}

interface WorkItemLinkedHouseholdItemSummary {
  id: string;
  name: string;
  category: HouseholdItemCategory;
  status: HouseholdItemStatus;
  targetDeliveryDate: string | null; // Computed by scheduling engine from constraints & dependencies
  earliestDeliveryDate: string | null; // User-editable constraint
  latestDeliveryDate: string | null; // User-editable constraint
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item does not exist

Notes:

  • Returns an empty array when no household items depend on this work item.
  • The response shape is a lightweight summary (no vendor, area, quantity, or budget info). Use GET /api/household-items/:id for full details on a specific household item.

EPIC-05: Budget Management

Budget management endpoints for tracking construction costs, vendors, invoices, financing sources, and subsidy programs. All endpoints in this section require authentication. Both admin and member roles can perform all budget operations (no role restriction).

Budget Category Response Shape

interface BudgetCategoryResponse {
  id: string;
  name: string;
  description: string | null;
  color: string | null;
  sortOrder: number;
  createdAt: string; // ISO 8601
  updatedAt: string; // ISO 8601
}

Budget Category Endpoints

Budget categories are a small, finite collection (typically 10-20 items). They are NOT paginated.

GET /api/budget-categories

Returns all budget categories sorted by sort_order ascending, then name ascending (#2215: the name tie-break makes the order deterministic when rows share a sort_order).

Auth required: Yes

Response (200 OK):

{
  "categories": [
    {
      "id": "bc-materials",
      "name": "Materials",
      "description": "Raw materials and building supplies",
      "color": "#3B82F6",
      "sortOrder": 0,
      "createdAt": "2026-02-20T10:00:00.000Z",
      "updatedAt": "2026-02-20T10:00:00.000Z"
    },
    {
      "id": "bc-labor",
      "name": "Labor",
      "description": "Contractor and worker labor costs",
      "color": "#EF4444",
      "sortOrder": 1,
      "createdAt": "2026-02-20T10:00:00.000Z",
      "updatedAt": "2026-02-20T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

POST /api/budget-categories

Creates a new budget category.

Auth required: Yes

Request body:

{
  "name": "Appliances",
  "description": "Kitchen and household appliances",
  "color": "#F472B6",
  "sortOrder": 10
}
Field Type Required Validation
name string Yes 1-100 characters. Must be unique (case-insensitive).
description string No Max 500 characters.
color string No Hex color code matching /^#[0-9A-Fa-f]{6}$/, or null
sortOrder integer No >= 0. Default: appended (see below)

Default position (#2215). When sortOrder is omitted the new category is appended: sort_order = MAX(sort_order) + 1 over the list (0 when the list is empty). Before #2215 the default was 0, which put a new row at the top among the rows sharing 0.

Response (201 Created):

{
  "budgetCategory": {
    "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "name": "Appliances",
    "description": "Kitchen and household appliances",
    "color": "#F472B6",
    "translationKey": null,
    "sortOrder": 10,
    "createdAt": "2026-02-20T14:00:00.000Z",
    "updatedAt": "2026-02-20T14:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty name, name too long, description too long, invalid color format, negative sortOrder
401 UNAUTHORIZED No valid session
409 CONFLICT A category with the same name already exists (case-insensitive)

PATCH /api/budget-categories/reorder

Saves the whole order of the list in one request (#2215). Body { "ids": string[] } with every id of the list exactly once; 200 OK with { "categories": [...] } in the new order; 409 REORDER_SET_MISMATCH when the ids are not exactly the current set. Full specification: Reference List Reorder.


GET /api/budget-categories/:id

Returns a single budget category.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Budget category UUID

Response (200 OK):

{
  "budgetCategory": {
    "id": "bc-materials",
    "name": "Materials",
    "description": "Raw materials and building supplies",
    "color": "#3B82F6",
    "translationKey": "budgetCategories.materials",
    "sortOrder": 0,
    "createdAt": "2026-02-20T10:00:00.000Z",
    "updatedAt": "2026-02-20T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget category ID does not exist

PATCH /api/budget-categories/:id

Updates a budget category's name, description, color, and/or sort order.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Budget category UUID

Request body (at least one field required):

{
  "name": "Building Materials",
  "description": "Updated description",
  "color": "#2563EB",
  "sortOrder": 0
}
Field Type Validation
name string 1-100 characters. Must be unique (case-insensitive).
description string | null Max 500 characters; null clears the description
color string | null Hex color code or null to clear
sortOrder integer >= 0

Renaming a built-in category (#2213). Same rule as PATCH /api/trades/:id: when name is given and its trimmed value differs from the stored name (case-sensitive), the server sets translation_key to NULL in the same UPDATE, so the typed name shows in every locale. A name equal to the stored one keeps the key.

Response (200 OK):

{
  "budgetCategory": {
    "id": "bc-materials",
    "name": "Building Materials",
    "description": "Updated description",
    "color": "#2563EB",
    "translationKey": null,
    "sortOrder": 0,
    "createdAt": "2026-02-20T10:00:00.000Z",
    "updatedAt": "2026-02-20T15:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget category ID does not exist
409 CONFLICT A category with the new name already exists (case-insensitive)

DELETE /api/budget-categories/:id

Deletes a budget category. Fails with 409 Conflict if the category is referenced by work item budget lines (via work_item_budgets.budget_category_id), by household item budget lines (via household_item_budgets.budget_category_id) or by subsidy programs (via subsidy_program_categories).

Auth required: Yes

Path parameters:

Parameter Type Description
id string Budget category UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget category ID does not exist
409 CATEGORY_IN_USE Category is referenced by budget lines or subsidy programs and cannot be deleted

Notes:

  • CATEGORY_IN_USE: details is not sent: the error is raised with suppressDetails (#315 hardening), so its counts reach only the server log and the response carries code and message alone. Server-side counts (log only): subsidyProgramCount, budgetLineCount, householdBudgetCount.

Vendor Response Shapes

Vendor summary (used in list responses):

interface VendorResponse {
  id: string;
  name: string;
  trade: TradeSummary | null;
  phone: string | null;
  email: string | null;
  address: string | null;
  notes: string | null;
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601
  updatedAt: string; // ISO 8601
}

Vendor detail (used in single-item responses, adds computed fields):

interface VendorDetailResponse extends VendorResponse {
  invoiceCount: number;
  contacts: VendorContact[]; // by createdAt, then id
  stillToPay: number;
  paid: number;
  offers: { count: number; amount: number; paid: number; stillToPay: number; remainingOnOffer: number };
  openTaskCount: number;
  onSiteDays: number;
  lastOnSite: string | null; // YYYY-MM-DD
}

Notes on computed fields (#2244):

  • invoiceCount is the total number of offers and invoices for this vendor (all statuses).
  • stillToPay, paid and offers come from one call of the money engine's computePayments over the company's own (invoices LEFT JOIN invoice_deposits) rows (ADR-039); the list rows carry the same three fields. stillToPay: pending records minus their non-refund progress payments, plus pending progress payments (also on offers); paid and submitted (claimed) never count; 0 if nothing is left. paid: paid + submitted, progress payments included, refunds netted. offers is MoneyOfferTotals (offers = status quotation).
  • openTaskCount is the number of work items assigned to the company whose status is not completed.
  • onSiteDays is the number of distinct dates of saved, manual daily_log entries whose metadata.vendorId names the company; lastOnSite is the latest of those dates (null when none).
  • outstandingBalance is removed (#2244); read stillToPay.

Vendor Endpoints

Vendors/contractors are a core budget management entity. The vendor list may grow to dozens of entries for a large construction project, so pagination is used. All vendor endpoints require authentication. Both admin and member roles can perform all vendor operations (no role restriction).

GET /api/vendors

Returns a paginated, searchable list of vendors (companies) sorted by name ascending. The Companies list (/companies, story 1.7, #2213) reads it.

Auth required: Yes

Query parameters:

Parameter Type Default Description
page integer 1 Page number (1-indexed)
pageSize integer 25 Items per page (max 100)
q string (none) Folded search over the company and its contact persons (see Search below). At most 500 characters before trimming (SEARCH_QUERY_RAW_MAX_LENGTH), else 400 VALIDATION_ERROR (#2213)
tradeIds string (none) Comma-separated trade ids the client matched q against by translated name; ORed into the q match (see Trade ids below). Ignored without a non-empty q
tradeId string (none) Filter by trade UUID. An unknown id is not an error; it matches no company. Combines with q (AND)
sortBy string name Sort field. One of: name, trade, last_on_site, still_to_pay, created_at, updated_at
sortOrder string asc Sort direction: asc or desc

Search (q, #2213). The list uses the same matcher as the vendor group of GET /api/search (EPIC-21 contract 6), with one field list shared by both, so a company the command search finds for a text is also in the list for that text. After trimming, an empty q is no filter. A company matches when fold(q) is a substring of fold() of any of: its name, its trade's stored name, its phone, its e-mail, or the name, phone or e-mail of any of its contact persons. When q holds at least 3 digits, it also matches when digits(q) is a substring of digits() of the company's phone or of any contact person's phone, so a digit run typed without spaces or separators finds a number stored with them. fold/digits are the SQL functions in Architecture › SQL functions (ä/ae, ö/oe, ü/ue, ß/ss and case fold alike); the client does no folding. q filters only; it does not change the sort (no relevance rank, unlike /api/search). The trade text matched is the stored trades.name; translated names are covered by tradeIds.

Trade ids (tradeIds, #2213). The translated display name of a built-in trade (t(translationKey)) exists only in the client, so the server cannot match it. The client therefore folds the typed text with foldSearchText and compares it (substring) with the folded display name of every trade it holds (useTrades), and sends the ids of the trades that match as tradeIds. The server then treats a company as matching q when the text match above holds or vendors.trade_id IN (tradeIds). Rules (shared with GET /api/search, one parser):

  • Comma-separated; entries trimmed, empties dropped, duplicates removed. Absent or empty = no trade term.
  • At most 50 entries after de-duplication and each 1–64 characters; otherwise 400 VALIDATION_ERROR with details.tradeIds. (Trades are a small list, see Trade Endpoints; 50 bounds the IN (…) list.)
  • Ids are bound parameters. Unknown ids are ignored (they match no company); no existence check, no error.
  • Only used together with a non-empty q. It widens the text match; it never filters on its own. tradeId stays the filter and still combines with AND, so tradeId=A&q=x&tradeIds=B returns companies of trade A that match x by text (B can't add any, since they are not in A).
  • The client sends it whenever at least one trade matches; an English stored name matching twice (text and id) is harmless.

Sort. last_on_site sorts by lastOnSite; companies without one come last in both directions. still_to_pay (#2244) sorts by stillToPay; it is computed in memory by the money engine (computePayments, the one Still-to-pay computation, ADR-039) for every company matching the filter, sorted, then paged. Every sort breaks ties by name ascending, then id ascending, so paging is stable.

Response (200 OK):

{
  "vendors": [
    {
      "id": "v1",
      "name": "ABC Plumbing",
      "trade": { "id": "trade-plumbing", "name": "Plumbing", "color": "#0EA5E9" },
      "phone": "555-0100",
      "email": "info@plumbing.example.com",
      "address": "123 Main St, Springfield",
      "notes": "Licensed and insured",
      "firstContactPhone": null,
      "contactCount": 2,
      "lastOnSite": "2026-02-18",
      "stillToPay": 1200.0,
      "paid": 3300.0,
      "offers": { "count": 1, "amount": 3000.0, "paid": 1000.0, "stillToPay": 0, "remainingOnOffer": 2000.0 },
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-20T10:00:00.000Z",
      "updatedAt": "2026-02-20T10:00:00.000Z"
    }
  ],
  "pagination": {
    "page": 1,
    "pageSize": 25,
    "totalItems": 1,
    "totalPages": 1
  },
  "scopeTotal": 1
}

scopeTotal (number, #2246): the number of companies matching only tradeId (the trade chip), ignoring q: the Companies list heading's "of m". Without tradeId it is the count of all companies. Always sent. The Companies list (ObjectList template, #2246) pages with pageSize=100 (infinite scroll) and keeps page/pageSize out of its own URL.

firstContactPhone (string | null): phone of the first contact person (by creation order) with a non-blank phone; null when none has one. List endpoint only. The UI shows it when the vendor has no phone of its own (a phone that is null or blank after trimming).

List-only fields (VendorListItem, shared/src/types/vendor.ts; story 1.7, #2213; always sent by this endpoint, optional in the type like firstContactPhone):

Field Type Meaning
contactCount number Number of contact persons (vendor_contacts rows) of the company. 0 when none
lastOnSite string | null YYYY-MM-DD: the latest entry_date of a saved (status = 'saved') daily_log diary entry whose metadata.vendorId is this company; null when there is none

lastOnSite source: daily_log is the only diary entry type that links a company (DailyLogMetadata.vendorId, validated against vendors on write). delivery entries carry the supplier as free text (metadata.vendor), which links nothing and is not used; drafts and automatic entries never count. The date is taken as stored (no cap at today). Computed in SQL (MAX(entry_date) grouped by json_extract(metadata, '$.vendorId'), joined to vendors) so sortBy=last_on_site sorts before paging; no index or schema change, a scan of the daily_log rows is cheap at this scale. Work-item actual dates are deliberately not a fallback: a task's actual start or end says when work was recorded as started or done, not that the company was on site, and mixing the two would make the column mean two things.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid query parameters (e.g., page < 1, pageSize > 100, invalid sortBy)
401 UNAUTHORIZED No valid session

POST /api/vendors

Creates a new vendor.

Auth required: Yes

Request body:

{
  "name": "ABC Plumbing",
  "tradeId": "trade-plumbing",
  "phone": "555-0100",
  "email": "info@plumbing.example.com",
  "address": "123 Main St, Springfield",
  "notes": "Licensed and insured"
}
Field Type Required Validation
name string Yes 1-200 characters
tradeId string No Must be a valid trade UUID
phone string No Max 50 characters
email string No Max 255 characters. Valid email format if provided.
address string No Max 500 characters
notes string No Max 10000 characters

The createdBy field is automatically set to the authenticated user's ID.

Response (201 Created):

{
  "vendor": {
    "id": "v1",
    "name": "ABC Plumbing",
    "trade": { "id": "trade-plumbing", "name": "Plumbing", "color": "#0EA5E9" },
    "phone": "555-0100",
    "email": "info@plumbing.example.com",
    "address": "123 Main St, Springfield",
    "notes": "Licensed and insured",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-02-20T14:00:00.000Z",
    "updatedAt": "2026-02-20T14:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty name, name too long, invalid email format, other field length violations
401 UNAUTHORIZED No valid session

GET /api/vendors/:id

Returns a single vendor with full details including computed invoice statistics.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Vendor UUID

Response (200 OK):

{
  "vendor": {
    "id": "v1",
    "name": "ABC Plumbing",
    "trade": { "id": "trade-plumbing", "name": "Plumbing", "color": "#0EA5E9" },
    "phone": "555-0100",
    "email": "info@plumbing.example.com",
    "address": "123 Main St, Springfield",
    "notes": "Licensed and insured",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "invoiceCount": 3,
    "contacts": [],
    "stillToPay": 4500.0,
    "paid": 0,
    "offers": { "count": 0, "amount": 0, "paid": 0, "stillToPay": 0, "remainingOnOffer": 0 },
    "openTaskCount": 2,
    "onSiteDays": 4,
    "lastOnSite": "2026-02-18",
    "createdAt": "2026-02-20T10:00:00.000Z",
    "updatedAt": "2026-02-20T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor ID does not exist

PATCH /api/vendors/:id

Updates a vendor's information. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Vendor UUID

Request body (all fields optional, at least one required):

{
  "name": "ABC Plumbing Co.",
  "tradeId": "trade-hvac",
  "phone": "555-0101",
  "email": "contact@plumbing.example.com",
  "address": "456 Oak Ave, Springfield",
  "notes": "Updated notes"
}
Field Type Validation
name string 1-200 characters
tradeId string | null Valid trade UUID or null to clear
phone string | null Max 50 characters; null clears the field
email string | null Max 255 characters. Valid email format if provided. null clears the field.
address string | null Max 500 characters; null clears the field
notes string | null Max 10000 characters; null clears the field

Response (200 OK):

{
  "vendor": {
    "id": "v1",
    "name": "ABC Plumbing Co.",
    "trade": { "id": "trade-hvac", "name": "HVAC", "color": "#8B5CF6" },
    "phone": "555-0101",
    "email": "contact@plumbing.example.com",
    "address": "456 Oak Ave, Springfield",
    "notes": "Updated notes",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "invoiceCount": 3,
    "contacts": [],
    "stillToPay": 4500.0,
    "paid": 0,
    "offers": { "count": 0, "amount": 0, "paid": 0, "stillToPay": 0, "remainingOnOffer": 0 },
    "openTaskCount": 2,
    "onSiteDays": 4,
    "lastOnSite": "2026-02-18",
    "createdAt": "2026-02-20T10:00:00.000Z",
    "updatedAt": "2026-02-20T15:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor ID does not exist

DELETE /api/vendors/:id

Deletes a vendor. Fails with 409 Conflict if the vendor has invoices or is referenced by work item budget lines (via work_item_budgets.vendor_id).

Auth required: Yes

Path parameters:

Parameter Type Description
id string Vendor UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor ID does not exist
409 VENDOR_IN_USE Vendor has invoices or is referenced by budget lines and cannot be deleted

Notes:

  • VENDOR_IN_USE: details is not sent: the error is raised with suppressDetails (#315 hardening), so its counts reach only the server log and the response carries code and message alone. Server-side counts (log only): invoiceCount, budgetLineCount.
  • Vendors with no invoices and no budget line references can be freely deleted.

Vendor Contact Endpoints

Contact persons of a company (vendor_contacts), nested under /api/vendors/:vendorId/contacts. Not paginated. Auth required on all: yes (member and admin).

interface VendorContact {
  id: string;
  vendorId: string;
  firstName: string | null;
  lastName: string | null;
  name: string; // computed: "firstName lastName", or whichever is set
  role: string | null;
  phone: string | null;
  email: string | null;
  notes: string | null;
  createdAt: string;
  updatedAt: string;
}

Body fields (create and update): firstName, lastName, role (each string | null, max 100), phone (max 50), email (max 255, format: email), notes (max 2000). At least one of firstName / lastName must be set after the write. Unknown properties are stripped.

Route Response Errors
GET /api/vendors/:vendorId/contacts 200 { "contacts": VendorContact[] } 401; 404 NOT_FOUND vendor
POST /api/vendors/:vendorId/contacts 201 { "contact": VendorContact } 400 VALIDATION_ERROR (no name, too long, bad email); 401; 404 vendor
PATCH /api/vendors/:vendorId/contacts/:contactId 200 { "contact": VendorContact } 400 (empty body, no name left, too long); 401; 404 vendor or contact

DELETE /api/vendors/:vendorId/contacts/:contactId

Deletes a contact person.

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204). Undo re-inserts the contact with its id and timestamps (contract 11 › Relation unlink snapshots).

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor or contact not found

Invoice Response Shape

type InvoiceStatus = 'pending' | 'paid' | 'claimed';
type InvoiceDepositStatus = 'pending' | 'paid' | 'claimed';
type InvoiceDepositEntryType = 'deposit' | 'refund';

interface InvoiceBudgetLineSummary {
  id: string; // invoice_budget_lines.id
  workItemBudgetId: string | null;
  householdItemBudgetId: string | null;
  itemizedAmount: number;
  budgetLineDescription: string | null; // from the linked budget line
  categoryName: string | null; // from the linked budget line's category
  categoryColor: string | null;
  plannedAmount: number; // from the linked budget line
}

interface InvoiceDeposit {
  id: string;
  invoiceId: string;
  amount: number; // > 0
  dueDate: string; // YYYY-MM-DD
  paidDate: string | null; // YYYY-MM-DD when status is paid or claimed
  claimedDate: string | null; // YYYY-MM-DD when status is claimed
  description: string | null; // max 500 chars
  status: InvoiceDepositStatus;
  entryType: InvoiceDepositEntryType; // 'deposit' | 'refund', default 'deposit', immutable after creation (Story #1876)
  budgetSourceId: string | null; // Story #1891: optional direct link to a budget source; tagged deposits contribute 100% to their source
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601 datetime
  updatedAt: string; // ISO 8601 datetime
}

interface InvoiceResponse {
  id: string;
  vendorId: string;
  vendorName: string; // Resolved from vendors table
  invoiceNumber: string | null;
  amount: number;
  date: string; // ISO 8601 date (YYYY-MM-DD)
  dueDate: string | null; // ISO 8601 date
  status: InvoiceStatus;
  notes: string | null;
  budgetLines: InvoiceBudgetLineSummary[]; // Linked budget lines with itemized amounts
  remainingAmount: number; // amount - SUM(budgetLines[].itemizedAmount)
  deposits: InvoiceDeposit[]; // ordered by dueDate ASC, createdAt ASC
  finalPaymentAmount: number; // amount - SUM(deposits[].amount), >= 0
  openAmount?: number; // Always present on GET /api/invoices rows (InvoiceListRow, #2238); present on GET /api/invoices/:invoiceId (InvoiceDetail, #2239); absent on vendor-list responses
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601
  updatedAt: string; // ISO 8601
}

Notes on budgetLines and remainingAmount:

  • budgetLines is always included in invoice responses (empty array if no budget lines are linked).
  • remainingAmount is computed server-side: invoice.amount - SUM(budgetLines[].itemizedAmount). It represents the portion of the invoice not yet attributed to any budget line.
  • The previous workItemBudgetId field has been removed from the invoice response (EPIC-15). Budget line linkage is now exclusively managed through the invoice_budget_lines junction table.

Notes on deposits and finalPaymentAmount:

  • deposits is ordered by dueDate ascending, then createdAt ascending. It is always populated with the invoice's full progress-payment list (deposits and refunds, every status) on every endpoint that returns an invoice: the detail, GET /api/vendors/:vendorId/invoices (every row goes through toInvoice) and, since #2238, GET /api/invoices on every chip and filter (before #2238 only when openOnly=true). finalPaymentAmount is refund-aware (computed from the full deposits data).
  • finalPaymentAmount is computed server-side: invoice.amount - Σ(deposit-type entries) - Σ(refund-type entries where status is paid or claimed). It represents the amount still awaiting payment after all deposit installments and received refunds. With no refund entries, this is identical to the pre-refund formula. openAmount (Story #2046, see the openOnly note above) is a separate, additive figure — it is not a replacement for finalPaymentAmount.

Notes on invoice status:

  • pending: Invoice received, payment not yet made (default)
  • paid: Payment has been completed
  • claimed: Invoice has been submitted for reimbursement (e.g., to a subsidy program or financing source)
  • quotation: A quote, not yet a bill. It is converted into the final invoice in place (new amount, lines and status pending or paid) with POST /api/invoices/:invoiceId/convert-quotation (Story #2107). paid is only a status: the invoice records no payment date.

The previous overdue status has been removed. Overdue detection is better handled as a computed state by comparing dueDate to the current date, rather than a manually-set status.


Invoice Endpoints

Invoices are nested under vendors and track payments for construction work. All invoice endpoints require authentication. Both admin and member roles can perform all invoice operations (no role restriction). All invoice endpoints require the parent vendor to exist.

The invoice list is NOT paginated. A vendor typically has fewer than ~50 invoices; pagination adds complexity without benefit at this scale.

GET /api/vendors/:vendorId/invoices

Returns all invoices for a vendor, sorted by date descending (newest first).

Auth required: Yes

Path parameters:

Parameter Type Description
vendorId string Vendor UUID

Response (200 OK):

{
  "invoices": [
    {
      "id": "inv-1",
      "vendorId": "v1",
      "vendorName": "ABC Construction",
      "invoiceNumber": "INV-2026-001",
      "amount": 2500.0,
      "date": "2026-03-15",
      "dueDate": "2026-04-15",
      "status": "pending",
      "notes": "First payment for foundation work",
      "budgetLines": [
        {
          "id": "ibl-1",
          "workItemBudgetId": "b1",
          "householdItemBudgetId": null,
          "itemizedAmount": 2500.0,
          "budgetLineDescription": "Foundation concrete materials",
          "categoryName": "Materials",
          "categoryColor": "#3B82F6",
          "plannedAmount": 8500.0
        }
      ],
      "remainingAmount": 0,
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-03-15T10:00:00.000Z",
      "updatedAt": "2026-03-15T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor ID does not exist

POST /api/vendors/:vendorId/invoices

Creates a new invoice for a vendor. The createdBy is automatically set to the authenticated user. Optionally, in the same transaction, it creates one cost line for the full amount on a task or purchase (costLine) and links a Paperless document (paperlessDocumentId) (#2241). Further budget line linking is done via the POST /api/invoices/:invoiceId/budget-lines endpoint (EPIC-15).

Auth required: Yes

Path parameters:

Parameter Type Description
vendorId string Vendor UUID

Request body:

{
  "invoiceNumber": "INV-2026-001",
  "amount": 2500.0,
  "date": "2026-03-15",
  "dueDate": "2026-04-15",
  "status": "pending",
  "notes": "First payment for foundation work"
}
Field Type Required Validation
invoiceNumber string No Max 100 characters
amount number Yes Must be > 0
date string Yes ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR
dueDate string No ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR. Must be >= date if provided.
status string No One of: pending, paid, claimed, quotation. Default: pending
notes string No Max 10000 characters
costLine object No { targetType: 'work_item' | 'household_item', targetId: string } (targetId min length 1). No other keys.
paperlessDocumentId integer No >= 1. Links that Paperless-ngx document to the new invoice.

Atomic cost line and document link (#2241): the invoice, the cost line and the document link are written in one database transaction; if any step fails nothing is created.

  • costLine creates one new budget line on the task (work_item) or purchase (household_item): plannedAmount = invoice amount, includesVat: true, confidence quote for status quotation else invoice, vendorId = the path vendor, budgetSourceId = the discretionary funding source (seeded by migration 0021, cannot be deleted, so it always exists), no description. It is never matched to an existing line. The line is linked to the invoice with itemizedAmount = amount, so remainingAmount is 0 in the response.
  • paperlessDocumentId creates the document link with attachment type quotation for status quotation, else invoice.
  • The invoice_created diary event fires once, after the commit.
  • Unknown body properties are stripped, not rejected (additionalProperties: false under Fastify's removeAdditional).

Response (201 Created):

{
  "invoice": {
    "id": "inv-1",
    "vendorId": "v1",
    "vendorName": "ABC Construction",
    "invoiceNumber": "INV-2026-001",
    "amount": 2500.0,
    "date": "2026-03-15",
    "dueDate": "2026-04-15",
    "status": "pending",
    "notes": "First payment for foundation work",
    "budgetLines": [],
    "remainingAmount": 2500.0,
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-03-15T10:00:00.000Z",
    "updatedAt": "2026-03-15T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing amount or date, amount <= 0, invalid date format, dueDate < date, invalid status, bad costLine or paperlessDocumentId
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor ID does not exist, or the task or purchase in costLine does not exist (nothing is created)

PATCH /api/vendors/:vendorId/invoices/:invoiceId

Updates an invoice. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically.

Auth required: Yes

Path parameters:

Parameter Type Description
vendorId string Source vendor UUID — used to locate the invoice being updated
invoiceId string Invoice UUID

Request body (all fields optional, at least one required):

{
  "invoiceNumber": "INV-2026-001-REV",
  "amount": 2750.0,
  "date": "2026-03-16",
  "dueDate": "2026-04-16",
  "status": "paid",
  "notes": "Updated after revision",
  "vendorId": "v2"
}
Field Type Validation
invoiceNumber string | null Max 100 characters; null clears the field
amount number Must be > 0. When the new amount is lower than the current one (compared in cents), the sum of linked itemizedAmount values must not exceed the new amount (ITEMIZED_SUM_EXCEEDS_INVOICE). Deposits are never checked: net deposits may exceed the new amount (#2188). An unchanged or raised amount is not checked. The check and the write run in one transaction.
date string ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR
dueDate string | null ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR; null clears the field. Must be >= date if both are set (uses existing date if not being updated).
status string One of: pending, paid, claimed, quotation
notes string | null Max 10000 characters; null clears the field
vendorId string Min length 1. Target vendor to reassign the invoice to. Omit to leave the vendor unchanged (backward compatible). If it references a vendor that does not exist, the endpoint returns 404 and the invoice is left unchanged.

Response (200 OK):

When the call changed status, the body also carries a top-level undo sibling of invoice (contract 11).

{
  "invoice": {
    "id": "inv-1",
    "vendorId": "v1",
    "vendorName": "ABC Construction",
    "invoiceNumber": "INV-2026-001-REV",
    "amount": 2750.0,
    "date": "2026-03-16",
    "dueDate": "2026-04-16",
    "status": "paid",
    "notes": "Updated after revision",
    "budgetLines": [
      {
        "id": "ibl-1",
        "workItemBudgetId": "b1",
        "householdItemBudgetId": null,
        "itemizedAmount": 2500.0,
        "budgetLineDescription": "Foundation concrete materials",
        "categoryName": "Materials",
        "categoryColor": "#3B82F6",
        "plannedAmount": 8500.0
      }
    ],
    "remainingAmount": 250.0,
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-03-15T10:00:00.000Z",
    "updatedAt": "2026-03-20T14:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR No fields provided, amount <= 0, malformed or impossible date (e.g. 2026-02-31), dueDate < date, invalid status
400 ITEMIZED_SUM_EXCEEDS_INVOICE amount is lowered below the sum of linked itemized amounts. details: { invoiceTotal, itemizedTotal }, where invoiceTotal is the requested new amount
401 UNAUTHORIZED No valid session
404 NOT_FOUND Source vendor (path vendorId) or invoice ID does not exist, the invoice does not belong to the source vendor, or the target vendor (body vendorId) does not exist (message: "Vendor not found")

Notes:

  • The invoice must belong to the specified source vendor (path vendorId). If the invoice exists but belongs to a different vendor, a 404 is returned (not 403) to avoid leaking information about other vendors' invoices.
  • Vendor reassignment: The path vendorId identifies the source vendor (used to locate the invoice); the optional vendorId in the request body is the target vendor to reassign the invoice to. If the body vendorId references a vendor that does not exist, the endpoint returns 404 (NOT_FOUND, "Vendor not found") and the invoice is left unchanged. Omitting the body vendorId leaves the vendor unchanged (backward compatible). Reassignment preserves the invoice's budget-line links.

DELETE /api/vendors/:vendorId/invoices/:invoiceId

Deletes an invoice.

Auth required: Yes

Path parameters:

Parameter Type Description
vendorId string Vendor UUID
invoiceId string Invoice UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Vendor ID or invoice ID does not exist, or invoice does not belong to this vendor

Notes:

  • Deleting an invoice will update the vendor's computed stillToPay, paid, offers and invoiceCount fields (visible in subsequent GET /api/vendors/:id responses).
  • The invoice must belong to the specified vendor. If the invoice exists but belongs to a different vendor, a 404 is returned.

Source Reports

Source reports provide detailed cost tracking for a specific budget source, filtered by status. Reports include invoices linked to the source through budget lines, their allocations, and associated documents (Paperless-ngx references with archive serial numbers and titles). Reports degrade gracefully when Paperless-ngx is unreachable—documents show with null metadata rather than failing the entire request.

All source report endpoints require authentication. Both admin and member roles can perform all operations (no role restriction).

GET /api/source-reports

Generate a source report for a given budget source and report type.

Auth required: Yes

Query parameters:

Parameter Type Required Description
type string Yes Report type: budget-overview (all statuses), claim (pending+paid), proof-of-funds (claimed only)
sourceId string Yes Budget source UUID

Response (200 OK):

{
  "report": {
    "type": "claim",
    "source": {
      "id": "src-1",
      "name": "Bank Loan A",
      "sourceType": "bank_loan",
      "reference": "Loan #2026-001",
      "contactAddress": "123 Finance St, City"
    },
    "invoices": [
      {
        "invoiceId": "inv-1",
        "vendorId": "v-1",
        "vendorName": "Builder Co",
        "invoiceNumber": "INV-2026-001",
        "date": "2026-03-01",
        "status": "paid",
        "invoiceAmount": 5000.0,
        "allocatedAmount": 2500.0,
        "lineKind": "invoice",
        "isSplit": false,
        "splitKind": null,
        "documents": [
          {
            "documentId": 42,
            "archiveSerialNumber": 1042,
            "title": "Invoice from Builder Co",
            "attachmentType": "invoice"
          }
        ],
        "budgetLinesForSource": [
          {
            "id": "ibl-1",
            "description": "Foundation works",
            "allocatedPortion": 2500.0,
            "linkedItem": {
              "type": "work_item",
              "id": "wi-1",
              "name": "Foundation",
              "areaId": "area-1",
              "areaName": "Basement"
            }
          }
        ],
        "depositsVisibleToSource": []
      }
    ],
    "totalAmount": 2500.0,
    "unallocatedInvoices": [
      {
        "invoiceId": "inv-2",
        "vendorId": "v-2",
        "vendorName": "Supplier B",
        "invoiceNumber": "INV-2026-002",
        "date": "2026-03-05",
        "status": "pending",
        "invoiceAmount": 1000.0
      }
    ],
    "generatedAt": "2026-03-20T14:30:00.000Z"
  }
}
Field Type Description
type string Echoed report type (budget-overview, claim, or proof-of-funds)
source object Budget source summary (id, name, sourceType, reference, contactAddress)
invoices array Invoices with budget lines linked to this source, filtered by report status slice
invoices[].invoiceId string Invoice UUID
invoices[].vendorId string Vendor UUID
invoices[].vendorName string Vendor display name
invoices[].invoiceNumber string | null Invoice number from the vendor (may be null)
invoices[].date string Invoice date (YYYY-MM-DD)
invoices[].status string Invoice status (unchanged; shows the invoice's own status, not per-piece status)
invoices[].invoiceAmount number Total invoice amount
invoices[].allocatedAmount number Net contribution to this source for the report's status slice, rounded to 2dp. Negative for refund-adjustment lines. Combines Rail A (line-derived pro-rata; tagged deposits omitted from the line apportionment but still reducing the residual) + Rail B (tagged deposits 100% to source).
invoices[].lineKind string invoice if allocatedAmount > 0, refund-adjustment if < 0
invoices[].isSplit boolean True iff the invoice's funding spans 2+ distinct budget sources, counted across both budget lines (via work_item_budgets/household_item_budgets) and deposits tagged with a budgetSourceId. A deposit-only invoice with two differently-tagged deposits is split; an invoice whose lines and tagged deposit all point at the same source is not.
invoices[].splitKind 'lines' | 'deposits' | 'both' | null Discriminates why the invoice is split. The predicate per arm is "does this arm contain a source ≠ the requested sourceId" — not "does this arm contain ≥2 distinct sources". 'lines': a budget line resolves to a foreign source. 'deposits': a tagged deposit references a foreign source. 'both': at least one of each. null: not split. A deposit-only foreign source paired with exactly one line-source (the requested source itself) still yields 'deposits' — the single-line arm is not exempt from the foreign-source check. splitKind !== null iff isSplit === true (structural invariant, not runtime-asserted). Never discloses the other source's identity, only the categorical value (#1911).
invoices[].budgetLinesForSource array Budget line subtraction rows scoped to the requested sourceId — not all of the invoice's lines. Lines funded by another source are absent, not present with allocatedPortion: 0. Absence semantics: [] means "none of this invoice's lines belong to this source" (or, for claim reports, none contributes a non-zero amount) — not "this invoice has no lines"; invoices included only via a tagged deposit (Rail B) are always []. Used client-side for exclusions (client subtracts from allocatedAmount). Never the basis for an unscoped conclusion such as "is this invoice split?" — use isSplit/splitKind (ADR-036). See "Budget Line Scope" below.
invoices[].budgetLinesForSource[].id string Budget line ID
invoices[].budgetLinesForSource[].description string | null Budget line description or null
invoices[].budgetLinesForSource[].allocatedPortion number Contribution of this line to the source in this report's status slice (subtraction-only, never independently summed)
invoices[].budgetLinesForSource[].linkedItem object | null Linked work item or household item: { type: 'work_item' | 'household_item', id, name, areaId, areaName } or null if unassigned
invoices[].budgetLinesForSource[].linkedItem.areaId string | null ID of the item's own leaf area (the direct area assignment), or null if the item has no area assignment
invoices[].budgetLinesForSource[].linkedItem.areaName string | null Name of the item's own leaf area (the direct area assignment), or null if the item has no area assignment. Never a parent-area path — always the leaf area name only.
invoices[].documents array Paperless-ngx document references for this invoice (filtered by the attachment-tier rule below, Story #1930)
invoices[].documents[].documentId number Paperless-ngx document ID
invoices[].documents[].archiveSerialNumber number | null Archive serial number from Paperless-ngx (null if Paperless unreachable/unconfigured)
invoices[].documents[].title string | null Document title from Paperless-ngx (null if Paperless unreachable/unconfigured)
invoices[].documents[].attachmentType string | null Attachment tier: quotation, deposit, invoice, or null for untagged (see Attachment Tier Rule below)
invoices[].depositsVisibleToSource array Deposit rows visible to the requested sourceId: the invoice's untagged deposits plus those tagged to this source. Deposits tagged to another source are absent. Absence semantics: [] means "no untagged and no this-source-tagged deposits" — not "this invoice has no deposits". Same-scope predicates (e.g. "does this source hold a tagged deposit?") are sound over this array; unscoped ones are not (ADR-036). Informational (never summed).
invoices[].depositsVisibleToSource[].id string Deposit ID
invoices[].depositsVisibleToSource[].amount number Deposit/refund amount (stored positive; display logic applies sign from entryType)
invoices[].depositsVisibleToSource[].status string Deposit status: pending, paid, claimed
invoices[].depositsVisibleToSource[].entryType string deposit or refund (Story #1876)
invoices[].depositsVisibleToSource[].dueDate string Due date (YYYY-MM-DD)
invoices[].depositsVisibleToSource[].paidDate string | null Paid date (YYYY-MM-DD) or null
invoices[].depositsVisibleToSource[].claimedDate string | null Claimed date (YYYY-MM-DD) or null
invoices[].depositsVisibleToSource[].budgetSourceId string | null Always either the requested sourceId (tagged to this source) or null (untagged). Never another source's ID — foreign-tagged deposits are filtered out of the array entirely. Story #1891.
totalAmount number Sum of all allocatedAmount values, computed from rounded per-line values (Rail A + Rail B)
unallocatedInvoices array Invoices matching the status slice with zero budget lines for this source AND no tagged deposits to this source
generatedAt string ISO 8601 timestamp when the report was generated

Source Scope — budgetLinesForSource[] and depositsVisibleToSource[] are scoped projections:

Both arrays are projections scoped to the requested sourceId, and their names say so (#2017). Neither is the invoice's full contents, and an empty array is never evidence that the invoice has no lines or no deposits.

invoices[].budgetLinesForSource[] contains only the invoice's budget lines funded by the requested sourceId, never the invoice's full set of lines. The Rail A query in sourceReportService.ts filters on work_item_budgets.budget_source_id = :sourceId / household_item_budgets.budget_source_id = :sourceId before any per-line detail is built, so a line belonging to a different source is absent from the array entirely — it is not present with allocatedPortion: 0.

invoices[].depositsVisibleToSource[] contains the invoice's untagged deposits plus those tagged to the requested sourceId. A deposit tagged to a different source is absent entirely, so every row's budgetSourceId is either null or the requested sourceId.

Consequences that consumers must not get wrong:

  • budgetLinesForSource[] is a subtraction basis, not an inventory. It exists so the client can subtract excluded lines from allocatedAmount (which is itself this-source-only). Summing it does not reconstruct invoiceAmount, and its length is not the invoice's line count.
  • Absence of a second line (or of a foreign-tagged deposit) is not evidence that the invoice is single-source. Whether an invoice's funding spans multiple sources is answered only by invoices[].isSplit, which is computed by a separate UNION over budget lines and tagged deposits across all sources, and why it is split only by invoices[].splitKind. Inferring either from budgetLinesForSource.length, from depositsVisibleToSource, or from sum(allocatedPortion) < invoiceAmount is a proxy that misclassifies deposit-only splits and invoices with unfunded remainders. This distinction is load-bearing for the split/deposit-reduced legend annotations (#1898, #1965) and for the splitKind discriminator (#1911).
  • Same-scope predicates over these arrays are correct. "Does this source hold a tagged deposit on this invoice?" is a question about the requested source, so depositsVisibleToSource.some((d) => d.budgetSourceId === sourceId) answers it exactly (hasOwnTaggedDeposit in buildReportContent.ts). The rule is scope-matching, not a ban on reading the arrays — see ADR-036: Scoped Projections and Predicate Scope.

Breaking rename (#2017). These fields were previously named budgetLines and deposits. The rename changed the JSON field names on this endpoint with no compatibility shim: the only consumer is the bundled client, which ships in the same container from the same repository, and no reference exists in docs/ or outside this repository.

One further filter applies to claim reports only: a this-source line whose contribution rounds to exactly 0 is omitted. This drops structurally-present-but-fully-offset lines (a quotation invoice whose bank-funded portion is eliminated by invoiced-but-untagged sweepable deposits). For budget-overview and proof-of-funds, zero-contribution lines of the requested source are retained.

Attachment Tier Rule (Story #1930):

Documents form an ordered evidentiary tier, and each report type declares a tier floor. A document is embedded in the report iff its tier is at or above the report type's floor:

Tier attachmentType
1 (weakest) quotation
2 deposit
3 (strongest) invoice
Report type Tier floor Embeds
budget-overview quotation (1) quotation, deposit, invoice
claim deposit (2) deposit, invoice — never quotation
proof-of-funds invoice (3) invoice only

attachmentType: null (untagged/legacy) is treated as tier invoice (3) — the strongest tier — so untagged documents are included in every report type. This is deliberate: null links are legacy/ambiguous data (pre-Story-#1877 links, or the invoice detail page's "Add Document" picker when the user skipped the type choice), not known-weak evidence; treating null as the weakest tier would silently drop attachments from claim/proof-of-funds reports for existing data.

The rule depends only on the report type and the document's own attachmentType — it does not consult the invoice's status, the deposit split, or the report's target-status slice. Implemented once, in server/src/services/shared/attachmentTierUtils.ts (isDocumentIncludedForReportType) — do not hard-code a report type's floor at a second site.

If an invoice has no documents or none survive filtering, the documents array is empty. Documents are never omitted for any other reason; they always appear (with null metadata if Paperless is unreachable, if the document does not carry the configured filter tag, or if that tag cannot be resolved).

Paperless-ngx Degradation:

  • If Paperless-ngx is unreachable or unconfigured, or the configured PAPERLESS_FILTER_TAG cannot be resolved, all document fields (archiveSerialNumber, title) are null
  • With PAPERLESS_FILTER_TAG configured, a document that does not carry the tag gets null archiveSerialNumber and title, like a missing one
  • The endpoint returns 200 OK; it never returns 5xx due to Paperless failures
  • Documents are never omitted; they always appear with null metadata

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid report type or missing query params
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget source does not exist

POST /api/source-reports/mark-claimed

Mark invoices as claimed within a budget source scope, with source-scoped deposit sweep. Updates both invoices and their deposits in a single transaction. All work is atomic—any error rolls back all changes.

Auth required: Yes

Request body:

{
  "sourceId": "src-123",
  "invoiceIds": ["inv-1", "inv-2"],
  "depositIds": ["dep-1", "dep-2", "dep-3"]
}
Field Type Required Validation
sourceId string Yes Budget source UUID (1+ chars). Must exist in the system.
invoiceIds array Yes Array of invoice UUIDs. May be empty (see validation rules below).
depositIds array Yes Array of deposit UUIDs. May be empty (always required field). Server filters to sweep-eligible deposits (budgetSourceId === null OR === sourceId).

Response (200 OK):

{
  "batchId": "b7c2e0a4-5d1f-4a43-9a53-0c6f1d2e8a11",
  "claimedInvoiceIds": ["inv-1", "inv-2"],
  "claimedDepositIds": ["dep-1", "dep-3"]
}
Field Type Description
batchId string UUID of the submission, carried by every system event of the call (#2243); the id POST /api/source-reports/batches/:batchId/revert takes
claimedInvoiceIds array Invoice UUIDs that were flipped from pending/paid to claimed
claimedDepositIds array Deposit UUIDs that were flipped to claimed status

Claimability Rules:

An invoice (from invoiceIds) is claimable if:

  1. Status is pending or paid, OR
  2. It has ≥1 sweep-eligible deposit whose current status allows claiming (per ALLOWED_TRANSITIONS)

This makes quotation invoices with sweepable deposits claimable (Story #1896), and already-claimed invoices with claimable deposits claimable.

Other-Source Budget Line Interest:

The "other-source budget line interest" check (used to determine the invoice flip rule in Behavior below) examines only budget lines with a non-null budget_source_id that differs from the operation's sourceId. Budget lines with a NULL budget_source_id (unattributed to any specific source) are deliberately NOT counted as "other-source interest" — they do not prevent the invoice from being claimed.

Deposit Sweep Scope:

Only deposits with budgetSourceId === null (untagged) OR budgetSourceId === sourceId (tagged to this source) are swept. Deposits tagged to another budget source are never modified by this operation.

Behavior:

  • Invoices with status pending or paid AND no other-source budget line interest → flipped to claimed; diary event fired (if enabled)
  • Invoices with status pending or paid BUT other-source budget line interest → left unchanged; only sweep-eligible deposits flipped (deposit-only close-out; see Story #1895)
  • Quotation-status invoices → left unchanged; only sweep-eligible deposits flipped (deposit-only close-out; see Story #1896)
  • Already-claimed invoices → left unchanged; only sweep-eligible deposits flipped (deposit-only close-out)
  • All sweep-eligible deposits whose ALLOWED_TRANSITIONS include claimed → flipped to claimed; claimedDate = today() (YYYY-MM-DD); paidDate preserved (never cleared); diary event fired (if enabled)

Request Validation Rules:

  • At least one of invoiceIds or depositIds must be non-empty; it is an error to provide both as empty arrays.
  • sourceId must refer to an existing budget source; if not found, the request returns 404.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Both invoiceIds and depositIds arrays are empty, or missing required field
401 UNAUTHORIZED No valid session
404 NOT_FOUND sourceId does not refer to an existing budget source
409 INVOICES_NOT_CLAIMABLE One or more invoices are not claimable. Details: { invoiceIds: [...offending IDs...] } — NO rows are modified if this error is thrown.

POST /api/source-reports/generate-content

Generate AI-assisted report content: a cover letter and factual descriptions for invoices. Story #1901, revised by #1931.

Auth required: Yes

Request body:

{
  "type": "budget-overview",
  "sourceId": "src-123",
  "language": "en",
  "includedInvoiceIds": ["inv-1", "inv-2"],
  "excludedLineIds": ["line-a"]
}
Field Type Required Validation
type string Yes Enum: budget-overview, claim, proof-of-funds
sourceId string Yes Budget source UUID. Must exist (1-100 chars).
language string Yes Enum: en (English), de (German)
includedInvoiceIds array Yes Array of invoice UUIDs to include (1-200 items, each 1-100 chars). Filtered server-side to report's actual invoices.
excludedLineIds array No Budget line IDs to exclude from descriptions and amount calculation (≤500 items).

Response (200 OK):

{
  "letterSubject": "Construction Project Financial Report — 2024",
  "letterBody": "Please find attached our construction project financial report...",
  "descriptions": {
    "inv-1": "Foundation excavation and site preparation",
    "inv-2": "Concrete foundation pour and curing"
  }
}
Field Type Description
letterSubject string Professional cover letter subject line.
letterBody string Formal cover letter body only, generated in the requested language. Excludes the salutation, the closing formula and the signature, which the client prints around it. A German body starts lowercase after the comma-terminated salutation (Sehr geehrte Damen und Herren,), unless its first word is a noun or the formal Sie.
descriptions object Map of invoiceId → usage description. Only included invoices appear.

Behavior:

  • Server-side re-fetches the report to avoid trusting client-provided invoice data
  • includedInvoiceIds are filtered to the report's actual invoice IDs server-side
  • Throws EMPTY_SELECTION if no included invoices overlap with the report
  • Budget line amounts are excluded from the total when their line ID is in excludedLineIds
  • Linked work-item and household-item descriptions are fetched and enriched
  • All invoice notes and item descriptions are truncated (500/300 chars)
  • Output descriptions are filtered to only included invoice IDs (defense-in-depth)
  • Generated content is NOT persisted; it is returned for client preview/editing

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid request structure or enum values
400 EMPTY_SELECTION No included invoices overlap with the report's actual invoice IDs
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget source does not exist
502 LLM_INVALID_RESPONSE LLM returned malformed JSON or missing required fields
502 LLM_UNREACHABLE LLM provider is unreachable (network error, timeout, or invalid URL)
502 LLM_UPSTREAM_ERROR LLM provider returned HTTP error (non-2xx status)
503 LLM_NOT_CONFIGURED LLM gateway is not configured (all LLM env vars not set)

Notes:

  • All LLM errors suppress details in the HTTP response to avoid leaking prompt data or LLM-returned vendor/amount information to the client
  • Generate-content is a read-only operation. No database modifications occur
  • Language selection controls the generated letter's language, independent of input data language
  • Currency is sourced from the CURRENCY environment variable (exposed via /api/config)
  • descriptions is keyed by the invoice IDs the server accepted from includedInvoiceIds; a client must not assume a key exists for an ID that was filtered out
  • Length limits: letterSubject 150, letterBody 2000, per-invoice description 200 characters. The limit stated in the prompt and the limit enforced by the response validator are the same value by construction — both derive from REPORT_CONTENT_LIMITS in server/src/services/llmGateway/contentLimits.ts (#1931). An overlong field is truncated to the limit, never rejected; a response omitting any requested invoice ID is rejected with LLM_INVALID_RESPONSE
  • letterBody, letterSubject, and each descriptions[].description are stripped of markdown and HTML markup server-side by the response validator before length truncation (#1952), so a field that exceeds its cap only because of markup characters is not truncated unnecessarily. Removed: emphasis markers (**, __, *, _) when genuinely flanked as emphasis, ATX heading markers (# through ###### ) at a line start, bullet markers (- , * , + ) at a line start, numbered markers (1. , 1) ) at a line start when the line belongs to a run of two or more consecutive numbered lines, and HTML tags (<b>, </p>, <br/>, etc.). Inner text is always preserved, as are all \n/\n\n line and paragraph breaks — a stripped marker never merges two lines. The stripped value is trimmed, so tag removal leaves no stray leading or trailing whitespace. If stripping would leave a field empty or whitespace-only, the original value is returned unchanged.
  • Stripping is deliberately conservative: a false positive that silently alters a reference number is worse than the markup it removes, because the reader has no way to know a character went missing. Emphasis markers follow CommonMark flanking rules — an opening marker must be followed by a non-space and a closing marker preceded by one, and _ additionally must not be intraword — so unpaired footnote markers (5 EUR* zzgl. MwSt), asterisks used for multiplication (3 * 2 Einheiten), and underscores inside identifiers, reference numbers, or e-mail addresses (budget_line_id, RE_2024_117, max_mustermann_bau@example.com) all pass through unchanged. Numbered markers are run-gated because German writes ordinals and dates with a trailing period, so a lone 15. Mai 2026 … or 2. Rate in Höhe von … at a line start is prose, not a list, and keeps its number; bullet markers are not run-gated, since a leading - is not idiomatic prose. Mid-line hyphens (Pos. 3 - Dachstuhl, Mo-Fr, 2024-117), # inside a line (Rechnung #2024-117), a < not opening a well-formed tag (Beträge < 500 EUR), and German characters (umlauts, ß, €) are likewise untouched. An already-compliant plain-prose field passes through byte-identical.
  • The prompt-level plain-prose instruction (REPORT_CONTENT_SYSTEM_PROMPT rule 4 in server/src/services/llmGateway/prompts.ts, #1932) remains in place and is complementary, not redundant: the prompt keeps the common case clean and cheap, the validator makes the guarantee hard. Clients render all three fields literally with no markup parsing (coverLetterPdf.ts, ReportContentEditor.tsx).
  • The letterBody exclusions (no salutation, closing formula or signature; German lowercase start) are enforced only by the prompt (REPORT_CONTENT_SYSTEM_PROMPT rule 4 and the output-format line in server/src/services/llmGateway/prompts.ts, #2159), not by the response validator. A model that ignores the instruction can still return a body that opens with a greeting or ends with a sign-off, which the client then prints next to its own salutation and closing. Clients must not assume the guarantee is hard; the user reviews and edits the body in ReportContentEditor.tsx before the PDF is generated.
  • Requires the LLM gateway to be configured. Clients should gate the UI on llmEnabled from GET /api/config rather than relying on the LLM_NOT_CONFIGURED error path

Invoice Deposits

Invoice deposits track staged partial payments within a single invoice. A deposit goes through a state machine: pending → paid, paid → claimed, paid → pending, claimed → paid. As of Story #1878, pending → claimed is also allowed (deposits can be claimed directly without going through paid first). Deposits are ordered by dueDate ascending, then createdAt ascending in response bodies.

All deposit endpoints require authentication. Both admin and member roles can perform all operations (no role restriction).

Refund cap (#2188): deposit-type entries are never checked against invoice.amount; net deposits (Σ deposit-type − Σ refund-type) may exceed it. The only sum rule is a refund cap, applied on refund-type POST and on a refund-type PATCH that increases the amount (compared in cents): rejected with REFUND_EXCEEDS_INVOICE when Σ refund-type (other entries + requested amount) > max(invoice.amount, Σ deposit-type). Both sums include entries in every status and use the tolerant exceedsAmount comparison (cent precision). Refund decreases/deletes, deposit-type creates/increases/decreases/deletes and amount-neutral edits (status, dates, description, source) are never checked. The cap is checked only at refund write time; it is not re-checked when deposits are decreased or deleted or when the invoice amount is lowered.

Every check reads the sums and writes the change in the same transaction.

GET /api/invoices/:invoiceId/deposits

Returns all deposits for a given invoice.

Auth required: Yes

Path parameters:

Parameter Type Description
invoiceId string Invoice UUID

Response (200 OK):

{
  "deposits": [
    {
      "id": "dep-1",
      "invoiceId": "inv-1",
      "amount": 500.0,
      "dueDate": "2026-04-01",
      "paidDate": null,
      "claimedDate": null,
      "description": "First installment",
      "status": "pending",
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-03-15T10:00:00.000Z",
      "updatedAt": "2026-03-15T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Invoice ID does not exist

POST /api/invoices/:invoiceId/deposits

Creates a new deposit or refund for an invoice. The createdBy is automatically set to the authenticated user. Refunds are entered as positive amounts; the application treats them as negative contributions in aggregations (Story #1876).

Auth required: Yes

Path parameters:

Parameter Type Description
invoiceId string Invoice UUID

Request body:

{
  "amount": 500.0,
  "dueDate": "2026-04-01",
  "description": "First installment",
  "entryType": "deposit",
  "status": "pending",
  "paidDate": null,
  "claimedDate": null,
  "budgetSourceId": null
}
Field Type Required Validation
amount number Yes Must be > 0 (refunds stored positive; negation is display-time)
dueDate string Yes ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR
description string No Max 500 characters
entryType string No One of: deposit, refund. Default: deposit. Immutable after creation.
status string No One of: pending, paid, claimed. Default: pending
paidDate string No ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR. Only valid when status is paid or claimed
claimedDate string No ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR. Only valid when status is claimed
budgetSourceId string No Budget source UUID or null. Links this deposit to a source for aggregations (Story #1891).

Invariants (see "Refund cap" in the section intro):

  • Deposit-type entry: never checked against invoice.amount (#2188).
  • Refund-type entry: Σ refund-type (existing) + amount ≤ max(invoice.amount, Σ deposit-type) (gross, all statuses).

Response (201 Created):

{
  "deposit": {
    "id": "dep-1",
    "invoiceId": "inv-1",
    "amount": 500.0,
    "dueDate": "2026-04-01",
    "paidDate": null,
    "claimedDate": null,
    "description": "First installment",
    "entryType": "deposit",
    "status": "pending",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-03-15T10:00:00.000Z",
    "updatedAt": "2026-03-15T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing amount or dueDate, amount <= 0, malformed or impossible date (e.g. 2026-02-31) in dueDate/paidDate/claimedDate
400 REFUND_EXCEEDS_INVOICE Refund-type entry: gross sum of refunds would exceed max(invoiceTotal, depositTotal). details: { invoiceTotal, depositTotal, refundCap, currentRefundSum, requestedAmount, availableHeadroom }, where depositTotal is the gross sum of deposit-type entries, refundCap = max(invoiceTotal, depositTotal) and availableHeadroom = max(0, refundCap − currentRefundSum), all rounded to cents
400 INVALID_DEPOSIT_STATUS_TRANSITION Status transition not allowed (e.g., creating with status claimed)
400 INVALID_DEPOSIT_DATE_FOR_STATUS paidDate/claimedDate set when status doesn't support them
401 UNAUTHORIZED No valid session
404 NOT_FOUND Invoice ID does not exist

PATCH /api/invoices/:invoiceId/deposits/:depositId

Updates a deposit. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically. Note: entryType is immutable after creation. Fastify's default removeAdditional: true AJV setting silently strips any entryType field from the PATCH body before validation — the request succeeds (200) but the stored value is unchanged, it is never rejected with a 400.

Auth required: Yes

Path parameters:

Parameter Type Description
invoiceId string Invoice UUID
depositId string Deposit UUID

Request body (all fields optional, at least one required):

{
  "amount": 600.0,
  "dueDate": "2026-04-05",
  "description": "Revised first installment",
  "status": "paid",
  "paidDate": "2026-04-02",
  "budgetSourceId": null
}
Field Type Validation
amount number Must be > 0. When the amount increases, only a refund-type entry is checked, against the refund cap from the section intro (the sums exclude this entry's current amount). A deposit-type entry, a decrease and an unchanged amount are never checked.
dueDate string ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR
description string | null Max 500 characters; null clears the field
status string One of: pending, paid, claimed. Transitions must be allowed.
paidDate string | null ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR. Only valid when status is paid or claimed
claimedDate string | null ISO 8601 calendar date (YYYY-MM-DD); impossible dates such as 2026-02-31 are rejected with VALIDATION_ERROR. Only valid when status is claimed
budgetSourceId string | null Budget source UUID or null. Editable regardless of deposit status or edit-mode (Story #1891).

State Machine & Date Side-Effects:

Allowed transitions:

  • pending → paid: Auto-sets paidDate = today() unless paidDate is supplied in the request. Clears claimedDate.
  • pending → claimed (Story #1878): Auto-sets both paidDate = today() and claimedDate = today() unless supplied in the request.
  • paid → claimed: Auto-sets claimedDate = today() unless claimedDate is supplied in the request. Preserves paidDate.
  • paid → pending: Clears both paidDate and claimedDate.
  • claimed → paid: Clears claimedDate, preserves paidDate.

Any other transition (claimed → pending) is disallowed and returns 400 INVALID_DEPOSIT_STATUS_TRANSITION. Sending the current status again (e.g. pending → pending) is not a transition: it is accepted, changes no status or date, and writes no event. The allowed targets come from the shared PROGRESS_PAYMENT_TRANSITIONS map (shared/src/lib/statusTransitions.ts, #2209); the error's details.allowedTargets keeps the order pending: [paid, claimed], paid: [claimed, pending], claimed: [paid].

Dates later than today are accepted for paidDate and claimedDate.

Response (200 OK):

When the call changed status, paidDate or claimedDate, the body also carries a top-level undo sibling of deposit (contract 11). The vendor-scoped twin behaves identically.

{
  "deposit": {
    "id": "dep-1",
    "invoiceId": "inv-1",
    "amount": 600.0,
    "dueDate": "2026-04-05",
    "paidDate": "2026-04-02",
    "claimedDate": null,
    "description": "Revised first installment",
    "entryType": "deposit",
    "status": "paid",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-03-15T10:00:00.000Z",
    "updatedAt": "2026-03-15T10:30:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid field values (amount <= 0, malformed or impossible date such as 2026-02-31, etc.)
400 REFUND_EXCEEDS_INVOICE Refund-type entry, amount increased: gross sum of refunds would exceed the refund cap. details as on POST (currentRefundSum excludes this entry)
400 INVALID_DEPOSIT_STATUS_TRANSITION Transition not allowed
400 INVALID_DEPOSIT_DATE_FOR_STATUS paidDate/claimedDate set when status doesn't support them
401 UNAUTHORIZED No valid session
404 NOT_FOUND Invoice or deposit does not exist, or deposit does not belong to invoice

DELETE /api/invoices/:invoiceId/deposits/:depositId

Deletes a deposit. Deletions are never checked against the invoice amount or the refund cap (#2188).

Auth required: Yes

Path parameters:

Parameter Type Description
invoiceId string Invoice UUID
depositId string Deposit UUID

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204 No Content). Undo re-inserts the deposit; the vendor-scoped twin behaves identically (contract 11 › Relation unlink snapshots).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Invoice or deposit does not exist, or deposit does not belong to invoice

Vendor-Scoped Deposit Endpoints

The same deposit operations are also available under vendor scope for consistency:

  • GET /api/vendors/:vendorId/invoices/:invoiceId/deposits
  • POST /api/vendors/:vendorId/invoices/:invoiceId/deposits
  • PATCH /api/vendors/:vendorId/invoices/:invoiceId/deposits/:depositId
  • DELETE /api/vendors/:vendorId/invoices/:invoiceId/deposits/:depositId

These endpoints follow the exact same specifications as their /api/invoices counterparts and are served by the same handlers. The vendor prefix additionally enforces :vendorId (#2236): when :invoiceId exists but belongs to another vendor, every vendor-scoped deposit route answers 404 NOT_FOUND ("Invoice not found") before the handler runs. An unknown invoice answers the handler's own 404, as on the /api/invoices prefix.


Standalone Invoice Endpoints

These endpoints provide cross-vendor access to invoices. They complement the vendor-scoped endpoints above and are useful for listing all invoices across all vendors or fetching a single invoice without knowing its vendor.

All standalone invoice endpoints require authentication. Both admin and member roles can perform all operations (no role restriction).

GET /api/invoices

Returns a paginated list of all invoices across all vendors. Supports filtering by status, vendor, and text search on invoice number, company name, or description.

Auth required: Yes

Query parameters:

Parameter Type Default Constraints Description
page integer 1 >= 1 Page number (1-indexed)
pageSize integer 25 1-100 Items per page
q string -- Max 200 Search in invoice number, company (vendor) name, or description (case-insensitive)
status pending | paid | claimed | quotation -- -- Filter by invoice status (quotation = offers)
vendorId string -- -- Filter by vendor UUID
amountMin number -- -- Filter: minimum invoice amount (inclusive)
amountMax number -- -- Filter: maximum invoice amount (inclusive)
dateFrom string (YYYY-MM-DD) -- -- Filter: earliest invoice date (inclusive)
dateTo string (YYYY-MM-DD) -- -- Filter: latest invoice date (inclusive)
dueDateFrom string (YYYY-MM-DD) -- -- Filter: earliest due date (inclusive)
dueDateTo string (YYYY-MM-DD) -- -- Filter: latest due date (inclusive)
sortBy date | amount | status | vendor_name | due_date | invoice_number date -- Sort field (shared tuple INVOICE_LIST_SORTS). invoice_number added in #2238. See the ordering notes below.
sortOrder asc | desc desc -- Sort direction
openOnly boolean false -- Story #2046: return only open items — invoices with status = 'pending' or at least one deposit with status = 'pending' (of either entryType)
notYetSplit boolean false -- #2238: only records whose row notYetSplit is true (the money service's Not yet split rule, see the row fields note)
withoutDocument boolean false -- #2238: only records with no document_links row (entity_type = 'invoice')
refundsDue boolean false -- #2238: only records with at least one refund entry with status = 'pending' (the set counted by summary.refundsDue)
due boolean false -- #2238: only records with a payable that is overdue or due within 7 days (see the due note)
ids string (comma-separated invoice ids) -- Max 4000 chars; at most 100 ids #2238: narrow the rows to these ids (row refresh after a change). See the ids note

Response (200 OK):

{
  "invoices": [
    {
      "id": "inv-1",
      "vendorId": "v1",
      "vendorName": "ABC Construction",
      "invoiceNumber": "INV-2026-001",
      "amount": 2500.0,
      "date": "2026-03-15",
      "dueDate": "2026-04-15",
      "status": "pending",
      "notes": "First payment for foundation work",
      "budgetLines": [
        {
          "id": "ibl-1",
          "workItemBudgetId": "b1",
          "householdItemBudgetId": null,
          "itemizedAmount": 2500.0,
          "budgetLineDescription": "Foundation concrete materials",
          "categoryName": "Materials",
          "categoryColor": "#3B82F6",
          "plannedAmount": 8500.0
        }
      ],
      "remainingAmount": 0,
      "deposits": [],
      "finalPaymentAmount": 2500.0,
      "openAmount": 2500.0,
      "paid": 0.0,
      "remainingOnOffer": null,
      "notYetSplit": false,
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-03-15T10:00:00.000Z",
      "updatedAt": "2026-03-15T10:00:00.000Z"
    }
  ],
  "pagination": {
    "page": 1,
    "pageSize": 25,
    "totalItems": 42,
    "totalPages": 2
  },
  "summary": {
    "pending": { "count": 5, "totalAmount": 12500.0 },
    "paid": { "count": 30, "totalAmount": 75000.0 },
    "claimed": { "count": 7, "totalAmount": 17500.0 },
    "quotation": { "count": 0, "totalAmount": 0.0 },
    "overdue": { "count": 2, "totalAmount": 5000.0 },
    "claimable": { "count": 5, "totalAmount": 12500.0 },
    "quotationCoveredByDeposits": 0.0,
    "openPayable": { "count": 5, "totalAmount": 25310.0 },
    "refundsDue": { "count": 1, "totalAmount": 1200.0 }
  },
  "filterMeta": {
    "amount": { "min": 150.0, "max": 25000.0 }
  },
  "chipCounts": {
    "matching": { "toPay": 3, "offers": 2, "paid": 9, "submitted": 1, "all": 14 },
    "outsideToPay": { "offers": 1, "paid": 9, "submitted": 1, "all": 11 },
    "totals": { "toPay": 5, "offers": 4, "paid": 30, "submitted": 7, "all": 44 }
  },
  "listTotals": {
    "openPayable": { "count": 5, "totalAmount": 25310.0 },
    "offers": { "count": 0, "amount": 0.0, "paid": 0.0, "stillToPay": 0.0, "remainingOnOffer": 0.0 }
  }
}

Shared types (shared/src/types/invoice.ts, #2238):

export const INVOICE_LIST_SORTS = ['date', 'amount', 'status', 'vendor_name', 'due_date', 'invoice_number'] as const;
export type InvoiceListSort = (typeof INVOICE_LIST_SORTS)[number];

/** The money service's figures for one record; identical on list rows and the detail. */
export interface InvoiceRecordMoney {
  openAmount: number;               // still to pay on this record (refunds excluded)
  paid: number;                     // the money service's Paid for this record
  remainingOnOffer: number | null;  // quotations only; null for every other status
  notYetSplit: boolean;             // the money service's Not yet split rule
}

/** GET /api/invoices/:invoiceId and every GET /api/invoices row (#2239). */
export type InvoiceDetail = Omit<Invoice, 'openAmount'> & InvoiceRecordMoney;

/** A row of GET /api/invoices: every list-only field is always present. */
export type InvoiceListRow = InvoiceDetail;

/** Totals over every row matching the request (all pages); `ids` never narrows them. */
export interface InvoiceListTotals {
  readonly openPayable: InvoiceStatusSummary; // computeOpenAmounts(matching rows).openPayable
  readonly offers: MoneyOfferTotals;          // toOfferTotals(computePayments(matching rows).offers)
}

export interface InvoiceListPaginatedResponse {
  invoices: InvoiceListRow[];
  pagination: PaginationMeta;
  summary: InvoiceStatusBreakdown;
  filterMeta?: FilterMeta;
  chipCounts: InvoiceChipCounts;
  listTotals: InvoiceListTotals;
}

Notes:

  • chipCounts (#2234, EPIC-21 P4.2): row counts for the Invoices list's status chips, always present (additive; no opt-in parameter). Shared type InvoiceChipCounts (shared/src/types/invoice.ts, chip tuple INVOICE_LIST_CHIPS = ['toPay', 'offers', 'paid', 'submitted']; all is added to every tally). The chips overlap, so they are not a GROUP BY status: an offer with a pending progress payment counts in both offers and toPay, and so does a paid invoice with a pending refund.

    Chip Predicate on an invoices row
    toPay the openOnly predicate: status = 'pending' OR at least one invoice_deposits row with status = 'pending'
    offers status = 'quotation'
    paid status = 'paid'
    submitted status = 'claimed'
    all every row
    • matching: counts over the rows that match every list condition except status, openOnly and ids (q, vendorId, dateFrom/dateTo, dueDateFrom/dueDateTo, amountMin/amountMax, and since #2238 notYetSplit, withoutDocument, refundsDue, due). So status=paid does not change chipCounts.
    • outsideToPay: of matching, the rows not in toPay, per chip (offers, paid, submitted, all). It feeds the list note "Searching all · n more in ‹Chip›" (a search with no explicit chip on a list whose starting chip is To pay; wiki Architecture › Object list).
    • totals: the same tally over all invoices with no condition: the m of the heading "Invoices (n of m)" (totals[chip] for the current chip, totals.all while searching all).
    • The toPay predicate is one server function shared with the openOnly filter, so the two cannot drift. Each tally is one aggregate pass (COUNT(*) plus SUM(CASE …)) over invoices ⋈ vendors; all inputs are bound parameters.
  • The summary field is always the global unfiltered summary across all invoices, regardless of the current filter or search parameters. This provides consistent dashboard-level totals. This also applies to summary.openPayable and summary.refundsDue (Story #2046) — they do not change when openOnly or any other filter is applied.

  • summary.claimable is Ready to submit (#2232): the money service's getReadyToSubmit(db), i.e. { count: money.readyToSubmit.count, totalAmount: money.readyToSubmit.amount } of GET /api/budget/overview (EPIC-21 contract 2, rule 9): the bank report's draw-down set over bank_loan / credit_line sources, count = distinct invoices. Own-funds and unfunded invoices never count. Global, like the rest of summary. summary.quotationCoveredByDeposits is unchanged (computeQuotationCoveredByDeposits).

  • summary.overdue includes pending invoices with a dueDate before today (local time). This is a subset of summary.pending and helps highlight invoices requiring immediate attention.

  • Results are sorted by date descending by default (newest invoices first). Exception: when openOnly=true and sortBy is omitted, results are instead sorted ascending by earliest open due date — the minimum of the invoice's own dueDate (when the invoice is pending) and the dueDates of its pending deposits. Invoices with no dated open item sort last. Passing an explicit sortBy overrides this and behaves as usual.

  • Ordering is total (#2238). Every order ends with id ascending as the tiebreaker, so batched loading (page 1, 2, …) never repeats or skips a row. due_date and invoice_number sort NULL last in both directions (<col> IS NULL ascending first, then the column in the requested direction). status stays accepted for old callers; the Invoices page no longer offers it (Group by › Status replaces it).

  • The q parameter matches the invoice number, the company (vendor) name, or the invoice description (notes) using case-insensitive substring matching; %, _ and \ match literally.

  • Story #2046 — openOnly: when true, only invoices that are themselves pending or that carry at least one pending deposit (either entryType) are returned. Pagination (totalItems/totalPages) counts invoices only — deposits never consume page capacity. Since #2238 every response (not only openOnly) populates deposits and carries openAmount; the rules below apply to every row:

    • openAmount = (status === 'pending' ? max(0, amount − Σ deposit-type entries of any status) : 0) + Σ (pending deposit-type entries). Pending refunds are excluded from openAmount — they are reported separately via summary.refundsDue.
    • summary.openPayable = { count, totalAmount } over all invoices (not just the current page/filter): totalAmount is the sum of every invoice's openAmount, count is the number of invoices with openAmount > 0. This total is computed from the exact same per-invoice figures returned in openAmount — summing invoices[].openAmount across every page of an openOnly=true result set, with no other filters applied, reproduces summary.openPayable.totalAmount exactly. If q, vendorId, or a date/amount range filter narrows the visible invoices, the summed openAmount values are only a subset and will not equal the (always global/filter-independent) summary.openPayable.totalAmount.
    • summary.refundsDue = { count, totalAmount }: totalAmount is the sum of pending refund-type deposit amounts (positive), count is the number of invoices carrying at least one pending refund. Refunds are never netted into openPayable.
    • A quotation invoice with a pending deposit is returned (its pending deposit is an open item), but the invoice's own amount contributes nothing to openAmount or openPayable.
  • Row fields (#2238, InvoiceListRow). Every row of every response carries, besides the Invoice shape:

    • deposits: the full progress-payment list (see the deposits note above);
    • openAmount: the openOnly formula above (refunds excluded), from computeOpenAmounts per record (toRecordMoney, shared with the detail; #2239), so Σ openAmount over every row of every page of an unfiltered request equals summary.openPayable.totalAmount;
    • paid: the money service's Paid for this one record (EPIC-21 contract 2, rule 8): the paid + claimed parts of aggregateInvoiceStatusBreakdown over this record's rows (the residual of a paid/claimed record, paid/claimed progress payments on any record, received refunds negative). Σ paid over every row of an unfiltered request equals money.paid. The server runs the same helper the money service runs, on one record's rows; there is no second formula;
    • remainingOnOffer: for status = 'quotation' only, max(0, amount − Σ deposit-type entries of any status), the money engine's per-offer term of records.offers.remainingOnOffer (glossary "Remaining on offer"); null for every other status;
    • notYetSplit: toCents(amount) − Σ toCents(itemized_amount) >= NOT_YET_SPLIT_MIN_CENTS (100), the money engine's Not yet split rule (moneyEngine.ts, step 2; the constant is exported and used there too) with the itemized cents from loadInvoiceLinks. The count of true rows over all invoices equals money.notYetSplit.recordCount.
  • listTotals (#2238, InvoiceListTotals), always present. Totals over every row matching the request — all conditions including status, openOnly and the four flags, over all pages, but never narrowed by ids. They are computed by running the money helpers on the matching records' rows (no second formula):

    • openPayable = computeOpenAmounts(matching rows).openPayable (count = rows with openAmount > 0). With no condition, or with openOnly=true and no other condition, it equals summary.openPayable and therefore money.stillToPay / money.stillToPayCount exactly (every row with openAmount > 0 is in the To pay set; the rest add 0);
    • offers = toOfferTotals(computePayments(matching rows).offers), the MoneyOfferTotals shape (count, amount = offered, paid, stillToPay, remainingOnOffer). With status=quotation and no other condition it equals money.records.offers exactly.
    • Figure rule. Only the unfiltered values are figures. The Invoices page marks its To pay footer data-figure="still-to-pay" only on the To pay chip with no search and no filter (wiki Architecture › Figure consistency); any narrowed listTotals value is a part of a figure and carries no marker. The Offers summary is a part (its Paid is the offers' share of Paid) and carries no marker either.
  • due (#2238). A record matches when (a) its own status = 'pending' and due_date <= today + 7 days, or (b) it has a deposit-type entry with status = 'pending' and due_date <= today + 7 days. No lower bound, so overdue payables match. Refunds never match. Today is date('now', 'localtime'), the same basis as summary.overdue; the 7-day window is PAYMENT_DUE_WINDOW_DAYS (attention rule N5, equal to the client's dueFlag() DUE_SOON_DAYS), bound as a parameter. Otherwise the record's own status does not matter, so an offer with a pending progress payment due soon matches.

  • notYetSplit, withoutDocument, refundsDue (#2238) are id-set / NOT EXISTS / EXISTS conditions on the same query; notYetSplit binds its id set as one JSON parameter (invoices.id IN (SELECT value FROM json_each(?))), never one parameter per id. false or absent = no condition (the flags only ever narrow).

  • ids (#2238). Split on ,, trimmed, empty entries dropped. Only the rows and pagination are narrowed; summary, filterMeta, chipCounts and listTotals ignore ids. With at least one usable id, page/pageSize are ignored: every matching row among the ids is returned in the normal order, and pagination is { page: 1, pageSize: 100, totalItems: n, totalPages: n > 0 ? 1 : 0 }. An id that does not exist or does not match the other conditions is simply absent (the client reads that as "no longer in this list"). No usable id after trimming = parameter ignored. More than 100 usable ids = 400 VALIDATION_ERROR (never silently truncated; a client holding more keys refetches the list instead).

  • Unknown query parameters are stripped, not rejected (additionalProperties: false with AJV removeAdditional); the 400s below come from type, enum, range and the ids count only.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid query parameters (page < 1, pageSize out of range, an unknown sortBy, more than 100 ids, etc.)
401 UNAUTHORIZED No valid session

GET /api/invoices/:invoiceId

Returns a single invoice by ID, regardless of which vendor it belongs to.

Auth required: Yes

Path parameters:

Parameter Type Description
invoiceId string Invoice UUID

Response (200 OK):

{
  "invoice": {
    "id": "inv-1",
    "vendorId": "v1",
    "vendorName": "ABC Construction",
    "invoiceNumber": "INV-2026-001",
    "amount": 2500.0,
    "date": "2026-03-15",
    "dueDate": "2026-04-15",
    "status": "pending",
    "notes": "First payment for foundation work",
    "budgetLines": [
      {
        "id": "ibl-1",
        "workItemBudgetId": "b1",
        "householdItemBudgetId": null,
        "itemizedAmount": 2500.0,
        "budgetLineDescription": "Foundation concrete materials",
        "categoryName": "Materials",
        "categoryColor": "#3B82F6",
        "plannedAmount": 8500.0
      }
    ],
    "remainingAmount": 0,
    "openAmount": 2500.0,
    "paid": 0,
    "remainingOnOffer": null,
    "notYetSplit": false,
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-03-15T10:00:00.000Z",
    "updatedAt": "2026-03-15T10:00:00.000Z"
  }
}

Per-record money figures (#2239, InvoiceDetail = Invoice + InvoiceRecordMoney). The detail carries the same four figures as a GET /api/invoices row, from one shared server helper (toRecordMoney) run on this record's rows. The detail of a record equals its GET /api/invoices?ids=<id> row on all four fields.

Field Type Description
openAmount number Still to pay on this record (refunds excluded); same rule as the list row.
paid number The money service's Paid for this one record (paid + claimed, refunds netted); same rule as the list row.
remainingOnOffer number | null Offers only (status = 'quotation'): max(0, amount − Σ non-refund progress payments); null for every other status.
notYetSplit boolean The money engine's Not yet split rule for this record.

POST, PATCH and convert responses are unchanged (Invoice, without these four fields); clients re-fetch the detail after a mutation.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Invoice ID does not exist

POST /api/invoices/:invoiceId/convert-quotation

Story #2107. Converts an invoice with status: 'quotation' into the final invoice in place. The call can set the final amount, dates, number, notes and status, re-amount the existing budget lines, and optionally link the final Paperless document. It is one atomic operation: every validation runs before any write, and all writes happen in a single SQLite transaction. The invoice keeps its id, vendor, deposits and budget-line links.

Auth required: Yes. Both admin and member roles may call it; there is no role restriction.

Path parameters:

Parameter Type Description
invoiceId string Invoice UUID

Request body (ConvertQuotationRequest in @cornerstone/shared):

The schema declares additionalProperties: false, but Fastify's default AJV options include removeAdditional: true. Unknown properties are therefore silently dropped, not rejected: the request proceeds as if they were absent.

{
  "amount": 11800.0,
  "date": "2026-09-28",
  "invoiceNumber": "RE-2026-0412",
  "dueDate": "2026-10-12",
  "notes": "Final invoice after site measurement",
  "status": "pending",
  "conversionNote": "Converted from quotation (quoted 12,500.00 €) on 30.09.2026.",
  "budgetLines": [
    { "id": "ibl-1", "itemizedAmount": 7300.0 },
    { "id": "ibl-2", "itemizedAmount": 4500.0 }
  ],
  "paperlessDocumentId": 412
}
Field Type Required Validation / semantics
amount number Yes Final invoice amount, major units, > 0
date string Yes Final invoice date, ISO 8601 (YYYY-MM-DD). Must be a real calendar date: impossible dates such as 2026-02-31 are rejected (round-trip check)
invoiceNumber string | null No Max 100 characters. Omitted = keep the quotation's number; null = clear. The value is trimmed and a blank string is stored as null.
dueDate string | null No ISO 8601, real calendar date. Omitted = keep the stored due date; null = clear. The effective due date (sent or stored) must be ≥ date, otherwise 400.
notes string | null No Max 10000 characters. The user's final notes before the history line. Omitted = keep the quotation's notes; null = clear them. See "Notes and the history line" below.
status string Yes Target status, one of pending, paid. paid is a status only: the conversion records no payment date, and the invoices table has no paid-date column. claimed and quotation are rejected.
conversionNote string Yes 1-1000 characters, and non-blank after trim. A localized history line composed by the client (the server has no i18n). Always appended to the notes.
budgetLines array No Max 200 entries. Partial re-amounting of existing lines, see below. Omitted or [] = keep the current itemization unchanged.
budgetLines[].id string Yes invoice_budget_lines.id (the id of an entry in Invoice.budgetLines). Must belong to this invoice; must not repeat within the array.
budgetLines[].itemizedAmount number Yes New itemized amount for that line, major units, > 0
paperlessDocumentId integer No >= 1. Paperless-ngx document to link to the invoice with attachmentType: 'invoice', see the document-link table below. Paperless is not contacted, so this works when Paperless is not configured.

Notes and the history line:

The stored notes is base + "\n\n" + conversionNote.trim(), where base is the trimmed notes from the request (or the stored notes when notes is omitted). When base is empty or null, the stored value is the trimmed conversionNote alone. The combined value must be at most 10000 characters, otherwise the call returns 400 VALIDATION_ERROR. Because conversionNote is required, every conversion made through the API leaves a trace in the invoice notes. Anything the client wants to record about the conversion (for example the quoted amount, or that the final payment was marked paid) goes into this free-text line. There is no structured field for it.

Budget lines (partial update):

  • Only the lines listed in budgetLines change. Every other line of the invoice keeps its itemizedAmount.
  • Lines cannot be added or removed here. To itemize new lines, use POST /api/invoices/:invoiceId/budget-lines or auto-itemize after the conversion.
  • The itemized-sum rule is checked against the resulting itemization (each listed line's new amount plus every unlisted line's current amount) and the new amount. It uses the tolerant exceedsAmount comparison. This applies even when budgetLines is omitted: a final amount below the existing itemized total is rejected with ITEMIZED_SUM_EXCEEDS_INVOICE.

Deposits:

Deposits are never modified and never block conversion; net deposits may exceed amount (#2188).

Document link (paperlessDocumentId):

The endpoint looks up an existing link with entityType: 'invoice', this invoiceId and this paperlessDocumentId:

Existing link Result
None A new link is created with attachmentType: 'invoice'
attachmentType: 'invoice' No-op
attachmentType: null (untagged) Upgraded in place to 'invoice'
attachmentType: 'quotation' or 'deposit' 409 DUPLICATE_DOCUMENT_LINK, and the whole conversion is rolled back. The quotation or deposit link is left unchanged.

Atomicity and side effects:

  • The whole operation runs in one transaction. It re-reads the invoice and runs every check, including the document-link tier decision, before the first write. Only then does it write the invoice, the line amounts and the document link. If any step fails, nothing is changed; there is no half-converted invoice.
  • The status check runs inside the transaction, so a second concurrent conversion of the same invoice gets 409 INVOICE_NOT_QUOTATION.
  • The invoice_status diary auto-event (quotation → the target status) is written after the transaction commits, as for PATCH. From #2223 the event is always written (DIARY_AUTO_EVENTS is removed, ADR-040 §8) and carries the converting user in created_by. Diary write failures are swallowed and logged, so they can neither roll back the conversion nor fail the request.
  • The endpoint is not idempotent: once the invoice is converted, a repeat call returns 409 INVOICE_NOT_QUOTATION.

Response (200 OK): { "invoice": Invoice }, the full invoice (see Invoice Response Shape) re-read after commit. status, amount, date, dueDate, invoiceNumber, notes, budgetLines[].itemizedAmount, remainingAmount, deposits and finalPaymentAmount reflect the conversion.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Schema failure (missing required field, status not pending/paid, bad date format, length limits), impossible calendar date, effective due date (sent or stored) < date, blank conversionNote, combined notes > 10000, duplicate budgetLines[].id
400 ITEMIZED_SUM_EXCEEDS_INVOICE The resulting itemized total exceeds amount. details: { invoiceTotal, itemizedTotal }
401 UNAUTHORIZED No valid session
404 NOT_FOUND The invoice does not exist, or a budgetLines[].id is not a line of this invoice ("Invoice budget line not found in this invoice")
409 INVOICE_NOT_QUOTATION The invoice's current status is not quotation. details: { status } (the current status)
409 DUPLICATE_DOCUMENT_LINK paperlessDocumentId is already linked to this invoice on the quotation or deposit tier

Checks run in this order. When a request breaks several rules, the first failure in this order is returned:

  1. Request-schema validation (Fastify/AJV, before the handler runs): 400 VALIDATION_ERROR. For example, status: 'claimed' sent to an already-converted invoice gets 400, not 409.
  2. Invoice exists: 404 NOT_FOUND.
  3. Invoice status is quotation: 409 INVOICE_NOT_QUOTATION.
  4. Fields (amount, calendar dates, effective due date ≥ date): 400 VALIDATION_ERROR.
  5. Notes (non-blank conversionNote, combined length ≤ 10000): 400 VALIDATION_ERROR.
  6. Budget lines (duplicate ids: 400; id not on this invoice: 404).
  7. Itemized sum: 400 ITEMIZED_SUM_EXCEEDS_INVOICE.
  8. Document-link lookup and tier decision: 409 DUPLICATE_DOCUMENT_LINK.

Only then are the invoice, the line amounts and the document link written. Every check precedes every write.


Budget Source Response Shape

type BudgetSourceType = 'bank_loan' | 'credit_line' | 'savings' | 'other' | 'discretionary';
type BudgetSourceStatus = 'active' | 'exhausted' | 'closed';

interface BudgetSourceResponse {
  id: string;
  name: string;
  sourceType: BudgetSourceType;
  totalAmount: number;
  usedAmount: number; // Computed: SUM(planned_amount) of linked budget lines (planned allocation perspective)
  availableAmount: number; // Computed: totalAmount - usedAmount (planned perspective)
  claimedAmount: number; // Computed: aggregates per-deposit 'claimed' contributions (proportional split) + residual when parent invoice is 'claimed'
  actualAvailableAmount: number; // Computed: totalAmount - claimedAmount (actual perspective)
  paidAmount: number; // Computed: aggregates per-deposit 'paid'+'claimed' (everything-non-pending) contributions (proportional split) + residual when parent invoice is 'paid'/'claimed'
  projectedAmount: number; // Computed: blended projection using confidence margins for non-invoiced lines
  isDiscretionary: boolean; // true for the system-managed Own funds source
  interestRate: number | null;
  terms: string | null;
  notes: string | null;
  reference: string | null; // Reference identifier (e.g., account number, contract ID) for Bank Report Wizard; Story #1877
  contactAddress: string | null; // Contact address for the source (e.g., bank address) for Bank Report Wizard; Story #1877
  status: BudgetSourceStatus;
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601
  updatedAt: string; // ISO 8601
}

Notes on computed fields:

  • usedAmount represents the planned allocation perspective -- summed over all work_item_budgets and household_item_budgets rows referencing this source: an invoiced line contributes its itemized_amount, a non-invoiced line its VAT-effective planned_amount (a net line, includes_vat = 0, is grossed up by 1 + VAT_RATE (server config vatRate, default 0.19, exposed by GET /api/config), rounded to cents, exactly like projectedAmount). It reflects how much of the source has been earmarked for budget lines, on the same gross basis as totalAmount.
  • availableAmount is totalAmount - usedAmount, showing how much planned capacity remains (VAT-effective, because usedAmount is).
  • claimedAmount represents the actual drawdown perspective -- the sum of claimed contributions from invoices linked (via invoice_budget_lines) to budget lines referencing this source. When an invoice has deposits, contributions are split proportionally across deposit statuses and the residual amount (see "Deposit-aware aggregation" below).
  • unclaimedAmount represents the sum of paid contributions (non-claimed, non-pending) from linked invoices, using the same proportional split.
  • paidAmount equals claimedAmount + unclaimedAmount (i.e., all non-pending contributions).
  • For the own-funds source (isDiscretionary: true), paidAmount and claimedAmount (and so unclaimedAmount) include the lines assigned to it by id (budget_source_id = its id), plus lines without a source and invoice remainders. Before #2228 the lines assigned by id were left out (bug fix). The money service's sources[].paid / submitted use the same figures.
  • actualAvailableAmount is totalAmount - claimedAmount, showing how much of the source's funds remain after actual drawdowns.
  • projectedAmount is a blended projection: invoiced budget lines use actual cost, non-invoiced lines apply confidence margins to planned_amount. A net line (includes_vat = 0) has its planned amount grossed up by 1 + VAT_RATE (server config vatRate, default 0.19, exposed by GET /api/config) before the margin is applied, the same as the budget overview and breakdown.
  • isDiscretionary is true only for the system-managed Own funds source (seeded by migration 0021 as "Discretionary Funding", renamed by migration 0049, #2242). This source captures invoice remainder amounts and budget lines with no assigned source. It cannot be deleted or have its sourceType changed.

Deposit-aware aggregation: When an invoice has deposits, each invoice_budget_lines row's contribution is split proportionally:

  • Each deposit d contributes ibl.itemizedAmount × (d.amount / invoice.amount) under d.status (e.g., a 5000€ deposit on a 10000€ invoice contributes 50% of the itemized amount under that deposit's status).
  • The residual contributes ibl.itemizedAmount × ((invoice.amount − Σ d.amount) / invoice.amount) under the parent invoice's status.
  • Zero-deposit invoices behave identically to before: 100% of ibl.itemizedAmount contributes under the parent invoice's status.
  • Implemented in server/src/services/shared/depositAggregateUtils.ts.

Tagged-deposit aggregation (Story #1891): Source reports use two independent aggregation rails:

  • Rail A (line-derived, pro-rata; tagged deposits omitted from the line apportionment only): Each invoice_budget_lines row's contribution for the source uses the pro-rata split above, BUT deposits with non-null budget_source_id do not receive a pro-rata apportionment across the invoice's budget lines — Rail B already credits them 100% to their own source. A tagged deposit remains in the residual denominator: the residual is (invoice.amount − Σ non-refund deposit amounts) / invoice.amount over all deposits, tagged or not, exactly as in the untagged pro-rata formula above. Omitting a tagged deposit from the residual as well would let the residual re-absorb money Rail B has already counted, double-counting it (fixed in #1894; the pre-fix text described that buggy behaviour). Note the residual fraction is invoice-level, so a deposit tagged to source A also reduces the Rail A share of a line belonging to source B on the same invoice — this is what makes the rails reconcile. This rail produces zero contribution to the source if the invoice has zero budget lines for that source and only tagged deposits to other sources.
  • Rail B (deposit-direct, 100% to source): The sum of all invoice_deposits rows where budget_source_id = sourceId, signed by entryType (deposit = +, refund = -), filtered to the report's target status slice (e.g., claimed, paid, or pending depending on report type). These deposits contribute 100% of their amount to their source, independent of budget lines. A deposit-only invoice (zero ibls for source) appears in the report if it has ≥1 tagged deposit.
  • Combined: allocatedAmount = round2dp(railA + railB). lineKind derives from the combined sign. unallocatedInvoices excludes invoices with ≥1 tagged deposit to the source.
  • Reconciliation invariant: for a single invoice fully covered by budget lines, summing allocatedAmount across all sources reconstructs the invoice amount exactly. Example — a 1000€ paid invoice with a 600€ line to source A, a 400€ line to source B, and a 150€ deposit tagged to A: residual fraction is (1000 − 150) / 1000 = 0.85, so A gets 600 × 0.85 + 150 = 660 and B gets 400 × 0.85 = 340, totalling 1000.

Budget Source Endpoints

Budget sources represent financing sources for the construction project (e.g., bank loans, credit lines, savings). Budget sources are a small collection and are NOT paginated. All budget source endpoints require authentication. Both admin and member roles can perform all budget source operations (no role restriction).

GET /api/budget-sources

Returns all budget sources. Non-discretionary sources are sorted alphabetically by name first, followed by the discretionary source last.

Auth required: Yes

Response (200 OK):

{
  "budgetSources": [
    {
      "id": "bs-1",
      "name": "ABC Bank Mortgage",
      "sourceType": "bank_loan",
      "totalAmount": 350000.0,
      "usedAmount": 125000.0,
      "availableAmount": 225000.0,
      "claimedAmount": 45000.0,
      "actualAvailableAmount": 305000.0,
      "paidAmount": 30000.0,
      "projectedAmount": 140000.0,
      "isDiscretionary": false,
      "interestRate": 3.5,
      "terms": "30-year fixed, monthly payments",
      "notes": "Primary financing source",
      "status": "active",
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-20T10:00:00.000Z",
      "updatedAt": "2026-02-20T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

POST /api/budget-sources

Creates a new budget source.

Auth required: Yes

Request body:

{
  "name": "ABC Bank Mortgage",
  "sourceType": "bank_loan",
  "totalAmount": 350000.0,
  "interestRate": 3.5,
  "terms": "30-year fixed, monthly payments",
  "notes": "Primary financing source",
  "status": "active"
}
Field Type Required Validation
name string Yes 1-200 characters
sourceType string Yes One of: bank_loan, credit_line, savings, other (note: discretionary is not allowed)
totalAmount number Yes At least 0.01 (route schema minimum: 0.01, service < 0.01 refused)
interestRate number | null No 0-100 if provided
terms string | null No Free text
notes string | null No Free text
reference string | null No 0-200 characters; reference identifier (account number, contract ID) for Bank Report Wizard
contactAddress string | null No 0-500 characters; contact address (e.g., bank address) for Bank Report Wizard
status string No One of: active, exhausted, closed. Default: active

The createdBy field is automatically set to the authenticated user's ID. The discretionary source type is reserved for the system-seeded Own funds source and cannot be used in create requests.

Response (201 Created):

{
  "budgetSource": {
    "id": "bs-1",
    "name": "ABC Bank Mortgage",
    "sourceType": "bank_loan",
    "totalAmount": 350000.0,
    "usedAmount": 0,
    "availableAmount": 350000.0,
    "claimedAmount": 0,
    "actualAvailableAmount": 350000.0,
    "paidAmount": 0,
    "projectedAmount": 0,
    "isDiscretionary": false,
    "interestRate": 3.5,
    "terms": "30-year fixed, monthly payments",
    "notes": "Primary financing source",
    "status": "active",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-02-20T14:00:00.000Z",
    "updatedAt": "2026-02-20T14:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty name, name too long, missing/invalid sourceType, totalAmount <= 0, interestRate out of range, invalid status
401 UNAUTHORIZED No valid session

GET /api/budget-sources/:id

Returns a single budget source with computed allocation and drawdown fields, plus the money service's figures for it (money, #2242).

Auth required: Yes

Path parameters:

Parameter Type Description
id string Budget source UUID

Response (200 OK): { "budgetSource": BudgetSource, "money": MoneySourceFigures } (BudgetSourceDetailResponse). money is the entry of GET /api/budget/overview money.sources with the same id, built by the same function (buildSources): amount, allocated, unallocated, overAllocated, paid, submitted and readyToSubmit (the bank report's draw-down set; { count: 0, amount: 0 } for a source that is not a bank_loan / credit_line). The budgetSource object is unchanged and money is added next to it (the JSON below shows budgetSource only). The system funding source (isDiscretionary) is named Own funds (migration 0049 renamed the seeded "Discretionary Funding" row; a name the user chose is kept), and its delete error DISCRETIONARY_SOURCE carries the message "Own funds cannot be deleted" (the frontend translates the code).

Response body (budgetSource only):

{
  "budgetSource": {
    "id": "bs-1",
    "name": "ABC Bank Mortgage",
    "sourceType": "bank_loan",
    "totalAmount": 350000.0,
    "usedAmount": 125000.0,
    "availableAmount": 225000.0,
    "claimedAmount": 45000.0,
    "actualAvailableAmount": 305000.0,
    "paidAmount": 30000.0,
    "projectedAmount": 140000.0,
    "isDiscretionary": false,
    "interestRate": 3.5,
    "terms": "30-year fixed, monthly payments",
    "notes": "Primary financing source",
    "status": "active",
    "createdBy": {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "displayName": "Admin User",
      "email": "admin@example.com"
    },
    "createdAt": "2026-02-20T10:00:00.000Z",
    "updatedAt": "2026-02-20T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget source ID does not exist

PATCH /api/budget-sources/:id

Updates a budget source's fields. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Budget source UUID

Request body (all fields optional, at least one required):

{
  "name": "ABC Bank Mortgage (Updated)",
  "sourceType": "bank_loan",
  "totalAmount": 400000.0,
  "interestRate": 3.25,
  "terms": "Updated terms",
  "notes": "Updated notes",
  "status": "active"
}
Field Type Validation
name string 1-200 characters
sourceType string One of: bank_loan, credit_line, savings, other (note: discretionary is not allowed). Rejected for the Own funds source (isDiscretionary: true)
totalAmount number At least 0.01. The Own funds source (isDiscretionary: true) accepts 0 or more (>= 0)
interestRate number | null 0-100 if provided; null clears the field
terms string | null null clears the field
notes string | null null clears the field
reference string | null 0-200 characters; null clears the field
contactAddress string | null 0-500 characters; null clears the field
status string One of: active, exhausted, closed

Response (200 OK):

Returns the updated BudgetSourceResponse object wrapped in { "budgetSource": ... }. When the call changed status, the body also carries a top-level undo sibling of budgetSource (contract 11); Undo restores status and updatedAt only.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR No fields provided, invalid field values
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget source ID does not exist

DELETE /api/budget-sources/:id

Deletes a budget source. Fails with 409 Conflict if the source is referenced by budget lines (via work_item_budgets.budget_source_id or household_item_budgets.budget_source_id), or if the source is the system-managed discretionary source.

Auth required: Yes

Path parameters:

Parameter Type Description
id string Budget source UUID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget source ID does not exist
409 BUDGET_SOURCE_IN_USE Budget source is referenced by budget lines and cannot be deleted
409 DISCRETIONARY_SOURCE The discretionary funding source cannot be deleted

Notes:

  • The DISCRETIONARY_SOURCE error is returned when attempting to delete the system-managed Own funds source:
    {
      "error": {
        "code": "DISCRETIONARY_SOURCE",
        "message": "Own funds cannot be deleted"
      }
    }
  • BUDGET_SOURCE_IN_USE: details is not sent: the error is raised with suppressDetails (#315 hardening), so its counts reach only the server log and the response carries code and message alone. Server-side counts (log only): budgetLineCount.
  • Budget sources with no budget line references can be freely deleted.

GET /api/budget-sources/:sourceId/budget-lines

Retrieves all budget lines allocated to a specific budget source, grouped by parent entity type (work item vs household item).

Auth required: Yes

Path parameters:

Parameter Type Description
sourceId string Budget source UUID

Response (200 OK):

interface BudgetSourceBudgetLine extends BaseBudgetLine {
  parentId: string;
  parentName: string;
  area: AreaSummary | null;
  hasClaimedInvoice: boolean;
}

interface BudgetSourceBudgetLinesResponse {
  workItemLines: BudgetSourceBudgetLine[];
  householdItemLines: BudgetSourceBudgetLine[];
}

Response shape (JSON):

{
  "workItemLines": [
    {
      "id": "budget-line-uuid",
      "description": "Optional description",
      "plannedAmount": 1500.0,
      "confidence": "quote",
      "confidenceMargin": 0.05,
      "budgetCategory": {
        "id": "cat-uuid",
        "name": "Materials",
        "description": "Building materials",
        "color": "#FF5733",
        "translationKey": "category.materials",
        "sortOrder": 1,
        "createdAt": "2026-02-07T12:00:00.000Z",
        "updatedAt": "2026-02-07T12:00:00.000Z"
      },
      "budgetSource": {
        "id": "source-uuid",
        "name": "Bank Loan",
        "sourceType": "bank_loan"
      },
      "vendor": {
        "id": "vendor-uuid",
        "name": "Home Depot",
        "trade": null
      },
      "actualCost": 500.0,
      "actualCostPaid": 250.0,
      "invoiceCount": 2,
      "invoiceLink": {
        "invoiceBudgetLineId": "ibl-uuid",
        "invoiceId": "invoice-uuid",
        "invoiceNumber": "INV-001",
        "invoiceDate": "2026-02-05T00:00:00.000Z",
        "invoiceStatus": "claimed",
        "itemizedAmount": 250.0,
        "vendorId": "vendor-uuid",
        "vendorName": "Home Depot"
      },
      "quantity": 10,
      "unit": "pieces",
      "unitPrice": 150.0,
      "includesVat": true,
      "createdBy": {
        "id": "user-uuid",
        "displayName": "John Doe",
        "email": "john@example.com"
      },
      "createdAt": "2026-02-07T12:00:00.000Z",
      "updatedAt": "2026-02-07T12:00:00.000Z",
      "parentId": "work-item-uuid",
      "parentName": "Foundation Work",
      "area": {
        "id": "area-uuid",
        "name": "Foundation",
        "color": "#FF5733"
      },
      "hasClaimedInvoice": true
    }
  ],
  "householdItemLines": []
}

Field notes:

  • BaseBudgetLine extends with four additional fields:
    • parentId: The work item or household item ID that owns this budget line
    • parentName: The title (work item) or name (household item) of the parent entity
    • area: The area that the parent entity is assigned to (null if parent has no area)
    • hasClaimedInvoice: Boolean — true if any linked invoice has status = 'claimed' OR any deposit on a linked invoice has status = 'claimed'. This is a status-existence check (not an amount threshold): a pending invoice with a claimed deposit returns true; a claimed invoice whose residual is fully absorbed by non-claimed deposits also returns true. Fixed in #1897 to be deposit-aware.
  • actualCost: Sum of itemized amounts from all linked invoices including quotations (any status). For quotation-linked lines, treat as a point estimate and apply ±5% (CONFIDENCE_MARGINS.quote) when surfacing a min/max range. See "Quotation handling for actualCost".
  • actualCostPaid: Deposit-aware proportional sum for 'paid'/'claimed' statuses. Each linked invoice's itemized amount is split proportionally across its deposits; each portion is counted if its contributing status is 'paid' or 'claimed'. When an invoice has no deposits the full itemized amount counts if the invoice's own status is 'paid' or 'claimed'. Quotations are always excluded. Fixed in #1897 to be deposit-aware (was previously whole-invoice-by-parent-status).
  • invoiceLink: The first linked invoice summary (null if no invoices linked); reflects the primary invoice for the budget line. Includes denormalized vendorId / vendorName so callers do not need a separate vendor fetch.
  • The area field in the response is null if the parent work item/household item has no area assignment
  • All invoice and related data is computed at query time (not stored)

Sorting:

Lines within each group (workItemLines, householdItemLines) are sorted by:

  1. Area name (ascending, nulls last)
  2. Parent item name (ascending)
  3. Created at (ascending)

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Budget source ID does not exist

PATCH /api/budget-sources/:sourceId/budget-lines/move

Move budget lines from the current budget source to a target budget source.

Auth required: Yes

Path parameters:

Parameter Type Description
sourceId string Source budget UUID

Request body:

interface MoveBudgetLinesRequest {
  workItemBudgetIds: string[];
  householdItemBudgetIds: string[];
  targetSourceId: string;
}
{
  "workItemBudgetIds": ["budget-line-uuid-1", "budget-line-uuid-2"],
  "householdItemBudgetIds": ["household-budget-line-uuid"],
  "targetSourceId": "target-source-uuid"
}

Response (200 OK):

interface MoveBudgetLinesResponse {
  movedWorkItemLines: number;
  movedHouseholdItemLines: number;
}
{
  "movedWorkItemLines": 2,
  "movedHouseholdItemLines": 1
}

Validation order:

The endpoint validates in this specific order:

  1. Source budget source exists (404 if missing)
  2. Target source ID is different from source ID (400 SAME_SOURCE if equal)
  3. Target budget source exists (404 if missing)
  4. At least one budget line ID is provided (400 EMPTY_SELECTION if both arrays empty)
  5. All budget line IDs belong to the source (409 STALE_OWNERSHIP if any missing or belong to different source)

Error responses:

HTTP Status Error Code When
400 SAME_SOURCE targetSourceId equals sourceId
400 EMPTY_SELECTION Both workItemBudgetIds and householdItemBudgetIds are empty arrays
401 UNAUTHORIZED No valid session
404 NOT_FOUND Source or target budget source does not exist
409 STALE_OWNERSHIP Any budget line ID is missing or belongs to a different source

Behavior:

  • The operation is atomic: either all budget lines are moved or none are (wrapped in a transaction).
  • Budget lines retain all their properties (planned amount, category, vendor, etc.) and only their budget_source_id and updated_at are changed.
  • Both empty arrays are allowed only if at least one is non-empty; providing two empty arrays triggers EMPTY_SELECTION.

Subsidy Program Response Shape

A subsidy program (UI: "grant") as returned by every endpoint below (SubsidyProgram, shared/src/types/subsidyProgram.ts):

{
  "id": "sp-1",
  "name": "Efficient heating grant",
  "description": "Grant for replacing an old boiler",
  "eligibility": "Owner-occupied homes",
  "reductionType": "percentage",
  "reductionValue": 20,
  "applicationStatus": "applied",
  "applicationDeadline": "2026-12-31",
  "notes": null,
  "maximumAmount": 5000,
  "applicableCategories": [{ "id": "bc-1", "name": "Heating", "...": "Budget Category shape" }],
  "includesNoCategoryItems": false,
  "createdBy": { "id": "u-1", "displayName": "Example User", "email": "user@example.com" },
  "createdAt": "2026-03-01T10:00:00.000Z",
  "updatedAt": "2026-03-02T09:30:00.000Z"
}
Field Type Description
reductionType string percentage or fixed
reductionValue number > 0; at most 100 when reductionType is percentage
applicationStatus string eligible, applied, approved, received, rejected
applicationDeadline string | null Stored as sent (not validated as a date)
maximumAmount number | null Cap on the grant amount, > 0, or null for no cap
applicableCategories BudgetCategory[] The budget categories the grant applies to (Budget Category Response Shape), ordered by category sortOrder, then name
includesNoCategoryItems boolean Whether cost lines without a budget category also qualify
createdBy UserSummary | null null when the creating user was deleted

Subsidy Program Endpoints

All endpoints require a valid session (member or admin).

GET /api/subsidy-programs

Lists all subsidy programs, sorted by name ascending. Not paginated.

Response (200 OK): { "subsidyPrograms": SubsidyProgram[] }

Error responses: 401 UNAUTHORIZED.


POST /api/subsidy-programs

Creates a subsidy program. Request fields are those of PATCH below; name, reductionType and reductionValue are required, applicationStatus defaults to eligible, includesNoCategoryItems to false, and the other fields to null / no categories. createdBy is the requesting user.

Optional items (GrantItemRef[], { "type": "work_item" | "household_item", "id": string }, at most 100): tasks and purchases to cover. The program, its category links and the item links are written in one transaction; an unknown item is a 404 NOT_FOUND and no program row is created. items is optional on the API (the New grant dialog asks for at least one). Unknown keys inside an item are stripped, not rejected.

The API keeps accepting reductionType: 'fixed' (legacy data, imports, test seeding); the UI never offers it for a new grant.

Response (201 Created): { "subsidyProgram": SubsidyProgram }

Error responses: 400 VALIDATION_ERROR (as for PATCH, plus a missing required field, an items entry without type / id, a type outside the enum, more than 100 items); 401 UNAUTHORIZED; 404 NOT_FOUND (an item in items does not exist; nothing is created).


GET /api/subsidy-programs/:id

Response (200 OK): { "subsidyProgram": SubsidyProgram, "money": GrantMoney } (SubsidyProgramDetailResponse, #2242).

GrantMoney comes from the money service (the engine figures GET /api/budget/overview money.grants uses, built by buildGrants):

Field Type Meaning
expected MoneyRange the grant's payout in the three scenarios; the zero range for a rejected grant (the engine skips rejected grants)
eligibleCost MoneyRange eligible cost the percentage applies to (after the cap); zero when rejected
capped boolean the cap (maximumAmount) reduced the payout; false when rejected
counted boolean approved or received (counts in the project totals); false when rejected
coveredItemCount integer linked tasks plus linked purchases (work_item_subsidies + household_item_subsidies)

Error responses: 401 UNAUTHORIZED; 404 NOT_FOUND (no program with that id).


GET /api/subsidy-programs/:id/items

The tasks and purchases the grant covers, with the grant's computed share of each (#2242).

Response (200 OK): { "items": GrantCoveredItem[] } (GrantCoveredItemsResponse):

{
  "items": [
    { "type": "work_item", "id": "wi-1", "title": "Roof", "area": { "id": "a-1", "name": "Roof", "color": null, "ancestors": [] }, "covered": 1250 },
    { "type": "household_item", "id": "hi-4", "title": "Heat pump", "area": null, "covered": 0 }
  ]
}

covered is the money engine's share of this grant for the item in the expected scenario (itemTotals, major units); 0 for a rejected grant. Order: tasks first, then purchases, each by title (case-insensitive), then id. area carries its ancestors as in list rows.

Error responses: 401 UNAUTHORIZED; 404 NOT_FOUND (no program with that id).


POST /api/subsidy-programs/:id/items

Links tasks and purchases to the grant (#2242; contract 2a).

Request: { "items": GrantItemRef[] } (LinkGrantItemsRequest): 1 to 100 entries of { "type": "work_item" | "household_item", "id": string (minLength 1) }. Unknown keys inside an entry are stripped, not rejected.

One transaction: the grant must exist and every item must exist; otherwise 404 NOT_FOUND and nothing is linked. Pairs that are already linked are skipped (idempotent: repeating a request changes nothing). Duplicate entries in one request count once.

Response (201 Created): the full GrantCoveredItemsResponse (as GET /api/subsidy-programs/:id/items), so the client replaces its list.

Error responses: 400 VALIDATION_ERROR (missing or empty items, more than 100, an entry without type / id, a type outside the enum); 401 UNAUTHORIZED; 404 NOT_FOUND (grant or an item). Removing a link keeps using DELETE /api/work-items/:workItemId/subsidies/:subsidyProgramId and DELETE /api/household-items/:householdItemId/subsidies/:subsidyProgramId (both 200 { undo }).


PATCH /api/subsidy-programs/:id

Updates a subsidy program. All fields are optional; only provided fields change, and updatedAt is set on every successful call.

Path parameters:

Parameter Type Description
id string Subsidy program UUID

Request body (at least one field):

{
  "applicationStatus": "approved",
  "maximumAmount": 4000,
  "categoryIds": ["bc-1", "bc-2"]
}
Field Type Validation
name string 1–200 characters after trimming; stored trimmed
reductionType string percentage or fixed
reductionValue number > 0. The percentage cap is checked on the effective pair: when the resulting reductionType (the request's, else the stored one) is percentage, the resulting reductionValue (the request's, else the stored one) must be at most 100. So a request that only switches reductionType to percentage is rejected while the stored value is over 100
description string | null No length limit; null clears
eligibility string | null No length limit; null clears
applicationStatus string eligible, applied, approved, received, rejected. Any status may follow any other (no transition rules)
applicationDeadline string | null Not validated as a date; null clears
notes string | null No length limit; null clears
maximumAmount number | null > 0, or null for no cap
categoryIds string[] Replaces all applicable categories ([] removes them all). Every id must be an existing budget category
includesNoCategoryItems boolean

Unknown fields are stripped by AJV (removeAdditional), not rejected; a body left empty after stripping returns 400 VALIDATION_ERROR.

Side effects:

  • When applicationStatus changes, a subsidy_status system event is written (ADR-020; de-duplicated per ADR-040 W1 and W3). Sending the current status again writes nothing.
  • When applicationStatus changes, the body carries an Undo token (contract 11); Undo restores applicationStatus and updatedAt only.

Response (200 OK): { "subsidyProgram": SubsidyProgram }, plus a top-level undo sibling when a token was issued:

{
  "subsidyProgram": { "id": "sp-1", "applicationStatus": "approved", "...": "SubsidyProgram" },
  "undo": { "token": "u_6f1c0d9e2a7b4c5d8e9f0a1b2c3d4e5f", "expiresAt": "2026-10-06T09:00:30.000Z" }
}

undo is absent (never null) when applicationStatus did not change.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR No fields; a field fails its rule above (bad enum, reductionValue ≤ 0 or a percentage over 100, maximumAmount ≤ 0, name blank or over 200 characters, unknown or duplicate categoryIds)
401 UNAUTHORIZED No valid session
404 NOT_FOUND No program with that id

DELETE /api/subsidy-programs/:id

Deletes a subsidy program, its category links, its purchase links (household_item_subsidies) and its document links. GET /api/delete-impact/subsidy_program/:id (contract 19) returns these counts in advance.

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND No program with that id
409 SUBSIDY_PROGRAM_IN_USE Tasks are linked to the program (work_item_subsidies); details not sent (#315, workItemCount is logged only)

Budget Overview Response Shape

Trimmed by EPIC-21 contract 25 (#2237, story 4.5). The fourteen legacy cost fields (availableFunds, sourceCount, minPlanned, maxPlanned, actualCost*, remainingVs*) and their ±5 % offer spread are removed. overview keeps only the grant-derived subsidySummary.

The response is { overview: BudgetOverview; money: MoneySummary }. All fields are computed (not stored). money is the money service's figure set (ADR-039, EPIC-21 contract 2) and is what UI reads. overview.subsidySummary stays for its last consumer, SubsidyProgramsPage (until story 4.10). Types: shared/src/types/budgetOverview.ts.

interface BudgetOverviewResponse {
  overview: BudgetOverview;
  money: MoneySummary; // EPIC-21 contract 2 (#2228)
}

interface BudgetOverview {
  subsidySummary: {
    totalReductions: number; // money.expectedGrants.expected
    activeSubsidyCount: number; // Count of non-rejected subsidy programs
    minTotalPayback: number; // money.expectedGrants.best (best scenario)
    maxTotalPayback: number; // money.expectedGrants.worst (worst scenario)
    oversubscribedSubsidies: OversubscribedSubsidy[]; // grants with capped = true
  };
}

interface OversubscribedSubsidy {
  subsidyProgramId: string;
  name: string;
  maximumAmount: number;
  maxPayout: number; // payout in the worst scenario
  uncappedMinPayback: number; // uncapped payout, best scenario
  uncappedMaxPayback: number; // uncapped payout, worst scenario
  minExcess: number; // max(0, uncappedMinPayback - payout in the best scenario)
  maxExcess: number; // uncappedMaxPayback - maxPayout
}

Notes on subsidy (grant) figures (derived from the money service since #2228, ADR-039 §3):

  • A grant counts only when its status is approved or received. eligible and applied grants are possible, not counted (money.possibleGrants); rejected grants are ignored everywhere. activeSubsidyCount still counts every non-rejected grant.
  • A grant applies to the lines of the work items and household items linked to it, narrowed by its categories (none = every line; otherwise the line's category is in the set, or the line has no category and the grant includes_no_category_items). A grant with no linked items pays 0.
  • Percentage grants pay rate × min(eligible cost, maximum_amount). Legacy fixed grants are shown unchanged: per linked item, reduction_value is split equally over that item's eligible lines (largest remainder, ties by line id) and each part is capped at the line's cost in the scenario; the sum is the uncapped payout; maximum_amount (if set) caps the payout, spread over the lines weighted by their capped parts (largest remainder); a linked item with no eligible line pays 0.
  • minTotalPayback / maxTotalPayback are the best / worst scenarios (unlinked lines at the low / high end of their margin; linked lines, offers included, at face value), not a min and a max of separate computations. totalReductions is the expected scenario. A grant is listed in oversubscribedSubsidies when its uncapped payout exceeds its payout in the worst scenario.

Budget Overview Endpoint

GET /api/budget/overview

Returns the project's money figures (money) and the grant summary (overview). The money service runs once per request and feeds both. No query parameters; no response schema (contract 25 rule 8).

Auth required: Yes (admin and member)

Response (200 OK; synthetic amounts):

{
  "overview": {
    "subsidySummary": {
      "totalReductions": 1500.0,
      "activeSubsidyCount": 2,
      "minTotalPayback": 1500.0,
      "maxTotalPayback": 1750.0,
      "oversubscribedSubsidies": []
    }
  },
  "money": { "...": "MoneySummary, see EPIC-21 contract 2" }
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

Notes:

  • With no budget data the shape is the same: zeros, empty arrays and money.leftToSpend.verdict: null.

Budget Financing Endpoint

GET /api/budget/financing

Money › Financing page (#2242): every funding source and every grant, with figures from the money service. No query parameters; no response schema.

Auth required: Yes (admin and member)

Response (200 OK; synthetic amounts):

{
  "sources": [
    {
      "id": "bs-1", "name": "Bank loan", "sourceType": "bank_loan", "status": "active",
      "isDiscretionary": false, "amount": 200000, "allocated": 150000, "unallocated": 50000,
      "overAllocated": 0, "paid": 40000, "submitted": 25000,
      "readyToSubmit": { "count": 2, "amount": 15000 }, "interestRate": 3.5
    }
  ],
  "grants": [
    {
      "id": "sp-1", "name": "Energy grant", "status": "applied", "reductionType": "percentage",
      "rate": 20, "eligibleCostCap": 30000, "deadline": "2026-12-01", "coveredItemCount": 3,
      "expected": { "expected": 4000, "best": 4000, "worst": 4000 }, "counted": false
    }
  ]
}
  • sources (FinancingSource[]): the money.sources entries of GET /api/budget/overview (same function, buildSources, same order: name, then id) plus interestRate (number | null, percent). The system source is included (named Own funds; isDiscretionary: true).
  • grants (FinancingGrant[]): every subsidy_programs row ordered by name, then id, rejected included (status is any of the five). reductionType / rate are the stored values (rate is a percent, or the amount of a legacy fixed grant). eligibleCostCap is maximum_amount, deadline is application_deadline. coveredItemCount counts linked tasks plus purchases. expected and counted are the engine's (buildGrants); a rejected grant has the zero range and counted: false.
  • Everything comes from one money-service run (getFinancing); no figure is computed in the route.

Error responses: 401 UNAUTHORIZED.


Budget Breakdown Response Shape

Replaced by EPIC-21 contract 25 (#2237, story 4.5). The legacy shape (workItems / householdItems area trees, costDisplay, projected*, raw*, actualCost, subsidyAdjustments, budgetSources, money: BreakdownMoney) and budgetBreakdownService.ts are deleted. The response is now { breakdown: BudgetBreakdown } (shared/src/types/budgetBreakdown.ts): lines, items, byArea, noArea, orphanLines, byType, byCategory, bySource, notYetSplit and total, each with BreakdownFigures (Expected cost, Expected grants, After grants, Paid, Still to pay, Not yet paid), plus sourceOptions and hasUnassignedLines. The shape, rules and example are in contract 25.

Budget Breakdown Endpoint

GET /api/budget/breakdown

See EPIC-21 contract 25. Query deselectedSources (comma-separated source ids and/or unassigned; a repeated parameter answers 400 VALIDATION_ERROR). Auth required: Yes (admin and member). Errors: 400, 401.


EPIC-06: Timeline, Gantt Chart & Dependency Management

Timeline and scheduling endpoints for milestones, auto-scheduling, and aggregated timeline data. All endpoints in this section require authentication. Both admin and member roles can perform all timeline operations (no role restriction).

Milestone Response Shapes

Milestone summary (used in list responses):

interface MilestoneSummary {
  id: number;
  title: string;
  description: string | null;
  targetDate: string; // ISO 8601 date
  isCompleted: boolean;
  completedAt: string | null; // ISO 8601 timestamp
  color: string | null;
  workItemCount: number; // Computed: count of linked work items
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601 timestamp
  updatedAt: string; // ISO 8601 timestamp
}

Milestone detail (used in single-item responses):

interface MilestoneDetail {
  id: number;
  title: string;
  description: string | null;
  targetDate: string; // ISO 8601 date
  isCompleted: boolean;
  completedAt: string | null; // ISO 8601 timestamp
  color: string | null;
  workItems: WorkItemSummary[]; // Linked work items (full summary)
  dependentWorkItems: WorkItemDependentSummary[]; // Work items that require this milestone to complete first
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601 timestamp
  updatedAt: string; // ISO 8601 timestamp
}

Dependent work item summary (used in MilestoneDetail.dependentWorkItems and the dependents endpoints):

interface WorkItemDependentSummary {
  id: string;
  title: string;
}

Milestone Endpoints

Milestones represent major project progress points. The milestone list is expected to remain small (fewer than ~50 milestones) so no pagination is used.

GET /api/milestones

Returns all milestones sorted by target_date ascending, with linked work item count, the projected date and the holds-up count (MilestoneListRowsResponse, #2246). Not paginated; the Milestones list (ObjectList template) filters, sorts and slices on the client.

Before computing, the route runs ensureDailyReschedule (as GET /api/attention does), so the projection starts from that day's dates.

Auth required: Yes

Response (200 OK):

{
  "milestones": [
    {
      "id": 1,
      "title": "Foundation Complete",
      "description": "All foundation work finished and inspected",
      "targetDate": "2026-04-15",
      "isCompleted": false,
      "completedAt": null,
      "color": "#EF4444",
      "workItemCount": 3,
      "createdBy": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-20T10:00:00.000Z",
      "updatedAt": "2026-02-20T10:00:00.000Z",
      "projectedDate": "2026-04-22",
      "holdsUp": { "count": 6, "startingWithin14Days": 2 }
    }
  ]
}

List-row fields (MilestoneListRow, #2246):

Field Type Meaning
workItemCount number Tasks counting toward the milestone: the list's Waits for
projectedDate string | null YYYY-MM-DD: the schedule projection's milestones.get(id).projectedDate (the latest projected end among the contributing tasks; the same date the Schedule and the milestone page use). null when no contributing task has an end date
holdsUp AttentionHoldsUp | null { count, startingWithin14Days } from computeHoldsUp(index, MILESTONE_NODE_PREFIX + id, { today, projection }) (EPIC-21 contract 1, the one holds-up definition, as GET /api/attention and GET /api/timeline). null when the milestone is completed or the count is 0. The list cell carries data-figure="holds-up". dependentWorkItemCount (direct dependents only) is deliberately not used

One schedule graph, one today (todayUtc()) and one projection serve every row of a response.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

POST /api/milestones

Creates a new milestone. The createdBy is automatically set to the authenticated user.

Auth required: Yes

Request body:

{
  "title": "Foundation Complete",
  "description": "All foundation work finished and inspected",
  "targetDate": "2026-04-15",
  "color": "#EF4444"
}
Field Type Required Validation
title string Yes 1-200 characters
description string | null No Max 2000 characters
targetDate string Yes ISO 8601 date (YYYY-MM-DD)
color string | null No Hex color code matching /^#[0-9A-Fa-f]{6}$/, or null

Response (201 Created):

{
  "id": 1,
  "title": "Foundation Complete",
  "description": "All foundation work finished and inspected",
  "targetDate": "2026-04-15",
  "isCompleted": false,
  "completedAt": null,
  "color": "#EF4444",
  "workItems": [],
  "createdBy": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "displayName": "Admin User",
    "email": "admin@example.com"
  },
  "createdAt": "2026-02-20T10:00:00.000Z",
  "updatedAt": "2026-02-20T10:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Empty title, title too long, missing targetDate, invalid date format, invalid color format
401 UNAUTHORIZED No valid session

GET /api/milestones/:id

Returns a single milestone with its linked work items (full summary).

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID

Response (200 OK):

{
  "id": 1,
  "title": "Foundation Complete",
  "description": "All foundation work finished and inspected",
  "targetDate": "2026-04-15",
  "isCompleted": false,
  "completedAt": null,
  "color": "#EF4444",
  "workItems": [
    {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "title": "Pour Foundation",
      "status": "in_progress",
      "startDate": "2026-03-15",
      "endDate": "2026-03-25",
      "actualStartDate": "2026-03-16",
      "actualEndDate": null,
      "durationDays": 10,
      "assignedUser": null,
      "assignedVendor": null,
      "area": null,
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z"
    }
  ],
  "dependentWorkItems": [
    {
      "id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "title": "Frame Walls",
      "status": "not_started",
      "startDate": "2026-04-16",
      "endDate": "2026-04-30",
      "actualStartDate": null,
      "actualEndDate": null,
      "durationDays": 14,
      "assignedUser": null,
      "assignedVendor": null,
      "area": null,
      "createdAt": "2026-02-15T10:00:00.000Z",
      "updatedAt": "2026-02-15T10:00:00.000Z"
    }
  ],
  "dependentHouseholdItems": [
    {
      "id": "c3d4e5f6-a7b8-9012-cdef-123456789012",
      "name": "Kitchen cabinets",
      "category": "furniture",
      "status": "planned",
      "targetDeliveryDate": "2026-05-01",
      "earliestDeliveryDate": null,
      "latestDeliveryDate": null
    }
  ],
  "projectedDate": "2026-04-18",
  "createdBy": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "displayName": "Admin User",
    "email": "admin@example.com"
  },
  "createdAt": "2026-02-20T10:00:00.000Z",
  "updatedAt": "2026-02-20T10:00:00.000Z"
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone ID does not exist

Fields added by #2250:

Field Type Description
dependentWorkItems WorkItemSummary[] Work items that have this milestone as a required predecessor (full summary; the POST /dependents response still returns { id, title })
dependentHouseholdItems WorkItemLinkedHouseholdItemSummary[] Household items (purchases) that depend on this milestone (same rows as GET /api/milestones/:id/dependent-household-items)
projectedDate string | null Server-projected date (YYYY-MM-DD); the same value the Milestones list row carries. Computed from one schedule projection per request

PATCH /api/milestones/:id

Updates a milestone. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically.

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID

Request body (all fields optional, at least one required):

{
  "title": "Foundation Complete & Inspected",
  "description": "Updated description",
  "targetDate": "2026-04-20",
  "isCompleted": true,
  "color": "#22C55E"
}
Field Type Validation
title string 1-200 characters
description string | null Max 2000 characters; null clears the description
targetDate string ISO 8601 date (YYYY-MM-DD)
isCompleted boolean true or false; setting to true auto-sets completedAt
color string | null Hex color code or null to clear

Notes:

  • When isCompleted is set to true, the completedAt field is automatically set to the current timestamp.
  • When isCompleted is set to false, the completedAt field is automatically cleared to null.

Response (200 OK):

Returns the updated MilestoneDetail object. When the call changed isCompleted or completedAt, the body also carries a top-level undo token (contract 11). Setting isCompleted to false clears completedAt; a completedAt later than today is accepted.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone ID does not exist

DELETE /api/milestones/:id

Deletes a milestone. Cascades to milestone-work-item associations (the work items themselves are not deleted).

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID

Response (204 No Content): Empty body.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone ID does not exist

Milestone-Work Item Link Endpoints

POST /api/milestones/:id/work-items

Links a work item to a milestone.

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID

Request body:

{
  "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
Field Type Required Validation
workItemId string Yes Must be a valid work item UUID

Response (201 Created):

{
  "milestoneId": 1,
  "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Missing workItemId
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone or work item does not exist
409 DUPLICATE_DEPENDENCY Work item is already linked to (contributes to) this milestone
409 CIRCULAR_DEPENDENCY Work item already depends on this milestone, so it cannot also contribute to it

Runs autoReschedule after the link is written (#2236), like POST /api/work-items/:workItemId/milestones/linked/:milestoneId.


DELETE /api/milestones/:id/work-items/:workItemId

Unlinks a work item from a milestone.

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID
workItemId string Work item UUID

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204 No Content). Undo re-inserts the link and reschedules. The delete now runs autoReschedule (#2236) (contract 11 › Relation unlink snapshots).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone, work item, or the link between them does not exist

Milestone Dependent Endpoints

A milestone has two kinds of work-item relationship. A linked work item contributes to the milestone (milestone_work_items, managed by the Milestone-Work Item Link Endpoints above). A dependent work item requires the milestone to complete before it can start (work_item_milestone_deps). A work item cannot be both for the same milestone: that pair would be a cycle, and the second link is rejected with 409 CIRCULAR_DEPENDENCY. The same two relationships are reachable from the work-item side through the Work Item Milestone Endpoints below.

POST /api/milestones/:id/dependents/:workItemId

Makes a work item depend on this milestone. Equivalent to POST /api/work-items/:workItemId/milestones/required/:milestoneId, with a different response shape.

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID
workItemId string Work item UUID

Request body: None.

Response (201 Created): the milestone's full dependent list after the insert.

{
  "dependentWorkItems": [
    { "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", "title": "Pour foundation" }
  ]
}

dependentWorkItems is WorkItemDependentSummary[] (see Milestone Response Shapes).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR id is not an integer
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone or work item does not exist
409 DUPLICATE_DEPENDENCY Work item already depends on this milestone
409 CIRCULAR_DEPENDENCY Work item already contributes to (is linked to) this milestone, so it cannot also depend on it

DELETE /api/milestones/:id/dependents/:workItemId

Removes a work item from the milestone's dependents.

Auth required: Yes

Path parameters: as for POST /api/milestones/:id/dependents/:workItemId.

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204 No Content). Undo re-inserts the dependency and reschedules (contract 11 › Relation unlink snapshots).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR id is not an integer
401 UNAUTHORIZED No valid session
404 NOT_FOUND Milestone, work item, or the dependency between them does not exist

GET /api/milestones/:id/dependent-household-items

Lists the household items that depend on this milestone (household-item dependencies with predecessorType: "milestone").

Auth required: Yes

Path parameters:

Parameter Type Description
id integer Milestone integer ID

Response (200 OK):

{
  "householdItems": [
    {
      "id": "hi-001",
      "name": "Kitchen Cabinets",
      "category": "hic-furniture",
      "status": "planned",
      "targetDeliveryDate": "2026-05-10",
      "earliestDeliveryDate": null,
      "latestDeliveryDate": null
    }
  ]
}

householdItems is WorkItemLinkedHouseholdItemSummary[] (see the work-item household-items response). The milestone's existence is not checked: an unknown id returns 200 with an empty list, not 404.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR id is not an integer
401 UNAUTHORIZED No valid session

Work Item Milestone Endpoints

Routes under /api/work-items/:workItemId/milestones manage the same two relationships as the milestone-side endpoints, from the work item's side. Required milestones are ones the work item depends on (work_item_milestone_deps). Linked milestones are ones it contributes to (milestone_work_items).

Response shape (WorkItemMilestones, returned by GET and both POST endpoints):

interface WorkItemMilestones {
  required: MilestoneSummaryForWorkItem[]; // Milestones this work item depends on
  linked: MilestoneSummaryForWorkItem[]; // Milestones this work item contributes to
}

interface MilestoneSummaryForWorkItem {
  id: number;
  name: string; // The milestone's `title`
  targetDate: string | null; // ISO 8601 date
}

GET /api/work-items/:workItemId/milestones

Returns the work item's required and linked milestones.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID

Response (200 OK):

{
  "required": [{ "id": 1, "name": "Permits approved", "targetDate": "2026-03-01" }],
  "linked": [{ "id": 2, "name": "Shell complete", "targetDate": "2026-06-30" }]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item does not exist

POST /api/work-items/:workItemId/milestones/required/:milestoneId

Makes the work item depend on the milestone.

Auth required: Yes

Path parameters:

Parameter Type Description
workItemId string Work item UUID
milestoneId integer Milestone integer ID

Request body: None.

Response (201 Created): the work item's full WorkItemMilestones after the insert.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR milestoneId is not an integer
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item or milestone does not exist
409 DUPLICATE_DEPENDENCY Work item already depends on this milestone
409 CIRCULAR_DEPENDENCY Work item already contributes to this milestone, so it cannot also depend on it

DELETE /api/work-items/:workItemId/milestones/required/:milestoneId

Removes the work item's dependency on the milestone.

Auth required: Yes

Path parameters: as for the POST above.

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204 No Content). Undo re-inserts the dependency and reschedules (contract 11 › Relation unlink snapshots).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR milestoneId is not an integer
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item, milestone, or the work item's dependency on it does not exist

POST /api/work-items/:workItemId/milestones/linked/:milestoneId

Links the work item to the milestone, so it contributes to the milestone's completion. Equivalent to POST /api/milestones/:id/work-items, with a different response shape.

Auth required: Yes

Path parameters: as for POST .../milestones/required/:milestoneId.

Request body: None.

Response (201 Created): the work item's full WorkItemMilestones after the insert.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR milestoneId is not an integer
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item or milestone does not exist
409 DUPLICATE_DEPENDENCY Work item is already linked to this milestone
409 CIRCULAR_DEPENDENCY Work item already depends on this milestone, so it cannot also contribute to it

DELETE /api/work-items/:workItemId/milestones/linked/:milestoneId

Unlinks the work item from the milestone.

Auth required: Yes

Path parameters: as for POST .../milestones/required/:milestoneId.

Response (200 OK): { "undo": UndoToken } (UndoableDeleteResponse, #2236; was 204 No Content). Undo re-inserts the link and reschedules (contract 11 › Relation unlink snapshots).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR milestoneId is not an integer
401 UNAUTHORIZED No valid session
404 NOT_FOUND Work item, milestone, or the link between them does not exist

Scheduling Endpoints

POST /api/schedule

Runs the auto-scheduling engine using the Critical Path Method (CPM). See ADR-014 for algorithm details.

The endpoint is read-only — it returns the computed schedule without persisting any changes to the database. The client displays results for user review, then applies accepted changes via individual PATCH /api/work-items/:id calls. This supports what-if analysis and gives users full control over which schedule changes to accept.

Auth required: Yes

Request body:

{
  "mode": "full",
  "anchorWorkItemId": null
}
Field Type Required Validation
mode string Yes One of: full, cascade
anchorWorkItemId string | null No Work item UUID. Required when mode is cascade; ignored when mode is full.
overrides array No Reschedule preview (EPIC-21 contract 3, #2247). 1–50 entries (SCHEDULE_OVERRIDES_MAX), see below.

overrides (preview of unsaved changes, #2247). Each entry is a ScheduleOverride:

{
  "mode": "cascade",
  "anchorWorkItemId": "wi-1",
  "overrides": [{ "workItemId": "wi-1", "endDate": "2026-10-30" }, { "workItemId": "wi-2", "durationDays": 5 }]
}
Field Type Required Validation
workItemId string Yes 1–64 characters; must name an existing work item (not a milestone node); unique across the array
startDate string No YYYY-MM-DD, a real calendar date
endDate string No YYYY-MM-DD, a real calendar date; not before startDate
durationDays integer No 0–3650 (SCHEDULE_DURATION_DAYS_MAX)

At least one of startDate, endDate, durationDays is required per entry. Other properties are stripped by the shared AJV removeAdditional setting (no 400).

Semantics (spec D1, server/src/services/schedulePreviewService.ts):

  • The overrides are applied in memory to a copy of the engine inputs. Nothing is persisted and no autoReschedule runs on this request.
  • durationDays drives the engine when present (EF = ES + duration); endDate is then ignored.
  • An endDate without durationDays becomes a duration from the task's effective start: override.startDate ?? actualStartDate ?? baseline projectedStartDate ?? startDate. With no effective start only endDate is set.
  • An override on a completed task is accepted and has no effect (Rule 1: actual dates win).
  • A request without overrides takes the unchanged code path and returns no impact field.

Scheduling modes:

Mode Description
full Schedules all work items from scratch based on dependencies, constraints, and the current date
cascade Starting from the anchor work item, propagates date changes only to downstream successors

Response (200 OK):

{
  "scheduledItems": [
    {
      "workItemId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "previousStartDate": "2026-03-01",
      "previousEndDate": "2026-03-15",
      "scheduledStartDate": "2026-03-05",
      "scheduledEndDate": "2026-03-19",
      "latestStartDate": "2026-03-05",
      "latestFinishDate": "2026-03-19",
      "totalFloat": 0,
      "isCritical": true
    },
    {
      "workItemId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "previousStartDate": null,
      "previousEndDate": null,
      "scheduledStartDate": "2026-03-20",
      "scheduledEndDate": "2026-03-30",
      "latestStartDate": "2026-03-25",
      "latestFinishDate": "2026-04-04",
      "totalFloat": 5,
      "isCritical": false
    }
  ],
  "criticalPath": ["a1b2c3d4-e5f6-7890-abcd-ef1234567890"],
  "warnings": [
    {
      "workItemId": "c3d4e5f6-a7b8-9012-cdef-123456789012",
      "type": "start_before_violated",
      "message": "Scheduled start date (2026-04-10) exceeds start-before constraint (2026-04-01)"
    },
    {
      "workItemId": "d4e5f6a7-b8c9-0123-defa-234567890123",
      "type": "no_duration",
      "message": "Work item has no duration set; scheduled as zero-duration"
    }
  ]
}

Response type definitions:

interface ScheduleResponse {
  scheduledItems: ScheduledItem[];
  criticalPath: string[]; // Array of work item IDs on the critical path (zero float)
  warnings: ScheduleWarning[];
}

interface ScheduledItem {
  workItemId: string;
  previousStartDate: string | null; // Current start_date before scheduling
  previousEndDate: string | null; // Current end_date before scheduling
  scheduledStartDate: string; // Earliest start (ES) - ISO 8601 date
  scheduledEndDate: string; // Earliest finish (EF) - ISO 8601 date
  latestStartDate: string; // Latest start (LS) - ISO 8601 date
  latestFinishDate: string; // Latest finish (LF) - ISO 8601 date
  totalFloat: number; // Days of slack: LS - ES (0 = critical)
  isCritical: boolean; // true if on the critical path
  isLate: boolean; // true when dates were clamped to today (behind schedule)
}

type ScheduleWarningType = 'start_before_violated' | 'no_duration' | 'already_completed';

interface ScheduleWarning {
  workItemId: string;
  type: ScheduleWarningType;
  message: string;
}

/** Response when the request carries `overrides` (#2247). */
interface SchedulePreviewResponse extends ScheduleResponse {
  impact: ScheduleImpact;
}

interface ScheduleImpact {
  /** Unfinished tasks, other than the overridden ones, whose shown end changes; newEndDate asc, then id. */
  movedItems: ScheduleImpactItem[];
  /** Unreached milestones whose projected date changes; newProjectedDate asc, then id. */
  milestones: ScheduleImpactMilestone[];
  /** Latest shown end (actual ?? projected) over all tasks, before and after. */
  projectEnd: { previousDate: string | null; newDate: string | null };
  /** computeHoldsUp on the previewed projection: the union over the overridden tasks. */
  holdsUp: AttentionHoldsUp; // { count, startingWithin14Days }
}

interface ScheduleImpactItem {
  workItemId: string;
  previousEndDate: string;
  newEndDate: string;
  deltaDays: number; // newEndDate - previousEndDate (negative = earlier)
  isLate: boolean; // late in the previewed forecast
}

interface ScheduleImpactMilestone {
  milestoneId: number;
  previousProjectedDate: string;
  newProjectedDate: string;
  deltaDays: number;
}

impact (#2247). Compares two projections of the same schedule graph (projectSchedule, contract 4) with the same today:

  • Before: the stored graph as it is.
  • After: the graph as Save + autoReschedule would store it. The overrides are applied, the engine runs without the today floor (the planned run), and every dated task takes those planned dates (settleGraph). Undated tasks with no predecessor, completed tasks (actual dates win) and milestone nodes are left as they are. That graph is then projected with the floor. Without this step, stored dates would pin the successors and the preview would understate what Save moves.
  • movedItems and milestones compare the shown dates (shownWorkItemDates: actual, else projected). Completed tasks, the overridden tasks themselves (they are the change) and reached milestones are never listed.
  • holdsUp calls computeHoldsUp (contract 1, the one holds-up definition) on the previewed index and projection for each overridden task and reports the union through summarizeHoldsUp. It is the preview's "Holds up n tasks" (data-figure="holds-up"); the "Moves n tasks" summary below it is a different count (dates that change) and carries no marker.

The top-level scheduledItems, criticalPath and warnings are the existing engine output (with the today floor) over the patched inputs, so a mode/anchorWorkItemId pair still means what it means without overrides.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid mode, missing anchorWorkItemId for cascade mode; overrides failing the schema (empty, more than 50, an entry with none of the three fields, a malformed date pattern, durationDays out of range)
400 VALIDATION_ERROR An override failing a semantic check. details: { workItemIds: string[], reason }, checked in this order: duplicate (a workItemId repeated), unknown_work_item, invalid_date (matches the pattern but is not a calendar date, e.g. 2026-02-30), start_after_end (startDate > endDate), end_before_start (an endDate alone before the task's effective start). Details carry ids and the reason only, never titles
401 UNAUTHORIZED No valid session
404 NOT_FOUND Anchor work item does not exist (cascade mode)
409 CIRCULAR_DEPENDENCY The dependency graph contains a cycle. The details field includes cycle (array of work item IDs; milestone nodes are filtered out). An override cannot introduce a cycle (it changes dates, not dependencies)

Notes:

  • The endpoint is read-only — no database changes are made. The client applies accepted changes via PATCH /api/work-items/:id.
  • Tasks with no dependencies and no constraints retain their current dates (they are not moved by the scheduling engine).
  • The scheduling engine only processes work items that have at least one dependency or a duration_days value. Orphan work items with no dates and no dependencies are excluded.
  • Items with status completed are included in the schedule calculation (their dates are fixed), but a warning of type already_completed is generated if their dates would change.
  • The criticalPath array contains work item IDs in topological order (from project start to project end).
  • The isLate flag indicates that the scheduling engine clamped an item's dates to today because they fell in the past. Rule 2: a not_started item whose computed start is before today has its start floored to today. Rule 3: an in_progress item whose computed end is before today has its end floored to today. When actual dates are set (Rule 1), they take precedence and isLate is false.
  • autoReschedule (server-side, after writes) persists the result of the same engine without Rules 2/3; isLate here is the preview with the floor. The floor is a read-time projection on the timeline and work-item reads (contract 4, #2199).
  • Reschedule preview (#2247). The client (ReschedulePanel, useReschedulePreview) sends { mode: "cascade", anchorWorkItemId, overrides: [one entry] }; Save is PATCH /api/work-items/:id with { durationDays, endDate }, which runs autoReschedule. Undo is a client re-PATCH of the previous stored { durationDays, endDate } (server undo tokens exist only for status columns).

Timeline Endpoints

GET /api/timeline

Returns an aggregated timeline view combining work items (with dates, dependencies, and scheduling metadata), milestones, and critical path information. This endpoint is optimized for rendering the Gantt chart and calendar views.

Auth required: Yes

Query parameters: none. The endpoint always returns the whole timeline (see Notes).

Response (200 OK):

{
  "workItems": [
    {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "title": "Foundation Work",
      "status": "in_progress",
      "startDate": "2026-03-01",
      "endDate": "2026-03-15",
      "actualStartDate": "2026-03-02",
      "actualEndDate": null,
      "durationDays": 14,
      "startAfter": "2026-02-28",
      "startBefore": "2026-03-10",
      "assignedUser": {
        "id": "550e8400-e29b-41d4-a716-446655440000",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "area": { "id": "area-kitchen", "name": "Kitchen", "color": "#F59E0B", "ancestors": [] }
    }
  ],
  "dependencies": [
    {
      "predecessorId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "successorId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "dependencyType": "finish_to_start",
      "leadLagDays": 0
    }
  ],
  "milestones": [
    {
      "id": 1,
      "title": "Foundation Complete",
      "targetDate": "2026-04-15",
      "isCompleted": false,
      "completedAt": null,
      "color": "#EF4444",
      "workItemIds": ["a1b2c3d4-e5f6-7890-abcd-ef1234567890"]
    }
  ],
  "householdItems": [
    {
      "id": "hi-001",
      "name": "Kitchen Faucet",
      "category": "fixtures",
      "status": "ordered",
      "targetDeliveryDate": "2026-05-15",
      "isLate": false,
      "earliestDeliveryDate": "2026-03-16",
      "latestDeliveryDate": "2026-04-01",
      "actualDeliveryDate": null,
      "area": { "id": "area-kitchen", "name": "Kitchen", "color": "#F59E0B", "ancestors": [] },
      "dependencyIds": [
        { "predecessorType": "work_item", "predecessorId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890" }
      ]
    }
  ],
  "criticalPath": ["a1b2c3d4-e5f6-7890-abcd-ef1234567890"],
  "dateRange": {
    "earliest": "2026-03-01",
    "latest": "2026-12-15"
  },
  "holdsUp": {
    "workItems": {
      "a1b2c3d4-e5f6-7890-abcd-ef1234567890": { "count": 2, "startingWithin14Days": 1 }
    },
    "milestones": { "1": { "count": 0, "startingWithin14Days": 0 } }
  }
}

Response type definitions:

interface TimelineResponse {
  workItems: TimelineWorkItem[];
  dependencies: TimelineDependency[];
  milestones: TimelineMilestone[];
  householdItems: TimelineHouseholdItem[];
  criticalPath: string[]; // Work item IDs on the critical path
  dateRange: TimelineDateRange | null; // null when no work items have dates
  holdsUp?: TimelineHoldsUp; // Always sent by the server (#2232); optional in the type only so older mocks stay valid
}

/** Holds-up counts from `computeHoldsUp` (EPIC-21 contract 1, the one definition), per timeline subject (#2232). */
interface TimelineHoldsUp {
  workItems: Record<string, AttentionHoldsUp>; // keyed by work item id; one entry per item in `workItems` (zero counts included)
  milestones: Record<string, AttentionHoldsUp>; // keyed by milestone id as a string; one entry per item in `milestones`
}

// AttentionHoldsUp (shared/src/types/attention.ts): { count: number; startingWithin14Days: number }

interface TimelineWorkItem {
  id: string;
  title: string;
  status: WorkItemStatus;
  startDate: string | null; // PLANNED start (stored); null for an undated task
  endDate: string | null; // PLANNED end (stored)
  projectedStartDate: string | null; // Forecast start (after the today floor); equals startDate unless late/held up; start of the forecast for an undated task
  projectedEndDate: string | null; // Forecast end; equals endDate unless late/held up
  isLate: boolean; // The today floor moved this item itself (never for completed or undated)
  lateDays: number | null; // >= 1 exactly when isLate
  isHeldUp: boolean; // Not late itself, but moved by an unfinished late predecessor
  actualStartDate: string | null; // Actual start date for delay tracking
  actualEndDate: string | null; // Actual end date for delay tracking
  durationDays: number | null;
  startAfter: string | null; // Earliest start constraint (scheduling)
  startBefore: string | null; // Latest start constraint (scheduling)
  assignedUser: UserSummary | null;
  assignedVendor: TimelineVendorSummary | null;
  area: AreaSummary | null;
}

/** Vendor on a timeline task (#2247): VendorSummary plus the company phone for the Schedule panel's Call button. */
interface TimelineVendorSummary extends VendorSummary {
  phone?: string | null; // Always sent by the server (vendors.phone, null when unset); optional in the type only so older mocks stay valid
}

interface TimelineDependency {
  predecessorId: string;
  successorId: string;
  dependencyType: DependencyType;
  leadLagDays: number;
}

interface TimelineMilestone {
  id: number;
  title: string;
  targetDate: string;
  isCompleted: boolean;
  completedAt: string | null; // ISO 8601 timestamp when completed
  color: string | null;
  workItemIds: string[]; // IDs of linked work items
  projectedDate: string | null; // Forecast: latest projectedEndDate among contributing work items; null when none
  isLate: boolean; // Not completed and projectedDate > targetDate
  lateDays: number | null; // projectedDate - targetDate in days when isLate, else null
  isEarly: boolean; // Not completed and projectedDate < targetDate
  earlyDays: number | null; // targetDate - projectedDate in days when isEarly, else null
  isCritical: boolean; // On the (forecast) critical path
}

interface TimelineDateRange {
  earliest: string; // ISO 8601 date — minimum over planned and forecast start dates of all returned work items
  latest: string; // ISO 8601 date — maximum over planned and forecast end dates of all returned work items
}

/** Household item entry — zero-duration CPM node scheduled via work item/milestone dependencies. */
interface TimelineHouseholdItem {
  id: string;
  name: string;
  category: HouseholdItemCategory;
  status: HouseholdItemStatus;
  targetDeliveryDate: string | null; // Computed by scheduling engine from constraints & dependencies
  isLate: boolean; // isPurchaseLate: not arrived, no actual delivery, (latestDeliveryDate ?? earliestDeliveryDate) < today
  earliestDeliveryDate: string | null; // User-editable constraint (minimum acceptable date)
  latestDeliveryDate: string | null; // User-editable constraint (hard deadline)
  actualDeliveryDate: string | null; // Actual delivery date (set when item arrives)
  area?: AreaSummary | null; // The purchase's area (#2198); always sent by the server
  dependencyIds: HouseholdItemDepRef[]; // References to work item/milestone predecessors
}

interface HouseholdItemDepRef {
  predecessorType: 'work_item' | 'milestone';
  predecessorId: string;
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

Notes:

  • The timeline endpoint returns ALL work items that have a stored or forecast date (startDate, endDate, projectedStartDate or projectedEndDate non-null), ALL dependencies, ALL milestones, and the current critical path.
  • The critical path is always computed over the full dataset using the CPM algorithm (same as POST /api/schedule but read-only). For the small dataset sizes expected (<200 items), this is performant.
  • startDate/endDate are the stored planned dates and are no longer replaced for not-started items (D-38). Forecast dates, isLate, lateDays and isHeldUp come from the one read-time projection (engine run without and with the today floor; contract 4, #2199). The critical path and milestone projectedDate/isLate/isEarly use the forecast run.
  • dateRange is computed from the returned work items over planned and forecast dates. If no work items have dates set, dateRange is null.
  • holdsUp (#2232) is computeHoldsUp (EPIC-21 contract 1, "Holds up") for every returned work item (node id = work item id) and every returned milestone (node id = MILESTONE_NODE_PREFIX + id), run on the same schedule graph, the same today (todayUtc()) and the same projection as the rest of the response. It is the "Holds up n tasks" figure of the Schedule and Calendar tooltips and equals the holdsUp of the subject's GET /api/attention row. It counts every unfinished task that moves if the subject slips (chains and milestone gates included), so it can differ from the number of direct successors in dependencies.
  • assignedVendor.phone (#2247) is the company's stored phone (vendors.phone), so the Schedule side panel can offer Call without a second request. Members can already read it through GET /api/vendors/:id.
  • This endpoint does not include budget information -- it is purpose-built for the Gantt chart and calendar views. Use GET /api/work-items/:id for full work item details including budgets.
  • The householdItems array contains household items with their computed delivery dates (earliestDeliveryDate, latestDeliveryDate) and dependency references. Household items are zero-duration CPM nodes positioned on the timeline based on their predecessor work items and milestones. The isLate flag is the one purchase Late definition, isPurchaseLate (server/src/services/shared/purchaseLate.ts, #2229): not arrived, no actual delivery date, and the planned date (latestDeliveryDate ?? earliestDeliveryDate) is before today. The daily reschedule stores it in household_items.is_late; flooring targetDeliveryDate to today no longer sets it.

Conventions

Base URL

All API endpoints are prefixed with /api/.

Error Response Shape

All error responses conform to the ApiErrorResponse interface defined in @cornerstone/shared:

interface ApiErrorResponse {
  error: {
    code: ErrorCode; // Machine-readable error code (see table below)
    message: string; // Human-readable description
    details?: Record<string, unknown>; // Optional additional context
  };
}

Example response:

{
  "error": {
    "code": "NOT_FOUND",
    "message": "User not found",
    "details": { "id": "550e8400-e29b-41d4-a716-446655440000" }
  }
}

The details field is omitted entirely (not null) when there is no additional context.

Error Codes

All error codes are defined as an ErrorCode string literal union type in @cornerstone/shared. The code field in every error response is constrained to one of these values.

Error Code HTTP Status Description When Used
NOT_FOUND 404 Resource not found A specific entity (by ID) does not exist
ROUTE_NOT_FOUND 404 API route not found The requested /api/* path does not match any registered route
VALIDATION_ERROR 400 Request validation failed Request body, query params, or path params fail JSON schema validation; business logic validation fails; or an unmapped Fastify/plugin error with a status below 500
UNAUTHORIZED 401 Not authenticated No valid session/token provided
FORBIDDEN 403 Not authorized Authenticated but insufficient permissions
CONFLICT 409 Resource conflict Duplicate unique constraint, optimistic locking conflict, or another state conflict that has no more specific code
INTERNAL_ERROR 500 Internal server error Unhandled/unexpected server errors, and unmapped Fastify/plugin errors with a status of 500 or above
SETUP_COMPLETE 403 Setup already done POST /api/auth/setup called when users already exist
INVALID_CREDENTIALS 401 Authentication failed Wrong email/password on login or password change
ACCOUNT_DEACTIVATED 401 Account disabled Deactivated account with a password attempting local login (an account without a password gets INVALID_CREDENTIALS)
ACCOUNT_LOCKED 423 Account temporarily locked Local login after too many failed attempts; details.lockedUntil carries the unlock timestamp
RATE_LIMIT_EXCEEDED 429 Too many requests Login endpoint rate limit exceeded (AUTH_RATE_LIMIT_MAX per AUTH_RATE_LIMIT_WINDOW), or the password-hashing queue is full on any scrypt endpoint
SELF_DEACTIVATION 409 Cannot deactivate self Admin trying to deactivate their own account
LAST_ADMIN 409 Cannot remove last admin Attempting to demote or deactivate the only remaining admin
OIDC_NOT_CONFIGURED 404 / 400 OIDC not available OIDC env vars not set: 404 on GET /api/auth/oidc/login; 400 on POST /api/users with authProvider: "oidc"
OIDC_MISSING_EMAIL 403 OIDC email claim missing OIDC callback: no account bound to the subject and the ID token has no email claim (surfaced as redirect missing_email)
OIDC_EMAIL_UNVERIFIED 403 OIDC email not verified OIDC callback: no account bound to the subject and the ID token's email_verified is not true (surfaced as redirect oidc_email_unverified)
OIDC_NO_MATCHING_ACCOUNT 403 No account for OIDC identity OIDC callback: no subject or unambiguous email match, and JIT provisioning is off, the email is ambiguous, or setup has not run (redirect oidc_no_matching_account)
CIRCULAR_DEPENDENCY 409 Link would create a cycle Work-item dependency create, the milestone contribute/require link endpoints, and POST /api/schedule. details.cycle (work item IDs) on work-item dependency create and schedule
DUPLICATE_DEPENDENCY 409 Link already exists Work-item and household-item dependency create (same predecessor/successor pair), and the four milestone link endpoints (same work item/milestone pair)
CATEGORY_IN_USE 409 Budget category in use Budget category is referenced by work items or subsidy programs and cannot be deleted
VENDOR_IN_USE 409 Vendor in use Vendor has invoices or is referenced by budget lines and cannot be deleted
BUDGET_SOURCE_IN_USE 409 Budget source in use Budget source is referenced by budget lines and cannot be deleted
DISCRETIONARY_SOURCE 409 Discretionary source is protected Attempt to delete the Own funds budget source
SUBSIDY_PROGRAM_IN_USE 409 Subsidy program in use Subsidy program is referenced by work items and cannot be deleted
BUDGET_LINE_IN_USE 409 Budget line in use Budget line has linked invoices and cannot be deleted
AREA_IN_USE 409 Area in use Area is still referenced and cannot be deleted
TRADE_IN_USE 409 Trade in use Trade is still referenced and cannot be deleted
SAME_SOURCE 400 Same source and target Budget-line move between sources where source and target are the same budget source
EMPTY_SELECTION 400 Nothing selected Budget-line move with no budget line IDs, or report-content generation with no invoices selected
STALE_OWNERSHIP 409 Stale budget-line ownership Budget-line move where one or more IDs do not exist or no longer belong to the specified source
REORDER_SET_MISMATCH 409 Reference list changed Reference-list reorder (PATCH …/reorder on budget categories, household item categories, orientations, and one sibling group of areas, #2218) whose ids are not exactly the current set; details.missingIds, details.unknownIds
SUBMISSION_ALREADY_REVERTED 409 Submission already reverted POST /api/source-reports/batches/:batchId/revert when a revert batch already names the submission (#2243)
SUBMISSION_CHANGED 409 Submission items changed Revert when any item of the submission is no longer claimed (or is gone); details.items: [{ type: 'invoice' | 'progress_payment', id, status }] is for clients and tests only; nothing is written (#2243)
INVOICES_NOT_CLAIMABLE 409 Invoices not claimable Source-report claim where one or more invoices are not in a claimable state; details.invoiceIds lists them
PAPERLESS_NOT_CONFIGURED 503 Paperless-ngx not configured /api/paperless/* (except /status) and invoice auto-itemize when PAPERLESS_URL/PAPERLESS_API_TOKEN are not set
PAPERLESS_UNREACHABLE 502 Paperless-ngx unreachable Network failure connecting to the Paperless-ngx instance
PAPERLESS_ERROR 502 Paperless-ngx upstream error Paperless-ngx returned a non-success status
DUPLICATE_DOCUMENT_LINK 409 Duplicate document link The document is already linked to the entity (on convert-quotation: on the quotation/deposit tier)
ITEMIZED_SUM_EXCEEDS_INVOICE 400 Itemized sum exceeds invoice Sum of invoice_budget_lines.itemized_amount would exceed the invoice amount
BUDGET_LINE_ALREADY_LINKED 409 Budget line linked elsewhere Linking a budget line that is already linked to a different invoice; on auto-itemize commits also the same line twice in one request, or append on a line already linked to this invoice (#2149)
BUDGET_LINE_ALREADY_ASSIGNED 409 Budget line already assigned Assigning an orphan budget line to a parent item when it already has one
INVALID_DEPOSIT_STATUS_TRANSITION 400 Invalid deposit status transition Attempted status transition is not allowed by the state machine
INVALID_DEPOSIT_DATE_FOR_STATUS 400 Invalid date for deposit status paidDate or claimedDate set when the status does not support it
REFUND_EXCEEDS_INVOICE 400 Refunds exceed refund cap Sum of refund-type entries would exceed the larger of the invoice amount and the deposit-type sum
INVOICE_NOT_QUOTATION 409 Invoice is not a quotation POST /api/invoices/:invoiceId/convert-quotation on an invoice whose status is not quotation (Story #2107)
INVALID_METADATA 400 Invalid diary metadata Diary entry metadata fails the per-entry-type shape rules (e.g. daily_log weather, signatures)
INVALID_ENTRY_TYPE 400 Invalid diary entry type Creating a diary entry with a non-manual (automatic-only) entry type
IMMUTABLE_ENTRY 403 Diary entry is immutable Updating an automatic entry or a saved diary entry with signatures, mutating a photo (upload, update, delete, reorder, annotation save/clear) on a saved diary entry with signatures (drafts stay editable, #2124), uploading a photo to an automatic diary entry (#2220), or promoting an automatic entry
ALREADY_SAVED 400 Diary entry already saved Promoting a diary entry whose status is already saved
PAYLOAD_TOO_LARGE 413 Payload too large Photo upload exceeds PHOTO_MAX_FILE_SIZE_MB, or Fastify body/multipart limits are exceeded
BACKUP_IN_PROGRESS 409 Backup/restore running Another backup or restore is already in progress
BACKUP_NOT_FOUND 404 Backup not found The named backup archive does not exist in BACKUP_DIR
BACKUP_FAILED 500 Backup failed Creating the backup archive failed
RESTORE_FAILED 500 Restore failed Restoring from a backup archive failed
LLM_NOT_CONFIGURED 503 LLM not configured LLM-backed endpoint called while the LLM_* variables are not set
LLM_UNREACHABLE 502 LLM provider unreachable Network failure calling the LLM provider (details suppressed from the response)
LLM_INVALID_RESPONSE 502 LLM invalid response LLM returned output that cannot be parsed/validated (details suppressed)
LLM_UPSTREAM_ERROR 502 LLM upstream error LLM provider returned an error status (details suppressed)

Clients translate by code, never by message. code is the stable contract. message is untranslated English developer text for logs and API consumers, and the UI must not render it. The client maps code to copy through translateApiError() (errors.json), or through a page-specific t() key where a generic code (typically CONFLICT) has a page-specific meaning. In client/src, an ESLint no-restricted-syntax rule rejects reading .error.message from an API error body, including destructuring message from it (#2129). ApiClientError#message carries the same server text; the lint rule does not see it, so code review enforces that one.

A specific reason is always the top-level code, never nested as details.code: the client cannot translate a reason it has to dig for. details carries data only (for example cycle or invoiceIds). The ErrorCode union is derived from the runtime tuple ERROR_CODES in @cornerstone/shared, and a client test fails when any member lacks an errors.json key in any locale. A code with no emitting path is deleted rather than kept as "Reserved".

Validation Error Details

When code is VALIDATION_ERROR and the error originates from Fastify's JSON schema validation (AJV), the details field contains field-level information:

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Validation failed",
    "details": {
      "fields": [
        {
          "path": "/name",
          "message": "must be string",
          "params": { "type": "string" }
        },
        {
          "path": "/",
          "message": "must have required property 'email'",
          "params": { "missingProperty": "email" }
        }
      ]
    }
  }
}

Each entry in fields includes:

  • path -- JSON pointer to the field that failed validation (e.g., /name, /address/zip), or / for root-level errors
  • message -- Human-readable validation failure message from AJV
  • params -- AJV-specific parameters for the validation rule (optional, present when AJV provides them)

Production Error Sanitization

In production mode (NODE_ENV=production), INTERNAL_ERROR responses always return the message "An internal error occurred" regardless of the actual error. This prevents leaking internal details (database errors, file paths, stack traces) to clients. In development mode, the original error message is preserved for debugging.

HTTP Status Codes

Code Usage
200 Successful retrieval or update
201 Successful creation
204 Successful deletion or action with no response body
302 OIDC redirects
400 Validation error (malformed request)
401 Not authenticated or invalid credentials
403 Not authorized (insufficient role) or action not permitted
404 Resource not found or route not found
409 Conflict (duplicate, self-deactivation, last admin, entity in use)
413 Payload too large (upload/body limits)
423 Account temporarily locked
429 Rate limit exceeded
500 Internal server error
502 Bad gateway (Paperless-ngx or LLM upstream failure)
503 Optional integration not configured (Paperless-ngx, LLM)

Content Type

All requests and responses use application/json, except:

  • OIDC redirect endpoints (GET /api/auth/oidc/login and GET /api/auth/oidc/callback) which return HTTP redirects (302)
  • 204 No Content responses which have no body

Pagination

Offset-based pagination is used for list endpoints that may return many items. The standard query parameters and response shape are:

Query parameters:

Parameter Type Default Constraints Description
page integer 1 >= 1 Page number (1-indexed)
pageSize integer 25 1-100 Items per page

Response metadata:

All paginated list responses include a pagination object alongside the items array:

{
  "items": [...],
  "pagination": {
    "page": 1,
    "pageSize": 25,
    "totalItems": 47,
    "totalPages": 2
  }
}
Field Type Description
page integer Current page number
pageSize integer Items per page (as requested, clamped to 1-100)
totalItems integer Total number of items matching the filter criteria
totalPages integer Computed: ceil(totalItems / pageSize)

Notes:

  • Page numbers are 1-indexed. Requesting page 0 or negative numbers returns a validation error.
  • Requesting a page beyond totalPages returns an empty items array with valid pagination metadata.
  • The GET /api/users endpoint does not use pagination (fewer than 5 users). The GET /api/areas endpoint does not use pagination (areas are expected to be fewer than ~100). The GET /api/trades endpoint does not use pagination (trades are expected to be fewer than ~50). The GET /api/budget-categories endpoint does not use pagination (budget categories are a small, finite collection). The GET /api/budget-sources endpoint does not use pagination (budget sources are a small, finite collection). The GET /api/work-items/:id/budgets endpoint does not use pagination (budget lines per work item are a small collection). The GET /api/vendors/:id/invoices endpoint does not use pagination (invoices per vendor are typically fewer than ~50). The GET /api/milestones endpoint does not use pagination (milestones are expected to be fewer than ~50).
  • The GET /api/invoices endpoint (standalone, cross-vendor) uses standard pagination since it spans all vendors and may return many items.

Filtering & Sorting

List endpoints support filtering and sorting via query parameters:

Filtering:

  • Filter parameters use the exact field name (e.g., status, assignedUserId, areaId)
  • Multiple values for the same filter are not supported in a single parameter; use one value per filter
  • Multiple different filters are combined with AND logic
  • Text search uses the q parameter for case-insensitive substring matching

Sorting:

  • sortBy specifies the field to sort by (default varies per endpoint)
  • sortOrder specifies direction: asc or desc (default varies per endpoint)
  • Only one sort field is supported per request

User Response Shape

All user objects returned by the API follow a consistent shape. The password_hash and oidc_subject fields are never included in API responses; whether a subject is bound is exposed only as the derived boolean oidcLinked.

authProvider and oidcLinked are independent: authProvider records how the account was created (local = setup/admin-created with a password, oidc = created without a password: OIDC just-in-time provisioning or an admin-created SSO-only account), while oidcLinked records whether an OIDC identity is currently bound. An oidc account from JIT provisioning is always linked; an admin-created SSO-only account is oidcLinked: false until its first OIDC sign-in. A local account that has signed in via OIDC once is authProvider: "local", oidcLinked: true, and can use both sign-in methods. See ADR-035.

interface UserResponse {
  id: string;
  email: string;
  displayName: string;
  role: 'admin' | 'member';
  authProvider: 'local' | 'oidc'; // how the account was CREATED — never changes on linking
  oidcLinked: boolean; // derived: oidc_subject IS NOT NULL — the account can sign in via OIDC
  createdAt: string; // ISO 8601
  updatedAt?: string; // ISO 8601 (included in profile endpoints)
  deactivatedAt?: string | null; // ISO 8601 or null (included in admin list)
}

The server emits oidcLinked, updatedAt and deactivatedAt on every user object (toUserResponse in server/src/services/userService.ts has a single shape); the optional markers above mirror the shared type, not what any endpoint omits. oidcLinked is required in the shared type (shared/src/types/user.ts), so a client never needs to handle an absent value.

The admin view of GET /api/users extends each row with lockedUntil (UserListItem in shared/src/types/user.ts); see that endpoint. toUserResponse, used by /me, login, PATCH and POST, is unchanged (#2219).


EPIC-08: Paperless-ngx Document Integration

Overview

EPIC-08 adds two groups of endpoints:

  1. Paperless-ngx Proxy (/api/paperless/*) -- proxies requests to a Paperless-ngx instance, transforming responses into Cornerstone's own shapes. Keeps the API token server-side.
  2. Document Links (/api/document-links) -- CRUD for linking Paperless-ngx documents to Cornerstone entities (work items, household items, invoices, funding sources, grants). The entity types are the shared tuple DOCUMENT_LINK_ENTITY_TYPES (work_item, household_item, invoice, budget_source, subsidy_program; DocumentLinkEntityType is derived from it, #2233), which the route schemas also use.

Both groups require authentication. All proxy endpoints return 503 SERVICE_UNAVAILABLE with error code PAPERLESS_NOT_CONFIGURED if the Paperless-ngx integration is not configured (missing PAPERLESS_URL or PAPERLESS_API_TOKEN environment variables).

Environment Variables

Variable Default Description
PAPERLESS_URL (none) Base URL of the Paperless-ngx instance
PAPERLESS_API_TOKEN (none) API authentication token for Paperless-ngx
PAPERLESS_FILTER_TAG (none) Optional tag name. When set, only documents with this tag are visible through Cornerstone (see Filter tag below)

Filter tag (PAPERLESS_FILTER_TAG)

When a filter tag is configured, Cornerstone shows only documents that carry it, and it fails closed: it never falls back to unfiltered results.

Read Tag configured and resolved Tag configured but unresolvable (not found, lookup error or timeout) No tag configured
GET /api/paperless/documents (list and full-text search, also with excludeLinked: every upstream call of that mechanism carries the tag) restricted to the tag (tags__id__all) 502 PAPERLESS_ERROR unfiltered
By-id reads: GET /api/paperless/documents/:id, …/:id/thumb, …/:id/preview, and every read built on the single-document fetch (getDocument: invoice auto-itemize and its preview) a document without the tag returns 404 NOT_FOUND, the same shape as a missing document 502 PAPERLESS_ERROR any document
Command search, Paperless group (EPIC-21 contract 6) restricted to the tag group status: "unavailable" unfiltered
Linked-document metadata (bulk fetch, getDocuments): GET /api/document-links (by entity; the by-document form makes no Paperless call and carries no document metadata), GET /api/source-reports, POST /api/source-reports/generate-content a linked document without the tag is treated like a missing one: its metadata is null (see each endpoint) every linked document is treated as missing (all metadata null); the request still succeeds any document
Upload: POST /api/paperless/documents the document is tagged with it 502 PAPERLESS_ERROR; nothing is sent to Paperless uploaded untagged

The 404 for an untagged document is deliberate: a caller cannot tell an existing but hidden document from a missing one. GET /api/paperless/status reports filterTag: null when the configured tag does not resolve.

See ADR-015 for design rationale.


Paperless-ngx Proxy Endpoints

GET /api/paperless/status

Returns the current status of the Paperless-ngx integration (configured, reachable).

Auth required: Yes

Response (200 OK -- configured and reachable):

{
  "configured": true,
  "reachable": true,
  "error": null
}

Response (200 OK -- configured but unreachable):

{
  "configured": true,
  "reachable": false,
  "error": "Connection refused"
}

Response (200 OK -- not configured):

{
  "configured": false,
  "reachable": false,
  "error": null
}

Every response also carries two fields the examples above omit:

Field Type Meaning
paperlessUrl string | null The browser-facing base URL for document links: PAPERLESS_EXTERNAL_URL, else PAPERLESS_URL, passed through publicUrlWithoutCredentials (user info, query and hash removed, path kept; #2217); null when not configured
filterTag string | null PAPERLESS_FILTER_TAG when it resolves to an existing tag; null when unset, not found, or unreachable

Notes:

  • This endpoint always returns 200 OK. The status is encoded in the response body.
  • Unlike other proxy endpoints, this does not return 503 when Paperless-ngx is not configured.
  • The reachable check makes a lightweight call to the Paperless-ngx API to verify connectivity.
  • Time limit (#2217): the probe and the filter-tag lookup share one AbortSignal.timeout(5000). When it fires, the response is reachable: false with error: "Timed out after 5 s"; it is never a 5xx. Before #2217 the probe had no limit and hung for as long as the upstream did.
  • error is the upstream failure text with hosts and ports replaced by <host>. Clients must not render it (Settings › Integrations shows its own sentence); it stays for logs and diagnostics.

GET /api/paperless/documents

Search and browse documents in Paperless-ngx.

Auth required: Yes

Query parameters:

Parameter Type Default Description
query string (none) Full-text search query
tags string (none) Comma-separated Paperless-ngx tag IDs to filter by
correspondent number (none) Filter by Paperless-ngx correspondent ID
documentType number (none) Filter by Paperless-ngx document type ID
page number 1 Page number (1-indexed)
pageSize number 25 Items per page (max 100)
sortBy string created Sort field: created, added, modified, title, archive_serial_number
sortOrder string desc Sort direction: asc or desc
excludeLinked boolean false Hide documents linked to any record (#2252, see "Hide already linked" below)

Response (200 OK):

{
  "documents": [
    {
      "id": 42,
      "title": "Invoice from Builder Co",
      "content": "This invoice covers the foundation work...",
      "tags": [
        {
          "id": 5,
          "name": "invoice",
          "color": "#ff0000",
          "documentCount": 15
        }
      ],
      "created": "2026-01-15",
      "added": "2026-01-16T08:30:00Z",
      "modified": "2026-01-16T08:30:00Z",
      "correspondent": "Builder Co",
      "documentType": "Invoice",
      "archiveSerialNumber": 1042,
      "originalFileName": "invoice-2026-001.pdf",
      "pageCount": 2,
      "searchHit": {
        "score": 0.95,
        "highlights": "This <em>invoice</em> covers the foundation work...",
        "rank": 1
      }
    }
  ],
  "pagination": {
    "page": 1,
    "pageSize": 25,
    "totalItems": 142,
    "totalPages": 6
  }
}

Notes:

  • The searchHit field is only present when a query parameter is provided. It is null for non-search listings.
  • The content field may be truncated for list views. Use the detail endpoint for full content.
  • Tag, correspondent, and document type names are resolved server-side (the Paperless-ngx API returns IDs; the proxy resolves them to names).
  • Filter tag: with PAPERLESS_FILTER_TAG set, results are restricted to that tag; if the tag cannot be resolved the request fails with 502 PAPERLESS_ERROR and never returns unfiltered results (see Filter tag above).
  • Page stability (#2101): for listings without query, the proxy sends Paperless-ngx ordering=<sortBy>,id (both keys in the same direction as sortOrder). The id tiebreaker makes the order total, so consecutive page requests neither repeat nor skip documents. This matters because created is date-only (API v9+), so ties are common. Full-text searches (query set) are ranked by Paperless-ngx's search index, which accepts only a single sort field, so no tiebreaker is sent and page boundaries are not guaranteed stable. Clients that accumulate pages (the infinite-scroll document browser) must de-duplicate by id.

Hide already linked (excludeLinked=true, #2252, ADR-015 §7):

"Linked" means linked to any record of any kind: the ids of GET /api/document-links/linked-ids (documentLinkService.getAllLinkedDocumentIds), read from the local database on each request. The response shape is unchanged and the result is exact: documents holds only unlinked documents, every page except the last is full, and totalItems / totalPages count unlinked documents only (never a lower bound). All other parameters, the filter tag, the ordering and the page-stability rule above apply unchanged; searchHit keeps its meaning (set for query listings, null otherwise). With no linked document anywhere, the request is the plain listing (no extra upstream calls).

Mechanism (with params = the Paperless-ngx filters and ordering of the plain listing, including the filter tag; offset = (page − 1) × pageSize):

  1. Probe: one GET /api/documents/?<params>&page=1&page_size=1 → count and all (Paperless-ngx returns the ids of the whole filtered result in all). all is used only when it is an array whose length equals count.
  2. totalItems: with a usable all, the number of its ids that are not linked. Otherwise count − Σ count(GET /api/documents/?<params>&id__in=<chunk>&page_size=1) over the linked ids in chunks of 100 (parallel). linkedInResult = count − totalItems.
  3. offset ≥ totalItems → empty documents, no further call.
  4. Page, no query and a usable all: all is in list order, so the page's ids are the unlinked ids of all sliced at [offset, offset + pageSize). One GET /api/documents/?<params>&id__in=<ids>&page_size=<n> fetches them (the filter tag is enforced on this call too); a document that vanished in between is dropped.
  5. Page otherwise (full-text query, or no usable all; for a search all is unordered or partial): one GET /api/documents/?<params>&page=1&page_size=<w>&truncate_content=true with w = min(count, offset + pageSize + linkedInResult). At most linkedInResult of the first w results are linked, so the window contains the requested page; the linked ones are dropped and the unlinked ones sliced at [offset, offset + pageSize). One upstream call, one consistent snapshot of the search ranking. No cap: a cap would make the count or paging inexact; the window only grows with the depth a user scrolls into one search, and list rows carry no content.

Upstream calls: one probe, 0 or ⌈linked ids / 100⌉ count calls, one page call (none when step 3 applies), plus the tag, correspondent and type name lookups of the plain listing. Changes in Paperless-ngx between these calls can shift a page like any concurrent change does in the plain listing.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR A parameter has an invalid value (e.g. pageSize above 100, excludeLinked not a boolean, tags not a comma-separated id list). Validated before the Paperless-ngx check, so it also answers when Paperless-ngx is not configured
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR Paperless-ngx returned an error, or the configured filter tag cannot be resolved

GET /api/paperless/documents/:id

Fetch metadata for a single Paperless-ngx document.

Auth required: Yes

URL parameters:

Parameter Type Description
id number Paperless-ngx document ID

Response (200 OK):

{
  "document": {
    "id": 42,
    "title": "Invoice from Builder Co",
    "content": "Full document text content here...",
    "tags": [
      {
        "id": 5,
        "name": "invoice",
        "color": "#ff0000",
        "documentCount": 15
      }
    ],
    "created": "2026-01-15",
    "added": "2026-01-16T08:30:00Z",
    "modified": "2026-01-16T08:30:00Z",
    "correspondent": "Builder Co",
    "documentType": "Invoice",
    "archiveSerialNumber": 1042,
    "originalFileName": "invoice-2026-001.pdf",
    "pageCount": 2
  }
}

Error responses:

HTTP Status Error Code When
404 NOT_FOUND Document not found in Paperless-ngx, or (filter tag configured) the document does not carry the filter tag
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR Paperless-ngx returned an error, or the configured filter tag cannot be resolved

GET /api/paperless/documents/:id/thumb

Fetch the thumbnail image for a Paperless-ngx document. Returns a binary image response (passthrough from Paperless-ngx).

Auth required: Yes

URL parameters:

Parameter Type Description
id number Paperless-ngx document ID

Response (200 OK):

  • Content-Type: image/webp (or as returned by Paperless-ngx)
  • Body: Binary image data

Error responses:

HTTP Status Error Code When
404 NOT_FOUND Document not found in Paperless-ngx, or (filter tag configured) the document does not carry the filter tag
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR The configured filter tag cannot be resolved

GET /api/paperless/documents/:id/preview

Fetch the preview PDF or original document for viewing. Returns a binary response (passthrough from Paperless-ngx).

Auth required: Yes

URL parameters:

Parameter Type Description
id number Paperless-ngx document ID

Response (200 OK):

  • Content-Type: application/pdf (or as returned by Paperless-ngx)
  • Body: Binary document data

Error responses:

HTTP Status Error Code When
404 NOT_FOUND Document not found in Paperless-ngx, or (filter tag configured) the document does not carry the filter tag
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR The configured filter tag cannot be resolved

GET /api/paperless/tags

List all tags from Paperless-ngx.

Auth required: Yes

Response (200 OK):

{
  "tags": [
    {
      "id": 5,
      "name": "invoice",
      "color": "#ff0000",
      "documentCount": 15
    },
    {
      "id": 12,
      "name": "contract",
      "color": "#0000ff",
      "documentCount": 8
    }
  ]
}

Notes:

  • Tags are not paginated (Paperless-ngx instances typically have fewer than ~100 tags).
  • The documentCount field indicates how many documents in Paperless-ngx have this tag.

Error responses:

HTTP Status Error Code When
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR Paperless-ngx returned an error

GET /api/paperless/correspondents

List all correspondents from Paperless-ngx. Used to populate the searchable correspondent (vendor) filter in the Paperless-first invoice picker.

Auth required: Yes

Response (200 OK):

{
  "correspondents": [
    { "id": 3, "name": "Builder Co" },
    { "id": 7, "name": "Elektro Müller GmbH" }
  ]
}

Notes:

  • Correspondents are not paginated (Paperless-ngx instances typically have fewer than ~100 correspondents). Sorted by id ascending.
  • Returns { "correspondents": [] } when the instance has no correspondents (empty-state, not an error).
  • Mirrors the GET /api/paperless/tags pattern. Not cached — fetched on demand when the picker opens.
  • The returned id is passed verbatim to the correspondent query parameter on GET /api/paperless/documents to filter the document list.

Error responses:

HTTP Status Error Code When
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR Paperless-ngx returned an error

GET /api/paperless/document-types

List all document types from Paperless-ngx (#2252). Feeds the Documents page's Type filter, whose options are the complete set, not derived from loaded rows.

Auth required: Yes

Response (200 OK, PaperlessDocumentTypeListResponse):

{
  "documentTypes": [
    { "id": 1, "name": "Invoice" },
    { "id": 4, "name": "Contract" }
  ]
}

Notes:

  • Mirrors GET /api/paperless/correspondents: one upstream call (/api/document_types/?page_size=1000), not paginated, sorted by id ascending, not cached, { "documentTypes": [] } when there are none.
  • The configured filter tag is not applied (as for tags and correspondents): a type name is not a document.
  • The returned id is passed verbatim to the documentType query parameter on GET /api/paperless/documents.

Error responses:

HTTP Status Error Code When
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR Paperless-ngx returned an error

POST /api/paperless/documents

Upload a PDF document to Paperless-ngx's consumption pipeline for processing and archival.

Auth required: Yes

Content-Type: multipart/form-data

Request body (multipart fields):

Field Type Required Description
document file Yes PDF file to upload (must be application/pdf)
title string Yes Display title for the document in Paperless-ngx

Response (201 Created):

{
  "taskId": "12345678-1234-5678-1234-567812345678"
}
Field Type Description
taskId string Paperless-ngx consumption task UUID. Upload processes asynchronously; no document ID returned. The document appears in Paperless-ngx after consumption completes.

Notes:

  • Only PDF files are accepted. Non-PDF uploads return 400 VALIDATION_ERROR.
  • File must be provided in the document multipart field. Missing file returns 400 VALIDATION_ERROR.
  • Title must be provided in the title multipart field. Missing title returns 400 VALIDATION_ERROR.
  • If PAPERLESS_FILTER_TAG is configured, the document is automatically tagged with that tag. If the tag cannot be resolved, the upload fails with 502 PAPERLESS_ERROR and nothing is sent to Paperless-ngx, so an upload can never land outside the tag (it would be invisible to Cornerstone).
  • Documents do NOT appear immediately in Paperless-ngx; they are queued for consumption. Query the consumption task status in Paperless-ngx to track progress.
  • File size is limited by the global multipart limit (default 50MB); oversized files return 413 PAYLOAD_TOO_LARGE.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Non-PDF file, missing file, or missing title
401 UNAUTHORIZED No valid session
413 PAYLOAD_TOO_LARGE File exceeds the multipart size limit
503 PAPERLESS_NOT_CONFIGURED Paperless-ngx integration not set up
502 PAPERLESS_UNREACHABLE Cannot connect to Paperless-ngx
502 PAPERLESS_ERROR Paperless-ngx returned an error, or the configured filter tag cannot be resolved (nothing uploaded)

Document Link Endpoints

These endpoints manage the links between Cornerstone entities and Paperless-ngx documents. They operate on the local document_links table, not on Paperless-ngx directly.

POST /api/document-links

Create a link between a Cornerstone entity and a Paperless-ngx document.

Auth required: Yes

Request body:

{
  "entityType": "invoice",
  "entityId": "550e8400-e29b-41d4-a716-446655440000",
  "paperlessDocumentId": 42,
  "attachmentType": "quotation"
}
Field Type Validation
entityType string Required. One of: work_item, household_item, invoice, budget_source, subsidy_program.
entityId string Required. Must reference an existing entity of the given type.
paperlessDocumentId number Required. Must be a positive integer.
attachmentType string | null | undefined Optional. Only meaningful for entityType='invoice'. One of: quotation, deposit, invoice. Ignored for other entity types (normalized to null). Story #1877

Response (201 Created):

{
  "documentLink": {
    "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "entityType": "invoice",
    "entityId": "550e8400-e29b-41d4-a716-446655440000",
    "paperlessDocumentId": 42,
    "attachmentType": "quotation",
    "createdBy": {
      "id": "user-uuid",
      "displayName": "Admin User"
    },
    "createdAt": "2026-03-01T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid entity type, missing fields, or invalid ID format
404 NOT_FOUND Referenced entity does not exist
409 DUPLICATE_DOCUMENT_LINK This exact link already exists

GET /api/document-links

Two forms: by entity (entityType + entityId: the links of one record, enriched with Paperless-ngx document metadata) and by document (paperlessDocumentId, #2252: the links of one Paperless-ngx document, enriched with the linked records; described after the by-entity form). Exactly one form per request.

Auth required: Yes

Query parameters:

Parameter Type Required Description
entityType string By entity (with entityId) One of: work_item, household_item, invoice, budget_source, subsidy_program
entityId string By entity (with entityType) ID of the entity to get links for
paperlessDocumentId integer By document (alone) Paperless-ngx document id, ≥ 1 (#2252)

Response, by entity (200 OK):

{
  "documentLinks": [
    {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "entityType": "invoice",
      "entityId": "550e8400-e29b-41d4-a716-446655440000",
      "paperlessDocumentId": 42,
      "attachmentType": "invoice",
      "createdBy": {
        "id": "user-uuid",
        "displayName": "Admin User"
      },
      "createdAt": "2026-03-01T10:00:00.000Z",
      "document": {
        "id": 42,
        "title": "Invoice from Builder Co",
        "content": null,
        "tags": [
          {
            "id": 5,
            "name": "invoice",
            "color": "#ff0000",
            "documentCount": 15
          }
        ],
        "created": "2026-01-15",
        "added": "2026-01-16T08:30:00Z",
        "modified": "2026-01-16T08:30:00Z",
        "correspondent": "Builder Co",
        "documentType": "Invoice",
        "archiveSerialNumber": 1042,
        "originalFileName": "invoice-2026-001.pdf",
        "pageCount": 2
      }
    }
  ]
}

Notes:

  • Document metadata is fetched from Paperless-ngx on each request (no caching in Phase 1).
  • If a linked document has been deleted from Paperless-ngx, the document field will be null. The link itself is preserved.
  • With PAPERLESS_FILTER_TAG configured, a linked document that does not carry the tag is treated the same way: document is null and the link is preserved. If the configured tag cannot be resolved, every document is null (see Paperless-ngx › Filter tag).
  • The content field is set to null in list responses to reduce payload size. Use GET /api/paperless/documents/:id for full content.
  • Document links are not paginated (an entity typically has fewer than ~20 linked documents).
  • If Paperless-ngx is not configured, document links are returned without metadata (all document fields are null).

Response, by document (200 OK, DocumentLinksForDocumentResponse, #2252):

{
  "documentLinks": [
    {
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "entityType": "invoice",
      "entityId": "550e8400-e29b-41d4-a716-446655440000",
      "paperlessDocumentId": 42,
      "attachmentType": "invoice",
      "createdBy": { "id": "user-uuid", "displayName": "Admin User" },
      "createdAt": "2026-03-01T10:00:00.000Z",
      "entity": { "title": "INV-1001", "vendorName": "Builder Co", "invoiceStatus": "pending" }
    },
    {
      "id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "entityType": "work_item",
      "entityId": "wi-7",
      "paperlessDocumentId": 42,
      "attachmentType": null,
      "createdBy": null,
      "createdAt": "2026-03-02T09:00:00.000Z",
      "entity": { "title": "Pour foundation", "vendorName": null, "invoiceStatus": null }
    }
  ]
}

Each entry is a DocumentLinkWithEntity: the link fields exactly as in the by-entity form, without document, plus entity: DocumentLinkEntitySummary | null:

Field Type Value
title string | null work_item: title; household_item, budget_source, subsidy_program: name; invoice: invoice number (may be null)
vendorName string | null invoice only: the company's name; otherwise null
invoiceStatus InvoiceStatus | null invoice only: its status (quotation = offer), so the client composes the display title; otherwise null

Notes (by document):

  • Every link of the document across all five kinds, ordered by createdAt then id ascending; not paginated. An unknown id (or a document with no links) returns { "documentLinks": [] }, never 404.
  • entity is null when the linked record no longer exists; the link is still returned so it can be unlinked.
  • Local data only: no Paperless-ngx call is made, so this form works the same with Paperless-ngx not configured or unreachable, and the filter tag does not apply (the response carries no document metadata).
  • Raw fields, no UI words: the client builds kind words and invoice titles (invoiceDisplayTitle).

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Neither form given (entityType and entityId are required without paperlessDocumentId), only one of entityType/entityId, both forms given ("Use either paperlessDocumentId or entityType and entityId"), or an invalid value (entityType not one of the five, paperlessDocumentId not an integer ≥ 1)

GET /api/document-links/linked-ids

Returns the distinct set of Paperless-ngx document IDs that are linked to any entity in the system (across all entity types and entity IDs). Used by the document picker to implement system-wide "hide already-linked documents" filtering.

Auth required: Yes (session cookie)

Query parameters: None

Response (200 OK):

{
  "paperlessDocumentIds": [42, 99, 137]
}

Returns an empty array [] when no documents are linked anywhere.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session

PATCH /api/document-links/:id

Tag, retag, or untag (clear) a document link's attachment type. This is used to categorize invoice source documents by their role (quotation, deposit, or invoice) in the cost claim report.

Non-invoice entity types ignore the provided value and always store null (attachment typing is only meaningful for invoices). Story #1877.

Auth required: Yes

URL parameters:

Parameter Type Description
id string Document link ID

Request body:

{
  "attachmentType": "quotation"
}
Field Type Validation
attachmentType string | null Required. One of: quotation, deposit, invoice, or null to remove the tag.

Response (200 OK):

{
  "documentLink": {
    "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "entityType": "invoice",
    "entityId": "550e8400-e29b-41d4-a716-446655440000",
    "paperlessDocumentId": 42,
    "attachmentType": "quotation",
    "createdBy": {
      "id": "user-uuid",
      "displayName": "Admin User"
    },
    "createdAt": "2026-03-01T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid attachmentType value
401 UNAUTHORIZED No valid session
404 NOT_FOUND Document link not found

DELETE /api/document-links/:id

Remove a link between a Cornerstone entity and a Paperless-ngx document. The document itself stays in Paperless-ngx. The Documents panel's Unlink applies at once and offers Undo (#2233).

Auth required: Yes

URL parameters:

Parameter Type Description
id string Document link ID

Response (200 OK): DeleteDocumentLinkResponse (shared/src/types/document.ts) — 204 No Content before #2233.

{ "undo": { "token": "u_0123456789abcdef0123456789abcdef", "expiresAt": "2026-01-01T10:00:30.000Z" } }

POST /api/undo/:token re-inserts the link with its original id, type and creation time (EPIC-21 contract 11 › Document link snapshots). Removing a link because its record was deleted issues no token.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Document link not found

EPIC-04: Household Items & Furniture Management

Household items and furniture management endpoints. Household items are a distinct entity from work items (see ADR-016), with their own purchase status workflow, category system, delivery tracking, and budget integration. All endpoints require authentication. Both admin and member roles can perform all household item operations (no role restriction).

Household Item Response Shapes

/**
 * Household item category is a string ID referencing the household_item_categories table.
 * No longer a fixed enum — categories are user-manageable via CRUD endpoints.
 * Default seeded values: 'hic-furniture', 'hic-appliances', 'hic-fixtures',
 * 'hic-decor', 'hic-electronics', 'hic-outdoor', 'hic-storage', 'hic-other'.
 */
type HouseholdItemCategory = string;

/** Household item category entity (from household_item_categories table). */
interface HouseholdItemCategoryEntity {
  id: string;
  name: string;
  color: string | null;
  sortOrder: number;
  createdAt: string; // ISO 8601
  updatedAt: string; // ISO 8601
}

type HouseholdItemStatus = 'planned' | 'purchased' | 'scheduled' | 'arrived';

/** Returned in list responses (GET /api/household-items). */
interface HouseholdItemSummary {
  id: string;
  name: string;
  description: string | null;
  category: HouseholdItemCategory;
  status: HouseholdItemStatus;
  vendor: VendorSummary | null;
  url: string | null;
  area: AreaSummary | null;
  quantity: number;
  orderDate: string | null; // YYYY-MM-DD
  targetDeliveryDate: string | null; // YYYY-MM-DD (computed by scheduling engine)
  isLate: boolean; // isPurchaseLate: not arrived, no actual delivery, (latestDeliveryDate ?? earliestDeliveryDate) < today
  actualDeliveryDate: string | null; // YYYY-MM-DD
  earliestDeliveryDate: string | null; // YYYY-MM-DD (user-editable constraint)
  latestDeliveryDate: string | null; // YYYY-MM-DD (user-editable constraint)
  createdBy: UserSummary | null;
  createdAt: string; // ISO 8601
  updatedAt: string; // ISO 8601
  /** Number of budget lines. */
  budgetLineCount: number;
  /** Sum of the VAT-effective plannedAmount across all budget lines (net lines grossed up by 1 + VAT_RATE, rounded to cents per line). */
  totalPlannedAmount: number;
  /** Budget aggregates (planned, actual, subsidy, net). `totalPlanned` equals `totalPlannedAmount` (VAT-effective). `subsidyReduction` is the item's share of the **counted** (approved / received) grants in the expected scenario, capped and pro rata, from the money service (ADR-039, #2228; equal to `expectedTotalPayback` of `GET /api/household-items/:id/subsidy-payback`); `netCost = totalPlanned - subsidyReduction`. */
  budgetSummary: HouseholdItemBudgetAggregate;
}

/** Returned by POST, PATCH and the dependency routes (GET /api/household-items/:id returns the superset HouseholdItemPageDetail). */
interface HouseholdItemDetail extends HouseholdItemSummary {
  /** Dependencies on work items and milestones. */
  dependencies: HouseholdItemDepDetail[];
  /** Linked subsidy program summaries. */
  subsidies: SubsidyProgramSummary[];
}

/** The company of a purchase page, with the phone for the strip's Call button (#2249). */
interface HouseholdItemPageVendor extends HouseholdItemVendorSummary {
  phone: string | null;
}

/** The category entry of a purchase page (name plus translation key, for a translated label). */
interface HouseholdItemPageCategory {
  id: string;
  name: string;
  translationKey: string | null;
}

/** First unfinished predecessor (latest end date first, nulls last, then title, then id) and how many more (#2249). */
interface PurchaseWaitsFor {
  type: 'work_item' | 'milestone';
  id: string;
  title: string;
  more: number; // unfinished predecessors beyond this one
}

/** GET /api/household-items/:id only (#2249). */
interface HouseholdItemPageDetail extends Omit<HouseholdItemDetail, 'vendor'> {
  vendor: HouseholdItemPageVendor | null;
  categoryEntry: HouseholdItemPageCategory;
  budgets: HouseholdItemBudgetLine[]; // as GET /api/household-items/:householdItemId/budgets
  expectedCost: number | null; // as HouseholdItemListRow.expectedCost; null without cost lines
  grants: ItemGrantChip[]; // as HouseholdItemListRow.grants (rejected excluded)
  waitsFor: PurchaseWaitsFor | null; // null when every predecessor is finished, or there are none
}

expectedCost comes from the same helper as the Purchases list rows (loadPurchaseMoney in householdItemService, used by listHouseholdItemRows and getHouseholdItemPageDetail), so the purchase page and the Purchases list never disagree. The strip's Invoiced figure is the existing budgetSummary.totalActual (the list's Invoiced column). Unfinished in waitsFor means a work item whose status is not completed, or a milestone that is not completed. Every other response that returns a household item detail (POST, PATCH, the dependency routes) keeps HouseholdItemDetail.

/** A single dependency row with predecessor details. */
interface HouseholdItemDepDetail {
  householdItemId: string;
  predecessorType: 'work_item' | 'milestone';
  predecessorId: string;
  dependencyType: 'finish_to_start' | 'start_to_start' | 'finish_to_finish' | 'start_to_finish';
  leadLagDays: number;
  predecessor: {
    id: string;
    title: string; // work item title or milestone title
    status: string | null; // work item status (null for milestones)
    endDate: string | null; // work item endDate or milestone targetDate
    area: AreaSummary | null; // non-null only when predecessorType === 'work_item'; null for milestone predecessors
  };
}

/** Lightweight subsidy reference embedded in detail response. */
interface SubsidyProgramSummary {
  id: string;
  name: string;
  reductionType: 'percentage' | 'fixed';
  reductionValue: number;
  applicationStatus: SubsidyApplicationStatus;
}

Household Item Endpoints

GET /api/household-items

Returns a paginated, filterable, sortable list of household items (purchases). The Purchases list (/project/household-items, ObjectList template, #2246) reads it: it pages with pageSize=100 (infinite scroll) and never puts page/pageSize in its own URL.

Auth required: Yes

Query parameters:

Parameter Type Default Description
page number 1 Page number (1-indexed)
pageSize number 25 Items per page (max 100)
category string (all) Filter by category ID (e.g., hic-furniture, hic-appliances). Must be a valid household_item_categories.id.
status string (all) Filter by one status (planned, purchased, scheduled, arrived) — the list's status chip
vendorId string (all) Filter by vendor UUID
areaId string (all) Filter by area UUID or comma-separated list of area UUIDs. For each supplied ID the server expands descendants server-side; results are the union of all matching subtrees. Use the sentinel value __none__ to include items with no area assignment (e.g., ?areaId=__none__ returns only unassigned items; ?areaId=__none__,<uuid> returns unassigned items OR items in the specified area subtree).
q string (none) Full-text search across name, description
targetDeliveryFrom date (none) YYYY-MM-DD, inclusive lower bound on targetDeliveryDate (#2246). A purchase without the date never matches a set bound
targetDeliveryTo date (none) YYYY-MM-DD, inclusive upper bound on targetDeliveryDate (#2246)
actualDeliveryFrom date (none) YYYY-MM-DD, inclusive lower bound on actualDeliveryDate (#2246)
actualDeliveryTo date (none) YYYY-MM-DD, inclusive upper bound on actualDeliveryDate (#2246)
expectedCostMin number (none) Minimum row expectedCost (inclusive; no cost lines counts as 0) (#2246)
expectedCostMax number (none) Maximum row expectedCost (inclusive) (#2246)
grantsMin number (none) Minimum grant reduction (budgetSummary.subsidyReduction, inclusive) (#2246)
grantsMax number (none) Maximum grant reduction (inclusive) (#2246)
afterGrantsMin number (none) Minimum row afterGrants (inclusive; null counts as 0) (#2246)
afterGrantsMax number (none) Maximum row afterGrants (inclusive) (#2246)
plannedCostMin number (none) Filter: minimum total VAT-effective planned cost (inclusive; same basis as totalPlannedAmount). Kept for API clients; the list sends expectedCostMin
plannedCostMax number (none) Filter: maximum total VAT-effective planned cost (inclusive)
actualCostMin number (none) Filter: minimum invoiced amount (inclusive) — the list's Invoiced range
actualCostMax number (none) Filter: maximum invoiced amount (inclusive)
budgetLinesMin number (none) Filter: minimum cost-line count (inclusive)
budgetLinesMax number (none) Filter: maximum cost-line count (inclusive)
group string (none) Only status (#2246): rows ordered by status rank (planned, purchased, scheduled, arrived) first, then by the sort; the response carries groupCounts
sortBy string created_at Sort field (HOUSEHOLD_ITEM_LIST_SORTS): name, category, status (status rank, not alphabetical), order_date, target_delivery_date, actual_delivery_date, delivery (actualDeliveryDate ?? targetDeliveryDate), expected_cost, invoiced, created_at, updated_at. The Purchases list sends name by default
sortOrder string desc Sort direction: asc or desc

Ordering (#2246). The whole matching set is filtered, sorted and grouped in memory (one money-service run per request), then paged, so expected_cost/invoiced sorts, the engine-valued ranges, groupCounts and totals cover every row, not just the page. Null dates and amounts sort last in both directions; ties break by name, then id.

Grant chips (#2242): every row carries grants (ItemGrantChip[]: { id, name, reductionType, rate }): the grants linked to the purchase, rejected grants excluded, ordered by name then id, [] when none. One batched query per page. The field is optional in the shared type (HouseholdItemSummary.grants?): it is always sent on GET list rows (HouseholdItemListRow.grants, required) and absent from every other response that returns a household item summary.

Response (200 OK, HouseholdItemListRowsResponse):

{
  "items": [
    {
      "id": "hi-001",
      "name": "Kitchen Island",
      "description": "Custom maple kitchen island with granite top",
      "category": "hic-furniture",
      "status": "purchased",
      "vendor": {
        "id": "v-ikea",
        "name": "IKEA",
        "trade": null
      },
      "url": "https://example.com/kitchen-island",
      "area": { "id": "area-kitchen", "name": "Kitchen", "color": "#F59E0B", "ancestors": [] },
      "quantity": 1,
      "orderDate": "2026-03-01",
      "targetDeliveryDate": "2026-04-15",
      "isLate": false,
      "actualDeliveryDate": null,
      "earliestDeliveryDate": "2026-04-10",
      "latestDeliveryDate": "2026-04-20",
      "createdBy": {
        "id": "u1",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-28T10:00:00.000Z",
      "updatedAt": "2026-03-01T14:30:00.000Z",
      "budgetLineCount": 2,
      "totalPlannedAmount": 3500.0,
      "budgetSummary": {
        "totalPlanned": 3500.0,
        "totalActual": 0,
        "subsidyReduction": 0,
        "netCost": 3500.0
      },
      "grants": [],
      "expectedCost": 3500.0,
      "afterGrants": 3500.0,
      "paid": 0
    }
  ],
  "pagination": {
    "page": 1,
    "pageSize": 25,
    "totalItems": 12,
    "totalPages": 1
  },
  "groupCounts": { "planned": 5, "purchased": 4, "scheduled": 1, "arrived": 2 },
  "scopeTotal": 12,
  "totals": { "expectedCost": 21400.0, "invoiced": 9800.0, "paid": 7500.0 }
}

List-row and list-level fields (#2246):

Field Type Meaning
items[].expectedCost number | null The money engine's expected cost of the purchase (getHouseholdItemListMoney, the same figure as cost.expected / Money › Overview); null when the purchase has no cost lines (the list shows Not set). Replaces totalPlannedAmount on the list, which is planned-only and differs from the engine once a line is invoiced
items[].afterGrants number | null expectedCost − budgetSummary.subsidyReduction, in cents; null when expectedCost is null
items[].paid number Paid on the purchase under the ADR-039 per-line attribution (attributePayments); 0 when none
groupCounts Record<HouseholdItemStatus, number> Only with group=status: rows matching the whole query per status; all four keys, zeros included
scopeTotal number Purchases matching only status (the chip), ignoring search and filters: the heading's "of m"
totals { expectedCost, invoiced, paid } Sums over every row matching the query (not the page), in cents then major units. invoiced = Σ budgetSummary.totalActual; a null expectedCost adds 0. These are partial sums over purchases, not project figures, so the list renders them without data-figure markers

filterMeta is no longer sent (#2246): the list's range filters are free min/max inputs. The HouseholdItemListResponse type remains for the legacy shape only.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid query parameters (unknown sortBy/group, malformed date)
401 UNAUTHORIZED No valid session

Notes:

  • Planned cost is VAT-effective everywhere on this endpoint: totalPlannedAmount, budgetSummary.totalPlanned and the plannedCostMin/plannedCostMax filters all sum each budget line's planned_amount with net lines (includes_vat = 0) grossed up by 1 + VAT_RATE (server config vatRate, default 0.19, exposed by GET /api/config) and rounded to cents per line. This is the same figure the household-item detail page and GET /api/budget/breakdown show.
  • Unknown query parameters are stripped by AJV (removeAdditional), not rejected, so a legacy client sending a parameter this endpoint no longer reads gets an unfiltered answer for it rather than a 400.
  • Consistency (server tests, figures.consistency.test.ts): the unfiltered totals.expectedCost equals the engine's byType.household_items expected cost; the unfiltered totals.paid equals the sum of Paid over Money › Overview's purchase rows; each total equals the sum of its rows.

POST /api/household-items

Creates a new household item. The createdBy is automatically set to the authenticated user.

Auth required: Yes

Request body:

{
  "name": "Kitchen Island",
  "description": "Custom maple kitchen island with granite top",
  "category": "hic-furniture",
  "status": "planned",
  "vendorId": "v-ikea",
  "url": "https://example.com/kitchen-island",
  "areaId": "area-kitchen",
  "quantity": 1,
  "orderDate": null,
  "earliestDeliveryDate": "2026-04-01",
  "latestDeliveryDate": "2026-04-30",
  "actualDeliveryDate": null
}
Field Type Required Validation
name string Yes 1-500 characters
description string | null No Max 5000 characters
category string No A valid household_item_categories.id. Default: hic-other
status string No One of: planned, purchased, scheduled, arrived. Default: planned
vendorId string | null No Must be a valid vendor UUID if provided
url string | null No Max 2000 characters
areaId string | null No Must be a valid area UUID if provided
quantity number No Integer >= 1. Default: 1
orderDate string | null No YYYY-MM-DD format
earliestDeliveryDate string | null No YYYY-MM-DD format; user-editable constraint (minimum acceptable date)
latestDeliveryDate string | null No YYYY-MM-DD format; user-editable constraint (hard deadline)
actualDeliveryDate string | null No YYYY-MM-DD format

Response (201 Created):

{
  "householdItem": {
    "id": "hi-001",
    "name": "Kitchen Island",
    "...": "full HouseholdItemDetail shape"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid or missing fields
401 UNAUTHORIZED No valid session
400 VALIDATION_ERROR Non-existent vendorId or areaId

GET /api/household-items/:id

Returns a single household item as HouseholdItemPageDetail (#2249): the full HouseholdItemDetail (dependencies, linked subsidies, budget aggregates) plus the purchase page's vendor.phone, categoryEntry, budgets, expectedCost, grants and waitsFor (see the response shapes above).

Auth required: Yes

URL parameters:

Parameter Type Description
id string Household item UUID

Response (200 OK):

{
  "householdItem": {
    "id": "hi-001",
    "name": "Kitchen Island",
    "description": "Custom maple kitchen island with granite top",
    "category": "hic-furniture",
    "status": "purchased",
    "vendor": { "id": "v-ikea", "name": "IKEA", "trade": null },
    "url": "https://example.com/kitchen-island",
    "area": { "id": "area-kitchen", "name": "Kitchen", "color": "#F59E0B", "ancestors": [] },
    "quantity": 1,
    "orderDate": "2026-03-01",
    "targetDeliveryDate": "2026-04-15",
    "isLate": false,
    "actualDeliveryDate": null,
    "earliestDeliveryDate": "2026-04-10",
    "latestDeliveryDate": "2026-04-20",
    "createdBy": { "id": "u1", "displayName": "Admin User", "email": "admin@example.com" },
    "createdAt": "2026-02-28T10:00:00.000Z",
    "updatedAt": "2026-03-01T14:30:00.000Z",
    "budgetLineCount": 2,
    "totalPlannedAmount": 3500.0,
    "subsidies": [
      {
        "id": "sp-001",
        "name": "Energy Efficiency Grant",
        "reductionType": "percentage",
        "reductionValue": 15,
        "applicationStatus": "approved"
      }
    ],
    "categoryEntry": { "id": "hic-furniture", "name": "Furniture", "translationKey": "householdItemCategories.furniture" },
    "budgets": ["...HouseholdItemBudgetLine"],
    "expectedCost": 3500.0,
    "grants": [{ "id": "sp-001", "name": "Energy Efficiency Grant", "reductionType": "percentage", "rate": 15 }],
    "waitsFor": { "type": "work_item", "id": "wi-12", "title": "Kitchen walls plastered", "more": 0 }
  }
}

vendor carries phone here (null when the company has none).

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item ID not found

PATCH /api/household-items/:id

Updates an existing household item. All fields are optional; only provided fields are updated.

Auth required: Yes

URL parameters:

Parameter Type Description
id string Household item UUID

Request body:

{
  "name": "Updated Kitchen Island",
  "status": "scheduled",
  "areaId": "area-kitchen"
}
Field Type Required Validation
name string No 1-500 characters
description string | null No Max 5000 characters
category string No A valid household_item_categories.id
status string No One of the valid statuses
vendorId string | null No Must be a valid vendor UUID if provided; null clears the vendor
url string | null No Max 2000 characters
areaId string | null No Valid area UUID or null to clear
quantity number No Integer >= 1
orderDate string | null No YYYY-MM-DD format
earliestDeliveryDate string | null No YYYY-MM-DD format; user-editable constraint
latestDeliveryDate string | null No YYYY-MM-DD format; user-editable constraint
actualDeliveryDate string | null No YYYY-MM-DD format. Auto-set to today when status changes to "arrived" if not already set.

Response (200 OK):

When the call changed status or actualDeliveryDate, the body also carries a top-level undo sibling of householdItem (contract 11). Backward status moves keep actualDeliveryDate; a date later than today is accepted.

{
  "householdItem": {
    "...": "full HouseholdItemDetail shape"
  }
}

Notes:

  • When status is changed to "arrived" and actualDeliveryDate is not provided in the request and is currently null, the server auto-populates actualDeliveryDate with today's date (YYYY-MM-DD).
  • Explicit actualDeliveryDate values in the request body are always respected (auto-set does not override).
  • Mark ordered (#2249): every purchase StatusMenu sends { "status": "purchased", "orderDate": "YYYY-MM-DD" } in one call (the date step's answer). orderDate is not a status column, so it does not by itself issue an Undo token, but it is a tracked column: the Undo of that call restores the previous orderDate too.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid fields or no fields provided
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item not found; non-existent vendorId or areaId

DELETE /api/household-items/:id

Deletes a household item and all associated data (notes, budget lines, dependency links, subsidy links). Also cleans up any document_links entries for this household item.

Auth required: Yes

URL parameters:

Parameter Type Description
id string Household item UUID

Response (204 No Content)

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item ID not found

Notes:

  • Cascade deletes handle household_item_notes, household_item_budgets, household_item_deps, and household_item_subsidies via FK ON DELETE CASCADE.
  • document_links entries must be cleaned up in application code (no FK constraint on polymorphic table).

Household Item Note Endpoints

Notes are nested under household items. Each note represents a comment or observation about the item. Notes are NOT paginated (a household item typically has fewer than ~50 notes).

GET /api/household-items/:householdItemId/notes

Returns all notes for a household item, ordered by creation date (newest first).

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID

Response (200 OK):

{
  "notes": [
    {
      "id": "n1",
      "householdItemId": "hi-001",
      "content": "Confirmed color matches kitchen theme",
      "createdBy": {
        "id": "u1",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-03-01T10:00:00.000Z",
      "updatedAt": "2026-03-01T10:00:00.000Z"
    }
  ]
}

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item ID not found

POST /api/household-items/:householdItemId/notes

Creates a new note on a household item. The createdBy is automatically set to the authenticated user.

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID

Request body:

{
  "content": "Confirmed color matches kitchen theme"
}
Field Type Required Validation
content string Yes 1-10000 characters

Response (201 Created):

{
  "note": {
    "id": "n1",
    "householdItemId": "hi-001",
    "content": "Confirmed color matches kitchen theme",
    "createdBy": { "id": "u1", "displayName": "Admin User", "email": "admin@example.com" },
    "createdAt": "2026-03-01T10:00:00.000Z",
    "updatedAt": "2026-03-01T10:00:00.000Z"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid or missing content
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item ID not found

PATCH /api/household-items/:householdItemId/notes/:noteId

Updates an existing note. Only the note author or an admin can update a note.

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID
noteId string Note UUID

Request body:

{
  "content": "Updated note content"
}
Field Type Required Validation
content string Yes 1-10000 characters

Response (200 OK):

{
  "note": {
    "...": "full note shape"
  }
}

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid content
401 UNAUTHORIZED No valid session
403 FORBIDDEN User is not the author and not an admin
404 NOT_FOUND Household item or note ID not found

DELETE /api/household-items/:householdItemId/notes/:noteId

Deletes a note. Only the note author or an admin can delete a note.

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID
noteId string Note UUID

Response (204 No Content)

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
403 FORBIDDEN User is not the author and not an admin
404 NOT_FOUND Household item or note ID not found

Household Item Budget Line Endpoints

Budget lines are nested under household items. Each budget line represents a cost estimate or allocation with its own confidence level, optional vendor, budget category, and budget source. Budget lines are NOT paginated. The structure mirrors work item budget lines exactly. As of EPIC-15, household item budget lines can be linked to invoices via the invoice_budget_lines junction table (same model as work item budget lines).

GET /api/household-items/:householdItemId/budgets

Returns all budget lines for a household item.

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID

Response (200 OK):

{
  "budgets": [
    {
      "id": "hb-001",
      "householdItemId": "hi-001",
      "description": "Kitchen island base unit",
      "plannedAmount": 2500.0,
      "confidence": "quote",
      "confidenceMargin": 0.05,
      "budgetCategory": {
        "id": "bc-furniture",
        "name": "Furniture",
        "description": "Furniture and fixtures",
        "color": "#8B5CF6",
        "sortOrder": 3,
        "createdAt": "2026-02-20T10:00:00.000Z",
        "updatedAt": "2026-02-20T10:00:00.000Z"
      },
      "budgetSource": {
        "id": "bs-1",
        "name": "Savings Account",
        "sourceType": "savings"
      },
      "vendor": {
        "id": "v-ikea",
        "name": "IKEA",
        "trade": null
      },
      "actualCost": 0,
      "actualCostPaid": 0,
      "invoiceCount": 0,
      "createdBy": {
        "id": "u1",
        "displayName": "Admin User",
        "email": "admin@example.com"
      },
      "createdAt": "2026-02-28T10:00:00.000Z",
      "updatedAt": "2026-02-28T10:00:00.000Z"
    }
  ]
}

Notes:

  • As of EPIC-15, actualCost, actualCostPaid, and invoiceCount are computed from invoice_budget_lines for household item budget lines, using the same logic as work item budget lines. The invoiceLink field is also populated when a link exists.
  • confidenceMargin is a computed field: own_estimate = 0.20, professional_estimate = 0.10, quote = 0.05, invoice = 0.

Error responses:

HTTP Status Error Code When
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item ID not found

POST /api/household-items/:householdItemId/budgets

Creates a new budget line for a household item. The createdBy is automatically set to the authenticated user.

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID

Request body:

{
  "description": "Kitchen island base unit",
  "plannedAmount": 2500.0,
  "confidence": "quote",
  "budgetCategoryId": "bc-furniture",
  "budgetSourceId": "bs-1",
  "vendorId": "v-ikea"
}
Field Type Required Validation
description string | null No Max 500 characters
plannedAmount number Yes Must be >= 0
confidence string No One of: own_estimate, professional_estimate, quote, invoice. Default: own_estimate
budgetCategoryId string | null No Ignored -- automatically set to bc-household-items
budgetSourceId string Yes Must be a valid budget source UUID
vendorId string | null No Must be a valid vendor UUID if provided
quantity number | null No >= 0
unit string | null No Max 100 characters
unitPrice number | null No >= 0
includesVat boolean No Default true (plannedAmount is gross); false = net, VAT added at VAT_RATE

Response (201 Created):

{
  "budget": {
    "...": "full budget line shape"
  }
}

Notes:

  • budgetCategoryId is automatically assigned to bc-household-items (seeded by migration 0016). Any user-supplied value is silently ignored. This ensures all household item budget lines are grouped under the "Household Items" budget category for reporting.

Error responses:

HTTP Status Error Code When
400 VALIDATION_ERROR Invalid or missing fields
401 UNAUTHORIZED No valid session
404 NOT_FOUND Household item not found (a non-existent budget source or vendor is 400 VALIDATION_ERROR)

PATCH /api/household-items/:householdItemId/budgets/:budgetId

Updates an existing budget line. All fields are optional; only provided fields are updated. The updatedAt timestamp is set automatically. This endpoint supports same-table moves only (move within household items) — to move a budget line to a work item, use PATCH /api/invoices/:invoiceId/budget-lines/:id instead.

Auth required: Yes

Path parameters:

Parameter Type Description
householdItemId string Household item UUID
budgetId string Budget line UUID

Request body (all fields optional, at least one required):

{
  "plannedAmount": 2800.0,
  "confidence": "invoice"
}
Field Type Validation
description string | null Max 500 characters; null clears the description
plannedAmount number Must be >= 0
confidence string One of: own_estimate, professional_estimate, quote, invoice
budgetCategoryId string | null Valid budget category UUID or null to clear
budgetSourceId string | null Valid budget source UUID or null to clear
vendorId string | null Valid vendor UUID or null to clear

Response (200 OK):

{
  "budget": {
    "...": "full budget line shape"
  }
}

Error responses:

HTTP Status Error Code When

Clone this wiki locally