Skip to content

Save movements via the app API on lsze test - #922

Merged
rzueger merged 5 commits into
developfrom
claude/mcp-api-feasibility-93xd5a
Nov 19, 2026
Merged

rzueger merged 5 commits into
developfrom
claude/mcp-api-feasibility-93xd5a

Conversation

@rzueger

@rzueger rzueger commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

The first small step towards an API layer between the clients and the
Realtime Database. With the new per-project flag movementWritesViaApi,
the web app creates and updates departures and arrivals through
POST/PATCH /api/app/movements/... instead of writing to the RTDB
directly.

The flag is on only in lsze test and in the Cypress API run.
Production and all other aerodromes behave as before.

Commits

  1. Add error envelope and request IDs for new API routes:
    functions/api/http/:
    • ApiError with {error:{code,message,details}};
    • X-Request-Id;
    • an async route wrapper that logs internal errors without leaking
      them;
    • appAuth, which verifies the ID token with checkRevoked.
      Rejected tokens get 401, infrastructure faults get 500.
  2. Add movement domain core with validation and access rules:
    pure code in functions/movements/domain that mirrors the movement
    write rules: field validation, ownership, movement lock, merge
    semantics. negativeTimestamp is always derived from dateTime,
    which closes the bypass of the lock with a backdated date. The
    functions config loader now uses the generated config when any
    feature flag is set.
  3. Add app API endpoint for saving movements: hexagonal (domain /
    application / adapters / http). Writes go through the RTDB REST API
    with auth_variable_override set to the caller, so the existing
    database rules still check every write. If the domain accepts a
    write and the rules reject it, the mismatch is logged as an error
    and the caller gets a 500. Body limits: 64 KB, 100 fields, 8 levels
    deep.
  4. Save movements via the API on lsze test: remote.saveMovement
    delegates to remoteApi.ts when the flag is on.
    • Double saves are ignored while one is running (takeLeading).
    • The checkout redirect waits for a pending payment-method save.
    • Customs form link saves go the same way.
  5. Run the movement Cypress specs with writes via the API:
    • CI runs the movement specs a second time with the flag on
      (npm run cy:test:api), against the same cypress-testing
      project.
    • projects/cypress-testing-api.json extends cypress-testing and
      only switches the flag on; projects.load() now supports
      extends.
    • deploy:cypress builds the functions config from it, so the
      cypress-testing functions serve the routes.
    • New specs in cypress/integration/movements_api, run only with
      the flag:
      • create and edit go through the API;
      • a validation error from the API is shown and nothing is stored;
      • a double click saves once.

Before CI can pass

The Cypress API run needs the new routes in the cypress-testing
functions. They are deployed manually from this branch:
cd functions && npm run deploy:cypress.

Unchanged

  • RTDB rules (firebase-rules-template.json,
    tasks/processFirebaseRules.js). Direct client writes are still
    allowed everywhere, including lsze test.
  • Reads and realtime listeners, deleting movements, and card payments
    still use the RTDB directly.

Test plan

  • Every commit passes npm test, npm run typecheck and
    npm run test:functions on its own. At the last commit:
    352 / 7756 root tests and 107 / 4526 functions tests.
  • npm run test:rules in the database emulator:
    • The domain and the built rules agree on every case for lsze (590),
      lszt (572), lspv (572) and lsze with lockOnDateTime (590).
    • The API writes as the caller: the rules see the uid and
      auth.token.email.
  • End to end against the database and Auth emulators. The real
    sagas (not the forms) ran with the lsze test __CONF__ against
    the real API app:
    • create, edit and payment method;
    • guest and kiosk;
    • other pilot 403, admin allowed;
    • invalid input 400, revoked token 401, locked 409;
    • no direct database writes.
  • The flag-off configs (all other projects and environments) return
    404 on the new routes and keep writing to the RTDB directly.
  • npm run build --project=cypress-testing-api builds the app with
    the flag compiled in.
  • Cypress, both runs, in CI after deploy:cypress. Not run locally:
    the Cypress binary could not be downloaded in the dev environment.
  • First real write on lsze-test after deploy: one create, one edit
    by another pilot (should fail), and check the logs.
  • Checkout flow in a browser on lsze-test (not covered by Cypress).

