Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions functions/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ if (projectConfig.reportApiEnabled === true) {
const {apiKeyOrAdminAuth} = require('./apiKeyAuth')
require('./reports').registerReportRoutes(api, projectConfig, apiKeyOrAdminAuth(SCOPES.REPORTS_AIRSTAT))
}
// Movement writes of the web app (movementWritesViaApi), for signed-in users.
if (projectConfig.movementWritesViaApi === true) {
// The movement API: the web app's movement writes (movementWritesViaApi),
// and API clients with tokens of the central login (centralLogin, e.g. the
// Open Digital MCP hub). centralLogin alone leaves the app writing to the
// database as before.
if (projectConfig.movementWritesViaApi === true || projectConfig.centralLogin) {
require('../movements').registerMovementRoutes(api, projectConfig)
}

Expand Down
13 changes: 12 additions & 1 deletion functions/api/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('functions', () => {
expect(fbAdminAuth).toHaveBeenCalledWith(req, {}, next);
});

it('has no movement write routes unless the tenant enables movementWritesViaApi', () => {
it('has no movement routes unless the tenant enables movementWritesViaApi or centralLogin', () => {
[
{ reportApiEnabled: false, movementWritesViaApi: false },
{ reportApiEnabled: true, aerodrome: { ICAO: 'LSZE', runways: [] } },
Expand Down Expand Up @@ -151,6 +151,17 @@ describe('functions', () => {
expect(movementLayers.map(layer => layer.route.stack.length)).toEqual([5, 5, 4, 4, 5]);
});

it('serves the movement API to central login clients when the tenant sets centralLogin', () => {
const centralLogin = { issuer: 'https://login.example.com', jwksUri: 'https://login.example.com/jwks', audience: 'flightbox-test' };
const { api } = loadApi({ reportApiEnabled: false, movementWritesViaApi: false, centralLogin });
const paths = routeLayers(api).map(layer => layer.route.path).flat();

expect(paths).toEqual(expect.arrayContaining([
'/api/app/movements/:type', '/api/app/movements/:type/:key', '/api/app/movements',
'/api/app/movements/arrivals/:key/payment',
]));
});

it('lets admins manage API keys when the tenant has an API feature', () => {
const { api, fbAdminAuth } = loadApi({ reportApiEnabled: true, aerodrome: { ICAO: 'LSZE', runways: [] } });

Expand Down
2 changes: 1 addition & 1 deletion functions/fees/fees.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('functions/fees: tariff data identical to the client', () => {
const path = require('path');
const clientData = name => require(path.join(__dirname, '../../src/util/landingFeeStrategies', `${name}_data.json`));

it.each(['lspl', 'lsze'])('%s_data.json', name => {
it.each(['lspl', 'lsze', 'lszm'])('%s_data.json', name => {
expect(require(`./landingFeeStrategies/${name}_data.json`)).toEqual(clientData(name));
});
});
2 changes: 2 additions & 0 deletions functions/fees/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// server-owned fees.
const lspl = require('./landingFeeStrategies/lspl');
const lsze = require('./landingFeeStrategies/lsze');
const lszm = require('./landingFeeStrategies/lszm');

const AircraftOrigin = Object.freeze({
CLUB: 'club',
Expand All @@ -17,6 +18,7 @@ const AircraftOrigin = Object.freeze({
const strategies = {
lspl,
lsze,
lszm,
};

const roundToFiveCents = (val) => Math.round(val * 20) / 20;
Expand Down
73 changes: 73 additions & 0 deletions functions/fees/landingFeeStrategies/lszm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

// Server-side port of src/util/landingFeeStrategies/lszm.ts (Mollis). The
// client keeps its copy for live display in the wizard; this is the
// authoritative version. Parity with the client over a grid of inputs:
// src/util/landingFeeStrategies/lszm.parity.spec.js.
const data = require('./lszm_data.json');
const { getMtowFee } = require('./utils');
const { isHelicopter } = require('../../aircraftCategories');
const { flightTypeAircraftType } = require('../../reports/airstat/tables');

// AircraftOrigin values (see functions/fees/index.js / src landingFees.ts).
const CLUB = 'club';
const HOME_BASE = 'homeBase';
const GLIDER_FLIGHT_TYPE = /^glider_(instruction|private)_(self|winch|aerotow)$/;

const isHomebaseOrigin = (aircraftOrigin) => aircraftOrigin === HOME_BASE || aircraftOrigin === CLUB;

const getFactorName = (isHomebase, isHeli, isInstruction) => {
if (!isHomebase) {
return 'non_homebase_plane_helicopter';
}
if (isHeli) {
return 'homebase_helicopter';
}
if (isInstruction) {
return 'homebase_plane_instruction';
}
return 'homebase_plane';
};

const gliderMatch = (flightType) => {
const match = GLIDER_FLIGHT_TYPE.exec(flightType);
if (!match) {
throw new Error('Unsupported flight type ' + flightType);
}
return { isInstruction: match[1] === 'instruction', method: match[2] };
};

const getGliderFee = (flightType, isHomebase) => {
const { isInstruction, method } = gliderMatch(flightType);
let factorName = `${isHomebase ? 'homebase' : 'non_homebase'}_${method}`;
if (isHomebase && isInstruction) {
factorName += '_instruction';
}
const fee = data.fees.glider[factorName];
return typeof fee === 'number' ? { fee } : undefined;
};

const getLandingFee = (mtow, flightType, aircraftOrigin, aircraftCategory) => {
const isHomebase = isHomebaseOrigin(aircraftOrigin);
if (flightTypeAircraftType(aircraftCategory) === 'glider') {
return getGliderFee(flightType, isHomebase);
}
const factorName = getFactorName(isHomebase, isHelicopter(aircraftCategory), flightType === 'instruction');
return { fee: getMtowFee(data.fees[factorName], mtow) };
};

const getGoAroundFee = () => undefined;

const getVat = (isHomebase, isHeli, isInstruction) => data.vat[getFactorName(isHomebase, isHeli, isInstruction)];

const getVatRate = (flightType, aircraftOrigin, aircraftCategory) => {
const isHomebase = isHomebaseOrigin(aircraftOrigin);
if (flightTypeAircraftType(aircraftCategory) === 'glider') {
const { isInstruction, method } = gliderMatch(flightType);
// Same factor as home base (non-instruction) for others with a winch.
return method === 'winch' && !isHomebase ? getVat(true, false, false) : getVat(isHomebase, false, isInstruction);
}
return getVat(isHomebase, isHelicopter(aircraftCategory), flightType === 'instruction');
};

module.exports = { getLandingFee, getGoAroundFee, getVatRate };
94 changes: 94 additions & 0 deletions functions/fees/landingFeeStrategies/lszm_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"vat": {
"non_homebase_plane_helicopter": 8.1,
"homebase_plane": 8.1,
"homebase_plane_instruction": 8.1,
"homebase_helicopter": 8.1
},
"fees": {
"glider": {
"non_homebase_winch": 20,
"non_homebase_self": 22.8,
"homebase_winch": 20,
"homebase_self": 20,
"homebase_winch_instruction": 19,
"homebase_self_instruction": 19
},
"non_homebase_plane_helicopter": [
{ "max_weight": 1000, "fee": 27.6 },
{ "max_weight": 1300, "fee": 33.6 },
{ "max_weight": 2000, "fee": 38.4 },
{ "max_weight": 3000, "fee": 57.6 },
{ "max_weight": 4000, "fee": 80.4 },
{ "max_weight": 5000, "fee": 105.6 },
{ "max_weight": 6000, "fee": 133.2 },
{ "max_weight": 7000, "fee": 163.2 },
{ "max_weight": 8000, "fee": 194.4 },
{ "max_weight": 9000, "fee": 228 },
{ "max_weight": 10000, "fee": 264 },
{ "max_weight": 12500, "fee": 360 },
{ "max_weight": 15000, "fee": 540 },
{ "max_weight": 20000, "fee": 720 },
{ "max_weight": 30000, "fee": 1080 },
{ "max_weight": 40000, "fee": 1440 },
{ "max_weight": 50000, "fee": 2160 }
],
"homebase_plane": [
{ "max_weight": 1000, "fee": 20.0 },
{ "max_weight": 1300, "fee": 28.0 },
{ "max_weight": 2000, "fee": 32.0 },
{ "max_weight": 3000, "fee": 48.0 },
{ "max_weight": 4000, "fee": 67.0 },
{ "max_weight": 5000, "fee": 88.0 },
{ "max_weight": 6000, "fee": 111.0 },
{ "max_weight": 7000, "fee": 136.0 },
{ "max_weight": 8000, "fee": 162.0 },
{ "max_weight": 9000, "fee": 190.0 },
{ "max_weight": 10000, "fee": 220.0 },
{ "max_weight": 12500, "fee": 300.0 },
{ "max_weight": 15000, "fee": 450.0 },
{ "max_weight": 20000, "fee": 600.0 },
{ "max_weight": 30000, "fee": 900.0 },
{ "max_weight": 40000, "fee": 1000.0 },
{ "max_weight": 50000, "fee": 1200.0 }
],
"homebase_plane_instruction": [
{ "max_weight": 1000, "fee": 19 },
{ "max_weight": 1300, "fee": 22.4 },
{ "max_weight": 2000, "fee": 25.6 },
{ "max_weight": 3000, "fee": 38.4 },
{ "max_weight": 4000, "fee": 53.6 },
{ "max_weight": 5000, "fee": 70.4 },
{ "max_weight": 6000, "fee": 88.8 },
{ "max_weight": 7000, "fee": 108.8 },
{ "max_weight": 8000, "fee": 129.6 },
{ "max_weight": 9000, "fee": 152 },
{ "max_weight": 10000, "fee": 176 },
{ "max_weight": 12500, "fee": 240 },
{ "max_weight": 15000, "fee": 360 },
{ "max_weight": 20000, "fee": 480 },
{ "max_weight": 30000, "fee": 720 },
{ "max_weight": 40000, "fee": 950 },
{ "max_weight": 50000, "fee": 1100 }
],
"homebase_helicopter": [
{ "max_weight": 1000, "fee": 19 },
{ "max_weight": 1300, "fee": 19 },
{ "max_weight": 2000, "fee": 19.2 },
{ "max_weight": 3000, "fee": 28.8 },
{ "max_weight": 4000, "fee": 40.2 },
{ "max_weight": 5000, "fee": 52.8 },
{ "max_weight": 6000, "fee": 66.6 },
{ "max_weight": 7000, "fee": 81.6 },
{ "max_weight": 8000, "fee": 97.2 },
{ "max_weight": 9000, "fee": 114 },
{ "max_weight": 10000, "fee": 132 },
{ "max_weight": 12500, "fee": 180 },
{ "max_weight": 15000, "fee": 270 },
{ "max_weight": 20000, "fee": 360 },
{ "max_weight": 30000, "fee": 540 },
{ "max_weight": 40000, "fee": 720 },
{ "max_weight": 50000, "fee": 1080 }
]
}
}
20 changes: 12 additions & 8 deletions projects/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ when the database holds the strategy under `/settings/landingFeesStrategy` (set
function `computeArrivalFeesOnWrite` then recomputes the fee fields of every arrival that is written, from the app or
the API, with MTOW and category from the aircraft register where the registration is known, and records where they came
from in `aircraftDataSource` (`registry` or `manual`). Only strategies ported to `functions/fees/landingFeeStrategies`
can be used this way: `lspl` and `lsze`. Arrivals stored before are priced at their next change.
can be used this way: `lspl`, `lsze` and `lszm` (each with a parity test against the app's tariff). Arrivals stored
before are priced at their next change.

### `environments`

Expand Down Expand Up @@ -222,8 +223,8 @@ always read from the top level.

`enabledFlightTypes`, `loginForm` and `lockOnDateTime` must be set at the top level: the database rules are
built from the top level only, and the functions apply the same movement rules, so the functions deploy fails
when an environment sets one of them. For the same reason, while `movementWritesViaApi` is on, an
environment-level `aerodrome` may not change the `ICAO`, the runway names or the departure and arrival route
when an environment sets one of them. For the same reason, while the movement API is on (`movementWritesViaApi` or
`centralLogin`), an environment-level `aerodrome` may not change the `ICAO`, the runway names or the departure and arrival route
names of the top-level one.

### `theme`
Expand Down Expand Up @@ -313,7 +314,8 @@ Differences to direct database writes:
allowed the write.
* The server sets `negativeTimestamp`, `createdBy` and `createdBy_orderKey`; the values of the app are ignored.

Switching it on or off: the routes only exist in functions deployed with the flag (`functions/api/index.js`),
Switching it on or off: the routes only exist in functions deployed with the flag or with `centralLogin`
(`functions/api/index.js`),
while the app has the flag built in, also in tabs and kiosks that stay open after a deployment. An app with the
flag and functions without it means every save fails with `Request failed (HTTP 404)` (also if the functions
reject the generated configuration, e.g. a `firebaseProjectId` mismatch). The test deployment
Expand All @@ -331,8 +333,9 @@ Example: `true`

### `centralLogin`

Lets the app API (the routes of `movementWritesViaApi`) also accept access tokens of the central login (the Open
Digital account, `odch/odch-login`), next to the Firebase ID tokens of the app. The Open Digital MCP hub
Offers the movement API (the routes of `movementWritesViaApi`) and lets it accept access tokens of the central
login (the Open Digital account, `odch/odch-login`), next to the Firebase ID tokens of the app. With `centralLogin`
alone the routes exist for API clients, while the app keeps writing to the database directly. The Open Digital MCP hub
(`odch/mcp-hub`) uses them to record departures and arrivals for a pilot from an AI client (Claude etc.): it
exchanges the pilot's token at the login (token exchange, RFC 8693) for a token for exactly this instance and
calls `POST/PATCH /api/app/movements/...` with it, and `GET /api/app/movements?from=&to=` to list movements.
Expand Down Expand Up @@ -380,8 +383,9 @@ the arrival paid as for the app.

Set it per environment. Only the functions read it (`functions/project-config.generated.json`, validated by
`tasks/generateServerConfig.js`); without it, tokens of the login are rejected like any other unknown token.
Set on `lsze` test, with the dev login (`odch-login-dev`) at its `*.run.app` address until
`login.opendigital.ch` exists.
Set on `lsze` and `lszm` test, with the dev login (`odch-login-dev`) at its `*.run.app` address until
`login.opendigital.ch` exists. For landing fees on arrivals recorded through the API, the instance also needs
`/settings/landingFeesStrategy` (see `landingFeesStrategy`).

### `customsSelfDeclarationEnabled`

Expand Down
7 changes: 6 additions & 1 deletion projects/lszm.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@
"firebaseProjectId": "lszm-test",
"firebaseDatabaseUrl": "https://lszm-test-eu.europe-west1.firebasedatabase.app",
"firebaseApiKey": "AIzaSyBP3IcalmLfA6gi9bq4Ln6aDyelL4gOLGg",
"passkeysEnabled": true
"passkeysEnabled": true,
"centralLogin": {
"issuer": "https://odch-login-422294830636.europe-west1.run.app",
"jwksUri": "https://odch-login-422294830636.europe-west1.run.app/jwks",
"audience": "flightbox-lszm-test"
}
},
"production": {
"customsSelfDeclarationEnabled": true,
Expand Down
40 changes: 40 additions & 0 deletions src/util/landingFeeStrategies/lszm.parity.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Parity: the server's port of the Mollis tariff (functions/fees, used by
// computeArrivalFeesOnWrite) prices every arrival like the app's wizard:
// fee and VAT rate, or the same error, for all weight band edges, flight
// types, aircraft origins and categories.
const client = require('./lszm').default;
const server = require('../../../functions/fees/landingFeeStrategies/lszm');
const data = require('./lszm_data.json');
const { categories } = require('../aircraftCategories');

const FLIGHT_TYPES = [
'private', 'instruction', 'aerotow', 'commercial', 'military',
'glider_private_aerotow', 'glider_private_winch', 'glider_private_self',
'glider_instruction_aerotow', 'glider_instruction_winch', 'glider_instruction_self',
];
const ORIGINS = ['club', 'homeBase', 'other'];
const edges = data.fees.non_homebase_plane_helicopter.map(entry => entry.max_weight);
const MTOWS = [0, 1, ...edges.flatMap(edge => [edge - 1, edge, edge + 1]), 60000];

const outcome = fn => {
try {
return { value: fn() };
} catch (e) {
return { error: e.message };
}
};

describe('lszm tariff: server and app agree', () => {
it.each(categories)('for a %s', category => {
for (const flightType of FLIGHT_TYPES) {
for (const origin of ORIGINS) {
expect(outcome(() => server.getVatRate(flightType, origin, category)))
.toEqual(outcome(() => client.getVatRate(flightType, origin, category)));
for (const mtow of MTOWS) {
expect(outcome(() => server.getLandingFee(mtow, flightType, origin, category)))
.toEqual(outcome(() => client.getLandingFee(mtow, flightType, origin, category)));
}
}
}
});
});
15 changes: 8 additions & 7 deletions tasks/generateServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,18 @@ const movementRuleView = aerodrome => JSON.stringify({
arrivalRoutes: aerodrome.arrivalRoutes.map(({ name }) => name).sort()
});

// The database rules are built from the top-level aerodrome only. With
// movement writes through the API, an environment-level aerodrome must not
// change what the movement rules read, or the API would accept runways or
// routes the rules reject (and the other way round).
// The database rules are built from the top-level aerodrome only. With the
// movement API (movementWritesViaApi or centralLogin), an environment-level
// aerodrome must not change what the movement rules read, or the API would
// accept runways or routes the rules reject (and the other way round).
function assertMovementAerodromeMatchesRules(project, env, conf, aerodrome) {
if (!hasOwn(conf.environments[env], 'aerodrome')) {
return;
}
const topLevel = buildAerodrome(project, env, conf.aerodrome);
if (movementRuleView(topLevel) !== movementRuleView(aerodrome)) {
throw configError(project, env, `environments.${env}.aerodrome`,
'must not change ICAO, runway or route names while movementWritesViaApi is on: '
'must not change ICAO, runway or route names while the movement API is on: '
+ 'the database rules read them from the top level only');
}
}
Expand Down Expand Up @@ -238,7 +238,8 @@ function buildServerConfig(project, conf, env) {
}
const aerodrome = buildAerodrome(project, env, merged.aerodrome);
const movementWritesViaApi = readFlag(project, env, merged, 'movementWritesViaApi');
if (movementWritesViaApi) {
const centralLogin = buildCentralLogin(project, env, merged);
if (movementWritesViaApi || centralLogin.centralLogin) {
assertMovementAerodromeMatchesRules(project, env, conf, aerodrome);
}

Expand All @@ -258,7 +259,7 @@ function buildServerConfig(project, conf, env) {
// What the arrival wizard offers for paying the landing fee.
paymentMethods: buildPaymentMethods(project, env, merged),
homebasePayment: readFlag(project, env, merged, 'homebasePayment'),
...buildCentralLogin(project, env, merged)
...centralLogin
};
}

Expand Down
Loading
Loading