Known issues / notes

  • Already broken today: computeArrivalFees writes
    aircraftDataSource onto arrivals, which the rules (and so the
    domain) don't allow. Arrivals with computed fees therefore can't be
    edited, either directly or through the API. A separate fix is
    needed.
  • Deploy order: in firebase-hosting-dev.yml, hosting and
    functions deploy in parallel. If the bundle goes live before the
    functions, lsze-test saves get 404 until the functions are up.
  • Behaviour on lsze test (documented in projects/Configuration.md):
    • Saving offline fails immediately instead of being queued.
    • Creates are not idempotent: a lost response followed by a retry
      can create a duplicate.
    • The client fetch has no timeout.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EsQwcjvDCVeFpizmLUnv9B

New app API routes (movement writes via the API) need one error
format, traceable requests and token auth that yields an actor.
functions/api/http/ adds an ApiError with the {error:{code,message,
details}} envelope and its status mapping, an X-Request-Id middleware,
an async route wrapper that logs internal errors without leaking them,
and appAuth, which verifies the ID token (checkRevoked) and answers
401 for rejected tokens but 500 for infrastructure faults such as
unreachable signing keys. Existing routes are not wired to it.

The new specs run in the node environment, so the root jest setup
only sets the language in localStorage where localStorage exists.
Pure CommonJS core in functions/movements/domain that mirrors the
movement write rules of the Realtime Database: a tenant policy built
from the generated functions config, validateMovement (fields,
formats, enums, conditional rules), checkWriteAccess (ownership and
movement lock) and prepareWrite (update merge, server-derived
negativeTimestamp, createdBy and order key). negativeTimestamp is
always derived from dateTime, closing the backdated-lock bypass.

The generator now emits the fields the policy needs and the new
movementWritesViaApi flag (set in no project yet). It refuses
per-environment values the rules cannot see. Own "__proto__" keys
and Object.prototype field names are handled as data. A parity
suite runs the shared cases against the built rules in the emulator.

The functions config loader now uses the generated config when any
feature flag is set, not only reportApiEnabled.
POST /app/movements/:type creates and PATCH /app/movements/:type/:key
updates a departure or arrival (also under /api), registered only when
the functions config has movementWritesViaApi. The route authenticates
the app user, loads the movement and write settings, runs the domain
core (prepareWrite, validateMovement, checkWriteAccess) and writes via
the RTDB REST API with auth_variable_override set to the caller, so
the database rules still apply as a second line of defence. A rules
rejection after the domain accepted is logged and answered as 500.

Only JSON object bodies up to 64 KB, 100 fields and 8 levels deep are
accepted, also behind the Cloud Functions body parsers, so malformed
input gets a 400 instead of a 500 or an oversized error list.

The client does not use the endpoint yet.
With the new movementWritesViaApi flag, remote.saveMovement sends
creates (POST) and updates incl. the payment method (PATCH) to the
app API with the user's ID token instead of writing to the Realtime
Database. Errors carry status, code, reason and validation errors
from the envelope, so the existing commit failure dialog shows them.
With the flag off the database path is unchanged.

Because a create through the API is slow and not idempotent, further
saves are ignored while one is running, a lost response is reported
as "unknown whether saved", and the checkout redirect waits for a
pending payment method write. A token refresh that fails offline is
reported as a network error, not as signed out.

The flag is switched on in lsze test only, to try the API there
before other aerodromes. Configuration.md documents the differences
and how to switch the flag on and off.
@rzueger
rzueger force-pushed the claude/mcp-api-feasibility-93xd5a branch from f375428 to 16fc2ca Compare November 19, 2026 17:28
@rzueger rzueger changed the title MCP aerodrome status and movement writes via the API (test flags) Save movements via the app API on lsze test Nov 19, 2026
The movement specs only covered direct database writes. CI now runs
them a second time with the app built with movementWritesViaApi
(npm run cy:test:api), against the same cypress-testing project.

projects/cypress-testing-api.json extends cypress-testing and only
switches the flag on; projects.load() now supports "extends" (top
level replaced, environments merged by name). deploy:cypress builds
the functions config from it, so the cypress-testing functions serve
the movement routes while the default run keeps writing directly.

New specs in cypress/integration/movements_api, run only with the
flag: create and edit go through the API, a validation error of the
API is shown and nothing is stored, and a double click saves once.
@rzueger
rzueger force-pushed the claude/mcp-api-feasibility-93xd5a branch from 1212cd9 to 32fc23a Compare November 19, 2026 20:34
@rzueger
rzueger merged commit 1d64001 into develop Nov 19, 2026
2 checks passed
@rzueger
rzueger deleted the claude/mcp-api-feasibility-93xd5a branch November 19, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants