merge dev into feat/postgres-support

This commit is contained in:
Maurice
2026-11-17 11:10:01 +01:00
3032 changed files with 153102 additions and 20800 deletions
+12
View File
@@ -74,11 +74,13 @@ DEMO_MODE=false # Demo mode: resets data hourly
# TREK_PLACES_ENABLED=false # Whether place search asks the TREK Places API at https://places.liketrek.com: suggestions, the full search, the category buttons on the map, the Road trip search along the route, import geocoding, offline downloads, and the OpenStreetMap ways and horse routes Tours reads. It is on by default and has no switch in the admin panel, because whether searches leave the instance is a deployment decision. Set false to search the way it did before 4.3.0 (OpenStreetMap, or your Google/Amap key) and send nothing to that host. (default: true)
# TREK_PLACES_URL=https://places.liketrek.com # Base URL of a copy of the Places API you run yourself; it has to answer the same /v1 API. Read from configuration rather than typed by a user, so an address on your LAN or Docker network works without ALLOW_INTERNAL_NETWORK. (default: https://places.liketrek.com)
# TREK_PLACES_CONTRIBUTE=admin # Whether an admin may let this installation send place details its users typed in (website, phone, e-mail, opening hours, description, picture) to the TREK Places API, so every installation gets better details. admin shows the choice in the admin panel, where it stays off until an admin agrees; off takes the choice away and nothing is ever sent. Nothing is sent outside NODE_ENV=production, in demo mode, with a custom TREK_PLACES_URL or with TREK_SERVICES=off. (default: admin)
# FLIGHT_LIVE_ENABLED=false # Whether booked flights show their live status (position, departure, landing, estimated arrival) from the free, keyless ADS-B feeds adsb.lol and, on a self-hosted install, adsb.fi. Only the flight's callsign is sent, never who is flying or where to. When set it overrides the switch in the admin panel: false turns it off, true keeps it on. Unset, the admin panel decides. (default: true)
# TREK_SERVICES=auto # How this installation identifies itself to the TREK services. On first use it registers itself in the background and keeps a key in the database; nothing to click, no account. off sends no key, and searches use the shared pool. (default: auto)
# TREK_SERVICES_KEY= # A key issued to you for this installation. Wins over the stored one and turns self-registration off; meant for hosted and pre-provisioned instances. (default: none)
# TREK_SERVICES_HOSTS=places.liketrek.com,photon.liketrek.com,valhalla.liketrek.com # Comma-separated hosts that receive the installation key over https. No other host ever sees it. (default: places.liketrek.com,photon.liketrek.com,valhalla.liketrek.com)
# TREK_PHOTON_URL=https://photon.liketrek.com # The address search (Photon) of the TREK services. Once the installation holds a key, search and reverse lookups ask it before Nominatim and fall back to Nominatim whenever it does not answer. Never asked with TREK_SERVICES=off, without a key, or when NOMINATIM_URL is set. (default: https://photon.liketrek.com)
# TREK_VALHALLA_URL=https://valhalla.liketrek.com # The Valhalla of the TREK services. Routes and elevation are asked of it unless Admin > Default user settings picks another routing service; the browser never talks to it, this server relays its questions with the installation key. (default: https://valhalla.liketrek.com)
# TREK_WALLET_SIGNER_URL=https://push.liketrek.com/v1/wallet/pass # Signs the Apple Wallet pass of a booking when someone taps "Add to Apple Wallet" in the iOS app. Only liketrek holds the pass certificate, so the pass's texts (the booking's name, times, places, seat or room and confirmation code, nothing else) go there once per tap and are not stored. Never asked with TREK_SERVICES=off. (default: https://push.liketrek.com/v1/wallet/pass)
# PLACES_API_KEY= # Google Maps API key (Places API (New) enabled) for place search, photos, details and Google transit. Overrides the key set in Admin > Settings > API Keys, whose field then shows this variable read-only. The value never reaches the browser.
# GOOGLE_LIST_DATA_BASE= # Optional HTTP(S) base URL of a list-data service, e.g. https://maps.example.com. Paste a URL beginning with this base to fetch and parse it directly. Unset keeps the original Google list import flow.
@@ -107,6 +109,16 @@ DEMO_MODE=false # Demo mode: resets data hourly
# SMTP_FROM=TREK <noreply@example.com> # Sender address of every email TREK sends.
# SMTP_SKIP_TLS_VERIFY=false # true accepts a self-signed certificate on an internal relay. Not for production.
# MAIL_INGEST_TRANSPORT=off # How forwarded booking mails reach TREK: off (uploads only), imap or webhook. Each MAIL_INGEST_* variable overrides its field in Admin > Integrations > Booking mail import.
# MAIL_INGEST_IMAP_HOST=imap.example.com # The mailbox TREK reads every two minutes. A host on your own network needs ALLOW_INTERNAL_NETWORK=true.
# MAIL_INGEST_IMAP_PORT=993 # 993 for TLS, 143 for STARTTLS.
# MAIL_INGEST_IMAP_TLS=tls # tls (from the first byte) or starttls.
# MAIL_INGEST_IMAP_USER= # IMAP login.
# MAIL_INGEST_IMAP_PASS= # IMAP password.
# MAIL_INGEST_IMAP_MAILBOX=INBOX # The folder the mails arrive in. Read mails move on to TREK/processed and are never deleted.
# MAIL_INGEST_ADDRESS_TEMPLATE=trek+{token}@example.com # The forwarding address; each person's token stands where {token} stands.
# MAIL_INGEST_WEBHOOK_SECRET= # The key the webhook sender signs each mail with (HMAC-SHA256), at least 32 characters.
# WEBAUTHN_RP_ID=trek.example.com # The domain passkeys are bound to. Overrides the setting in the admin panel; unset, it comes from the hostname of APP_URL. An IP address is refused.
# WEBAUTHN_ORIGINS=https://trek.example.com # Comma-separated origins allowed for passkey sign-in. Overrides the setting in the admin panel; unset, the origin of APP_URL is used.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
+26 -25
View File
@@ -1,6 +1,6 @@
{
"name": "@trek/server",
"version": "5.1.0",
"version": "5.1.4",
"engines": {
"node": "^22.22.2 || ^24.15.0 || >=26.0.0"
},
@@ -61,23 +61,23 @@
]
},
"dependencies": {
"@aws-lite/client": "^0.23.7",
"@aws-lite/client": "^0.24.1",
"@aws-lite/s3": "^0.2.7",
"@mikro-orm/cli": "^7.2.1",
"@mikro-orm/core": "^7.2.1",
"@mikro-orm/migrations": "^7.2.1",
"@mikro-orm/cli": "^7.2.7",
"@mikro-orm/core": "^7.2.7",
"@mikro-orm/migrations": "^7.2.7",
"@mikro-orm/nestjs": "^7.1.0",
"@mikro-orm/postgresql": "^7.2.1",
"@mikro-orm/seeder": "^7.2.1",
"@mikro-orm/postgresql": "^7.2.7",
"@mikro-orm/seeder": "^7.2.7",
"@mikro-orm/sql": "^7.2.1",
"@mikro-orm/sqlite": "^7.2.1",
"@modelcontextprotocol/sdk": "^1.32.1",
"@mikro-orm/sqlite": "^7.2.7",
"@modelcontextprotocol/sdk": "^1.32.2",
"@nestjs/common": "^11.2.5",
"@nestjs/config": "^4.0.4",
"@nestjs/core": "^11.2.5",
"@nestjs/platform-express": "^11.2.5",
"@nestjs/platform-ws": "^11.2.5",
"@nestjs/schedule": "^6.1.3",
"@nestjs/schedule": "^12.0.4",
"@nestjs/swagger": "^11.4.7",
"@nestjs/websockets": "^11.2.5",
"@simplewebauthn/server": "^13.3.3",
@@ -89,40 +89,41 @@
"cookie-parser": "^1.4.7",
"cors": "^2.8.6",
"cron": "^4.4.0",
"dotenv": "^16.6.1",
"dotenv": "^18.0.10",
"exifr": "7.1.3",
"express": "^4.22.3",
"fast-xml-parser": "^5.11.1",
"fast-xml-parser": "^5.12.1",
"helmet": "^8.3.0",
"imapflow": "^1.0.0",
"jimp": "^1.6.1",
"json5": "^2.2.3",
"jsonwebtoken": "^9.0.3",
"kysely": "^0.29.6",
"multer": "^2.5.0",
"nestjs-zod": "^5.5.0",
"nodemailer": "^10.1.4",
"nestjs-zod": "^5.6.0",
"nodemailer": "^10.5.0",
"otplib": "^12.0.1",
"pdf-parse": "^2.4.5",
"qrcode": "^1.5.4",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.2",
"semver": "^7.8.5",
"semver": "^7.8.6",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.23.15",
"typescript": "^6.0.3",
"tz-lookup": "^6.1.25",
"undici": "^7.30.1",
"unzipper": "^0.12.5",
"uuid": "^14.0.2",
"ws": "^8.21.3",
"uuid": "^14.0.3",
"ws": "^8.23.0",
"zod": "^4.6.7"
},
"devDependencies": {
"@aws-lite/s3-types": "^0.2.8",
"@eslint/js": "^10.0.1",
"@mikro-orm/entity-generator": "^7.2.1",
"@mikro-orm/entity-generator": "^7.2.7",
"@nestjs/testing": "^11.2.5",
"@swc/core": "^1.16.2",
"@swc/core": "^1.16.22",
"@trivago/prettier-plugin-sort-imports": "^6.0.2",
"@types/archiver": "^7.0.0",
"@types/bcryptjs": "^2.4.6",
@@ -132,22 +133,22 @@
"@types/cors": "^2.8.19",
"@types/express": "^4.17.25",
"@types/jsonwebtoken": "^9.0.10",
"@types/multer": "^2.2.0",
"@types/multer": "^2.3.0",
"@types/node": "^25.9.8",
"@types/nodemailer": "^8.0.2",
"@types/qrcode": "^1.5.6",
"@types/semver": "^7.8.0",
"@types/supertest": "^6.0.3",
"@types/supertest": "^7.2.1",
"@types/unzipper": "^0.10.11",
"@types/ws": "^8.18.1",
"@types/ws": "^8.18.2",
"@vitest/coverage-istanbul": "^4.1.11",
"eslint": "^9.39.5",
"eslint-config-flat-gitignore": "^2.4.0",
"eslint-config-prettier": "^10.1.8",
"prettier": "^3.9.8",
"prettier": "^3.10.1",
"prettier-plugin-organize-imports": "^4.3.0",
"supertest": "^7.2.2",
"typescript-eslint": "^8.70.0",
"supertest": "^7.3.1",
"typescript-eslint": "^8.72.1",
"unplugin-swc": "^1.6.0",
"vitest": "^4.1.11"
}
+115
View File
@@ -0,0 +1,115 @@
#!/usr/bin/env node
// Build server/assets/airlines-icao.json, the IATA airline code -> ICAO callsign prefix table the
// live flight status uses to turn a booked flight number ("LH 716") into the callsign an aircraft
// transmits ("DLH716").
//
// Source: VRS standing data (github.com/vradarserver/standing-data, CC0): airlines/schema-01 for
// the codes and the file list of routes/schema-01 for which airline still flies.
//
// Only airlines with both codes are kept. IATA reuses its two-character codes, so one IATA code
// often names several ICAO airlines (LH: Lufthansa and Lufthansa Cargo; U2: easyJet and United
// Feeder Service). The airline list carries no "active" flag, so the rule is: the ICAO airline
// with the most route data in the standing data wins (the summed size of its routes files), since
// routes are only recorded for callsigns seen flying. A shared code where no candidate has any
// route data is left out rather than guessed; a booking with it is then simply not tracked.
import fs from 'node:fs';
import https from 'node:https';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const OUT = path.join(__dirname, '..', 'assets', 'airlines-icao.json');
const AIRLINES = 'https://raw.githubusercontent.com/vradarserver/standing-data/main/airlines/schema-01/airlines.csv';
const TREE = 'https://api.github.com/repos/vradarserver/standing-data/git/trees/main?recursive=1';
function fetchText(url) {
return new Promise((resolve, reject) => {
https
.get(url, { headers: { 'User-Agent': 'trek-build-airlines', Accept: '*/*' } }, (res) => {
if (res.statusCode !== 200) return reject(new Error(`HTTP ${res.statusCode} for ${url}`));
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => resolve(data));
})
.on('error', reject);
});
}
/** One CSV line into its fields: optional double quotes, "" as an escaped quote. */
function parseLine(line) {
const fields = [];
let cur = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const ch = line[i];
if (inQuotes) {
if (ch === '"' && line[i + 1] === '"') {
cur += '"';
i++;
} else if (ch === '"') inQuotes = false;
else cur += ch;
} else if (ch === '"') inQuotes = true;
else if (ch === ',') {
fields.push(cur);
cur = '';
} else cur += ch;
}
fields.push(cur);
return fields;
}
const csv = (await fetchText(AIRLINES)).replace(/^/, '');
const tree = JSON.parse(await fetchText(TREE));
if (tree.truncated) throw new Error('The standing-data tree came back truncated; the route sizes would be incomplete');
// Bytes of route data per ICAO code: routes/schema-01/<letter>/<CODE>-all.csv or <CODE>-<digit>.csv.
const routeBytes = new Map();
for (const entry of tree.tree) {
const match = /^routes\/schema-01\/[A-Z0-9]\/([A-Z]{3})-(?:all|\d)\.csv$/.exec(entry.path);
if (!match) continue;
routeBytes.set(match[1], (routeBytes.get(match[1]) ?? 0) + (entry.size ?? 0));
}
const rows = csv.split(/\r?\n/);
const header = parseLine(rows[0]);
const ICAO = header.indexOf('ICAO');
const IATA = header.indexOf('IATA');
const candidates = new Map();
for (const line of rows.slice(1)) {
if (!line.trim()) continue;
const fields = parseLine(line);
const icao = fields[ICAO]?.trim().toUpperCase();
const iata = fields[IATA]?.trim().toUpperCase();
if (!icao || !iata || !/^[A-Z]{3}$/.test(icao) || !/^(?=.*[A-Z])[A-Z0-9]{2}$/.test(iata)) continue;
const list = candidates.get(iata) ?? [];
if (!list.includes(icao)) list.push(icao);
candidates.set(iata, list);
}
const table = {};
let shared = 0;
let dropped = 0;
for (const iata of [...candidates.keys()].sort()) {
const list = candidates.get(iata);
if (list.length === 1) {
table[iata] = list[0];
continue;
}
shared++;
const ranked = list
.map((icao) => ({ icao, bytes: routeBytes.get(icao) ?? 0 }))
.sort((a, b) => b.bytes - a.bytes || a.icao.localeCompare(b.icao));
if (ranked[0].bytes === 0) {
dropped++;
continue;
}
table[iata] = ranked[0].icao;
}
fs.writeFileSync(OUT, JSON.stringify(table));
console.log(
`Wrote ${Object.keys(table).length} airline codes to ${OUT}; ${shared} shared IATA codes resolved by route data, ${dropped} left out without any`,
);
+147 -14
View File
@@ -14,7 +14,13 @@
// FeatureCollections that the server serves at runtime (no network at boot): one for
// every country's borders, and one per country for its regions.
//
// A few countries take their regions from another source instead (REGION_SOURCES below:
// Ireland's counties from Tailte Éireann).
//
// geoBoundaries: CC BY 4.0 — https://www.geoboundaries.org/ (attribution required).
// Tailte Éireann: CC BY 4.0 — https://data-osi.opendata.arcgis.com/ (attribution required).
import { simplifySharedBorders } from './lib/shared-border-simplify.mjs';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -317,13 +323,86 @@ const A3_TO_A2 = {
*/
const ADM_LEVEL = { GBR: 'ADM2' };
/**
* The 26 counties of Ireland, keyed by the COUNTY field of Tailte Éireann's layer, with
* their ISO 3166-2:IE codes (the source carries none).
*/
const IE_COUNTIES = {
CARLOW: ['IE-CW', 'Carlow'],
CAVAN: ['IE-CN', 'Cavan'],
CLARE: ['IE-CE', 'Clare'],
CORK: ['IE-CO', 'Cork'],
DONEGAL: ['IE-DL', 'Donegal'],
DUBLIN: ['IE-D', 'Dublin'],
GALWAY: ['IE-G', 'Galway'],
KERRY: ['IE-KY', 'Kerry'],
KILDARE: ['IE-KE', 'Kildare'],
KILKENNY: ['IE-KK', 'Kilkenny'],
LAOIS: ['IE-LS', 'Laois'],
LEITRIM: ['IE-LM', 'Leitrim'],
LIMERICK: ['IE-LK', 'Limerick'],
LONGFORD: ['IE-LD', 'Longford'],
LOUTH: ['IE-LH', 'Louth'],
MAYO: ['IE-MO', 'Mayo'],
MEATH: ['IE-MH', 'Meath'],
MONAGHAN: ['IE-MN', 'Monaghan'],
OFFALY: ['IE-OY', 'Offaly'],
ROSCOMMON: ['IE-RN', 'Roscommon'],
SLIGO: ['IE-SO', 'Sligo'],
TIPPERARY: ['IE-TA', 'Tipperary'],
WATERFORD: ['IE-WD', 'Waterford'],
WESTMEATH: ['IE-WH', 'Westmeath'],
WEXFORD: ['IE-WX', 'Wexford'],
WICKLOW: ['IE-WW', 'Wicklow'],
};
/**
* Countries whose regions come from somewhere other than geoBoundaries, because no
* geoBoundaries level is the one people think in.
*
* IRL: ADM1 is the four provinces and ADM2 the 166 local electoral areas; there is no
* county layer at all (#2922). Tailte Éireann (CC BY 4.0) publishes the 26 counties,
* with Dublin, Cork, Galway and Limerick each one county (city and county together),
* which is what ISO 3166-2:IE lists.
*
* Each entry names the download, the file it is cached under, how many features it must
* yield, the tolerance its full-detail outlines are simplified with (see
* lib/shared-border-simplify.mjs) and how one feature's properties become the
* geoBoundaries `shapeName`/`shapeISO` that normalizeAdm1 reads, so everything after the
* download treats it like any other country. A feature it cannot place fails the build.
*/
const REGION_SOURCES = {
IRL: {
url:
'https://services-eu1.arcgis.com/FH5XCsx8rYXqnjF5/arcgis/rest/services/' +
'Counties___OSi_National_Statutory_Boundaries___Generalised_20m/FeatureServer/0/query' +
'?where=1%3D1&outFields=COUNTY&outSR=4326&f=geojson',
cacheFile: 'tailte-eireann-counties-2019-generalised-20m.geojson',
count: 26,
tolerance: 0.002,
toShape: ({ COUNTY }) => {
const county = IE_COUNTIES[COUNTY];
return county && { shapeISO: county[0], shapeName: county[1] };
},
},
};
const COUNTRIES = Object.keys(A3_TO_A2); // every ISO alpha-3 code (ADM1 fetch list)
// ATLAS_ONLY=IRL[,GBR…] rebuilds just those countries' regions: their admin1/<A2> files
// are rewritten and their features swapped into admin1.geojson.gz in place, while admin0
// and every other country stay as they are. Saves fetching the whole world for one fix.
const ONLY = (process.env.ATLAS_ONLY || '')
.split(',')
.map((s) => s.trim().toUpperCase())
.filter(Boolean);
for (const a3 of ONLY) if (!A3_TO_A2[a3]) throw new Error(`ATLAS_ONLY: unknown alpha-3 code ${a3}`);
// Cache raw downloads so re-runs (e.g. to tune simplification) don't re-fetch ~360 files.
const CACHE_DIR = path.join(__dirname, '..', '.atlas-geo-cache', GB_REF);
async function fetchGeo(url) {
const cacheFile = path.join(CACHE_DIR, url.split('/').slice(-1)[0]);
async function fetchGeo(url, cacheName = url.split('/').slice(-1)[0]) {
const cacheFile = path.join(CACHE_DIR, cacheName);
if (fs.existsSync(cacheFile)) {
const cached = fs.readFileSync(cacheFile, 'utf8');
return cached === '' ? null : JSON.parse(cached);
@@ -422,6 +501,30 @@ function normalizeAdm0Feature(f) {
};
}
// A country's raw region layer: from its REGION_SOURCES entry when it has one, else the
// geoBoundaries level ADM_LEVEL names (ADM1 by default).
async function fetchRegions(a3) {
const source = REGION_SOURCES[a3];
if (!source) return fetchGeo(MEDIA(a3, ADM_LEVEL[a3] || 'ADM1'));
const geo = await fetchGeo(source.url, source.cacheFile);
if (!geo?.features) throw new Error(`${a3}: region source could not be downloaded`);
const features = geo.features.map((f) => {
const shape = source.toShape(f.properties || {});
if (!shape) throw new Error(`${a3}: region source has a feature it cannot place: ${JSON.stringify(f.properties)}`);
return { type: 'Feature', properties: shape, geometry: quantizeGeometry(f.geometry, ADM1_DECIMALS) };
});
const codes = new Set(features.map((f) => f.properties.shapeISO));
if (features.length !== source.count || codes.size !== source.count) {
throw new Error(
`${a3}: expected ${source.count} distinct regions, the source has ${features.length} (${codes.size} distinct)`,
);
}
// By code, so the output does not depend on the order the service happens to answer in.
features.sort((a, b) => (a.properties.shapeISO < b.properties.shapeISO ? -1 : 1));
return { features: simplifySharedBorders(features, source.tolerance) };
}
function normalizeAdm1(geo, a3, countryName) {
if (!geo?.features) return [];
const a2 = A3_TO_A2[a3] || null;
@@ -477,7 +580,46 @@ function normalizeAdm1(geo, a3, countryName) {
.filter(Boolean);
}
const write = (name, features) => {
const fc = { type: 'FeatureCollection', features };
const gz = zlib.gzipSync(Buffer.from(JSON.stringify(fc)), { level: 9 });
const file = path.join(OUT_DIR, `${name}.geojson.gz`);
fs.writeFileSync(file, gz);
console.log(
`[atlas-geo] wrote ${path.relative(path.join(__dirname, '..'), file)} — ${features.length} features, ${(gz.length / 1e6).toFixed(1)} MB gz`,
);
};
const readShipped = (name) =>
JSON.parse(zlib.gunzipSync(fs.readFileSync(path.join(OUT_DIR, `${name}.geojson.gz`))).toString('utf8')).features;
// The ATLAS_ONLY path. A full build writes the countries in COUNTRIES order, each one's
// features in a single run, so a rebuilt country goes back where its run was, or where
// it would have been, and admin1.geojson.gz ends up as a full build would leave it.
async function rebuildOnly(countries) {
console.log(`[atlas-geo] rebuilding the regions of ${countries.join(', ')} only`);
fs.mkdirSync(CACHE_DIR, { recursive: true });
const a3ToName = Object.fromEntries(readShipped('admin0').map((f) => [f.properties.ADM0_A3, f.properties.NAME]));
const order = new Map(COUNTRIES.map((a3, i) => [A3_TO_A2[a3], i]));
let bundle = readShipped('admin1');
for (const a3 of countries) {
const a2 = A3_TO_A2[a3];
const features = normalizeAdm1(await fetchRegions(a3), a3, a3ToName[a3] || a3);
if (!features.length) throw new Error(`${a3}: no regions to write`);
const first = bundle.findIndex((f) => f.properties.iso_a2 === a2);
const at = first !== -1 ? first : bundle.findIndex((f) => order.get(f.properties.iso_a2) > order.get(a2));
const rest = bundle.filter((f) => f.properties.iso_a2 !== a2);
const cut = at === -1 ? rest.length : at;
bundle = [...rest.slice(0, cut), ...features, ...rest.slice(cut)];
write(`admin1/${a2}`, features);
}
write('admin1', bundle);
}
async function main() {
if (ONLY.length) return rebuildOnly(ONLY);
console.log(`[atlas-geo] geoBoundaries ref ${GB_REF}; ${COUNTRIES.length} countries`);
fs.mkdirSync(OUT_DIR, { recursive: true });
fs.mkdirSync(CACHE_DIR, { recursive: true });
@@ -496,9 +638,10 @@ async function main() {
}
}
// ADM1 (sub-national regions) — per-country gbOpen (carries ISO 3166-2 `shapeISO`).
// ADM1 (sub-national regions) — per-country gbOpen (carries ISO 3166-2 `shapeISO`),
// or the country's own source (REGION_SOURCES).
console.log('[atlas-geo] downloading ADM1 (regions)…');
const adm1Raw = await pool(COUNTRIES, (a3) => fetchGeo(MEDIA(a3, ADM_LEVEL[a3] || 'ADM1')));
const adm1Raw = await pool(COUNTRIES, fetchRegions);
const adm1Features = [];
let withCodes = 0;
COUNTRIES.forEach((a3, idx) => {
@@ -507,16 +650,6 @@ async function main() {
adm1Features.push(...feats);
});
const write = (name, features) => {
const fc = { type: 'FeatureCollection', features };
const gz = zlib.gzipSync(Buffer.from(JSON.stringify(fc)), { level: 9 });
const file = path.join(OUT_DIR, `${name}.geojson.gz`);
fs.writeFileSync(file, gz);
console.log(
`[atlas-geo] wrote ${path.relative(path.join(__dirname, '..'), file)} — ${features.length} features, ${(gz.length / 1e6).toFixed(1)} MB gz`,
);
};
write('admin0', adm0Features);
// Read only by two shipped migrations (Migration20200101021500/…031500), whose bodies
// never change; the server itself reads the per-country files below.
+19 -7
View File
@@ -36,17 +36,29 @@ fi
# which on a large or network-backed one (NFS, CephFS, Longhorn) kept the
# container busy for minutes before Node printed its first line (#2918). So only
# the dir and what sits directly in it are looked at: a volume that is not
# node's yet (a fresh one, or one copied in as root) shows there, and is handed
# over once. A volume that refuses chown altogether (NFS with root_squash) is
# said once per start instead of being walked file by file; such a pod belongs
# under runAsUser 1000, see the Helm page of the wiki. Best effort either way: a
# node's yet (a fresh one, or one whose top level was copied in as root) shows
# there, and is handed over once. Files copied in as root further down, into a
# subdirectory node already owns, do not show and are left alone; the
# Troubleshooting page of the wiki has the one-off chown for that.
#
# Only the owning user has to be node, because that is what Node runs as and
# what gosu leaves it with. The group is left to whoever set it: a Kubernetes
# fsGroup other than 1000 gives every file its own gid, and taking that back
# here would make the kubelet's OnRootMismatch walk the volume again on the
# next start, and this script after it, every time. Only a root group (a fresh
# volume under plain docker) is handed to node as well.
#
# A volume that refuses chown altogether (NFS with root_squash) is said once
# per start instead of being walked file by file; such a pod belongs under
# runAsUser 1000, see the Helm page of the wiki. Best effort either way: a
# read-only host filesystem or a locked-down bind mount must keep booting.
for dir in /app/data /app/uploads; do
[ -d "$dir" ] || continue
if [ -n "$(find "$dir" -maxdepth 1 \( ! -user node -o ! -group node \) -print -quit 2>/dev/null)" ]; then
if chown node:node "$dir" 2>/dev/null; then
if [ -n "$(find "$dir" -maxdepth 1 \( ! -user node -o -group 0 \) -print -quit 2>/dev/null)" ]; then
if chown node "$dir" 2>/dev/null; then
echo "Correcting ownership of $dir for the node user..."
chown -R -h node:node "$dir" 2>/dev/null || true
chown -R -h node "$dir" 2>/dev/null || true
find "$dir" -group 0 -exec chgrp -h node {} + 2>/dev/null || true
else
echo "WARNING: $dir cannot be handed to the node user (chown refused); run the container as uid 1000 instead."
fi
+3 -3
View File
@@ -4,7 +4,7 @@
"@typescript-eslint/no-non-null-assertion": 263,
"@typescript-eslint/no-require-imports": 18,
"@typescript-eslint/no-unsafe-function-type": 1,
"@typescript-eslint/no-unused-vars": 36,
"@typescript-eslint/no-unused-vars": 35,
"no-await-in-loop": 556,
"no-console": 232,
"no-empty": 6
@@ -16,8 +16,8 @@
"no-misleading-character-class": 1
},
"tests": {
"@typescript-eslint/no-explicit-any": 1555,
"@typescript-eslint/no-non-null-assertion": 1755,
"@typescript-eslint/no-explicit-any": 1553,
"@typescript-eslint/no-non-null-assertion": 1747,
"@typescript-eslint/no-require-imports": 25,
"@typescript-eslint/no-unsafe-function-type": 7,
"@typescript-eslint/no-unused-vars": 96,
@@ -0,0 +1,159 @@
/*
* Simplify the polygons of one country's regions without opening gaps between them.
*
* geoBoundaries ships a "_simplified" file per country, so the Atlas builder normally
* never simplifies anything itself. A region source that only offers full-detail
* outlines (Tailte Éireann's counties, #2922) needs it done here, and simplifying each
* polygon on its own would not do: two neighbours would each drop different points of
* the border they share, leaving slivers that belong to neither county, and a point in
* a sliver resolves to no region at all.
*
* So the rings are cut into arcs the way TopoJSON does it. A vertex is a junction when
* it does not have the same two neighbours everywhere it appears: where a shared border
* starts or ends, or where three regions meet. Between two junctions both sides hold
* the same run of points, and each arc is simplified (Douglas-Peucker) in one canonical
* direction, so both sides get the same points back. A ring with no junction at all (an
* island, or an enclave shared whole with its surroundings) is rotated to a canonical
* start first, for the same reason.
*
* Expects rings that are already quantized (identical borders carry identical
* coordinates) and closed. A ring left with fewer than three distinct points is dropped,
* a polygon whose outer ring is dropped goes with its holes, and a geometry with nothing
* left becomes null.
*/
const key = (pt) => `${pt[0]},${pt[1]}`;
// Squared distance from p to the segment a-b.
function segmentDistance2(p, a, b) {
const dx = b[0] - a[0];
const dy = b[1] - a[1];
const len2 = dx * dx + dy * dy;
let t = len2 === 0 ? 0 : ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / len2;
t = Math.max(0, Math.min(1, t));
const x = a[0] + t * dx - p[0];
const y = a[1] + t * dy - p[1];
return x * x + y * y;
}
/** Douglas-Peucker over an open polyline; both ends are always kept. */
export function douglasPeucker(points, tolerance) {
if (points.length <= 2) return points.slice();
const keep = new Uint8Array(points.length);
keep[0] = 1;
keep[points.length - 1] = 1;
const tol2 = tolerance * tolerance;
const stack = [[0, points.length - 1]];
while (stack.length) {
const [first, last] = stack.pop();
let maxDist = -1;
let index = -1;
for (let i = first + 1; i < last; i++) {
const d = segmentDistance2(points[i], points[first], points[last]);
if (d > maxDist) {
maxDist = d;
index = i;
}
}
if (index !== -1 && maxDist > tol2) {
keep[index] = 1;
stack.push([first, index], [index, last]);
}
}
return points.filter((_, i) => keep[i]);
}
// An arc simplified in its canonical direction, handed back in the direction it came in.
function simplifyArc(arc, tolerance) {
const head = key(arc[0]);
const tail = key(arc[arc.length - 1]);
const reversed = head === tail ? key(arc[1]) > key(arc[arc.length - 2]) : head > tail;
const canonical = reversed ? arc.slice().reverse() : arc;
const out = douglasPeucker(canonical, tolerance);
return reversed ? out.reverse() : out;
}
function polygonsOf(geometry) {
if (!geometry) return [];
return geometry.type === 'Polygon'
? [geometry.coordinates]
: geometry.type === 'MultiPolygon'
? geometry.coordinates
: [];
}
// A closed ring without its repeated closing point.
const open = (ring) => (ring.length > 1 && key(ring[0]) === key(ring[ring.length - 1]) ? ring.slice(0, -1) : ring);
/** Every vertex whose neighbours differ between the rings (or places) it appears in. */
function findJunctions(rings) {
const neighbours = new Map();
const junctions = new Set();
for (const ring of rings) {
const n = ring.length;
for (let i = 0; i < n; i++) {
const k = key(ring[i]);
const a = key(ring[(i - 1 + n) % n]);
const b = key(ring[(i + 1) % n]);
const pair = a < b ? `${a}|${b}` : `${b}|${a}`;
const seen = neighbours.get(k);
if (seen === undefined) neighbours.set(k, pair);
else if (seen !== pair) junctions.add(k);
}
}
return junctions;
}
function simplifyRing(ring, junctions, tolerance) {
const n = ring.length;
const cuts = [];
for (let i = 0; i < n; i++) if (junctions.has(key(ring[i]))) cuts.push(i);
let points;
if (cuts.length === 0) {
// No junction: start at the smallest vertex so a twin ring starts at the same place.
let start = 0;
for (let i = 1; i < n; i++) if (key(ring[i]) < key(ring[start])) start = i;
const rotated = [...ring.slice(start), ...ring.slice(0, start), ring[start]];
points = simplifyArc(rotated, tolerance).slice(0, -1);
} else {
points = [];
for (let c = 0; c < cuts.length; c++) {
const from = cuts[c];
const to = cuts[(c + 1) % cuts.length];
const arc = [];
for (let i = from; ; i = (i + 1) % n) {
arc.push(ring[i]);
if (arc.length > 1 && i === to) break;
}
points.push(...simplifyArc(arc, tolerance).slice(0, -1));
}
}
if (new Set(points.map(key)).size < 3) return null;
return [...points, points[0]];
}
/**
* Simplify every feature's (Multi)Polygon with the given tolerance (in coordinate
* units), keeping the borders that features share identical on both sides. Returns new
* features; properties are passed through untouched.
*/
export function simplifySharedBorders(features, tolerance) {
const rings = features.flatMap((f) => polygonsOf(f.geometry).flatMap((poly) => poly.map(open)));
const junctions = findJunctions(rings);
return features.map((f) => {
const polygons = polygonsOf(f.geometry)
.map((poly) => {
const [outer, ...holes] = poly.map((ring) => simplifyRing(open(ring), junctions, tolerance));
return outer ? [outer, ...holes.filter(Boolean)] : null;
})
.filter(Boolean);
const geometry =
polygons.length === 0
? null
: polygons.length === 1
? { type: 'Polygon', coordinates: polygons[0] }
: { type: 'MultiPolygon', coordinates: polygons };
return { ...f, geometry };
});
}
+27
View File
@@ -317,6 +317,9 @@ async function main() {
// service_key is the installation's key towards the liketrek services.
// Missed here it reads back as unusable, the installation registers anew and
// loses its id, and a revoked key would come back as a fresh one.
//
// The mail import's IMAP password and webhook secret: missed, the box stops
// being read and every signed mail is refused until an admin types them again.
for (const key of [
'oidc_client_secret',
'smtp_pass',
@@ -327,6 +330,8 @@ async function main() {
'amap_api_key',
'web_push_vapid_private_key',
'service_key',
'mail_ingest_imap_pass',
'mail_ingest_webhook_secret',
]) {
const row = db.prepare('SELECT value FROM app_settings WHERE key = ?').get(key) as { value: string } | undefined;
if (!row?.value) continue;
@@ -567,6 +572,28 @@ async function main() {
}
}
// --- mail_ingest: forwarding address tokens and the stored mails ---
// A person sees their address again through its token_enc; a forwarded mail
// waits for its reading, and for the inbox's ways out, as raw_eml_enc. Missed,
// the addresses still take mail (they are found by hash) but show as unset,
// and every stored mail reads as unreadable.
for (const [table, column] of [
['mail_ingest_addresses', 'token_enc'],
['mail_ingest_messages', 'raw_eml_enc'],
] as const) {
if (!tableExists(table)) continue;
const rows = db.prepare(`SELECT id, ${column} AS value FROM ${table} WHERE ${column} IS NOT NULL`).all() as {
id: number;
value: string;
}[];
for (const row of rows) {
const newVal = migrateApiKeyValue(row.value, `${table}[${row.id}].${column}`);
if (newVal !== null) {
db.prepare(`UPDATE ${table} SET ${column} = ? WHERE id = ?`).run(newVal, row.id);
}
}
}
// --- addons: the AI Parsing addon's instance-wide API key ---
// Stored encrypted as `apiKey` inside the addon's JSON config
// (llm-parse/llm-config.ts). Left out, LLM parsing quietly runs without a
+1 -1
View File
@@ -34,7 +34,7 @@
"src/db/repositories/RoadtripDayTracks.repository.ts": 1,
"src/db/repositories/RoadtripPreferences.repository.ts": 1,
"src/db/repositories/RoadtripVias.repository.ts": 6,
"src/db/repositories/Tags.repository.ts": 3,
"src/db/repositories/Tags.repository.ts": 2,
"src/db/repositories/TodoItems.repository.ts": 2,
"src/db/repositories/TripDocumentLinks.repository.ts": 5,
"src/db/repositories/TripInviteTokens.repository.ts": 4,
+6
View File
@@ -40,6 +40,7 @@
"DocumentSyncItems": "doc-sync",
"FerrataTiles": "ferratas",
"FileLinks": "files",
"FlightLiveStates": "flight-live",
"GoogleApiUsage": "google-quota",
"GooglePlacePhotoMeta": "place-photos",
"HiddenCountries": "atlas",
@@ -58,6 +59,11 @@
"JourneyShareTokens": "journey",
"JourneyTrips": "journey",
"Journeys": "journey",
"MailIngestAddresses": "mail-ingest",
"MailIngestCandidates": "mail-ingest",
"MailIngestCursor": "mail-ingest",
"MailIngestMessages": "mail-ingest",
"MailIngestPrefs": "mail-ingest",
"McpTokens": "tokens",
"NotificationChannelPreferences": "notifications",
"Notifications": "notifications",
+6 -6
View File
@@ -3,18 +3,18 @@
"scripts/generate-entities.ts": 2259,
"src/db/repositories/Places.repository.ts": 1435,
"src/db/repositories/Reservations.repository.ts": 1551,
"src/db/repositories/Trips.repository.ts": 1121,
"src/db/repositories/Trips.repository.ts": 1120,
"src/db/repositories/Users.repository.ts": 1445,
"src/nest/atlas/atlas.service.ts": 1177,
"src/nest/atlas/atlas.service.ts": 1174,
"src/nest/auth/auth.service.ts": 1143,
"src/nest/budget/budget.service.ts": 1612,
"src/nest/collections/collections.service.ts": 1426,
"src/nest/budget/budget.service.ts": 1607,
"src/nest/collections/collections.service.ts": 1414,
"src/nest/doc-sync/doc-sync.service.ts": 1060,
"src/nest/doc-sync/providers/synology-drive.client.ts": 1160,
"src/nest/journey/journey-domain.service.ts": 1659,
"src/nest/places/places.service.ts": 1546,
"src/nest/plugins/plugin-runtime.service.ts": 1460,
"src/nest/plugins/runtime/plugin-sdk.ts": 1093,
"src/nest/reservations/reservations.service.ts": 1237,
"src/nest/vacay/vacay.service.ts": 1409
"src/nest/reservations/reservations.service.ts": 1168,
"src/nest/vacay/vacay.service.ts": 1393
}
@@ -47,7 +47,7 @@
"tests/unit/nest/auth.controller.test.ts": 1,
"tests/unit/nest/auth.service.test.ts": 9,
"tests/unit/nest/backup.controller.test.ts": 1,
"tests/unit/nest/booking-import/booking-import.service.test.ts": 3,
"tests/unit/nest/booking-import/booking-import.service.test.ts": 1,
"tests/unit/nest/booking-import/import-jobs.service.test.ts": 1,
"tests/unit/nest/booking-import/kitinerary-extractor.service.test.ts": 1,
"tests/unit/nest/budget.service.calc.test.ts": 1,
@@ -97,7 +97,7 @@
"tests/unit/nest/memories/trek-photo-cache.job.test.ts": 1,
"tests/unit/nest/notification-preferences.service.test.ts": 4,
"tests/unit/nest/notification-transports.test.ts": 3,
"tests/unit/nest/notifications/storage-health-notifier.service.test.ts": 2,
"tests/unit/nest/notifications/storage-health-notifier.service.test.ts": 1,
"tests/unit/nest/notifications/vapid-keys.service.test.ts": 2,
"tests/unit/nest/notifications/web-push.service.test.ts": 1,
"tests/unit/nest/oauth.controller.test.ts": 1,
-1
View File
@@ -28,7 +28,6 @@
"src/nest/integrations/airtrail-link.service.ts#AirtrailLinkService.pushReservationToAirtrail": 2,
"src/nest/integrations/airtrail-sync.service.ts#AirtrailSyncService.runAirtrailSync": 2,
"src/nest/integrations/airtrail-sync.service.ts#AirtrailSyncService.syncOwner": 8,
"src/nest/integrations/airtrail.service.ts#AirtrailService.saveSettings": 2,
"src/nest/integrations/dawarich-sync.service.ts#DawarichSyncService.runSync": 2,
"src/nest/integrations/dawarich-sync.service.ts#DawarichSyncService.syncTripWindow": 3,
"src/nest/integrations/dawarich-sync.service.ts#DawarichSyncService.syncUserOnce": 3,
+4 -2
View File
@@ -105,8 +105,10 @@ Every variable has exactly one owner, and the owner decides how it is read:
`app.get(httpConfig.KEY)` for the pre-init Express layer). Today:
`httpConfig` (TRUST_PROXY, HSTS_INCLUDE_SUBDOMAINS, HTTP_KEEP_ALIVE_TIMEOUT_MS),
`storageConfig` (TREK_PLACE_PHOTO_DIR), `transitConfig` (TRANSIT_API_URL,
for TransitService) and `kitineraryConfig` (KITINERARY_EXTRACTOR_PATH and
PATH, for KitineraryExtractorService's binary probe). A module whose provider
for TransitService), `kitineraryConfig` (KITINERARY_EXTRACTOR_PATH and
PATH, for KitineraryExtractorService's binary probe), `servicesConfig`
(TREK_SERVICES and its hosts) and `mailIngestConfig` (MAIL_INGEST_*, for
MailIngestSettingsService, each over its stored setting). A module whose provider
injects one imports `AppConfigModule`, so it also builds on its own in a test.
- **`readEnv()` / `RuntimeEnvService`** (`derive.ts`): everything else, both
the runtime-toggled values and the boot-stable ones that code outside a Nest
+25
View File
@@ -69,6 +69,8 @@ const DEFAULT_SERVICE_HOSTS = ['places.liketrek.com', 'photon.liketrek.com', 'va
const DEFAULT_PHOTON_URL = 'https://photon.liketrek.com';
/** The TREK services' Valhalla, asked through this server's routing relay. */
const DEFAULT_VALHALLA_URL = 'https://valhalla.liketrek.com';
/** Signs the Apple Wallet passes of bookings; only liketrek holds the pass certificate. */
const DEFAULT_WALLET_PASS_URL = 'https://push.liketrek.com/v1/wallet/pass';
/**
* How this installation identifies itself to the liketrek services. Unset is
@@ -84,6 +86,28 @@ export function deriveServices(raw: RawEnv) {
hosts: hosts?.length ? hosts : DEFAULT_SERVICE_HOSTS,
photonUrl: stripTrailingSlashes(raw.TREK_PHOTON_URL?.trim() || DEFAULT_PHOTON_URL),
valhallaUrl: stripTrailingSlashes(raw.TREK_VALHALLA_URL?.trim() || DEFAULT_VALHALLA_URL),
walletPassUrl: raw.TREK_WALLET_SIGNER_URL?.trim() || DEFAULT_WALLET_PASS_URL,
};
}
/**
* How the booking mail import takes mail in, set in the environment rather
* than in the admin dialog: each value wins over its stored setting when it is
* non-empty (MailIngestSettingsService applies them). Unset leaves the dialog
* in charge, so an install that sets none of them behaves as before.
*/
export function deriveMailIngest(raw: RawEnv) {
const read = (value: string | undefined) => value?.trim() || null;
return {
transport: read(raw.MAIL_INGEST_TRANSPORT)?.toLowerCase() ?? null,
imapHost: read(raw.MAIL_INGEST_IMAP_HOST),
imapPort: read(raw.MAIL_INGEST_IMAP_PORT),
imapTls: read(raw.MAIL_INGEST_IMAP_TLS)?.toLowerCase() ?? null,
imapUser: read(raw.MAIL_INGEST_IMAP_USER),
imapPass: read(raw.MAIL_INGEST_IMAP_PASS),
imapMailbox: read(raw.MAIL_INGEST_IMAP_MAILBOX),
addressTemplate: read(raw.MAIL_INGEST_ADDRESS_TEMPLATE),
webhookSecret: read(raw.MAIL_INGEST_WEBHOOK_SECRET),
};
}
@@ -94,6 +118,7 @@ export const BOOT_DERIVERS = {
transit: deriveTransit,
kitinerary: deriveKitinerary,
services: deriveServices,
mailIngest: deriveMailIngest,
} as const;
export type BootNamespace = keyof typeof BOOT_DERIVERS;
+12
View File
@@ -112,6 +112,12 @@ export function deriveSession(raw: RawEnv) {
};
}
/** A boolean variable as the stored settings spell it, or undefined when unset or not a boolean. */
function flagText(raw: string | undefined): 'true' | 'false' | undefined {
const value = parseBool(raw);
return value === undefined ? undefined : value ? 'true' : 'false';
}
/**
* Whether this instance is centrally administered: somebody other than its
* admin user owns the configuration, the credentials and the upgrade schedule.
@@ -313,6 +319,12 @@ export function deriveIntegrations(raw: RawEnv) {
* request whose prompt plus cap overruns its context (#2677).
*/
llmMaxOutputTokens: positiveIntOr(raw.LLM_MAX_OUTPUT_TOKENS, 0) || undefined,
/**
* The operator's word on live flight status, as the admin setting spells it
* ('true'/'false'), or undefined when unset or not a boolean: then the admin
* switch (`flight_live_enabled`) decides.
*/
flightLiveEnabled: flagText(raw.FLIGHT_LIVE_ENABLED),
// TRANSIT_API_URL, KITINERARY_EXTRACTOR_PATH and PATH are boot-stable and
// read only by Nest providers, so the transitConfig and kitineraryConfig
// tokens own them (boot-derive.ts).
+16
View File
@@ -119,6 +119,20 @@ export const envSchema = z.object({
SMTP_FROM: anyString,
SMTP_SKIP_TLS_VERIFY: boolStr,
// Booking mail import: each wins over its field in the admin dialog
MAIL_INGEST_TRANSPORT: oneOf(['off', 'imap', 'webhook', 'cloud']),
MAIL_INGEST_IMAP_HOST: anyString,
MAIL_INGEST_IMAP_PORT: integer(1, 65535, 'must be a port number (1-65535)'),
MAIL_INGEST_IMAP_TLS: oneOf(['tls', 'starttls']),
MAIL_INGEST_IMAP_USER: anyString,
MAIL_INGEST_IMAP_PASS: anyString,
MAIL_INGEST_IMAP_MAILBOX: anyString,
MAIL_INGEST_ADDRESS_TEMPLATE: optionalWith(
(v) => v.includes('{token}') && v.includes('@'),
'must hold {token} and an @, like trek+{token}@example.com',
),
MAIL_INGEST_WEBHOOK_SECRET: optionalWith((v) => v.length >= 32, 'must be at least 32 characters'),
// WebAuthn
WEBAUTHN_RP_ID: anyString,
WEBAUTHN_ORIGINS: anyString,
@@ -199,12 +213,14 @@ export const envSchema = z.object({
GOOGLE_LIST_DATA_BASE: url,
TREK_PLACES_URL: url,
TREK_PLACES_ENABLED: boolStr,
FLIGHT_LIVE_ENABLED: boolStr,
TREK_PLACES_CONTRIBUTE: oneOf(['off', 'admin']),
TREK_SERVICES: oneOf(['auto', 'off']),
TREK_SERVICES_KEY: optionalWith(isServiceKeyShape, 'must be a TREK services key (tk1.<kid>.<payload>.<signature>)'),
TREK_SERVICES_HOSTS: anyString,
TREK_PHOTON_URL: url,
TREK_VALHALLA_URL: url,
TREK_WALLET_SIGNER_URL: url,
PLACES_API_KEY: anyString,
AMAP_API_BASE: url,
AMAP_API_KEY: anyString,
+2
View File
@@ -34,6 +34,8 @@ export const SECRET_ENV_KEYS: ReadonlySet<string> = new Set([
'CARTO_API_KEY',
'DEMO_ADMIN_PASS',
'TREK_DB_URL',
'MAIL_INGEST_IMAP_PASS',
'MAIL_INGEST_WEBHOOK_SECRET',
]);
/** How a variable's value appears in the boot report. */
+4 -2
View File
@@ -6,9 +6,10 @@ import { AppModule } from './nest/app.module';
import { apiDocsEnabled } from './nest/common/api-docs.kill-switch';
import { validateBodyContracts } from './nest/common/validate-body-contracts';
import { validateManagedRoutes } from './nest/common/validate-managed-routes';
import { validateRouteGuards } from './nest/common/validate-route-guards';
import { PUBLIC_ROUTE_ALLOW_LIST, validateRouteGuards } from './nest/common/validate-route-guards';
import { DatabaseLifecycle } from './nest/database/database-lifecycle.service';
import { withRequestContext } from './nest/database/request-context';
import { MANAGED_PUBLIC_ROUTES } from './nest/managed/managed-ext.module';
import { setupApiDocs } from './nest/platform/api-docs';
import { MCP_METADATA_MIDDLEWARE } from './nest/platform/mcp-metadata.middleware';
import { applyPlatformUploads, applyPlatformStatic } from './nest/platform/platform.routes';
@@ -247,7 +248,8 @@ export async function buildApp(): Promise<INestApplication> {
// Fail closed on the anonymous surface too: a route that answers without a
// session must carry @Public() with a reason AND be on the reviewed list.
// Default-deny protects what exists; this is what keeps the exemptions honest.
validateRouteGuards(app);
// The managed attachment point declares its own anonymous routes; empty in this repository.
validateRouteGuards(app, [...PUBLIC_ROUTE_ALLOW_LIST, ...MANAGED_PUBLIC_ROUTES]);
// And on the other direction of the same question: a route that a centrally
// administered install withholds from its own admin must say why, and be on a
// list somebody reviewed. Runs in every e2e harness because they share this
@@ -31,6 +31,7 @@ export class DayAssignments {
accommodation_id?: number | null;
route_excluded: number & Opt = 0;
tour_stage_key?: string | null;
stay_minutes?: number | null;
assignment_participants_collection = new Collection<AssignmentParticipants>(this);
file_links_collection = new Collection<FileLinks>(this);
reservations_collection = new Collection<Reservations>(this);
@@ -70,6 +71,7 @@ export const DayAssignmentsSchema = defineEntity({
accommodation_id: p.integer().nullable().index('idx_day_assignments_accommodation_id'),
route_excluded: p.integer().default(0),
tour_stage_key: p.text().nullable(),
stay_minutes: p.integer().nullable(),
assignment_participants_collection: () => p.oneToMany(AssignmentParticipants).mappedBy('assignment').hidden(),
file_links_collection: () => p.oneToMany(FileLinks).mappedBy('assignment').hidden(),
reservations_collection: () => p.oneToMany(Reservations).mappedBy('assignment').hidden(),
@@ -0,0 +1,65 @@
import { EntityRepositoryType, type Opt, PrimaryKeyProp, defineEntity, p } from '@mikro-orm/core';
import { FlightLiveStatesRepository } from '../repositories/FlightLiveStates.repository';
import { DbTimestampType } from '../types';
export class FlightLiveStates {
[EntityRepositoryType]?: FlightLiveStatesRepository;
[PrimaryKeyProp]?: ['callsign', 'service_date', 'origin', 'destination'];
callsign!: string;
service_date!: string;
origin!: string;
destination!: string;
phase: string & Opt = 'scheduled';
departed_at?: string | null;
landed_at?: string | null;
lat?: number | null;
lng?: number | null;
altitude_ft?: number | null;
ground_speed_kt?: number | null;
track_deg?: number | null;
vertical_rate_fpm?: number | null;
on_ground?: number | null;
registration?: string | null;
aircraft_type?: string | null;
source?: string | null;
first_seen_at?: string | null;
last_seen_at?: string | null;
fetched_at?: string | null;
next_poll_at?: string | null;
fail_count: number & Opt = 0;
departed_notified: number & Opt = 0;
landed_notified: number & Opt = 0;
delay_notified: number & Opt = 0;
}
export const FlightLiveStatesSchema = defineEntity({
class: FlightLiveStates,
repository: () => FlightLiveStatesRepository,
properties: {
callsign: p.text().primary(),
service_date: p.text().primary(),
origin: p.text().primary(),
destination: p.text().primary(),
phase: p.text().default('scheduled'),
departed_at: p.type(DbTimestampType).nullable(),
landed_at: p.type(DbTimestampType).nullable(),
lat: p.double().nullable(),
lng: p.double().nullable(),
altitude_ft: p.double().nullable(),
ground_speed_kt: p.double().nullable(),
track_deg: p.double().nullable(),
vertical_rate_fpm: p.double().nullable(),
on_ground: p.integer().nullable(),
registration: p.text().nullable(),
aircraft_type: p.text().nullable(),
source: p.text().nullable(),
first_seen_at: p.type(DbTimestampType).nullable(),
last_seen_at: p.type(DbTimestampType).nullable(),
fetched_at: p.type(DbTimestampType).nullable(),
next_poll_at: p.type(DbTimestampType).nullable(),
fail_count: p.integer().default(0),
departed_notified: p.integer().default(0),
landed_notified: p.integer().default(0),
delay_notified: p.integer().default(0),
},
});
@@ -0,0 +1,43 @@
import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { MailIngestAddressesRepository } from '../repositories/MailIngestAddresses.repository';
import { DbTimestampType } from '../types';
import { MailIngestMessages } from './MailIngestMessages.entity';
import { Trips } from './Trips.entity';
import { Users } from './Users.entity';
export class MailIngestAddresses {
[EntityRepositoryType]?: MailIngestAddressesRepository;
id!: number & Opt;
token_hash!: string;
token_enc!: string;
user!: Ref<Users>;
user_id!: number;
trip?: Ref<Trips> | null;
trip_id?: number | null;
created_at!: string & Opt;
revoked_at?: string | null;
last_used_at?: string | null;
mail_ingest_messages_collection = new Collection<MailIngestMessages>(this);
}
export const MailIngestAddressesSchema = defineEntity({
class: MailIngestAddresses,
repository: () => MailIngestAddressesRepository,
indexes: [
{ name: 'idx_mail_ingest_addresses_user', properties: ['user_id', 'trip_id'] },
],
uniques: [{ properties: ['token_hash'] }],
properties: {
id: p.integer().primary(),
token_hash: p.text(),
token_enc: p.text(),
user: () => p.manyToOne(Users).ref().deleteRule('cascade').hidden(),
user_id: p.integer().persist(false),
trip: () => p.manyToOne(Trips).ref().deleteRule('cascade').nullable().hidden().index('idx_mail_ingest_addresses_trip'),
trip_id: p.integer().nullable().persist(false).index('idx_mail_ingest_addresses_trip'),
created_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
revoked_at: p.type(DbTimestampType).nullable(),
last_used_at: p.type(DbTimestampType).nullable(),
mail_ingest_messages_collection: () => p.oneToMany(MailIngestMessages).mappedBy('address').hidden(),
},
});
@@ -0,0 +1,81 @@
import { EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { MailIngestCandidatesRepository } from '../repositories/MailIngestCandidates.repository';
import { DbTimestampType } from '../types';
import { MailIngestMessages } from './MailIngestMessages.entity';
import { Trips } from './Trips.entity';
import { Users } from './Users.entity';
export class MailIngestCandidates {
[EntityRepositoryType]?: MailIngestCandidatesRepository;
id!: number & Opt;
message!: Ref<MailIngestMessages>;
message_id!: number;
user!: Ref<Users>;
user_id!: number;
position: number & Opt = 0;
item_json?: string | null;
source?: string | null;
checks_json?: string | null;
uncertain_fields?: string | null;
match_json?: string | null;
suggestedTrip?: Ref<Trips> | null;
suggested_trip_id?: number | null;
dedupe_key?: string | null;
content_hash?: string | null;
resolution: string & Opt = 'pending';
reason?: string | null;
resolvedTrip?: Ref<Trips> | null;
resolved_trip_id?: number | null;
created_ids_json?: string | null;
attach_error?: string | null;
resolved_by?: number | null;
resolved_at?: string | null;
undo_until?: string | null;
created_at!: string & Opt;
}
export const MailIngestCandidatesSchema = defineEntity({
class: MailIngestCandidates,
repository: () => MailIngestCandidatesRepository,
indexes: [
{
name: 'idx_mail_ingest_candidates_resolved',
properties: ['resolved_trip_id', 'content_hash'],
},
{
name: 'idx_mail_ingest_candidates_user',
properties: ['user_id', 'resolution'],
},
{
name: 'idx_mail_ingest_candidates_message',
properties: ['message_id', 'position'],
},
],
properties: {
id: p.integer().primary(),
message: () => p.manyToOne(MailIngestMessages).ref().deleteRule('cascade').hidden(),
message_id: p.integer().persist(false),
user: () => p.manyToOne(Users).ref().deleteRule('cascade').hidden(),
user_id: p.integer().persist(false),
position: p.integer().default(0),
item_json: p.text().nullable(),
source: p.text().nullable(),
checks_json: p.text().nullable(),
uncertain_fields: p.text().nullable(),
match_json: p.text().nullable(),
suggestedTrip: () => p.manyToOne(Trips).ref().nullable().hidden().index('idx_mail_ingest_candidates_suggested'),
suggested_trip_id: p.integer().nullable().persist(false).index('idx_mail_ingest_candidates_suggested'),
dedupe_key: p.text().nullable(),
content_hash: p.text().nullable(),
resolution: p.text().default('pending'),
reason: p.text().nullable(),
resolvedTrip: () => p.manyToOne(Trips).ref().nullable().hidden(),
resolved_trip_id: p.integer().nullable().persist(false),
created_ids_json: p.text().nullable(),
attach_error: p.text().nullable(),
resolved_by: p.integer().nullable(),
resolved_at: p.type(DbTimestampType).nullable(),
undo_until: p.type(DbTimestampType).nullable(),
created_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
},
});
@@ -0,0 +1,33 @@
import { EntityRepositoryType, type Opt, defineEntity, p } from '@mikro-orm/core';
import { MailIngestCursorRepository } from '../repositories/MailIngestCursor.repository';
import { DbTimestampType } from '../types';
export class MailIngestCursor {
[EntityRepositoryType]?: MailIngestCursorRepository;
id!: number & Opt;
transport!: string;
mailbox!: string;
uid_validity?: number | null;
last_uid?: number | null;
last_ok_at?: string | null;
last_error?: string | null;
lease_owner?: string | null;
lease_until?: string | null;
}
export const MailIngestCursorSchema = defineEntity({
class: MailIngestCursor,
repository: () => MailIngestCursorRepository,
uniques: [{ properties: ['transport', 'mailbox'] }],
properties: {
id: p.integer().primary(),
transport: p.text(),
mailbox: p.text(),
uid_validity: p.integer().nullable(),
last_uid: p.integer().nullable(),
last_ok_at: p.type(DbTimestampType).nullable(),
last_error: p.text().nullable(),
lease_owner: p.text().nullable(),
lease_until: p.type(DbTimestampType).nullable(),
},
});
@@ -0,0 +1,91 @@
import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { MailIngestMessagesRepository } from '../repositories/MailIngestMessages.repository';
import { DbTimestampType } from '../types';
import { MailIngestAddresses } from './MailIngestAddresses.entity';
import { MailIngestCandidates } from './MailIngestCandidates.entity';
import { Trips } from './Trips.entity';
import { Users } from './Users.entity';
export class MailIngestMessages {
[EntityRepositoryType]?: MailIngestMessagesRepository;
id!: number & Opt;
transport!: string;
message_id_hdr?: string | null;
dedupe_hash!: string;
address?: Ref<MailIngestAddresses> | null;
address_id?: number | null;
user!: Ref<Users>;
user_id!: number;
trip?: Ref<Trips> | null;
trip_id?: number | null;
from_addr?: string | null;
from_verified: number & Opt = 0;
auth_results?: string | null;
subject?: string | null;
received_at!: string & Opt;
size!: number;
raw_eml_enc?: string | null;
raw_purged_at?: string | null;
status: string & Opt = 'queued';
reason?: string | null;
hints?: string | null;
attachments?: string | null;
forwarding_code?: string | null;
attempts: number & Opt = 0;
next_attempt_at!: string & Opt;
lease_until?: string | null;
ai_used: number & Opt = 0;
processed_at?: string | null;
mail_ingest_candidates_collection = new Collection<MailIngestCandidates>(this);
}
export const MailIngestMessagesSchema = defineEntity({
class: MailIngestMessages,
repository: () => MailIngestMessagesRepository,
indexes: [
{
name: 'idx_mail_ingest_messages_address',
properties: ['address_id', 'received_at'],
},
{
name: 'idx_mail_ingest_messages_user',
properties: ['user_id', 'received_at'],
},
{
name: 'idx_mail_ingest_messages_due',
properties: ['status', 'next_attempt_at'],
},
],
uniques: [{ properties: ['dedupe_hash'] }],
properties: {
id: p.integer().primary(),
transport: p.text(),
message_id_hdr: p.text().nullable(),
dedupe_hash: p.text(),
address: () => p.manyToOne(MailIngestAddresses).ref().nullable().hidden(),
address_id: p.integer().nullable().persist(false),
user: () => p.manyToOne(Users).ref().deleteRule('cascade').hidden(),
user_id: p.integer().persist(false),
trip: () => p.manyToOne(Trips).ref().nullable().hidden().index('idx_mail_ingest_messages_trip'),
trip_id: p.integer().nullable().persist(false).index('idx_mail_ingest_messages_trip'),
from_addr: p.text().nullable(),
from_verified: p.integer().default(0),
auth_results: p.text().nullable(),
subject: p.text().nullable(),
received_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
size: p.integer(),
raw_eml_enc: p.text().nullable(),
raw_purged_at: p.type(DbTimestampType).nullable(),
status: p.text().default('queued'),
reason: p.text().nullable(),
hints: p.text().nullable(),
attachments: p.text().nullable(),
forwarding_code: p.text().nullable(),
attempts: p.integer().default(0),
next_attempt_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
lease_until: p.type(DbTimestampType).nullable(),
ai_used: p.integer().default(0),
processed_at: p.type(DbTimestampType).nullable(),
mail_ingest_candidates_collection: () => p.oneToMany(MailIngestCandidates).mappedBy('message').hidden(),
},
});
@@ -0,0 +1,27 @@
import { EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { MailIngestPrefsRepository } from '../repositories/MailIngestPrefs.repository';
import { Users } from './Users.entity';
export class MailIngestPrefs {
[EntityRepositoryType]?: MailIngestPrefsRepository;
id!: number & Opt;
user!: Ref<Users>;
user_id!: number;
enabled: number & Opt = 1;
ai_enabled: number & Opt = 1;
auto_add: number & Opt = 1;
}
export const MailIngestPrefsSchema = defineEntity({
class: MailIngestPrefs,
repository: () => MailIngestPrefsRepository,
uniques: [{ properties: ['user'] }],
properties: {
id: p.integer().primary(),
user: () => p.manyToOne(Users).ref().deleteRule('cascade').hidden(),
user_id: p.integer().persist(false),
enabled: p.integer().default(1),
ai_enabled: p.integer().default(1),
auto_add: p.integer().default(1),
},
});
+5
View File
@@ -3,6 +3,7 @@ import { TagsRepository } from '../repositories/Tags.repository';
import { DbTimestampType } from '../types';
import { CollectionPlaces } from './CollectionPlaces.entity';
import { Places } from './Places.entity';
import { Trips } from './Trips.entity';
import { Users } from './Users.entity';
export class Tags {
@@ -13,6 +14,8 @@ export class Tags {
name!: string;
color?: string | null = '#10b981';
created_at?: string | null;
trip?: Ref<Trips> | null;
trip_id?: number | null;
collection_place_tags_inverse = new Collection<CollectionPlaces>(this);
place_tags_inverse = new Collection<Places>(this);
}
@@ -27,6 +30,8 @@ export const TagsSchema = defineEntity({
name: p.text(),
color: p.text().nullable(),
created_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`),
trip: () => p.manyToOne(Trips).ref().deleteRule('cascade').nullable().hidden().index('idx_tags_trip_id'),
trip_id: p.integer().nullable().persist(false).index('idx_tags_trip_id'),
collection_place_tags_inverse: () => p.manyToMany(CollectionPlaces).mappedBy('collection_place_tags').hidden(),
place_tags_inverse: () => p.manyToMany(Places).mappedBy('place_tags').hidden(),
},
+23
View File
@@ -19,6 +19,9 @@ import { DocumentSyncItems } from './DocumentSyncItems.entity';
import { InviteTokens } from './InviteTokens.entity';
import { JourneyEntries } from './JourneyEntries.entity';
import { JourneyTrips } from './JourneyTrips.entity';
import { MailIngestAddresses } from './MailIngestAddresses.entity';
import { MailIngestCandidates } from './MailIngestCandidates.entity';
import { MailIngestMessages } from './MailIngestMessages.entity';
import { PackingBags } from './PackingBags.entity';
import { PackingCategoryAssignees } from './PackingCategoryAssignees.entity';
import { PackingItems } from './PackingItems.entity';
@@ -28,6 +31,7 @@ import { Reservations } from './Reservations.entity';
import { RoadtripDayBoundaries } from './RoadtripDayBoundaries.entity';
import { RoadtripPreferences } from './RoadtripPreferences.entity';
import { ShareTokens } from './ShareTokens.entity';
import { Tags } from './Tags.entity';
import { TodoCategoryAssignees } from './TodoCategoryAssignees.entity';
import { TodoItems } from './TodoItems.entity';
import { TourDrafts } from './TourDrafts.entity';
@@ -58,6 +62,7 @@ export class Trips {
created_at?: string | null;
updated_at?: string | null;
reminder_sent_for?: string | null;
costs_mode: TTripsCostsMode & Opt = TripsCostsMode.SPLIT;
assistant_attachments_collection = new Collection<AssistantAttachments>(this);
assistant_threads_collection = new Collection<AssistantThreads>(this);
budget_category_order_collection = new Collection<BudgetCategoryOrder>(this);
@@ -76,6 +81,10 @@ export class Trips {
invite_tokens_collection = new Collection<InviteTokens>(this);
journey_entries_collection = new Collection<JourneyEntries>(this);
journey_trips_collection = new Collection<JourneyTrips>(this);
mail_ingest_addresses_collection = new Collection<MailIngestAddresses>(this);
mail_ingest_candidates_collection = new Collection<MailIngestCandidates>(this);
mail_ingest_candidates_collection1 = new Collection<MailIngestCandidates>(this);
mail_ingest_messages_collection = new Collection<MailIngestMessages>(this);
packing_bags_collection = new Collection<PackingBags>(this);
packing_category_assignees_collection = new Collection<PackingCategoryAssignees>(this);
packing_items_collection = new Collection<PackingItems>(this);
@@ -85,6 +94,7 @@ export class Trips {
roadtrip_day_boundaries_collection = new Collection<RoadtripDayBoundaries>(this);
roadtrip_preferences_collection = new Collection<RoadtripPreferences>(this);
share_tokens_collection = new Collection<ShareTokens>(this);
tags_collection = new Collection<Tags>(this);
todo_category_assignees_collection = new Collection<TodoCategoryAssignees>(this);
todo_items_collection = new Collection<TodoItems>(this);
tour_drafts_collection = new Collection<TourDrafts>(this);
@@ -98,6 +108,13 @@ export class Trips {
trip_photos_collection = new Collection<TripPhotos>(this);
}
export const TripsCostsMode = {
SPLIT: 'split',
TOTALS: 'totals',
} as const;
export type TTripsCostsMode = (typeof TripsCostsMode)[keyof typeof TripsCostsMode];
export const TripsSchema = defineEntity({
class: Trips,
repository: () => TripsRepository,
@@ -124,6 +141,7 @@ export const TripsSchema = defineEntity({
created_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`).index('idx_trips_created_at'),
updated_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`),
reminder_sent_for: p.text().nullable(),
costs_mode: p.enum(() => TripsCostsMode).default('split'),
assistant_attachments_collection: () => p.oneToMany(AssistantAttachments).mappedBy('trip').hidden(),
assistant_threads_collection: () => p.oneToMany(AssistantThreads).mappedBy('trip').hidden(),
budget_category_order_collection: () => p.oneToMany(BudgetCategoryOrder).mappedBy('trip').hidden(),
@@ -142,6 +160,10 @@ export const TripsSchema = defineEntity({
invite_tokens_collection: () => p.oneToMany(InviteTokens).mappedBy('trip').hidden(),
journey_entries_collection: () => p.oneToMany(JourneyEntries).mappedBy('sourceTrip').hidden(),
journey_trips_collection: () => p.oneToMany(JourneyTrips).mappedBy('trip').hidden(),
mail_ingest_addresses_collection: () => p.oneToMany(MailIngestAddresses).mappedBy('trip').hidden(),
mail_ingest_candidates_collection: () => p.oneToMany(MailIngestCandidates).mappedBy('suggestedTrip').hidden(),
mail_ingest_candidates_collection1: () => p.oneToMany(MailIngestCandidates).mappedBy('resolvedTrip').hidden(),
mail_ingest_messages_collection: () => p.oneToMany(MailIngestMessages).mappedBy('trip').hidden(),
packing_bags_collection: () => p.oneToMany(PackingBags).mappedBy('trip').hidden(),
packing_category_assignees_collection: () => p.oneToMany(PackingCategoryAssignees).mappedBy('trip').hidden(),
packing_items_collection: () => p.oneToMany(PackingItems).mappedBy('trip').hidden(),
@@ -151,6 +173,7 @@ export const TripsSchema = defineEntity({
roadtrip_day_boundaries_collection: () => p.oneToMany(RoadtripDayBoundaries).mappedBy('trip').hidden(),
roadtrip_preferences_collection: () => p.oneToMany(RoadtripPreferences).mappedBy('trip').hidden(),
share_tokens_collection: () => p.oneToMany(ShareTokens).mappedBy('trip').hidden(),
tags_collection: () => p.oneToMany(Tags).mappedBy('trip').hidden(),
todo_category_assignees_collection: () => p.oneToMany(TodoCategoryAssignees).mappedBy('trip').hidden(),
todo_items_collection: () => p.oneToMany(TodoItems).mappedBy('trip').hidden(),
tour_drafts_collection: () => p.oneToMany(TourDrafts).mappedBy('trip').hidden(),
+12
View File
@@ -39,6 +39,10 @@ import { JourneyRecordedDays } from './JourneyRecordedDays.entity';
import { JourneyRecordingPrep } from './JourneyRecordingPrep.entity';
import { JourneyShareTokens } from './JourneyShareTokens.entity';
import { Journeys } from './Journeys.entity';
import { MailIngestAddresses } from './MailIngestAddresses.entity';
import { MailIngestCandidates } from './MailIngestCandidates.entity';
import { MailIngestMessages } from './MailIngestMessages.entity';
import { MailIngestPrefs } from './MailIngestPrefs.entity';
import { McpTokens } from './McpTokens.entity';
import { NotificationChannelPreferences } from './NotificationChannelPreferences.entity';
import { Notifications } from './Notifications.entity';
@@ -176,6 +180,10 @@ export class Users {
journey_recording_prep_collection = new Collection<JourneyRecordingPrep>(this);
journey_share_tokens_collection = new Collection<JourneyShareTokens>(this);
journeys_collection = new Collection<Journeys>(this);
mail_ingest_addresses_collection = new Collection<MailIngestAddresses>(this);
mail_ingest_candidates_collection = new Collection<MailIngestCandidates>(this);
mail_ingest_messages_collection = new Collection<MailIngestMessages>(this);
mail_ingest_prefs_collection = new Collection<MailIngestPrefs>(this);
mcp_tokens_collection = new Collection<McpTokens>(this);
notification_channel_preferences_collection = new Collection<NotificationChannelPreferences>(this);
notifications_collection = new Collection<Notifications>(this);
@@ -335,6 +343,10 @@ export const UsersSchema = defineEntity({
journey_recording_prep_collection: () => p.oneToMany(JourneyRecordingPrep).mappedBy('user').hidden(),
journey_share_tokens_collection: () => p.oneToMany(JourneyShareTokens).mappedBy('createdByRef').hidden(),
journeys_collection: () => p.oneToMany(Journeys).mappedBy('user').hidden(),
mail_ingest_addresses_collection: () => p.oneToMany(MailIngestAddresses).mappedBy('user').hidden(),
mail_ingest_candidates_collection: () => p.oneToMany(MailIngestCandidates).mappedBy('user').hidden(),
mail_ingest_messages_collection: () => p.oneToMany(MailIngestMessages).mappedBy('user').hidden(),
mail_ingest_prefs_collection: () => p.oneToMany(MailIngestPrefs).mappedBy('user').hidden(),
mcp_tokens_collection: () => p.oneToMany(McpTokens).mappedBy('user').hidden(),
notification_channel_preferences_collection: () => p.oneToMany(NotificationChannelPreferences).mappedBy('user').hidden(),
notifications_collection: () => p.oneToMany(Notifications).mappedBy('sender').hidden(),
+12
View File
@@ -40,6 +40,7 @@ import { DocumentProvidersSchema } from './DocumentProviders.entity';
import { DocumentSyncItemsSchema } from './DocumentSyncItems.entity';
import { FerrataTilesSchema } from './FerrataTiles.entity';
import { FileLinksSchema } from './FileLinks.entity';
import { FlightLiveStatesSchema } from './FlightLiveStates.entity';
import { GoogleApiUsageSchema } from './GoogleApiUsage.entity';
import { GooglePlacePhotoMetaSchema } from './GooglePlacePhotoMeta.entity';
import { HiddenCountriesSchema } from './HiddenCountries.entity';
@@ -58,6 +59,11 @@ import { JourneyRecordingPrepSchema } from './JourneyRecordingPrep.entity';
import { JourneyShareTokensSchema } from './JourneyShareTokens.entity';
import { JourneyTripsSchema } from './JourneyTrips.entity';
import { JourneysSchema } from './Journeys.entity';
import { MailIngestAddressesSchema } from './MailIngestAddresses.entity';
import { MailIngestCandidatesSchema } from './MailIngestCandidates.entity';
import { MailIngestCursorSchema } from './MailIngestCursor.entity';
import { MailIngestMessagesSchema } from './MailIngestMessages.entity';
import { MailIngestPrefsSchema } from './MailIngestPrefs.entity';
import { McpTokensSchema } from './McpTokens.entity';
import { MigrationsSchema } from './Migrations.entity';
import { NotificationChannelPreferencesSchema } from './NotificationChannelPreferences.entity';
@@ -209,6 +215,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
DocumentSyncItemsSchema,
FerrataTilesSchema,
FileLinksSchema,
FlightLiveStatesSchema,
GoogleApiUsageSchema,
GooglePlacePhotoMetaSchema,
HiddenCountriesSchema,
@@ -227,6 +234,11 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
JourneyShareTokensSchema,
JourneyTripsSchema,
JourneysSchema,
MailIngestAddressesSchema,
MailIngestCandidatesSchema,
MailIngestCursorSchema,
MailIngestMessagesSchema,
MailIngestPrefsSchema,
McpTokensSchema,
MigrationsSchema,
NotificationChannelPreferencesSchema,
+83
View File
@@ -6,8 +6,15 @@ import {
assistantChangePreviewWriteSchema,
assistantMessagePartWriteSchema,
assistantStoredPartsSchema,
bookingImportPreviewItemSchema,
ferrataSchema,
hikingPreferencesSchema,
mailIngestAttachmentSchema,
mailIngestCheckResultSchema,
mailIngestCheckSchema,
mailIngestFieldSchema,
mailIngestHintSchema,
mailIngestSuggestedTripSchema,
obstacleSnapshotSchema,
paddleGeometrySchema,
paddleLockSchema,
@@ -26,9 +33,15 @@ import {
type DawarichTrackSegment,
type AssistantChangePreview,
type AssistantMessagePart,
type BookingImportPreviewItem,
type Ferrata,
type GeometryPoint,
type HikingPreferences,
type MailIngestAttachment,
type MailIngestCheck,
type MailIngestCheckResult,
type MailIngestField,
type MailIngestHint,
type ObstacleSnapshot,
type PaddleGeometry,
type PaddleLock,
@@ -531,3 +544,73 @@ export const PLACE_DETAILS_CACHE_PAYLOAD: JsonColumn<Record<string, unknown> | n
schema: z.record(z.string(), z.unknown()).nullable(),
fallback: () => null,
};
// ── The booking mail import (mail-ingest) ──────────────────────────────────
/** The booking read from a mail, as the inbox and the form show it; null for an entry that stands for the mail. */
export const MAIL_INGEST_ITEM: JsonColumn<BookingImportPreviewItem | null> = {
column: 'mail_ingest_candidates.item_json',
schema: bookingImportPreviewItemSchema.nullable(),
fallback: () => null,
};
/** The server's checks on the booking, by name. A broken row reads as none run. */
export const MAIL_INGEST_CHECKS: JsonColumn<Partial<Record<MailIngestCheck, MailIngestCheckResult>>> = {
column: 'mail_ingest_candidates.checks_json',
schema: z.partialRecord(mailIngestCheckSchema, mailIngestCheckResultSchema),
fallback: () => ({}),
};
/** The fields of the booking marked for a second look. */
export const MAIL_INGEST_UNCERTAIN_FIELDS: JsonColumn<MailIngestField[]> = {
column: 'mail_ingest_candidates.uncertain_fields',
schema: z.array(mailIngestFieldSchema),
fallback: () => [],
};
const mailIngestMatchSchema = z.object({
/** A change or cancellation of an earlier booking rather than a new one. */
kind: z.enum(['new_booking', 'change', 'cancellation']),
/** The trip the AI proposed to create when none fits; never created on its own. */
suggested_new_trip: mailIngestSuggestedTripSchema.nullable(),
});
type MailIngestMatch = z.infer<typeof mailIngestMatchSchema>;
/** How the booking was matched to a trip, past the suggested trip's own column. */
export const MAIL_INGEST_MATCH: JsonColumn<MailIngestMatch> = {
column: 'mail_ingest_candidates.match_json',
schema: mailIngestMatchSchema,
fallback: () => ({ kind: 'new_booking', suggested_new_trip: null }),
};
const idList = z.array(z.number().int().positive());
const mailIngestCreatedSchema = z.object({
reservation_ids: idList,
place_ids: idList,
budget_item_ids: idList,
file_ids: idList,
/** The booking as it was written, to tell an edit since then. */
fingerprint: z.string().nullable(),
});
export type MailIngestCreated = z.infer<typeof mailIngestCreatedSchema>;
/** What adding the booking created, for its undo. A broken row undoes nothing. */
export const MAIL_INGEST_CREATED: JsonColumn<MailIngestCreated> = {
column: 'mail_ingest_candidates.created_ids_json',
schema: mailIngestCreatedSchema,
fallback: () => ({ reservation_ids: [], place_ids: [], budget_item_ids: [], file_ids: [], fingerprint: null }),
};
/** What the inbox notes about a mail without holding it back. */
export const MAIL_INGEST_HINTS: JsonColumn<MailIngestHint[]> = {
column: 'mail_ingest_messages.hints',
schema: z.array(mailIngestHintSchema),
fallback: () => [],
};
/** The attachments a mail carried that a booking travels in, without their bytes. */
export const MAIL_INGEST_ATTACHMENTS: JsonColumn<MailIngestAttachment[]> = {
column: 'mail_ingest_messages.attachments',
schema: z.array(mailIngestAttachmentSchema),
fallback: () => [],
};
@@ -20,4 +20,5 @@ export interface DayAssignmentsTable {
accommodation_id: InsertOptional<number | null>;
route_excluded: InsertOptional<number>;
tour_stage_key: InsertOptional<string | null>;
stay_minutes: InsertOptional<number | null>;
}
@@ -0,0 +1,31 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface FlightLiveStatesTable {
callsign: string;
service_date: string;
origin: string;
destination: string;
phase: InsertOptional<string>;
departed_at: InsertOptional<string | null>;
landed_at: InsertOptional<string | null>;
lat: InsertOptional<number | null>;
lng: InsertOptional<number | null>;
altitude_ft: InsertOptional<number | null>;
ground_speed_kt: InsertOptional<number | null>;
track_deg: InsertOptional<number | null>;
vertical_rate_fpm: InsertOptional<number | null>;
on_ground: InsertOptional<number | null>;
registration: InsertOptional<string | null>;
aircraft_type: InsertOptional<string | null>;
source: InsertOptional<string | null>;
first_seen_at: InsertOptional<string | null>;
last_seen_at: InsertOptional<string | null>;
fetched_at: InsertOptional<string | null>;
next_poll_at: InsertOptional<string | null>;
fail_count: InsertOptional<number>;
departed_notified: InsertOptional<number>;
landed_notified: InsertOptional<number>;
delay_notified: InsertOptional<number>;
}
@@ -0,0 +1,14 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface MailIngestAddressesTable {
id: InsertOptional<number>;
token_hash: string;
token_enc: string;
user_id: number;
trip_id: InsertOptional<number | null>;
created_at: InsertOptional<string>;
revoked_at: InsertOptional<string | null>;
last_used_at: InsertOptional<string | null>;
}
@@ -0,0 +1,27 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface MailIngestCandidatesTable {
id: InsertOptional<number>;
message_id: number;
user_id: number;
position: InsertOptional<number>;
item_json: InsertOptional<string | null>;
source: InsertOptional<string | null>;
checks_json: InsertOptional<string | null>;
uncertain_fields: InsertOptional<string | null>;
match_json: InsertOptional<string | null>;
suggested_trip_id: InsertOptional<number | null>;
dedupe_key: InsertOptional<string | null>;
content_hash: InsertOptional<string | null>;
resolution: InsertOptional<string>;
reason: InsertOptional<string | null>;
resolved_trip_id: InsertOptional<number | null>;
created_ids_json: InsertOptional<string | null>;
attach_error: InsertOptional<string | null>;
resolved_by: InsertOptional<number | null>;
resolved_at: InsertOptional<string | null>;
undo_until: InsertOptional<string | null>;
created_at: InsertOptional<string>;
}
@@ -0,0 +1,15 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface MailIngestCursorTable {
id: InsertOptional<number>;
transport: string;
mailbox: string;
uid_validity: InsertOptional<number | null>;
last_uid: InsertOptional<number | null>;
last_ok_at: InsertOptional<string | null>;
last_error: InsertOptional<string | null>;
lease_owner: InsertOptional<string | null>;
lease_until: InsertOptional<string | null>;
}
@@ -0,0 +1,31 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface MailIngestMessagesTable {
id: InsertOptional<number>;
transport: string;
message_id_hdr: InsertOptional<string | null>;
dedupe_hash: string;
address_id: InsertOptional<number | null>;
user_id: number;
trip_id: InsertOptional<number | null>;
from_addr: InsertOptional<string | null>;
from_verified: InsertOptional<number>;
auth_results: InsertOptional<string | null>;
subject: InsertOptional<string | null>;
received_at: InsertOptional<string>;
size: number;
raw_eml_enc: InsertOptional<string | null>;
raw_purged_at: InsertOptional<string | null>;
status: InsertOptional<string>;
reason: InsertOptional<string | null>;
hints: InsertOptional<string | null>;
attachments: InsertOptional<string | null>;
forwarding_code: InsertOptional<string | null>;
attempts: InsertOptional<number>;
next_attempt_at: InsertOptional<string>;
lease_until: InsertOptional<string | null>;
ai_used: InsertOptional<number>;
processed_at: InsertOptional<string | null>;
}
@@ -0,0 +1,11 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface MailIngestPrefsTable {
id: InsertOptional<number>;
user_id: number;
enabled: InsertOptional<number>;
ai_enabled: InsertOptional<number>;
auto_add: InsertOptional<number>;
}
+1
View File
@@ -8,4 +8,5 @@ export interface TagsTable {
name: string;
color: InsertOptional<string | null>;
created_at: InsertOptional<string | null>;
trip_id: InsertOptional<number | null>;
}
+1
View File
@@ -17,4 +17,5 @@ export interface TripsTable {
created_at: InsertOptional<string | null>;
updated_at: InsertOptional<string | null>;
reminder_sent_for: InsertOptional<string | null>;
costs_mode: InsertOptional<string>;
}
+12
View File
@@ -43,6 +43,7 @@ import type { DocumentProvidersTable } from './DocumentProviders.table';
import type { DocumentSyncItemsTable } from './DocumentSyncItems.table';
import type { FerrataTilesTable } from './FerrataTiles.table';
import type { FileLinksTable } from './FileLinks.table';
import type { FlightLiveStatesTable } from './FlightLiveStates.table';
import type { GoogleApiUsageTable } from './GoogleApiUsage.table';
import type { GooglePlacePhotoMetaTable } from './GooglePlacePhotoMeta.table';
import type { HiddenCountriesTable } from './HiddenCountries.table';
@@ -61,6 +62,11 @@ import type { JourneyRecordingPrepTable } from './JourneyRecordingPrep.table';
import type { JourneyShareTokensTable } from './JourneyShareTokens.table';
import type { JourneyTripsTable } from './JourneyTrips.table';
import type { JourneysTable } from './Journeys.table';
import type { MailIngestAddressesTable } from './MailIngestAddresses.table';
import type { MailIngestCandidatesTable } from './MailIngestCandidates.table';
import type { MailIngestCursorTable } from './MailIngestCursor.table';
import type { MailIngestMessagesTable } from './MailIngestMessages.table';
import type { MailIngestPrefsTable } from './MailIngestPrefs.table';
import type { McpTokensTable } from './McpTokens.table';
import type { MigrationsTable } from './Migrations.table';
import type { NotificationChannelPreferencesTable } from './NotificationChannelPreferences.table';
@@ -218,6 +224,7 @@ export interface DB {
document_sync_items: DocumentSyncItemsTable;
ferrata_tiles: FerrataTilesTable;
file_links: FileLinksTable;
flight_live_states: FlightLiveStatesTable;
google_api_usage: GoogleApiUsageTable;
google_place_photo_meta: GooglePlacePhotoMetaTable;
hidden_countries: HiddenCountriesTable;
@@ -236,6 +243,11 @@ export interface DB {
journey_share_tokens: JourneyShareTokensTable;
journey_trips: JourneyTripsTable;
journeys: JourneysTable;
mail_ingest_addresses: MailIngestAddressesTable;
mail_ingest_candidates: MailIngestCandidatesTable;
mail_ingest_cursor: MailIngestCursorTable;
mail_ingest_messages: MailIngestMessagesTable;
mail_ingest_prefs: MailIngestPrefsTable;
mcp_tokens: McpTokensTable;
migrations: MigrationsTable;
notification_channel_preferences: NotificationChannelPreferencesTable;
@@ -0,0 +1,15 @@
import { Migration } from '@mikro-orm/migrations';
/**
* A chat message deleted before this release kept its text in the row; only the
* client hid it. Deleting now blanks the text with the flag
* (`CollabMessagesRepository.softDelete`), and this clears what older deletes
* left behind. The row itself stays for the "message deleted" placeholder.
*/
export class Migration20200101046370_a_deleted_chat_message_keeps_no_text extends Migration {
override name = 'Migration20200101046370_a_deleted_chat_message_keeps_no_text';
override up(): void {
this.addSql("UPDATE collab_messages SET text = '' WHERE deleted = 1 AND text != ''");
}
}
@@ -0,0 +1,111 @@
import { Migration } from '@mikro-orm/migrations';
interface RegionRow {
id: number;
user_id: number;
region_code: string;
region_name: string;
}
/** The 26 counties the Atlas now ships for Ireland: ISO 3166-2:IE code, name, province. */
const COUNTIES: ReadonlyArray<readonly [code: string, name: string, province: string]> = [
['IE-CW', 'Carlow', 'IE-L'],
['IE-D', 'Dublin', 'IE-L'],
['IE-KE', 'Kildare', 'IE-L'],
['IE-KK', 'Kilkenny', 'IE-L'],
['IE-LS', 'Laois', 'IE-L'],
['IE-LD', 'Longford', 'IE-L'],
['IE-LH', 'Louth', 'IE-L'],
['IE-MH', 'Meath', 'IE-L'],
['IE-OY', 'Offaly', 'IE-L'],
['IE-WH', 'Westmeath', 'IE-L'],
['IE-WX', 'Wexford', 'IE-L'],
['IE-WW', 'Wicklow', 'IE-L'],
['IE-CE', 'Clare', 'IE-M'],
['IE-CO', 'Cork', 'IE-M'],
['IE-KY', 'Kerry', 'IE-M'],
['IE-LK', 'Limerick', 'IE-M'],
['IE-TA', 'Tipperary', 'IE-M'],
['IE-WD', 'Waterford', 'IE-M'],
['IE-G', 'Galway', 'IE-C'],
['IE-LM', 'Leitrim', 'IE-C'],
['IE-MO', 'Mayo', 'IE-C'],
['IE-RN', 'Roscommon', 'IE-C'],
['IE-SO', 'Sligo', 'IE-C'],
['IE-CN', 'Cavan', 'IE-U'],
['IE-DL', 'Donegal', 'IE-U'],
['IE-MN', 'Monaghan', 'IE-U'],
];
/** "County Dublin", "Co. Dublin" and "dublin" all name Dublin. */
const countyKey = (name: string): string =>
name
.trim()
.toLowerCase()
.replace(/^(county|co\.?)\s+/, '');
/**
* Ireland gained its counties (#2922).
*
* geoBoundaries has no county layer for Ireland, so the Atlas showed the four
* provinces (IE-C, IE-L, IE-M, IE-U) next to a UK of counties. The bundle now ships
* Tailte Éireann's 26 counties under their ISO 3166-2:IE codes. What exists already:
*
* `place_regions` is a re-derivable cache full of province rows that nothing would
* ever re-derive, so Ireland's rows are cleared and come back as counties on the next
* Atlas load, as Great Britain's did when it gained its counties.
*
* `visited_regions` holds what a user marked by hand. A marked province stays as it
* is: which of its counties the user saw is not ours to guess. A row whose name is a
* county under another code (a Natural Earth or geocoder code from older versions)
* takes the county's code, and is dropped where the user already holds that county.
*
* `hidden_regions` remembers a region the user switched off. A hidden province hid
* every place in it, so each of its counties is hidden in its place; the province row
* stays and keeps hiding a marked province.
*
* `bucket_list` is a wish, not a polygon, and is left alone.
*/
export class Migration20200101046380_ireland_gained_its_counties extends Migration {
override name = 'Migration20200101046380_ireland_gained_its_counties';
override async up(): Promise<void> {
await this.execute(`DELETE FROM place_regions WHERE UPPER(COALESCE(country_code, '')) = 'IE'`);
// Queued in order, run after the reads below: the codes come from the table
// above and the row ids from the database, nothing a user typed.
for (const [code, , province] of COUNTIES) {
this.addSql(
`INSERT INTO hidden_regions (user_id, region_code, country_code)
SELECT h.user_id, '${code}', h.country_code FROM hidden_regions h
WHERE h.region_code = '${province}' AND UPPER(h.country_code) = 'IE'
AND NOT EXISTS (SELECT 1 FROM hidden_regions x WHERE x.user_id = h.user_id AND x.region_code = '${code}')`,
);
}
const rows = (await this.execute(
`SELECT id, user_id, region_code, region_name FROM visited_regions
WHERE UPPER(COALESCE(country_code, '')) = 'IE' ORDER BY id`,
)) as RegionRow[];
const counties = new Map(COUNTIES.map(([code, name]) => [countyKey(name), { code, name }]));
const countyCodes = new Set(COUNTIES.map(([code]) => code));
const held = new Set(rows.map((r) => `${r.user_id}|${r.region_code}`));
for (const row of rows) {
if (countyCodes.has(row.region_code)) continue;
const county = counties.get(countyKey(row.region_name || ''));
if (!county) continue;
const target = `${row.user_id}|${county.code}`;
const id = Number(row.id);
if (!Number.isInteger(id)) continue;
if (held.has(target)) {
this.addSql(`DELETE FROM visited_regions WHERE id = ${id}`);
} else {
this.addSql(
`UPDATE visited_regions SET region_code = '${county.code}', region_name = '${county.name}' WHERE id = ${id}`,
);
held.add(target);
}
}
}
}
@@ -0,0 +1,24 @@
import { addColumnIfMissing } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/**
* A trip can keep its costs as totals only (#1714): who paid, the shares and
* settling up are left out of Costs, for travellers who track what a trip costs
* but never split it.
*
* `costs_mode` is 'split' (what every trip did so far) or 'totals'. 'split' as
* the default keeps this from being a breaking change: SQLite fills every
* existing row with it on the ALTER, so every trip looks as it did before.
*/
export class Migration20200101046390_a_trip_can_keep_its_costs_as_totals_only extends Migration {
override name = 'Migration20200101046390_a_trip_can_keep_its_costs_as_totals_only';
override async up(): Promise<void> {
await addColumnIfMissing(
this,
'trips',
'costs_mode',
`costs_mode TEXT NOT NULL DEFAULT 'split' CHECK (costs_mode IN ('split', 'totals'))`,
);
}
}
@@ -0,0 +1,51 @@
import { Migration } from '@mikro-orm/migrations';
/**
* Live flight status: what the open ADS-B feeds last said about one flight on
* one day, keyed by the callsign it transmits, its local departure date and
* its two airports. A through flight keeps one flight number over two sectors
* (FRA to BOM to SIN as LH 754 on one day), and each sector has a state of its own.
* One row serves every trip that booked the same flight, so the feeds are asked
* once per flight, not once per traveller. Next to the derived state sit the
* poll's bookkeeping (when to ask next, how often it failed) and whether each
* notification already went out, so a departure is announced once.
*
* No foreign key: the row describes a flight in the air, not a booking, and is
* found again from the bookings by its key.
*/
export class Migration20200101046400_a_booked_flight_shows_where_it_is extends Migration {
override name = 'Migration20200101046400_a_booked_flight_shows_where_it_is';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS flight_live_states (
callsign TEXT NOT NULL,
service_date TEXT NOT NULL,
origin TEXT NOT NULL,
destination TEXT NOT NULL,
phase TEXT NOT NULL DEFAULT 'scheduled',
departed_at DATETIME,
landed_at DATETIME,
lat REAL,
lng REAL,
altitude_ft REAL,
ground_speed_kt REAL,
track_deg REAL,
vertical_rate_fpm REAL,
on_ground INTEGER,
registration TEXT,
aircraft_type TEXT,
source TEXT,
first_seen_at DATETIME,
last_seen_at DATETIME,
fetched_at DATETIME,
next_poll_at DATETIME,
fail_count INTEGER NOT NULL DEFAULT 0,
departed_notified INTEGER NOT NULL DEFAULT 0,
landed_notified INTEGER NOT NULL DEFAULT 0,
delay_notified INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (callsign, service_date, origin, destination)
)
`);
}
}
@@ -0,0 +1,163 @@
import { Migration } from '@mikro-orm/migrations';
/**
* A person forwards a booking mail to an address of their own, and TREK adds
* the booking to the matching trip, or keeps it in an inbox with a reason.
*
* `mail_ingest_addresses` holds one row per forwarding address: the person's
* own (`trip_id` null) or one for a single trip, which belongs to the member
* who made it (`user_id`). The token is stored twice: its sha256 for the
* lookup, and encrypted so its owner can see the address again. A renewed or
* revoked address keeps its row with `revoked_at` set, so a mail to it is
* recognised and refused.
*
* `mail_ingest_prefs` holds a person's switches (import, AI reading, adding
* on its own or asking first); without a row every switch is on.
*
* `mail_ingest_cursor` is the position and the lease of a reader: an IMAP box
* by (uid_validity, last_uid), and the processing pipeline itself, so two
* processes on one database never read the same mail at once.
*
* `mail_ingest_messages` holds each mail as it arrived, encrypted
* (`raw_eml_enc`) until the retention job purges it, with what the
* pipeline made of it. `dedupe_hash` keeps a mail delivered twice from
* being read twice.
*
* `mail_ingest_candidates` holds each booking read from a mail, the server's
* checks on it, the trip it went to or was suggested for, and what became of
* it: the ids it created (for the undo, open until `undo_until`), or why it
* waits in the inbox.
*
* Every row goes with its person; a trip that goes clears its references, so
* the inbox entry stays and can be filed elsewhere. Every foreign key leads
* an index.
*/
export class Migration20200101046410_a_forwarded_booking_mail_lands_in_its_trip extends Migration {
override name = 'Migration20200101046410_a_forwarded_booking_mail_lands_in_its_trip';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS mail_ingest_addresses (
id INTEGER PRIMARY KEY,
token_hash TEXT NOT NULL,
token_enc TEXT NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
trip_id INTEGER REFERENCES trips(id) ON DELETE CASCADE,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
revoked_at DATETIME,
last_used_at DATETIME,
UNIQUE(token_hash)
)
`);
this.addSql('CREATE INDEX IF NOT EXISTS idx_mail_ingest_addresses_user ON mail_ingest_addresses(user_id, trip_id)');
this.addSql('CREATE INDEX IF NOT EXISTS idx_mail_ingest_addresses_trip ON mail_ingest_addresses(trip_id)');
this.addSql(`
CREATE TABLE IF NOT EXISTS mail_ingest_prefs (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
enabled INTEGER NOT NULL DEFAULT 1,
ai_enabled INTEGER NOT NULL DEFAULT 1,
auto_add INTEGER NOT NULL DEFAULT 1,
UNIQUE(user_id)
)
`);
this.addSql(`
CREATE TABLE IF NOT EXISTS mail_ingest_cursor (
id INTEGER PRIMARY KEY,
transport TEXT NOT NULL CHECK(transport IN ('imap', 'cloud', 'pipeline')),
mailbox TEXT NOT NULL,
uid_validity INTEGER,
last_uid INTEGER,
last_ok_at DATETIME,
last_error TEXT,
lease_owner TEXT,
lease_until DATETIME,
UNIQUE(transport, mailbox)
)
`);
this.addSql(`
CREATE TABLE IF NOT EXISTS mail_ingest_messages (
id INTEGER PRIMARY KEY,
transport TEXT NOT NULL CHECK(transport IN ('imap', 'webhook', 'cloud', 'upload')),
message_id_hdr TEXT,
dedupe_hash TEXT NOT NULL,
address_id INTEGER REFERENCES mail_ingest_addresses(id) ON DELETE SET NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL,
from_addr TEXT,
from_verified INTEGER NOT NULL DEFAULT 0,
auth_results TEXT,
subject TEXT,
received_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
size INTEGER NOT NULL,
raw_eml_enc TEXT,
raw_purged_at DATETIME,
status TEXT NOT NULL DEFAULT 'queued'
CHECK(status IN ('queued', 'processing', 'processed', 'discarded', 'failed')),
reason TEXT,
hints TEXT,
attachments TEXT,
forwarding_code TEXT,
attempts INTEGER NOT NULL DEFAULT 0,
next_attempt_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
lease_until DATETIME,
ai_used INTEGER NOT NULL DEFAULT 0,
processed_at DATETIME,
UNIQUE(dedupe_hash)
)
`);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_messages_due ON mail_ingest_messages(status, next_attempt_at)',
);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_messages_user ON mail_ingest_messages(user_id, received_at)',
);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_messages_address ON mail_ingest_messages(address_id, received_at)',
);
this.addSql('CREATE INDEX IF NOT EXISTS idx_mail_ingest_messages_trip ON mail_ingest_messages(trip_id)');
this.addSql(`
CREATE TABLE IF NOT EXISTS mail_ingest_candidates (
id INTEGER PRIMARY KEY,
message_id INTEGER NOT NULL REFERENCES mail_ingest_messages(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
position INTEGER NOT NULL DEFAULT 0,
item_json TEXT,
source TEXT CHECK(source IN ('ai', 'kitinerary', 'json_ld', 'ics', 'pkpass', 'text')),
checks_json TEXT,
uncertain_fields TEXT,
match_json TEXT,
suggested_trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL,
dedupe_key TEXT,
content_hash TEXT,
resolution TEXT NOT NULL DEFAULT 'pending'
CHECK(resolution IN ('pending', 'auto_applied', 'applied', 'filed', 'discarded', 'duplicate', 'conflict',
'undone')),
reason TEXT,
resolved_trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL,
created_ids_json TEXT,
attach_error TEXT,
resolved_by INTEGER,
resolved_at DATETIME,
undo_until DATETIME,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_candidates_message ON mail_ingest_candidates(message_id, position)',
);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_candidates_user ON mail_ingest_candidates(user_id, resolution)',
);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_candidates_suggested ON mail_ingest_candidates(suggested_trip_id)',
);
this.addSql(
'CREATE INDEX IF NOT EXISTS idx_mail_ingest_candidates_resolved ON mail_ingest_candidates(resolved_trip_id, content_hash)',
);
}
}
@@ -0,0 +1,20 @@
import { addColumnIfMissing } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/**
* #3092: how long the drive stands at a stop belongs to the visit, not to the
* place. A place planned twice on one road trip day (once to stop, once only to
* steer the route past it) could not be given two different stays, because the
* only value was `places.duration_minutes`.
*
* `stay_minutes` is the visit's own length. It stays null on every existing row,
* and a null visit keeps reading the place's `duration_minutes`, so a trip
* planned before this step drives exactly as it did.
*/
export class Migration20200101046420_a_visit_keeps_its_own_stay extends Migration {
override name = 'Migration20200101046420_a_visit_keeps_its_own_stay';
override async up(): Promise<void> {
await addColumnIfMissing(this, 'day_assignments', 'stay_minutes', 'stay_minutes INTEGER');
}
}
@@ -0,0 +1,44 @@
import { Migration } from '@mikro-orm/migrations';
/**
* The hourly job that tells every forwarding address holder once that the mail
* import is paused keeps its "already told" mark and its lease in
* `mail_ingest_cursor`, as the `notice` reader. The CHECK from
* Migration20200101046410 only knew `imap`, `cloud` and `pipeline`, so taking
* that lease failed on the insert (an `ON CONFLICT DO NOTHING` does not cover a
* CHECK) and nobody was ever told.
*
* SQLite cannot change a CHECK in place, so the table is rebuilt with `notice`
* added. No table points at `mail_ingest_cursor` and it has no index of its own
* beyond the `UNIQUE(transport, mailbox)`, so it is copied, dropped and renamed
* on its own. Every row keeps its id, position and lease.
*/
export class Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders extends Migration {
override name = 'Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders';
override up(): void {
this.addSql(`
CREATE TABLE mail_ingest_cursor_new (
id INTEGER PRIMARY KEY,
transport TEXT NOT NULL CHECK(transport IN ('imap', 'cloud', 'pipeline', 'notice')),
mailbox TEXT NOT NULL,
uid_validity INTEGER,
last_uid INTEGER,
last_ok_at DATETIME,
last_error TEXT,
lease_owner TEXT,
lease_until DATETIME,
UNIQUE(transport, mailbox)
)
`);
this.addSql(`
INSERT INTO mail_ingest_cursor_new (
id, transport, mailbox, uid_validity, last_uid, last_ok_at, last_error, lease_owner, lease_until
)
SELECT id, transport, mailbox, uid_validity, last_uid, last_ok_at, last_error, lease_owner, lease_until
FROM mail_ingest_cursor
`);
this.addSql('DROP TABLE mail_ingest_cursor');
this.addSql('ALTER TABLE mail_ingest_cursor_new RENAME TO mail_ingest_cursor');
}
}
@@ -0,0 +1,21 @@
import { addColumnIfMissing } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/**
* A tag can belong to a trip. Every tag used to be one person's: a place of a
* shared trip carried labels only their owner could list, pick again or rename,
* and a co-traveller had to make their own tag of the same name.
*
* A tag with `trip_id` set is the trip's own. Every member reads it, anyone who
* may edit the trip's places may change it, and it goes when the trip goes.
* `user_id` keeps who created it. The column stays null on every existing row,
* which leaves those what they were: personal tags.
*/
export class Migration20200101046440_a_trip_owns_its_tags extends Migration {
override name = 'Migration20200101046440_a_trip_owns_its_tags';
override async up(): Promise<void> {
await addColumnIfMissing(this, 'tags', 'trip_id', 'trip_id INTEGER REFERENCES trips(id) ON DELETE CASCADE');
this.addSql('CREATE INDEX IF NOT EXISTS idx_tags_trip_id ON tags (trip_id)');
}
}
@@ -179,9 +179,13 @@ export class CollabMessagesRepository extends TrekRepository<CollabMessages> {
return await this.insert({ trip: trip_id, user: user_id, text, reply_to, origin });
}
/** CB54 (`deleteMessage`, inside its transaction) — `UPDATE collab_messages SET deleted = 1 WHERE id = ?`, soft delete — the row survives for the placeholder. */
/**
* CB54 (`deleteMessage`, inside its transaction) — `UPDATE collab_messages SET deleted = 1, text = '' WHERE id = ?`,
* soft delete: the row survives for the placeholder, its text does not, so a deleted message is gone from the
* database and every backup made after it, not only hidden from the client.
*/
async softDelete(id: number): Promise<void> {
await this.nativeUpdate({ id }, { deleted: 1 });
await this.nativeUpdate({ id }, { deleted: 1, text: '' });
}
// -------------------------------------------------------------------------
@@ -171,6 +171,13 @@ export class CollectionPlacesRepository extends TrekRepository<CollectionPlaces>
category_id: number | null;
image_url: string | null;
links: string | null;
google_place_id: string | null;
google_ftid: string | null;
osm_id: string | null;
website: string | null;
phone: string | null;
price: number | null;
currency: string | null;
collection_id: number;
owner_id: number;
}>,
@@ -84,6 +84,42 @@ type CollectionPlacesWriteKyselyDB = Pick<DB, 'collection_places'>;
type CollectionPlaceLabelsKyselyDB = Pick<DB, 'collection_place_labels'>;
type CollectionPlacesPageKyselyDB = Pick<DB, 'collection_places' | 'collection_place_labels'>;
/** Which of a list's places a page holds (list_collection_places, #2647); every filter left out matches all. */
interface CollectionPlacePageFilter {
status?: string;
labelId?: number;
unlabeled?: boolean;
}
/**
* A place's position in its list: the keys both a page and getCollection order
* by, in that order. A missing sort_order reads as the column default and a
* missing created_at as the earliest, so the order is total on every engine and
* a page can go on after any place.
*/
export interface CollectionPlacePosition {
sortOrder: number;
createdAt: string;
id: number;
}
/** A saved place as a page names it: the fields an agent finds it by, plus its position. */
export interface CollectionPlacePageRow {
id: number;
name: string;
status: string;
address: string | null;
lat: number | null;
lng: number | null;
google_place_id: string | null;
website: string | null;
phone: string | null;
sort_key: number;
created_key: string;
}
/**
* `collections` — the root of the collections cluster (Plan 3h Task 1, part
* A). Owns every CL1-CL36 statement not moved to `CollectionMembersRepository`/
@@ -254,6 +290,23 @@ export class CollectionsRepository extends TrekRepository<Collections> {
await this.readDb().deleteFrom('collections').where('id', '=', id).execute();
}
/**
* `deleteCollection`: deletes a list like `deleteById` and hands back the uploads it
* named, its cover and its saved places' images, read before the cascade takes their
* rows, so they can go like a single place's do once nothing else names them (#2924).
*/
async deleteWithImages(id: number): Promise<Array<string | null>> {
const cover = (await this.coverImage(id)) ?? null;
const places = await this.placesReadDb()
.selectFrom('collection_places')
.select('image_url')
.where('collection_id', '=', id)
.where('image_url', 'is not', null)
.execute();
await this.deleteById(id);
return [cover, ...places.map((place) => place.image_url)];
}
/**
* CL36 (`reorderCollections`, TX `:712`) — `UPDATE collections SET
* sort_order=? WHERE id=?`, looped per visible id. The legacy statement
@@ -327,7 +380,9 @@ export class CollectionsRepository extends TrekRepository<Collections> {
* CL16/CL17 (`getCollection`/`exportCollection`'s dup-text pair, collapsed
* onto ONE repository method as the brief directs) — the same
* `cp.*`/category-alias select as {@link findPlaceRowById}, scoped by
* `cp.collection_id` and ordered by `cp.sort_order, cp.created_at`.
* `cp.collection_id` and ordered by the place's position: `sort_order`,
* `created_at`, then `id`, so two places saved in the same second keep one
* order, the one list_collection_places pages through (#2647).
*/
async listPlaceRows(collectionId: number): Promise<CollectionPlaceRow[]> {
return await this.placesReadDb()
@@ -365,8 +420,99 @@ export class CollectionsRepository extends TrekRepository<Collections> {
'c.icon as category_icon',
])
.where('cp.collection_id', '=', collectionId)
.orderBy('cp.sort_order')
.orderBy('cp.created_at')
.orderBy((eb) => eb.fn.coalesce('cp.sort_order', eb.val(0)))
.orderBy((eb) => eb.fn.coalesce('cp.created_at', eb.val('')))
.orderBy('cp.id')
.execute();
}
/** The places of a list the filter matches, before any paging. */
private filteredPlaces(collectionId: number, filter: CollectionPlacePageFilter) {
const { status, labelId, unlabeled } = filter;
let query = this.kysely<CollectionPlacesPageKyselyDB>()
.selectFrom('collection_places as cp')
.where('cp.collection_id', '=', collectionId);
if (status !== undefined) query = query.where('cp.status', '=', status);
if (labelId !== undefined) {
query = query.where((eb) =>
eb.exists(
eb
.selectFrom('collection_place_labels as cpl')
.select('cpl.label_id')
.whereRef('cpl.collection_place_id', '=', 'cp.id')
.where('cpl.label_id', '=', labelId),
),
);
}
if (unlabeled) {
query = query.where((eb) =>
eb.not(
eb.exists(
eb
.selectFrom('collection_place_labels as cpl')
.select('cpl.label_id')
.whereRef('cpl.collection_place_id', '=', 'cp.id'),
),
),
);
}
return query;
}
/** How many places of a list the filter matches (list_collection_places' total). */
async countPlacePage(collectionId: number, filter: CollectionPlacePageFilter): Promise<number> {
const row = await this.filteredPlaces(collectionId, filter)
.select((eb) => eb.fn.countAll<number>().as('n'))
.executeTakeFirstOrThrow();
return Number(row.n);
}
/**
* Up to `limit` places of a list the filter matches, in the list's order
* (listPlaceRows'), starting after the position `after` names: a keyset, so
* a place that stops matching between two pages moves none of the others.
*/
async listPlacePage(
collectionId: number,
filter: CollectionPlacePageFilter,
after: CollectionPlacePosition | null,
limit: number,
): Promise<CollectionPlacePageRow[]> {
let query = this.filteredPlaces(collectionId, filter);
if (after) {
query = query.where((eb) => {
const sortKey = eb.fn.coalesce('cp.sort_order', eb.val(0));
const createdKey = eb.fn.coalesce('cp.created_at', eb.val(''));
return eb.or([
eb(sortKey, '>', after.sortOrder),
eb.and([
eb(sortKey, '=', after.sortOrder),
eb.or([
eb(createdKey, '>', after.createdAt),
eb.and([eb(createdKey, '=', after.createdAt), eb('cp.id', '>', after.id)]),
]),
]),
]);
});
}
return await query
.select((eb) => [
'cp.id',
'cp.name',
'cp.status',
'cp.address',
'cp.lat',
'cp.lng',
'cp.google_place_id',
'cp.website',
'cp.phone',
eb.fn.coalesce('cp.sort_order', eb.val(0)).as('sort_key'),
eb.fn.coalesce('cp.created_at', eb.val('')).as('created_key'),
])
.orderBy('sort_key')
.orderBy('created_key')
.orderBy('cp.id')
.limit(limit)
.execute();
}
@@ -266,6 +266,8 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
accommodation_id: number | null;
/** MDT-02: one stage of a multi-day tour, by its starting stop's key; absent for the whole tour or a place. */
tour_stage_key?: string | null;
/** #3092: the visit's own stay; absent or null reads the place's. */
stay_minutes?: number | null;
}): Promise<number> {
return await this.insert({
day: input.day_id,
@@ -274,6 +276,7 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
notes: input.notes,
accommodation_id: input.accommodation_id,
tour_stage_key: input.tour_stage_key ?? null,
stay_minutes: input.stay_minutes ?? null,
});
}
@@ -424,6 +427,14 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
await this.nativeUpdate({ id }, { end_day });
}
/**
* `UPDATE day_assignments SET stay_minutes = ? WHERE id = ?` (#3092). Null hands the
* visit back to the stay its place carries.
*/
async setStayMinutes(id: number, stay_minutes: number | null): Promise<void> {
await this.nativeUpdate({ id }, { stay_minutes });
}
/** AS25 — `UPDATE day_assignments SET notes = ? WHERE id = ?`. */
async setNotes(id: number, notes: string | null): Promise<void> {
await this.nativeUpdate({ id }, { notes });
@@ -542,6 +553,8 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
end_day: number;
/** MDT-02: the copied tour keeps its stops' keys, so a day holding one of its stages keeps holding it. */
tour_stage_key?: string | null;
/** #3092: the visit's own stay goes with it. */
stay_minutes?: number | null;
}): Promise<number> {
return await this.insert({
day: input.day_id,
@@ -555,6 +568,7 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
assignment_end_time: input.assignment_end_time,
end_day: input.end_day,
tour_stage_key: input.tour_stage_key ?? null,
stay_minutes: input.stay_minutes ?? null,
});
}
@@ -637,7 +651,9 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
'p.lng as lng',
eb.fn.coalesce('a.assignment_time', 'p.place_time').as('time'),
eb.fn.coalesce('a.assignment_end_time', 'p.end_time').as('end_time'),
'p.duration_minutes as duration_minutes',
// A visit's own stay before the place's (#3092): the same place planned twice
// on a day can be stood at for two different lengths.
eb.fn.coalesce('a.stay_minutes', 'p.duration_minutes').as('duration_minutes'),
'a.end_day as end_day',
'a.leg_transport_mode as leg_transport_mode',
'a.incoming_leg_transport_mode as incoming_leg_transport_mode',
@@ -0,0 +1,210 @@
import type { FlightLiveStates } from '../entities/FlightLiveStates.entity';
import type { DB } from '../kysely/db';
import type { DbTimestamp } from '../types';
import { TrekRepository } from './_shared/trek-repository';
/** A flight booking as the live flight status reads it; the metadata is the stored text. */
export interface FlightBookingDbRow {
id: number;
trip_id: number;
trip_title: string;
metadata: string | null;
day_id: number | null;
end_day_id: number | null;
reservation_time: string | null;
reservation_end_time: string | null;
}
interface FlightEndpointDbRow {
reservation_id: number;
role: string;
sequence: number;
name: string;
code: string | null;
lat: number;
lng: number;
timezone: string | null;
local_date: string | null;
local_time: string | null;
}
/**
* One flight state, every column; timestamps in the canonical UTC text. `T`
* is `string` for a row read back and `DbTimestamp` for one being written.
*/
interface FlightLiveStateFields<T extends string> {
callsign: string;
service_date: string;
origin: string;
destination: string;
phase: string;
departed_at: T | null;
landed_at: T | null;
lat: number | null;
lng: number | null;
altitude_ft: number | null;
ground_speed_kt: number | null;
track_deg: number | null;
vertical_rate_fpm: number | null;
on_ground: number | null;
registration: string | null;
aircraft_type: string | null;
source: string | null;
first_seen_at: T | null;
last_seen_at: T | null;
fetched_at: T | null;
next_poll_at: T | null;
fail_count: number;
departed_notified: number;
landed_notified: number;
delay_notified: number;
}
export type FlightLiveStateRow = FlightLiveStateFields<string>;
export type FlightLiveStateWrite = FlightLiveStateFields<DbTimestamp>;
const BOOKING_COLUMNS = [
'reservations.id',
'reservations.trip_id',
'reservations.metadata',
'reservations.day_id',
'reservations.end_day_id',
'reservations.reservation_time',
'reservations.reservation_end_time',
] as const;
const ENDPOINT_COLUMNS = [
'reservation_id',
'role',
'sequence',
'name',
'code',
'lat',
'lng',
'timezone',
'local_date',
'local_time',
] as const;
/**
* `flight_live_states`: what the open ADS-B feeds last said about a flight on
* a day, one row per (callsign, service date, origin, destination) whichever
* trips booked it.
*
* The flight bookings, their endpoints, the days they name and their
* travellers are read here too, through Kysely: the live status only reads
* them, and the reservations domain has no read of flights across all trips.
*/
export class FlightLiveStatesRepository extends TrekRepository<FlightLiveStates> {
/** The flight bookings of one trip. */
async listFlightBookingsForTrip(trip_id: number): Promise<FlightBookingDbRow[]> {
return await this.kysely<Pick<DB, 'reservations' | 'trips'>>()
.selectFrom('reservations')
.innerJoin('trips', 'trips.id', 'reservations.trip_id')
.select([...BOOKING_COLUMNS, 'trips.title as trip_title'])
.where('reservations.trip_id', '=', trip_id)
.where('reservations.type', '=', 'flight')
.orderBy('reservations.id')
.execute();
}
/**
* The flight bookings of every trip that is not archived and whose dates
* (where it has any) reach into `from_date`..`to_date`: the candidates for a
* poll. Which leg is really in its window the caller works out from the
* schedule.
*/
async listFlightBookingsBetween(from_date: string, to_date: string): Promise<FlightBookingDbRow[]> {
return await this.kysely<Pick<DB, 'reservations' | 'trips'>>()
.selectFrom('reservations')
.innerJoin('trips', 'trips.id', 'reservations.trip_id')
.select([...BOOKING_COLUMNS, 'trips.title as trip_title'])
.where('reservations.type', '=', 'flight')
.where((eb) => eb.or([eb('trips.is_archived', 'is', null), eb('trips.is_archived', '=', 0)]))
.where((eb) => eb.or([eb('trips.end_date', 'is', null), eb('trips.end_date', '>=', from_date)]))
.where((eb) => eb.or([eb('trips.start_date', 'is', null), eb('trips.start_date', '<=', to_date)]))
.orderBy('reservations.id')
.execute();
}
/** The endpoints of the given bookings, in their order. */
async listEndpoints(reservation_ids: readonly number[]): Promise<FlightEndpointDbRow[]> {
if (reservation_ids.length === 0) return [];
return await this.kysely<Pick<DB, 'reservation_endpoints'>>()
.selectFrom('reservation_endpoints')
.select([...ENDPOINT_COLUMNS])
.where('reservation_id', 'in', [...reservation_ids])
.orderBy('reservation_id')
.orderBy('sequence')
.execute();
}
/** The date of each given trip day that has one. */
async listDayDates(day_ids: readonly number[]): Promise<Array<{ id: number; date: string }>> {
if (day_ids.length === 0) return [];
const rows = await this.kysely<Pick<DB, 'days'>>()
.selectFrom('days')
.select(['id', 'date'])
.where('id', 'in', [...day_ids])
.execute();
return rows.flatMap((row) => (row.date ? [{ id: row.id, date: row.date }] : []));
}
/**
* The users put on a booking as its travellers who still belong to its trip:
* the owner or a member. Someone removed from the trip stays on the
* traveller list until it is edited, and must hear nothing more of it.
*/
async listTravelerIds(reservation_id: number): Promise<number[]> {
const rows = await this.kysely<Pick<DB, 'reservation_travelers' | 'reservations' | 'trips' | 'trip_members'>>()
.selectFrom('reservation_travelers as rt')
.innerJoin('reservations as r', 'r.id', 'rt.reservation_id')
.innerJoin('trips as t', 't.id', 'r.trip_id')
.select('rt.user_id')
.where('rt.reservation_id', '=', reservation_id)
.where((eb) =>
eb.or([
eb('t.user_id', '=', eb.ref('rt.user_id')),
eb.exists(
eb
.selectFrom('trip_members as tm')
.select('tm.id')
.whereRef('tm.trip_id', '=', 'r.trip_id')
.whereRef('tm.user_id', '=', 'rt.user_id'),
),
]),
)
.orderBy('rt.user_id')
.execute();
return rows.map((row) => row.user_id);
}
/** Every stored state of the given callsigns, whatever their day. */
async findForCallsigns(callsigns: readonly string[]): Promise<FlightLiveStateRow[]> {
if (callsigns.length === 0) return [];
return await this.kysely<Pick<DB, 'flight_live_states'>>()
.selectFrom('flight_live_states')
.selectAll()
.where('callsign', 'in', [...callsigns])
.execute();
}
/** Writes a flight's whole state, replacing the one stored under its key. */
async saveState(row: FlightLiveStateWrite): Promise<void> {
const { callsign, service_date, origin, destination, ...changes } = row;
await this.kysely<Pick<DB, 'flight_live_states'>>()
.insertInto('flight_live_states')
.values({ callsign, service_date, origin, destination, ...changes })
.onConflict((oc) => oc.columns(['callsign', 'service_date', 'origin', 'destination']).doUpdateSet(changes))
.execute();
}
/** Drops the states of flights that departed before `service_date`; answers how many went. */
async deleteBefore(service_date: string): Promise<number> {
const result = await this.kysely<Pick<DB, 'flight_live_states'>>()
.deleteFrom('flight_live_states')
.where('service_date', '<', service_date)
.executeTakeFirst();
return Number(result.numDeletedRows);
}
}
@@ -52,6 +52,19 @@ type JourneyListKyselyDB = Pick<
/** The `journeys`/`journey_contributors` tables {@link JourneysRepository.listRecipientUserIds} reads. */
type JourneyRecipientsKyselyDB = Pick<DB, 'journeys' | 'journey_contributors'>;
/** The tables {@link JourneysRepository.listGalleryPhotos} and {@link JourneysRepository.deleteUnlinkedPhotos} touch. */
type JourneyPhotoFilesKyselyDB = Pick<DB, 'journeys' | 'journey_photos' | 'trek_photos' | 'trip_photos'>;
/** A photo behind a journey's gallery, with the stored paths its row names. */
export interface JourneyGalleryPhotoFiles {
photo_id: number;
file_path: string | null;
thumbnail_path: string | null;
}
/** Ids per DELETE, well under SQLite's bound-parameter limit. */
const PHOTO_DELETE_BATCH = 500;
/**
* `journeys` — the root of the journey cluster (Plan 3g Task 1, R9's Part A).
* Every method here is a Part-A-owned statement (JG1-JG29 group) or a shared
@@ -261,4 +274,49 @@ export class JourneysRepository extends TrekRepository<Journeys> {
async deleteOwnedByUser(userId: number): Promise<void> {
await this.nativeDelete({ user: userId });
}
/**
* The photos in the gallery of one journey, or of every journey a user owns,
* with the paths their `trek_photos` rows store. Read before the journey is
* deleted: the cascade takes the gallery rows, and with them the only way
* back to the photos.
*/
async listGalleryPhotos(scope: { journeyId: number } | { ownerId: number }): Promise<JourneyGalleryPhotoFiles[]> {
const [column, value] =
'journeyId' in scope ? (['j.id', scope.journeyId] as const) : (['j.user_id', scope.ownerId] as const);
return await this.kysely<JourneyPhotoFilesKyselyDB>()
.selectFrom('journey_photos as jp')
.innerJoin('journeys as j', 'j.id', 'jp.journey_id')
.innerJoin('trek_photos as tp', 'tp.id', 'jp.photo_id')
.select(['jp.photo_id', 'tp.file_path', 'tp.thumbnail_path'])
.distinct()
.where(column, '=', value)
.execute();
}
/**
* Deletes the `trek_photos` rows among `photoIds` that no trip and no journey
* links any more, after their journeys went: the same rule as
* `TrekPhotoRegistrationService.deleteIfOrphan` with `local`, for a whole
* gallery at once. A row left behind would keep naming its file, and the
* storage reclaim counts every row with a path as live (#2924). A photo still
* in another journey's gallery, or in a trip's, keeps its row. Answers how
* many rows went.
*/
async deleteUnlinkedPhotos(photoIds: readonly number[]): Promise<number> {
const pages = Array.from({ length: Math.ceil(photoIds.length / PHOTO_DELETE_BATCH) }, (_unused, i) =>
photoIds.slice(i * PHOTO_DELETE_BATCH, (i + 1) * PHOTO_DELETE_BATCH),
);
// One page after the other: they share the caller's transaction.
return await pages.reduce<Promise<number>>(async (previous, page) => {
const deleted = await previous;
const result = await this.kysely<JourneyPhotoFilesKyselyDB>()
.deleteFrom('trek_photos')
.where('id', 'in', page)
.where('id', 'not in', (eb) => eb.selectFrom('trip_photos').select('photo_id'))
.where('id', 'not in', (eb) => eb.selectFrom('journey_photos').select('photo_id'))
.executeTakeFirst();
return deleted + Number(result.numDeletedRows);
}, Promise.resolve(0));
}
}
@@ -0,0 +1,113 @@
import type { MailIngestAddresses } from '../entities/MailIngestAddresses.entity';
import type { DB } from '../kysely/db';
import type { DbTimestamp } from '../types';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** One `mail_ingest_addresses` row as stored. */
export interface MailIngestAddressRow {
id: number;
token_hash: string;
token_enc: string;
user_id: number;
trip_id: number | null;
created_at: string;
revoked_at: string | null;
last_used_at: string | null;
}
const _mailIngestAddressRowKeys: AssertRowKeys<MailIngestAddressRow, MailIngestAddresses> = true;
/** A new address: whose it is, for which trip (null for the person's own), and its token in both forms. */
interface MailIngestAddressInsert {
userId: number;
tripId: number | null;
tokenHash: string;
tokenEnc: string;
createdAt: DbTimestamp;
}
type AddressesKyselyDB = Pick<DB, 'mail_ingest_addresses'>;
/**
* `mail_ingest_addresses`: the forwarding addresses. A person has at most one
* live address of their own and one per trip; renewing one revokes the old row
* instead of deleting it, so a mail that still reaches the old token is
* recognised and refused rather than taken for a stranger's.
*/
export class MailIngestAddressesRepository extends TrekRepository<MailIngestAddresses> {
private db() {
return this.kysely<AddressesKyselyDB>();
}
async insertAddress(row: MailIngestAddressInsert): Promise<number> {
const inserted = await this.db()
.insertInto('mail_ingest_addresses')
.values({
user_id: row.userId,
trip_id: row.tripId,
token_hash: row.tokenHash,
token_enc: row.tokenEnc,
created_at: row.createdAt,
})
.returning('id')
.executeTakeFirstOrThrow();
return inserted.id;
}
/** The person's live address for the trip, or their own one with `tripId` null. */
async findLive(userId: number, tripId: number | null): Promise<MailIngestAddressRow | null> {
let query = this.db()
.selectFrom('mail_ingest_addresses')
.selectAll()
.where('user_id', '=', userId)
.where('revoked_at', 'is', null);
query = tripId === null ? query.where('trip_id', 'is', null) : query.where('trip_id', '=', tripId);
const row = await query.orderBy('id', 'desc').executeTakeFirst();
return row ?? null;
}
/** The address a token names, revoked or not. */
async findByTokenHash(tokenHash: string): Promise<MailIngestAddressRow | null> {
const row = await this.db()
.selectFrom('mail_ingest_addresses')
.selectAll()
.where('token_hash', '=', tokenHash)
.executeTakeFirst();
return row ?? null;
}
async findById(id: number): Promise<MailIngestAddressRow | null> {
const row = await this.db().selectFrom('mail_ingest_addresses').selectAll().where('id', '=', id).executeTakeFirst();
return row ?? null;
}
/** Revokes the person's live address for the trip (their own with `tripId` null); answers how many went. */
async revokeLive(userId: number, tripId: number | null, at: DbTimestamp): Promise<number> {
let query = this.db()
.updateTable('mail_ingest_addresses')
.set({ revoked_at: at })
.where('user_id', '=', userId)
.where('revoked_at', 'is', null);
query = tripId === null ? query.where('trip_id', 'is', null) : query.where('trip_id', '=', tripId);
const result = await query.executeTakeFirst();
return Number(result.numUpdatedRows);
}
/** The people who hold at least one address that is not revoked. */
async listHolders(): Promise<number[]> {
const rows = await this.db()
.selectFrom('mail_ingest_addresses')
.select('user_id')
.distinct()
.where('revoked_at', 'is', null)
.orderBy('user_id')
.execute();
return rows.map((row) => row.user_id);
}
/** Notes that a mail came in on the address. */
async touchUsed(id: number, at: DbTimestamp): Promise<void> {
await this.db().updateTable('mail_ingest_addresses').set({ last_used_at: at }).where('id', '=', id).execute();
}
}
@@ -0,0 +1,492 @@
import type { MailIngestCandidates } from '../entities/MailIngestCandidates.entity';
import type { DB } from '../kysely/db';
import type { DbTimestamp } from '../types';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
import type { Expression, SqlBool } from 'kysely';
/** One `mail_ingest_candidates` row as stored: the JSON columns as text. */
export interface MailIngestCandidateRow {
id: number;
message_id: number;
user_id: number;
position: number;
item_json: string | null;
source: string | null;
checks_json: string | null;
uncertain_fields: string | null;
match_json: string | null;
suggested_trip_id: number | null;
dedupe_key: string | null;
content_hash: string | null;
resolution: string;
reason: string | null;
resolved_trip_id: number | null;
created_ids_json: string | null;
attach_error: string | null;
resolved_by: number | null;
resolved_at: string | null;
undo_until: string | null;
created_at: string;
}
const _mailIngestCandidateRowKeys: AssertRowKeys<MailIngestCandidateRow, MailIngestCandidates> = true;
/** A booking read from a mail, as the pipeline writes it: waiting, or already decided in the same write. */
export interface MailIngestCandidateInsert {
messageId: number;
userId: number;
position: number;
itemJson: string | null;
source: string | null;
checksJson: string | null;
uncertainFields: string | null;
matchJson: string | null;
suggestedTripId: number | null;
dedupeKey: string | null;
contentHash: string | null;
resolution: string;
reason: string | null;
createdAt: DbTimestamp;
/** For an entry decided as it is written: the trip it went to, what it created, until when it can be undone. */
resolvedTripId?: number | null;
createdIdsJson?: string | null;
resolvedAt?: DbTimestamp | null;
undoUntil?: DbTimestamp | null;
}
/** What deciding an entry writes. */
interface MailIngestResolutionPatch {
resolution: string;
reason: string | null;
resolvedTripId: number | null;
createdIdsJson: string | null;
resolvedBy: number | null;
/** Null puts an entry back into the inbox. */
resolvedAt: DbTimestamp | null;
undoUntil: DbTimestamp | null;
}
/** A trip a booking from a mail may go to, as the import weighs it. */
export interface MailIngestTripRow {
id: number;
title: string;
start_date: string | null;
end_date: string | null;
/** The owner, for the permission check. */
user_id: number;
currency: string | null;
}
/** A place of a trip with a position, for naming its destinations and checking a booking lies near them. */
interface MailIngestTripPlaceRow {
trip_id: number;
name: string;
address: string | null;
lat: number;
lng: number;
}
/** A trip as the inbox names it to a person who can see it. */
interface MailIngestTripRefRow {
id: number;
title: string;
start_date: string | null;
end_date: string | null;
}
/** The person a mail is for, as the pipeline needs them. */
export interface MailIngestPersonRow {
id: number;
username: string;
email: string;
role: string;
}
/** What tells one booking from another already in a trip: its type, its start window and its reference. */
interface MailIngestSimilarProbe {
type: string;
startFrom: string | null;
startTo: string | null;
confirmation: string | null;
}
type CandidatesKyselyDB = Pick<
DB,
| 'mail_ingest_candidates'
| 'trips'
| 'trip_members'
| 'days'
| 'places'
| 'users'
| 'reservations'
| 'day_assignments'
| 'day_accommodations'
| 'budget_item_payers'
>;
/** The most places a trip lends its destinations and its geography to the import. */
const PLACES_PER_LOOKUP = 2000;
/**
* `mail_ingest_candidates`: the bookings read from forwarded mails, and the
* reads the import makes of the trips they may go to. Those reads touch the
* trips, days, places, users and reservations tables directly, by column, so
* the import weighs a booking without reaching into five other domains for
* one projection each; every write it makes to a trip goes through the
* owning services.
*/
export class MailIngestCandidatesRepository extends TrekRepository<MailIngestCandidates> {
private db() {
return this.kysely<CandidatesKyselyDB>();
}
async insertCandidate(row: MailIngestCandidateInsert): Promise<number> {
const inserted = await this.db()
.insertInto('mail_ingest_candidates')
.values({
message_id: row.messageId,
user_id: row.userId,
position: row.position,
item_json: row.itemJson,
source: row.source,
checks_json: row.checksJson,
uncertain_fields: row.uncertainFields,
match_json: row.matchJson,
suggested_trip_id: row.suggestedTripId,
dedupe_key: row.dedupeKey,
content_hash: row.contentHash,
resolution: row.resolution,
reason: row.reason,
created_at: row.createdAt,
resolved_trip_id: row.resolvedTripId ?? null,
created_ids_json: row.createdIdsJson ?? null,
resolved_at: row.resolvedAt ?? null,
undo_until: row.undoUntil ?? null,
})
.returning('id')
.executeTakeFirstOrThrow();
return inserted.id;
}
async findById(id: number): Promise<MailIngestCandidateRow | null> {
const row = await this.db()
.selectFrom('mail_ingest_candidates')
.selectAll()
.where('id', '=', id)
.executeTakeFirst();
return row ?? null;
}
/** The entries read from one mail, in the order the mail listed them. */
async listForMessage(messageId: number): Promise<MailIngestCandidateRow[]> {
return this.db()
.selectFrom('mail_ingest_candidates')
.selectAll()
.where('message_id', '=', messageId)
.orderBy('position', 'asc')
.orderBy('id', 'asc')
.execute();
}
/**
* Decides an entry, if it is still in one of the `from` states: one
* conditional update, so of two decisions at once only the first lands.
* With `held`, only while the entry still holds that trip and those
* created ids: a move keeps the resolution and changes only these.
*/
async resolve(
id: number,
from: readonly string[],
patch: MailIngestResolutionPatch,
held?: { tripId: number; createdIdsJson: string | null },
): Promise<boolean> {
let query = this.db()
.updateTable('mail_ingest_candidates')
.set({
resolution: patch.resolution,
reason: patch.reason,
resolved_trip_id: patch.resolvedTripId,
created_ids_json: patch.createdIdsJson,
resolved_by: patch.resolvedBy,
resolved_at: patch.resolvedAt,
undo_until: patch.undoUntil,
})
.where('id', '=', id)
.where('resolution', 'in', [...from]);
if (held) {
query = query.where('resolved_trip_id', '=', held.tripId);
query =
held.createdIdsJson === null
? query.where('created_ids_json', 'is', null)
: query.where('created_ids_json', '=', held.createdIdsJson);
}
const result = await query.executeTakeFirst();
return Number(result.numUpdatedRows) > 0;
}
/** Records the documents attached after the booking was written, or why that failed. */
async setAttachments(id: number, createdIdsJson: string, attachError: string | null): Promise<void> {
await this.db()
.updateTable('mail_ingest_candidates')
.set({ created_ids_json: createdIdsJson, attach_error: attachError })
.where('id', '=', id)
.execute();
}
/** The person's entries waiting in the inbox. */
async countPending(userId: number): Promise<number> {
const row = await this.db()
.selectFrom('mail_ingest_candidates')
.select((eb) => eb.fn.countAll<number>().as('n'))
.where('user_id', '=', userId)
.where('resolution', '=', 'pending')
.executeTakeFirstOrThrow();
return Number(row.n);
}
/**
* The person's entries, waiting (`open`) or decided, newest first; with
* `beforeId` only those older than it, so a page follows on from the last.
*/
async listForUser(
userId: number,
open: boolean,
beforeId: number | null,
limit: number,
): Promise<MailIngestCandidateRow[]> {
let query = this.db()
.selectFrom('mail_ingest_candidates')
.selectAll()
.where('user_id', '=', userId)
.where('resolution', open ? '=' : '<>', 'pending');
if (beforeId !== null) query = query.where('id', '<', beforeId);
return query.orderBy('id', 'desc').limit(limit).execute();
}
/** The entries made since `since`, counted by what became of them. */
async tallyResolutionsSince(since: DbTimestamp): Promise<Array<{ resolution: string; n: number }>> {
const rows = await this.db()
.selectFrom('mail_ingest_candidates')
.select((eb) => ['resolution', eb.fn.countAll<number>().as('n')])
.where('created_at', '>=', since)
.groupBy('resolution')
.execute();
return rows.map((row) => ({ resolution: row.resolution, n: Number(row.n) }));
}
/** How many entries wait for each person who has any, the most first; who they are is left out. */
async pendingPerUser(): Promise<number[]> {
const rows = await this.db()
.selectFrom('mail_ingest_candidates')
.select((eb) => ['user_id', eb.fn.countAll<number>().as('n')])
.where('resolution', '=', 'pending')
.groupBy('user_id')
.execute();
return rows.map((row) => Number(row.n)).sort((a, b) => b - a);
}
/** Whether a booking of this content went into the trip from a mail and was undone: never added again on its own. */
async wasUndoneInTrip(tripId: number, contentHash: string): Promise<boolean> {
const row = await this.db()
.selectFrom('mail_ingest_candidates')
.select('id')
.where('resolved_trip_id', '=', tripId)
.where('content_hash', '=', contentHash)
.where('resolution', '=', 'undone')
.executeTakeFirst();
return row !== undefined;
}
/** Retention: deletes the entries decided before `cutoff`; answers how many went. */
async deleteResolvedBefore(cutoff: DbTimestamp): Promise<number> {
const result = await this.db()
.deleteFrom('mail_ingest_candidates')
.where('resolution', '!=', 'pending')
.where('resolved_at', '<', cutoff)
.executeTakeFirst();
return Number(result.numDeletedRows);
}
// ── What the import reads of the trips ────────────────────────────────────
/**
* The trips the person owns or is a member of, not archived, that end on or
* after `endCutoff` or have no end date, most recent start first. Whether
* the person may add bookings to each is the service's check.
*/
async listOpenTrips(userId: number, endCutoff: string, limit: number): Promise<MailIngestTripRow[]> {
return this.db()
.selectFrom('trips as t')
.select(['t.id', 't.title', 't.start_date', 't.end_date', 't.user_id', 't.currency'])
.where((eb) =>
eb.or([
eb('t.user_id', '=', userId),
eb.exists(
eb
.selectFrom('trip_members as m')
.select('m.id')
.whereRef('m.trip_id', '=', 't.id')
.where('m.user_id', '=', userId),
),
]),
)
.where((eb) => eb.or([eb('t.is_archived', 'is', null), eb('t.is_archived', '=', 0)]))
.where((eb) => eb.or([eb('t.end_date', 'is', null), eb('t.end_date', '>=', endCutoff)]))
.orderBy('t.start_date', 'desc')
.orderBy('t.id', 'desc')
.limit(limit)
.execute();
}
/** One trip the person owns or is a member of and that is not archived, whatever its dates; null otherwise. */
async findOpenTrip(userId: number, tripId: number): Promise<MailIngestTripRow | null> {
const row = await this.db()
.selectFrom('trips as t')
.select(['t.id', 't.title', 't.start_date', 't.end_date', 't.user_id', 't.currency'])
.where('t.id', '=', tripId)
.where((eb) =>
eb.or([
eb('t.user_id', '=', userId),
eb.exists(
eb
.selectFrom('trip_members as m')
.select('m.id')
.whereRef('m.trip_id', '=', 't.id')
.where('m.user_id', '=', userId),
),
]),
)
.where((eb) => eb.or([eb('t.is_archived', 'is', null), eb('t.is_archived', '=', 0)]))
.executeTakeFirst();
return row ?? null;
}
/**
* The trips among `tripIds` the person owns or is a member of, archived or
* not: a suggestion names only a trip its reader can see.
*/
async listTripRefs(userId: number, tripIds: readonly number[]): Promise<MailIngestTripRefRow[]> {
if (tripIds.length === 0) return [];
return this.db()
.selectFrom('trips as t')
.select(['t.id', 't.title', 't.start_date', 't.end_date'])
.where('t.id', 'in', [...tripIds])
.where((eb) =>
eb.or([
eb('t.user_id', '=', userId),
eb.exists(
eb
.selectFrom('trip_members as m')
.select('m.id')
.whereRef('m.trip_id', '=', 't.id')
.where('m.user_id', '=', userId),
),
]),
)
.execute();
}
/** The dated days of these trips, by date. */
async listDatedDays(tripIds: readonly number[]): Promise<Array<{ id: number; trip_id: number; date: string }>> {
if (tripIds.length === 0) return [];
const rows = await this.db()
.selectFrom('days')
.select(['id', 'trip_id', 'date'])
.where('trip_id', 'in', [...tripIds])
.where('date', 'is not', null)
.orderBy('trip_id', 'asc')
.orderBy('date', 'asc')
.execute();
return rows.flatMap((row) => (row.date === null ? [] : [{ id: row.id, trip_id: row.trip_id, date: row.date }]));
}
/** The places of these trips that have a position, oldest first. */
async listPlacesWithPosition(tripIds: readonly number[]): Promise<MailIngestTripPlaceRow[]> {
if (tripIds.length === 0) return [];
const rows = await this.db()
.selectFrom('places')
.select(['trip_id', 'name', 'address', 'lat', 'lng'])
.where('trip_id', 'in', [...tripIds])
.where('lat', 'is not', null)
.where('lng', 'is not', null)
.orderBy('id', 'asc')
.limit(PLACES_PER_LOOKUP)
.execute();
return rows.flatMap((row) =>
row.lat === null || row.lng === null ? [] : [{ ...row, lat: row.lat, lng: row.lng }],
);
}
async findPerson(userId: number): Promise<MailIngestPersonRow | null> {
const row = await this.db()
.selectFrom('users')
.select(['id', 'username', 'email', 'role'])
.where('id', '=', userId)
.executeTakeFirst();
return row ?? null;
}
/** Whether the trip holds a booking an import stored under one of these sources and this key. */
async tripHasExternal(tripId: number, sources: readonly string[], externalId: string): Promise<boolean> {
const row = await this.db()
.selectFrom('reservations')
.select('id')
.where('trip_id', '=', tripId)
.where('external_source', 'in', [...sources])
.where('external_id', '=', externalId)
.executeTakeFirst();
return row !== undefined;
}
/**
* Whether the trip holds a booking that looks like this one: the same
* reference, or the same type starting in the same window.
*/
async tripHasSimilar(tripId: number, probe: MailIngestSimilarProbe): Promise<boolean> {
const { type, startFrom, startTo, confirmation } = probe;
const row = await this.db()
.selectFrom('reservations')
.select('id')
.where('trip_id', '=', tripId)
.where((eb) => {
const either: Expression<SqlBool>[] = [];
if (confirmation) either.push(eb('confirmation_number', '=', confirmation));
if (startFrom && startTo) {
either.push(
eb.and([
eb('type', '=', type),
eb('reservation_time', '>=', startFrom),
eb('reservation_time', '<=', startTo),
]),
);
}
// An empty list reads as false: a probe with neither names nothing.
return eb.or(either);
})
.executeTakeFirst();
return row !== undefined;
}
/** Whether a booking, a day plan entry or a stay still names the place. */
async placeInUse(placeId: number): Promise<boolean> {
const [booking, planned, stay] = await Promise.all([
this.db().selectFrom('reservations').select('id').where('place_id', '=', placeId).executeTakeFirst(),
this.db().selectFrom('day_assignments').select('id').where('place_id', '=', placeId).executeTakeFirst(),
this.db().selectFrom('day_accommodations').select('id').where('place_id', '=', placeId).executeTakeFirst(),
]);
return booking !== undefined || planned !== undefined || stay !== undefined;
}
/** How many payers the costs name: a cost someone settled with moves balances. */
async countPayers(budgetItemIds: readonly number[]): Promise<number> {
if (budgetItemIds.length === 0) return 0;
const row = await this.db()
.selectFrom('budget_item_payers')
.select((eb) => eb.fn.countAll<number>().as('n'))
.where('budget_item_id', 'in', [...budgetItemIds])
.executeTakeFirstOrThrow();
return Number(row.n);
}
}
@@ -0,0 +1,136 @@
import type { MailIngestCursor } from '../entities/MailIngestCursor.entity';
import type { DB } from '../kysely/db';
import type { DbTimestamp } from '../types';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/**
* What reads mail: an IMAP box, the TREK Cloud relay, or the processing
* pipeline itself; and the pause notice, whose good run is the pause it
* announced.
*/
type MailIngestCursorTransport = 'imap' | 'cloud' | 'pipeline' | 'notice';
/** One `mail_ingest_cursor` row as stored. */
interface MailIngestCursorRow {
id: number;
transport: string;
mailbox: string;
uid_validity: number | null;
last_uid: number | null;
last_ok_at: string | null;
last_error: string | null;
lease_owner: string | null;
lease_until: string | null;
}
const _mailIngestCursorRowKeys: AssertRowKeys<MailIngestCursorRow, MailIngestCursor> = true;
/** A reader's position after a good run; an IMAP reader names its box's uid validity and the last uid it took. */
interface MailIngestCursorPosition {
uidValidity?: number | null;
lastUid?: number | null;
}
type CursorKyselyDB = Pick<DB, 'mail_ingest_cursor'>;
/**
* `mail_ingest_cursor`: where each reader stands and who holds it. The lease
* is one conditional update, so of two processes on one database exactly one
* wins; a lease whose holder died runs out and the next run takes it.
*/
export class MailIngestCursorRepository extends TrekRepository<MailIngestCursor> {
private db() {
return this.kysely<CursorKyselyDB>();
}
/**
* Takes the reader's lease until `until` when it is free, run out at `now`,
* or already this owner's. The row is created on first use.
*
* @txIndependent the row is provisioned idempotently first; the lease itself
* is the one conditional update that decides the race.
*/
async acquireLease(
transport: MailIngestCursorTransport,
mailbox: string,
owner: string,
until: DbTimestamp,
now: DbTimestamp,
): Promise<boolean> {
await this.db()
.insertInto('mail_ingest_cursor')
.values({ transport, mailbox })
.onConflict((oc) => oc.columns(['transport', 'mailbox']).doNothing())
.execute();
const result = await this.db()
.updateTable('mail_ingest_cursor')
.set({ lease_owner: owner, lease_until: until })
.where('transport', '=', transport)
.where('mailbox', '=', mailbox)
.where((eb) => eb.or([eb('lease_until', 'is', null), eb('lease_until', '<', now), eb('lease_owner', '=', owner)]))
.executeTakeFirst();
return Number(result.numUpdatedRows) > 0;
}
/** Gives the lease back, if this owner still holds it. */
async releaseLease(transport: MailIngestCursorTransport, mailbox: string, owner: string): Promise<void> {
await this.db()
.updateTable('mail_ingest_cursor')
.set({ lease_owner: null, lease_until: null })
.where('transport', '=', transport)
.where('mailbox', '=', mailbox)
.where('lease_owner', '=', owner)
.execute();
}
/** A good run: its time, the reader's position where it has one, and no error. */
async recordOk(
transport: MailIngestCursorTransport,
mailbox: string,
at: DbTimestamp,
position: MailIngestCursorPosition = {},
): Promise<void> {
await this.db()
.updateTable('mail_ingest_cursor')
.set({
last_ok_at: at,
last_error: null,
...(position.uidValidity !== undefined && { uid_validity: position.uidValidity }),
...(position.lastUid !== undefined && { last_uid: position.lastUid }),
})
.where('transport', '=', transport)
.where('mailbox', '=', mailbox)
.execute();
}
/** Forgets the last good run, as if the reader had never run. */
async forgetOk(transport: MailIngestCursorTransport, mailbox: string): Promise<void> {
await this.db()
.updateTable('mail_ingest_cursor')
.set({ last_ok_at: null })
.where('transport', '=', transport)
.where('mailbox', '=', mailbox)
.execute();
}
/** A failed run, for the admin's status card; the position stays where the last good run left it. */
async recordError(transport: MailIngestCursorTransport, mailbox: string, error: string): Promise<void> {
await this.db()
.updateTable('mail_ingest_cursor')
.set({ last_error: error.slice(0, 500) })
.where('transport', '=', transport)
.where('mailbox', '=', mailbox)
.execute();
}
async findCursor(transport: MailIngestCursorTransport, mailbox: string): Promise<MailIngestCursorRow | null> {
const row = await this.db()
.selectFrom('mail_ingest_cursor')
.selectAll()
.where('transport', '=', transport)
.where('mailbox', '=', mailbox)
.executeTakeFirst();
return row ?? null;
}
}
@@ -0,0 +1,321 @@
import type { MailIngestMessages } from '../entities/MailIngestMessages.entity';
import type { DB } from '../kysely/db';
import type { DbTimestamp } from '../types';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** One `mail_ingest_messages` row as stored, the encrypted mail included. */
export interface MailIngestMessageRow {
id: number;
transport: string;
message_id_hdr: string | null;
dedupe_hash: string;
address_id: number | null;
user_id: number;
trip_id: number | null;
from_addr: string | null;
from_verified: number;
auth_results: string | null;
subject: string | null;
received_at: string;
size: number;
raw_eml_enc: string | null;
raw_purged_at: string | null;
status: string;
reason: string | null;
hints: string | null;
attachments: string | null;
forwarding_code: string | null;
attempts: number;
next_attempt_at: string;
lease_until: string | null;
ai_used: number;
processed_at: string | null;
}
const _mailIngestMessageRowKeys: AssertRowKeys<MailIngestMessageRow, MailIngestMessages> = true;
/** A mail as the inbox lists it: everything but the stored mail and its sender's signature data. */
export type MailIngestMessageHead = Omit<MailIngestMessageRow, 'raw_eml_enc' | 'auth_results'>;
/** A mail as a transport hands it in, before anything has read it. */
interface MailIngestMessageInsert {
transport: string;
messageIdHdr: string | null;
dedupeHash: string;
addressId: number;
userId: number;
tripId: number | null;
size: number;
rawEnc: string;
receivedAt: DbTimestamp;
}
/** What the pipeline made of a mail, written once it is done with it. */
export interface MailIngestMessageOutcome {
status: 'processed' | 'discarded' | 'failed';
reason: string | null;
subject: string | null;
fromAddr: string | null;
fromVerified: boolean;
authResults: string | null;
hints: string | null;
attachments: string | null;
forwardingCode: string | null;
aiUsed: boolean;
processedAt: DbTimestamp;
}
type MessagesKyselyDB = Pick<DB, 'mail_ingest_messages' | 'mail_ingest_candidates'>;
/**
* `mail_ingest_messages`: every forwarded mail, from its arrival to its
* purge. A transport only ever inserts (idempotently, by `dedupe_hash`); the
* pipeline claims due rows one by one with a conditional update, so a mail is
* read by one run at a time, and a run that died lets its claim run out.
*/
export class MailIngestMessagesRepository extends TrekRepository<MailIngestMessages> {
private db() {
return this.kysely<MessagesKyselyDB>();
}
/** Stores a new mail, queued for now; null when the same mail is in already. */
async insertIfNew(row: MailIngestMessageInsert): Promise<number | null> {
const inserted = await this.db()
.insertInto('mail_ingest_messages')
.values({
transport: row.transport,
message_id_hdr: row.messageIdHdr,
dedupe_hash: row.dedupeHash,
address_id: row.addressId,
user_id: row.userId,
trip_id: row.tripId,
size: row.size,
raw_eml_enc: row.rawEnc,
received_at: row.receivedAt,
next_attempt_at: row.receivedAt,
})
.onConflict((oc) => oc.column('dedupe_hash').doNothing())
.returning('id')
.executeTakeFirst();
return inserted?.id ?? null;
}
async findIdByDedupeHash(dedupeHash: string): Promise<number | null> {
const row = await this.db()
.selectFrom('mail_ingest_messages')
.select('id')
.where('dedupe_hash', '=', dedupeHash)
.executeTakeFirst();
return row?.id ?? null;
}
async findById(id: number): Promise<MailIngestMessageRow | null> {
const row = await this.db().selectFrom('mail_ingest_messages').selectAll().where('id', '=', id).executeTakeFirst();
return row ?? null;
}
/** These mails without their content, for the inbox rows that name them. */
async listHeads(ids: readonly number[]): Promise<MailIngestMessageHead[]> {
if (ids.length === 0) return [];
return this.db()
.selectFrom('mail_ingest_messages')
.select([
'id',
'transport',
'message_id_hdr',
'dedupe_hash',
'address_id',
'user_id',
'trip_id',
'from_addr',
'from_verified',
'subject',
'received_at',
'size',
'raw_purged_at',
'status',
'reason',
'hints',
'attachments',
'forwarding_code',
'attempts',
'next_attempt_at',
'lease_until',
'ai_used',
'processed_at',
])
.where('id', 'in', [...ids])
.execute();
}
/** The mails that came in since `since`, counted by status and reason, for the admin's status card. */
async tallySince(since: DbTimestamp): Promise<Array<{ status: string; reason: string | null; n: number }>> {
const rows = await this.db()
.selectFrom('mail_ingest_messages')
.select((eb) => ['status', 'reason', eb.fn.countAll<number>().as('n')])
.where('received_at', '>=', since)
.groupBy(['status', 'reason'])
.execute();
return rows.map((row) => ({ status: row.status, reason: row.reason, n: Number(row.n) }));
}
/** How many mails, of everyone, an AI read since `since`. */
async countAllAiReadSince(since: DbTimestamp): Promise<number> {
const row = await this.db()
.selectFrom('mail_ingest_messages')
.select((eb) => eb.fn.countAll<number>().as('n'))
.where('ai_used', '=', 1)
.where('received_at', '>=', since)
.executeTakeFirstOrThrow();
return Number(row.n);
}
/** How many mails came in on the address since `since`, for its daily limit. */
async countForAddressSince(addressId: number, since: DbTimestamp): Promise<number> {
const row = await this.db()
.selectFrom('mail_ingest_messages')
.select((eb) => eb.fn.countAll<number>().as('n'))
.where('address_id', '=', addressId)
.where('received_at', '>=', since)
.executeTakeFirstOrThrow();
return Number(row.n);
}
/** How many of the person's mails an AI read since `since`, for their daily limit. */
async countAiReadSince(userId: number, since: DbTimestamp): Promise<number> {
const row = await this.db()
.selectFrom('mail_ingest_messages')
.select((eb) => eb.fn.countAll<number>().as('n'))
.where('user_id', '=', userId)
.where('ai_used', '=', 1)
.where('received_at', '>=', since)
.executeTakeFirstOrThrow();
return Number(row.n);
}
/**
* Claims up to `limit` mails that are due: queued and past their next try,
* or claimed by a run whose lease ran out. Each claim is one conditional
* update, so a mail another run took in between is skipped, never read twice.
*
* @txIndependent one claim per mail on purpose: each is complete on its own,
* and a mail claimed stays claimed even when the next one is taken first by
* another run.
*/
async claimDue(now: DbTimestamp, leaseUntil: DbTimestamp, limit: number): Promise<number[]> {
const due = await this.db()
.selectFrom('mail_ingest_messages')
.select('id')
.where((eb) =>
eb.or([
eb.and([eb('status', '=', 'queued'), eb('next_attempt_at', '<=', now)]),
eb.and([eb('status', '=', 'processing'), eb('lease_until', '<', now)]),
]),
)
.orderBy('next_attempt_at', 'asc')
.orderBy('id', 'asc')
.limit(limit)
.execute();
const won = await Promise.all(due.map(({ id }) => this.claim(id, now, leaseUntil)));
return due.flatMap(({ id }, i) => (won[i] ? [id] : []));
}
/** One mail's claim: it is still queued, or its last claim ran out at `now`. */
private async claim(id: number, now: DbTimestamp, leaseUntil: DbTimestamp): Promise<boolean> {
const result = await this.db()
.updateTable('mail_ingest_messages')
.set((eb) => ({ status: 'processing', lease_until: leaseUntil, attempts: eb('attempts', '+', 1) }))
.where('id', '=', id)
.where((eb) =>
eb.or([eb('status', '=', 'queued'), eb.and([eb('status', '=', 'processing'), eb('lease_until', '<', now)])]),
)
.executeTakeFirst();
return Number(result.numUpdatedRows) > 0;
}
/** Writes what the pipeline made of the mail and lets go of its claim. */
async finish(id: number, outcome: MailIngestMessageOutcome): Promise<void> {
await this.db()
.updateTable('mail_ingest_messages')
.set({
status: outcome.status,
reason: outcome.reason,
subject: outcome.subject,
from_addr: outcome.fromAddr,
from_verified: outcome.fromVerified ? 1 : 0,
auth_results: outcome.authResults,
hints: outcome.hints,
attachments: outcome.attachments,
forwarding_code: outcome.forwardingCode,
ai_used: outcome.aiUsed ? 1 : 0,
processed_at: outcome.processedAt,
lease_until: null,
})
.where('id', '=', id)
.execute();
}
/** Puts a mail whose reading failed back in the queue, to be tried again at `nextAttemptAt`. */
async retryLater(id: number, nextAttemptAt: DbTimestamp): Promise<void> {
await this.db()
.updateTable('mail_ingest_messages')
.set({ status: 'queued', next_attempt_at: nextAttemptAt, lease_until: null })
.where('id', '=', id)
.execute();
}
/**
* Drops the stored mail of every message that no longer needs it: one that
* came in before `hardCutoff`, or one done before `doneCutoff` that has no
* entry left waiting in the inbox (the ways out read the mail). Answers how
* many were purged.
*/
async purgeRaw(doneCutoff: DbTimestamp, hardCutoff: DbTimestamp, at: DbTimestamp): Promise<number> {
const result = await this.db()
.updateTable('mail_ingest_messages')
.set({ raw_eml_enc: null, raw_purged_at: at })
.where('raw_eml_enc', 'is not', null)
.where((eb) =>
eb.or([
eb('received_at', '<', hardCutoff),
eb.and([
eb('status', 'in', ['processed', 'discarded', 'failed']),
eb('processed_at', '<', doneCutoff),
eb.not(
eb.exists(
eb
.selectFrom('mail_ingest_candidates as c')
.select('c.id')
.whereRef('c.message_id', '=', 'mail_ingest_messages.id')
.where('c.resolution', '=', 'pending'),
),
),
]),
]),
)
.executeTakeFirst();
return Number(result.numUpdatedRows);
}
/** Deletes the mails that came in before `cutoff` and have no inbox entry left; answers how many went. */
async deleteSettledBefore(cutoff: DbTimestamp): Promise<number> {
const result = await this.db()
.deleteFrom('mail_ingest_messages')
.where('received_at', '<', cutoff)
.where('status', '!=', 'queued')
.where('status', '!=', 'processing')
.where((eb) =>
eb.not(
eb.exists(
eb
.selectFrom('mail_ingest_candidates as c')
.select('c.id')
.whereRef('c.message_id', '=', 'mail_ingest_messages.id'),
),
),
)
.executeTakeFirst();
return Number(result.numDeletedRows);
}
}
@@ -0,0 +1,50 @@
import type { MailIngestPrefs } from '../entities/MailIngestPrefs.entity';
import type { DB } from '../kysely/db';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** One `mail_ingest_prefs` row as stored: 0/1 flags. */
interface MailIngestPrefsRow {
id: number;
user_id: number;
enabled: number;
ai_enabled: number;
auto_add: number;
}
const _mailIngestPrefsRowKeys: AssertRowKeys<MailIngestPrefsRow, MailIngestPrefs> = true;
/** The switches a write names; the others keep their value. */
export type MailIngestPrefsPatch = Partial<Pick<MailIngestPrefsRow, 'enabled' | 'ai_enabled' | 'auto_add'>>;
type PrefsKyselyDB = Pick<DB, 'mail_ingest_prefs'>;
/**
* `mail_ingest_prefs`: a person's switches for the mail import. A person
* without a row has every switch on, the column defaults, so only a change
* writes one.
*/
export class MailIngestPrefsRepository extends TrekRepository<MailIngestPrefs> {
private db() {
return this.kysely<PrefsKyselyDB>();
}
async findForUser(userId: number): Promise<MailIngestPrefsRow | null> {
const row = await this.db()
.selectFrom('mail_ingest_prefs')
.selectAll()
.where('user_id', '=', userId)
.executeTakeFirst();
return row ?? null;
}
/** Writes the named switches, creating the person's row with the defaults for the rest. */
async upsertForUser(userId: number, patch: MailIngestPrefsPatch): Promise<void> {
if (Object.keys(patch).length === 0) return;
await this.db()
.insertInto('mail_ingest_prefs')
.values({ user_id: userId, ...patch })
.onConflict((oc) => oc.column('user_id').doUpdateSet(patch))
.execute();
}
}
@@ -407,6 +407,12 @@ export class PackingItemsRepository extends TrekRepository<PackingItems> {
await this.nativeUpdate({ id }, { is_private, ...data, updated_at: currentTimestamp(platform) });
}
/** `removeContributor` on a Common item's owner (#3098): the item stays on the list, nobody's in particular. */
async clearOwner(id: number): Promise<void> {
const platform = this.getEntityManager().getPlatform();
await this.nativeUpdate({ id }, { owner: null, updated_at: currentTimestamp(platform) });
}
/** PK16 (`setItemSharing`'s replace-all) — `DELETE FROM packing_item_recipients WHERE item_id = ?`. */
async deleteRecipientsForItem(item_id: number): Promise<void> {
await this.db().deleteFrom('packing_item_recipients').where('item_id', '=', item_id).execute();
@@ -19,6 +19,11 @@ interface PlaceCountryRow {
country_code: string | null;
}
/** A place of a started trip, with the trip's last day (its end, or its start when it has none) for ordering by recency. */
export interface StartedTripPlaceCountryRow extends PlaceCountryRow {
trip_last_date: string | null;
}
type PlaceRegionsKyselyDB = Pick<DB, 'place_regions' | 'places' | 'trips' | 'trip_members'>;
/**
@@ -192,22 +197,32 @@ export class PlaceRegionsRepository extends TrekRepository<PlaceRegions> {
/**
* AT44 (`getTravelStats`) — `SELECT DISTINCT p.id, p.lat, p.lng, p.address,
* pr.country_code FROM places p JOIN trips t ON p.trip_id = t.id LEFT JOIN
* pr.country_code, COALESCE(t.end_date, t.start_date) AS trip_last_date
* FROM places p JOIN trips t ON p.trip_id = t.id LEFT JOIN
* trip_members tm ON t.id = tm.trip_id LEFT JOIN place_regions pr ON
* pr.place_id = p.id WHERE (t.user_id = ? OR tm.user_id = ?) AND
* COALESCE(t.start_date, t.end_date) IS NOT NULL AND COALESCE(t.start_date,
* t.end_date) <= ? ORDER BY p.id` — "only started trips count" (#1048).
* Every place comes back, a place without a cached region with a null
* country (#2875). `today` (UTC) is resolved by the caller once
* (`todayUtc()`) and bound here.
* (`todayUtc()`) and bound here. A place sits on one trip, so the trip's
* last day adds no row to the DISTINCT; the dashboard orders the countries
* by it (#2648).
*/
async listStartedTripPlaceCountriesForUser(userId: number, today: string): Promise<PlaceCountryRow[]> {
async listStartedTripPlaceCountriesForUser(userId: number, today: string): Promise<StartedTripPlaceCountryRow[]> {
return await this.db()
.selectFrom('places as p')
.innerJoin('trips as t', 't.id', 'p.trip_id')
.leftJoin('trip_members as tm', 'tm.trip_id', 't.id')
.leftJoin('place_regions as pr', 'pr.place_id', 'p.id')
.select(['p.id', 'p.lat', 'p.lng', 'p.address', 'pr.country_code'])
.select((eb) => [
'p.id',
'p.lat',
'p.lng',
'p.address',
'pr.country_code',
eb.fn.coalesce('t.end_date', 't.start_date').as('trip_last_date'),
])
.distinct()
.where((eb) => eb.or([eb('t.user_id', '=', userId), eb('tm.user_id', '=', userId)]))
.where((eb) => eb(eb.fn.coalesce('t.start_date', 't.end_date'), 'is not', null))
@@ -201,9 +201,12 @@ export class ReservationEndpointsRepository extends TrekRepository<ReservationEn
* = ?) AND e.role IN ('from', 'to') AND COALESCE(t.start_date, t.end_date)
* IS NOT NULL AND COALESCE(t.start_date, t.end_date) <= date('now') AND
* ${TRAVELER_OWNS}`. `today` resolved by the caller once (`todayUtc()`),
* the `TripsRepository.activeTrip`/`lastStartedTrip` precedent.
* the `TripsRepository.activeTrip`/`lastStartedTrip` precedent. The row also
* carries `COALESCE(t.end_date, t.start_date) AS trip_last_date`, by which
* the dashboard orders the countries (#2648); an endpoint sits on one trip,
* so it adds no row to the DISTINCT.
*/
async listOwnedEndpointsForUser(userId: number, today: string): Promise<TravelerOwnedEndpointRow[]> {
async listOwnedEndpointsForUser(userId: number, today: string): Promise<TravelerOwnedEndpointWithTripDateRow[]> {
const rows = await this.kysely<TravelerOwnedEndpointsWithTripsKyselyDB>()
.selectFrom('reservation_endpoints as e')
.innerJoin('reservations as r', 'r.id', 'e.reservation_id')
@@ -228,6 +231,7 @@ export class ReservationEndpointsRepository extends TrekRepository<ReservationEn
.else(eb.fn.coalesce('r.reservation_time', 'r.reservation_end_time'))
.end()
.as('fallback_time'),
eb.fn.coalesce('t.end_date', 't.start_date').as('trip_last_date'),
])
.distinct()
.where((eb) => eb.or([eb('t.user_id', '=', userId), eb('tm.user_id', '=', userId)]))
@@ -236,7 +240,7 @@ export class ReservationEndpointsRepository extends TrekRepository<ReservationEn
.where((eb) => eb(eb.fn.coalesce('t.start_date', 't.end_date'), '<=', today))
.where((eb) => travelerOwnsExpr(eb, userId))
.execute();
return rows as TravelerOwnedEndpointRow[];
return rows as TravelerOwnedEndpointWithTripDateRow[];
}
/**
@@ -318,6 +322,11 @@ export interface TravelerOwnedEndpointRow {
fallback_time: string | null;
}
/** AT45's projection: AT6's, plus the last day of the trip the endpoint is on. */
export interface TravelerOwnedEndpointWithTripDateRow extends TravelerOwnedEndpointRow {
trip_last_date: string | null;
}
/** {@link ReservationEndpointsRepository.listOwnedEndpointsForTrips}'s `reservation_endpoints`/`reservations`/`reservation_travelers` tables (`r` aliased for `travelerOwnsExpr`, R7). */
type TravelerOwnedEndpointsKyselyDB = ReservationTravelersOwnsKyselyDB &
Pick<DB, 'reservation_endpoints' | 'reservations'>;
@@ -6,8 +6,10 @@ import { concatKysely, startsWithIsoDateKysely, substringKysely } from '../diale
import { accommodationIdMatches } from './_shared/reservation-accommodation';
import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility';
import type { DayAssignmentRow } from './day-assignment-rows';
import type { ReservationAllColumnsRow, ReservationJoinKyselyDB, ReservationJoinRow } from './reservation-rows';
import { TrekRepository } from './_shared/trek-repository';
import { passContextQuery, type PassContextDB } from './_shared/reservation-pass-context';
import { originColumnValues, type ReservationOriginColumns } from './_shared/reservation-origin-columns';
import { idByExternalQuery, joinedReservationQuery, type ReservationJoinDB } from './_shared/reservation-joined-query';
import type { DB } from '../kysely/db';
import { ROADTRIP_RESERVATION_TYPES } from '@trek/shared';
import type {
@@ -19,11 +21,12 @@ import type {
PublicApiUnplannedPlaceRow,
PublicApiUnscheduledReservationRow,
} from './reservation-public-api-rows';
import type { ReservationAllColumnsRow, ReservationJoinRow } from './reservation-rows';
// The row types moved to their own modules; re-exported so callers keep one import path.
// The reservation row shapes live in their own module; re-exported so callers keep one import path.
export type { ReservationAllColumnsRow, ReservationJoinRow } from './reservation-rows';
// The public API row types moved to their own module; re-exported so callers keep one import path.
export type {
ReservationAllColumnsRow,
ReservationJoinRow,
PublicApiAccommodationRow,
PublicApiScheduledReservationRow,
PublicApiUnplannedPlaceRow,
@@ -135,8 +138,8 @@ type CalendarReservationKyselyDB = Pick<DB, 'reservations' | 'places' | 'day_acc
/**
* CL4 (`CalendarService.buildTripCalendar`'s assignment read, for every dated
* day at once) — `SELECT da.*, p.name as place_name, p.address as
* place_address, p.lat as place_lat, p.lng as place_lng, p.duration_minutes
* as place_duration_minutes, COALESCE(da.assignment_time, p.place_time) as
* place_address, p.lat as place_lat, p.lng as place_lng, COALESCE(da.stay_minutes,
* p.duration_minutes) as place_duration_minutes, COALESCE(da.assignment_time, p.place_time) as
* effective_time, COALESCE(da.assignment_end_time, p.end_time) as
* effective_end_time FROM
* day_assignments da JOIN places p ON da.place_id = p.id WHERE da.day_id IN
@@ -342,33 +345,15 @@ export interface SharePublicAccommodationRow {
type SharePublicAccommodationKyselyDB = ReservationVisibilityKyselyDB & Pick<DB, 'day_accommodations' | 'places'>;
/**
* Plan 3h Task 4 (`AirportsService.backfillFlightEndpoints`, AIR1) —
* additive. The `reservations`/`reservation_endpoints` tables the
* `NOT EXISTS` guard reads.
*/
/** `AirportsService.backfillFlightEndpoints` (AIR1): the tables its `NOT EXISTS` guard reads. */
type FlightsMissingEndpointsKyselyDB = Pick<DB, 'reservations' | 'reservation_endpoints'>;
export class ReservationsRepository extends TrekRepository<Reservations> {
// For its Wallet pass (_shared/reservation-pass-context.ts).
findPassContext(id: number, tripId: number) { return passContextQuery(this.kysely<PassContextDB>(), id, tripId); }
private joinedQuery() {
const platform = this.getEntityManager().getPlatform();
return this.kysely<ReservationJoinKyselyDB>()
.selectFrom('reservations as r')
.leftJoin('days as d', 'd.id', 'r.day_id')
.leftJoin('places as p', 'p.id', 'r.place_id')
.leftJoin('day_accommodations as ap', (join) =>
join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'ap.id')),
)
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
.selectAll('r')
.select([
'd.day_number as day_number',
'p.name as place_name',
'ap.place_id as accommodation_place_id',
'acc_p.name as accommodation_name',
'ap.start_day_id as accommodation_start_day_id',
'ap.end_day_id as accommodation_end_day_id',
]);
return joinedReservationQuery(this.kysely<ReservationJoinDB>(), this.getEntityManager().getPlatform());
}
/**
@@ -386,10 +371,15 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
/**
* RR1 (`ReservationsReadService.getReservationWithJoins`) — the SAME
* projection for one reservation, no `ORDER BY` (a single row).
* projection for one reservation, no `ORDER BY` (a single row), and only
* when it belongs to `trip_id`: every caller holds the trip it checked, so a
* booking id from another trip reads as missing rather than leaking its row.
*/
async findWithJoins(id: number | string): Promise<ReservationJoinRow | undefined> {
const row = await this.joinedQuery().where('r.id', '=', id as number).executeTakeFirst();
async findWithJoins(id: number | string, trip_id: number | string): Promise<ReservationJoinRow | undefined> {
const row = await this.joinedQuery()
.where('r.id', '=', id as number)
.where('r.trip_id', '=', trip_id as number)
.executeTakeFirst();
return row as ReservationJoinRow | undefined;
}
@@ -447,12 +437,16 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
* (trip_id, day_id, end_day_id, place_id, assignment_id, title,
* reservation_time, reservation_end_time, location, confirmation_number,
* notes, url, status, type, accommodation_id, metadata, needs_review)
* VALUES (?×17)`. `sync_enabled`/`ingest_state`/`day_plan_position`
* deliberately NOT named, so the column's own DB-level `DEFAULT` applies
* (`sync_enabled INTEGER DEFAULT 1`, `Migration20200101021800_…`;
* `ingest_state` `.default('live')`) — the caller has already resolved
* every value (the `|| null` coercions, `resolvedOrNull`, the numeric
* `accommodation_id` stringified for the TEXT column, R2).
* VALUES (?×17)`. `ingest_state`/`day_plan_position` deliberately NOT
* named, so the column's own DB-level `DEFAULT` applies (`ingest_state`
* `.default('live')`) — the caller has already resolved every value (the
* `|| null` coercions, `resolvedOrNull`, the numeric `accommodation_id`
* stringified for the TEXT column, R2).
*
* `external_source`/`external_id`/`sync_enabled` are named only when the
* caller passes them: an imported booking records where it came from, every
* other write leaves them to the column defaults as before (`sync_enabled
* INTEGER DEFAULT 1`, `Migration20200101021800_…`).
*/
async insertReservation(input: {
trip_id: number;
@@ -472,7 +466,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
accommodation_id: string | null;
metadata: string | null;
needs_review: number;
}): Promise<number> {
} & ReservationOriginColumns): Promise<number> {
return await this.insert({
trip: input.trip_id,
day: input.day_id,
@@ -491,9 +485,15 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
accommodation_id: input.accommodation_id,
metadata: input.metadata,
needs_review: input.needs_review,
...originColumnValues(input),
});
}
/** The booking an import already wrote into this trip under the same origin (_shared/reservation-joined-query.ts). */
findIdByExternal(trip_id: number, external_source: string, external_id: string): Promise<number | undefined> {
return idByExternalQuery(this.kysely<DB>(), trip_id, external_source, external_id);
}
/**
* RS41 (`ReservationsService.updateInTx`) — the 16-column `UPDATE
* reservations SET … WHERE id = ?`. The `PlacesRepository.updatePlace`/PL11
@@ -909,7 +909,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
'p.address as place_address',
'p.lat as place_lat',
'p.lng as place_lng',
'p.duration_minutes as place_duration_minutes',
eb.fn.coalesce('da.stay_minutes', 'p.duration_minutes').as('place_duration_minutes'),
eb.fn.coalesce('da.assignment_time', 'p.place_time').as('effective_time'),
eb.fn.coalesce('da.assignment_end_time', 'p.end_time').as('effective_end_time'),
])
+44 -16
View File
@@ -4,13 +4,14 @@ import { findOwnedByUser, listForOwner } from './_shared/owned-lookup';
import { toRow, type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** A `tags` row as the API emits it. */
/** A `tags` row as the API emits it. `trip_id` is null on a personal tag and names the trip on one of its own. */
export interface TagRow {
id: number;
user_id: number;
name: string;
color: string | null;
created_at: string | null;
trip_id: number | null;
}
const _tagRowKeys: AssertRowKeys<TagRow, Tags> = true;
@@ -45,6 +46,9 @@ export interface TagForPlaceRow {
*/
type PlaceTagsKyselyDB = Pick<DB, 'place_tags'>;
/** `tags` alone, for the reads that name their columns. */
type TagsKyselyDB = Pick<DB, 'tags'>;
/**
* `place_tags` joined to `places` for TP46 (`trips.service.ts::copy`'s
* place-tags read). The `trip_id` the caller passes stays `number | string`
@@ -97,11 +101,18 @@ export class TagsRepository extends TrekRepository<Tags> {
* `id` and the `defaultRaw` `created_at` the same way a legacy
* INSERT-then-reselect pair did.
*/
async createTag(input: { user_id: number; name: string; color: string }): Promise<TagRow> {
async createTag(input: {
user_id: number;
name: string;
color: string | null;
/** The trip the tag is for; left out, it is `user_id`'s personal one. */
trip_id?: number;
}): Promise<TagRow> {
const id = await this.insert({
user: input.user_id,
name: input.name,
color: input.color,
...(input.trip_id === undefined ? {} : { trip: input.trip_id }),
});
const inserted = await this.findOne({ id });
if (!inserted) {
@@ -193,22 +204,39 @@ export class TagsRepository extends TrekRepository<Tags> {
// ---------------------------------------------------------------------------
/**
* PL2 (`PlacesService.tagsOnTrip`) — `` SELECT id, user_id FROM tags WHERE
* id IN (${…}) ``, the ownership half of tag scoping: the caller
* (`tagsOnTrip`) intersects these rows' `user_id` against the trip's
* roster, and an id belonging to nobody on the roster drops silently.
* `$in` for the dynamic list (rule 17b).
* PL2 (`PlacesService.tagsOnTrip`) — `` SELECT id, user_id, trip_id FROM tags
* WHERE id IN (${…}) ``, what decides whether a trip may put a tag on a
* place: the caller keeps a trip's own tag for that trip and a personal one
* (`trip_id` null) while its `user_id` is on the roster, and every other id
* drops silently.
*/
async findByIds(ids: number[]): Promise<{ id: number; user_id: number }[]> {
async findByIds(ids: number[]): Promise<{ id: number; user_id: number; trip_id: number | null }[]> {
if (ids.length === 0) return [];
// `t.user` (unaliased, not separately joined) resolves to the physical
// `user_id` column and comes back keyed as `user_id` on the raw driver
// row — `TripsRepository.findAccessible`'s `.select(['t.id', 't.user',
// 't.currency'])` establishes the same shape.
return this.qb('t')
.select(['t.id', 't.user'])
.where({ id: { $in: ids } })
.execute<{ id: number; user_id: number }[]>('all', false);
return await this.kysely<TagsKyselyDB>()
.selectFrom('tags')
.select(['id', 'user_id', 'trip_id'])
.where('id', 'in', ids)
.execute();
}
// ---------------------------------------------------------------------------
// A trip's own tags (`trip_id` set): one list for everybody on the trip.
// ---------------------------------------------------------------------------
/** `SELECT * FROM tags WHERE trip_id = ? ORDER BY name ASC`: the trip's own tags, whoever created them. */
async listByTrip(tripId: number): Promise<TagRow[]> {
const tags = await this.find({ trip: tripId }, { orderBy: { name: 'asc' } });
return tags.map((tag) => toRow(tag) as TagRow);
}
/**
* `SELECT * FROM tags WHERE id = ? AND trip_id = ?`: one tag of the trip, or
* null for a personal tag and for another trip's, so a write scoped to a trip
* cannot reach either.
*/
async findByIdAndTrip(id: number, tripId: number): Promise<TagRow | null> {
const tag = await this.findOne({ id, trip: tripId });
return tag ? (toRow(tag) as TagRow) : null;
}
// ---------------------------------------------------------------------------
@@ -142,6 +142,46 @@ export class TrekPhotosRepository extends TrekRepository<TrekPhotos> {
return changed > 0;
}
/**
* Puts in the capture time and place a provider has just confirmed for this
* very photo, over whatever the row held (#3019: Synology rows that carry the
* next photo's). Unlike patchCaptureMetadata it replaces: the coordinates
* become the confirmed pair or none at all, while a missing time leaves the
* stored one, since every library photo has one and none is no correction.
* Answers whether the row changed.
*/
async replaceCaptureMetadata(
id: number,
taken_at: string | null,
lat: number | null,
lng: number | null,
): Promise<boolean> {
const row = await this.findOne({ id });
if (!row) return false;
const next = { taken_at: taken_at ?? row.taken_at ?? null, lat, lng };
if (next.taken_at === (row.taken_at ?? null) && next.lat === (row.lat ?? null) && next.lng === (row.lng ?? null)) {
return false;
}
await this.nativeUpdate({ id }, next);
return true;
}
/**
* One provider's photos after `afterId`, oldest first, `limit` at a time, with
* the user whose library each lives in: the pages a provider-wide repair walks.
*/
async listByProviderAfter(
provider: string,
afterId: number,
limit: number,
): Promise<Array<{ id: number; owner_id: number }>> {
const rows = await this.find(
{ provider, id: { $gt: afterId }, owner: { $ne: null }, asset_id: { $ne: null } },
{ orderBy: { id: 'asc' }, limit },
);
return rows.flatMap((row) => (row.owner ? [{ id: row.id, owner_id: row.owner.id }] : []));
}
/** PH11 — `DELETE FROM trek_photos WHERE id = ? AND provider != 'local'`; local photos are never auto-reclaimed here. */
async deleteNonLocal(id: number): Promise<number> {
return await this.nativeDelete({ id, provider: { $ne: 'local' } });
+11 -13
View File
@@ -1,5 +1,5 @@
import { QueryFlag } from '@mikro-orm/core';
import type { Trips } from '../entities/Trips.entity';
import type { TTripsCostsMode, Trips } from '../entities/Trips.entity';
import { coalesceParam, currentTimestamp, ilike, nowDateOffset } from '../dialect/sql-functions';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
@@ -36,6 +36,7 @@ export interface TripRawRow {
created_at: string | null;
updated_at: string | null;
reminder_sent_for: string | null;
costs_mode: TTripsCostsMode;
}
const _tripRawRowKeys: AssertRowKeys<TripRawRow, Trips> = true;
@@ -544,7 +545,7 @@ export class TripsRepository extends TrekRepository<Trips> {
/**
* TP37 (`trips.service.ts::copy`'s INSERT) — `INSERT INTO trips (user_id,
* title, description, start_date, end_date, currency, cover_image,
* is_archived, reminder_days) VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?)`. A
* is_archived, reminder_days, costs_mode) VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?)`. A
* DIFFERENT column set from `insertTrip` (TP18, `create`'s own INSERT):
* this one also writes `cover_image` (verbatim from the source row) and
* `is_archived` as a hard-coded literal `0` — a copy is never archived,
@@ -563,6 +564,7 @@ export class TripsRepository extends TrekRepository<Trips> {
currency: string | null;
cover_image: string | null;
reminder_days: number;
costs_mode: TTripsCostsMode;
}): Promise<number> {
return await this.insert({
user: input.user_id,
@@ -574,13 +576,14 @@ export class TripsRepository extends TrekRepository<Trips> {
cover_image: input.cover_image,
is_archived: 0,
reminder_days: input.reminder_days,
costs_mode: input.costs_mode,
});
}
/**
* TP25 (`trips.service.ts::updateTrip`) — `UPDATE trips SET title=?,
* description=?, start_date=?, end_date=?, currency=?, is_archived=?,
* cover_image=?, reminder_days=?, updated_at=CURRENT_TIMESTAMP WHERE
* cover_image=?, reminder_days=?, costs_mode=?, updated_at=CURRENT_TIMESTAMP WHERE
* id=?`. The `||`/`!== undefined` pre-image fallbacks are the SERVICE's
* own (R5/§18.6 — this write stays OUTSIDE the days-regeneration
* transaction, unchanged); this takes the final, already-decided values
@@ -597,16 +600,11 @@ export class TripsRepository extends TrekRepository<Trips> {
* here would tempt a caller to paper over that with its own `?? 0`, the
* exact regression the review found.
*/
async updateTripRow(id: number, data: {
title: string;
description: string | null;
start_date: string | null;
end_date: string | null;
currency: string;
is_archived: number | null;
cover_image: string | null;
reminder_days: number;
}): Promise<void> {
async updateTripRow(
id: number,
data: Pick<TripRawRow, 'title' | 'description' | 'start_date' | 'end_date' | 'is_archived' | 'cover_image' | 'costs_mode'>
& { currency: string; reminder_days: number },
): Promise<void> {
const platform = this.getEntityManager().getPlatform();
await this.nativeUpdate({ id }, { ...data, updated_at: currentTimestamp(platform) });
}
@@ -817,9 +817,9 @@ export class UsersRepository extends TrekRepository<Users> {
/**
* `UPDATE users SET oidc_sub = ?, oidc_issuer = ? WHERE id = ?` (O5, the
* email-matched/unlinked/verified-email branch; O6, the provider-switch
* branch — identical statement, two call sites, no `updated_at`).
* branch; with nulls the admin's "Remove SSO link", #3065; no `updated_at`).
*/
async linkOidcIdentity(id: number, sub: string, issuer: string): Promise<void> {
async linkOidcIdentity(id: number, sub: string | null, issuer: string | null): Promise<void> {
await this.nativeUpdate({ id }, { oidc_sub: sub, oidc_issuer: issuer });
}
@@ -0,0 +1,62 @@
import type { DB } from '../../kysely/db';
import { accommodationIdMatches } from './reservation-accommodation';
import type { Platform } from '@mikro-orm/core';
import type { Kysely } from 'kysely';
/** The tables the joined booking projection reads. */
export type ReservationJoinDB = Pick<DB, 'reservations' | 'days' | 'places' | 'day_accommodations'>;
/**
* A booking with its day number, its place's name and its stay's place and
* times: the projection `ReservationsRepository.listForTrip` and
* `findWithJoins` narrow with their own `WHERE`. Kept out of the repository
* file, which is at its size baseline. `accommodation_id` is TEXT and `ap.id`
* INTEGER, so the stay is matched through accommodationIdMatches: SQLite
* coerces the bare comparison, Postgres refuses it.
*/
export function joinedReservationQuery(kysely: Kysely<ReservationJoinDB>, platform: Platform) {
return kysely
.selectFrom('reservations as r')
.leftJoin('days as d', 'd.id', 'r.day_id')
.leftJoin('places as p', 'p.id', 'r.place_id')
.leftJoin('day_accommodations as ap', (join) =>
join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'ap.id')),
)
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
.selectAll('r')
.select([
'd.day_number as day_number',
'p.name as place_name',
'ap.place_id as accommodation_place_id',
'acc_p.name as accommodation_name',
'ap.start_day_id as accommodation_start_day_id',
'ap.end_day_id as accommodation_end_day_id',
'ap.check_in as accommodation_check_in',
'ap.check_in_end as accommodation_check_in_end',
'ap.check_out as accommodation_check_out',
]);
}
/**
* The booking an import already wrote into this trip under the same origin,
* or undefined: what `idx_reservations_external (external_source,
* external_id, trip_id)` would refuse, read before the write so a repeat
* import is reported instead of failing on the unique index
* (`ReservationsRepository.findIdByExternal`).
*/
export async function idByExternalQuery(
kysely: Kysely<Pick<DB, 'reservations'>>,
trip_id: number,
external_source: string,
external_id: string,
): Promise<number | undefined> {
const row = await kysely
.selectFrom('reservations')
.select(['id'])
.where('trip_id', '=', trip_id)
.where('external_source', '=', external_source)
.where('external_id', '=', external_id)
.executeTakeFirst();
return row?.id;
}
@@ -0,0 +1,21 @@
/**
* Where an imported booking came from and its key there, as the reservations
* table stores them. Named on an insert only when the caller passes them: an
* imported booking records its origin, every other write leaves the columns to
* their defaults (`sync_enabled INTEGER DEFAULT 1`). Kept out of
* Reservations.repository.ts, which is at its size baseline.
*/
export interface ReservationOriginColumns {
external_source?: string;
external_id?: string;
sync_enabled?: number;
}
/** The origin columns an insert names: only those the caller set. */
export function originColumnValues(input: ReservationOriginColumns): ReservationOriginColumns {
return {
...(input.external_source !== undefined && { external_source: input.external_source }),
...(input.external_id !== undefined && { external_id: input.external_id }),
...(input.sync_enabled !== undefined && { sync_enabled: input.sync_enabled }),
};
}
@@ -0,0 +1,46 @@
import type { DB } from '../../kysely/db';
import type { Kysely } from 'kysely';
/** The tables `passContextQuery` reads. */
export type PassContextDB = Pick<DB, 'reservations' | 'trips' | 'places' | 'day_accommodations'>;
/** What a booking's Wallet pass needs beyond the booking: where it is, and the trip's name. */
export interface ReservationPassContext {
trip_title: string | null;
place_lat: number | null;
place_lng: number | null;
place_name: string | null;
}
/**
* The trip's title and the place a booking is at (its own place, else its
* stay's), for its Apple Wallet pass (`ReservationsRepository.findPassContext`).
* Scoped to the trip, so a booking of another trip reads as none. Kept out of
* the repository file, which is at its size baseline.
*/
export async function passContextQuery(
kysely: Kysely<PassContextDB>,
id: number,
trip_id: number,
): Promise<ReservationPassContext | undefined> {
const row = await kysely
.selectFrom('reservations as r')
.innerJoin('trips as t', 't.id', 'r.trip_id')
.leftJoin('places as p', 'p.id', 'r.place_id')
// accommodation_id is TEXT; compared as a number so Postgres takes it as well as SQLite ("14" and "14.0" alike).
.leftJoin('day_accommodations as ap', (join) =>
join.on((eb) => eb(eb.cast<number>('r.accommodation_id', 'numeric'), '=', eb.ref('ap.id'))),
)
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
.where('r.id', '=', id)
.where('r.trip_id', '=', trip_id)
.select([
't.title as trip_title',
(eb) => eb.fn.coalesce('p.lat', 'acc_p.lat').as('place_lat'),
(eb) => eb.fn.coalesce('p.lng', 'acc_p.lng').as('place_lng'),
(eb) => eb.fn.coalesce('p.name', 'acc_p.name').as('place_name'),
])
.executeTakeFirst();
return row as ReservationPassContext | undefined;
}
@@ -115,6 +115,8 @@ export interface DayAssignmentRow {
route_excluded: number;
/** MDT-02: the stage of a multi-day tour the day holds, by its starting stop's key; null for anything else. */
tour_stage_key: string | null;
/** #3092: how long the drive stands at this visit; null reads the place's `duration_minutes`. */
stay_minutes: number | null;
}
const _dayAssignmentRowKeys: AssertRowKeys<DayAssignmentRow, DayAssignments> = true;
@@ -172,6 +174,7 @@ export interface RoadtripVisitRow {
lng: number | null;
time: string | null;
end_time: string | null;
/** The visit's own stay where it has one (#3092), else the place's. */
duration_minutes: number | null;
end_day: number;
leg_transport_mode: string | null;
+9 -17
View File
@@ -1,10 +1,7 @@
/**
* The rows `ReservationsRepository` answers with for a whole reservation, and
* the Kysely tables its joined read binds: what a reservation is read as, kept
* apart from the statements that read it. The repository re-exports the row
* types, so callers keep importing them from there.
* The row shapes ReservationsRepository reads: every column of a reservation,
* and the joined projection the trip list and the single reads share.
*/
import type { DB } from '../kysely/db';
/** `SELECT *` — every scalar column of `Reservations`, RS35's shape. */
export interface ReservationAllColumnsRow {
@@ -41,12 +38,13 @@ export interface ReservationAllColumnsRow {
* RS18/RR1's shared joined projection — `SELECT r.*, d.day_number, p.name as
* place_name, r.assignment_id, ap.place_id as accommodation_place_id,
* acc_p.name as accommodation_name, ap.start_day_id as
* accommodation_start_day_id, ap.end_day_id as accommodation_end_day_id FROM
* accommodation_start_day_id, ap.end_day_id as accommodation_end_day_id,
* ap.check_in/check_in_end/check_out as accommodation_check_in/_check_in_end/_check_out FROM
* reservations r LEFT JOIN days d ON r.day_id = d.id LEFT JOIN places p ON
* r.place_id = p.id LEFT JOIN day_accommodations ap ON r.accommodation_id =
* ap.id LEFT JOIN places acc_p ON ap.place_id = acc_p.id`. The service
* folds `accommodation_id` (`Math.trunc(Number(x))`), `day_positions`,
* `endpoints` and `travelers` on afterward.
* `endpoints` and `travelers` on afterward (unchanged from the legacy split).
*/
export interface ReservationJoinRow extends ReservationAllColumnsRow {
day_number: number | null;
@@ -55,14 +53,8 @@ export interface ReservationJoinRow extends ReservationAllColumnsRow {
accommodation_name: string | null;
accommodation_start_day_id: number | null;
accommodation_end_day_id: number | null;
/** The stay's own times, which a hotel booking's card falls back to. */
accommodation_check_in: string | null;
accommodation_check_in_end: string | null;
accommodation_check_out: string | null;
}
/**
* The tables the RS18/RR1 join binds (`ReservationsRepository.joinedQuery`).
* `reservations.accommodation_id` is a TEXT column with no foreign key to
* `day_accommodations`, so the QueryBuilder's relation joins cannot express
* `LEFT JOIN day_accommodations ap ON r.accommodation_id = ap.id`; the join
* is written in Kysely, as `DayAssignmentsRepository.effectiveStart` and
* `TripsRepository.tripSelectQuery` do.
*/
export type ReservationJoinKyselyDB = Pick<DB, 'reservations' | 'days' | 'places' | 'day_accommodations'>;
+8 -3
View File
@@ -278,8 +278,13 @@ export function applyGlobalMiddleware(
// verifying a real .heic upload in a browser, not just deleting the
// string here.
scriptSrc: ["'self'", "'wasm-unsafe-eval'", "'unsafe-eval'"],
styleSrc: ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com', 'https://unpkg.com'],
imgSrc: ["'self'", 'data:', 'blob:', 'https:'],
// No Google Fonts: every font ships with TREK (client/public/fonts), so no reader's IP goes to Google.
styleSrc: ["'self'", "'unsafe-inline'", 'https://unpkg.com'],
// trek-tile: is the iOS app's map tile store (client/src/native/tileCache.ts).
// The app's web view runs this page, and WebKit holds a scheme the app
// answers itself to img-src as well: without it every raster tile was
// refused and the app showed routes on a blank map.
imgSrc: ["'self'", 'data:', 'blob:', 'https:', 'trek-tile:'],
connectSrc: [
"'self'",
'ws:',
@@ -372,7 +377,7 @@ export function applyGlobalMiddleware(
// thumbnail answered 404 (#2341). Invisible in dev, where Vite serves the
// document without this header.
mediaSrc: ["'self'", 'blob:'],
fontSrc: ["'self'", 'https://fonts.gstatic.com', 'data:'],
fontSrc: ["'self'", 'data:'],
// 'self' so same-origin file previews can embed PDFs via <object>/<embed>
// (Firefox/Chrome enforce object-src; 'none' broke inline PDF previews there).
objectSrc: ["'self'"],
+5
View File
@@ -15,6 +15,11 @@ export function ok(data: unknown): McpTextResult {
return { content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }] };
}
/** Success result: `data` as one line of JSON, for an answer whose size matters more than how it reads (a page of a long list). */
export function okCompact(data: unknown): McpTextResult {
return { content: [{ type: 'text' as const, text: JSON.stringify(data) }] };
}
/** Error result with a verbatim message (no formatting, no prefix). */
export function errorResult(text: string): McpTextResult {
return { content: [{ type: 'text' as const, text }], isError: true };
+1
View File
@@ -3,6 +3,7 @@ export {
demoDenied,
errorResult,
ok,
okCompact,
TOOL_ANNOTATIONS_DELETE,
TOOL_ANNOTATIONS_NON_IDEMPOTENT,
TOOL_ANNOTATIONS_OPEN_WORLD_NON_IDEMPOTENT,
+2 -2
View File
@@ -32,9 +32,9 @@ export function addonGate(addonId: string) {
/**
* Gate on one of collab's sub-feature flags. Every collab entry rides both the
* addon toggle and its own flag — notes, polls or chat.
* addon toggle and its own flag — notes, polls, chat or links.
*/
export function collabFeatureGate(feature: 'chat' | 'notes' | 'polls') {
export function collabFeatureGate(feature: 'chat' | 'notes' | 'polls' | 'links') {
return async (_ctx: McpContext, self: HasAddons): Promise<boolean> =>
(await self.addons.isAddonEnabled(ADDON_IDS.COLLAB)) && (await self.addons.getCollabFeatures())[feature];
}
+6 -1
View File
@@ -120,8 +120,13 @@ export class AddonsService {
return this.photoProviders.listEnabledIds();
}
/** wanderer may be used: Tours is on and the admin allowed it under Tours. */
/**
* wanderer may be used: Tours is on and the admin allowed it under Tours. Never on a managed
* install, where it would send searches to a server the user picks; the admin switch is locked
* there, so a value stored before the install became managed must not keep it on.
*/
async isWandererAvailable(): Promise<boolean> {
if (readEnv().managed.enabled) return false;
return (await this.isAddonEnabled(ADDON_IDS.TOURS)) && (await this.getToursWanderer()).enabled;
}
+18 -1
View File
@@ -42,7 +42,7 @@ import {
Req,
UseGuards,
} from '@nestjs/common';
import { adminFeatureToggleResponseSchema } from '@trek/shared';
import { adminFeatureToggleResponseSchema, adminSsoUnlinkResponseSchema } from '@trek/shared';
import type { Request } from 'express';
@@ -156,6 +156,23 @@ export class AdminController {
return { success: true };
}
@Delete('users/:id/sso')
@ResponseContract(adminSsoUnlinkResponseSchema)
async unlinkUserSso(@CurrentUser() user: User, @Param('id') id: string, @Req() req: Request) {
const result = await this.admin.unlinkUserSso(id, user.id);
if (result.unlinked) {
await this.audit.writeAudit({
userId: user.id,
action: 'admin.user_sso_unlink',
resource: String(id),
ip: getClientIp(req),
details: { targetUser: result.email },
});
logInfo(`Admin ${user.email} removed the SSO link of user ${result.email}`);
}
return { success: true, unlinked: result.unlinked };
}
// ── Stats / permissions / audit ──
@Get('stats')
stats() {
+29
View File
@@ -398,6 +398,35 @@ export class AdminService {
return { success: true, email: target.email };
}
/**
* Forget the SSO identity stored on another account (#3065).
*
* A sign-in that arrives with a different subject of the same provider is
* refused, because that is another person at the provider as far as TREK can
* tell. When the provider itself re-issued the subject (a migrated or rebuilt
* IdP, a changed `sub` format), the account owner is locked out and editing the
* email does not help. Clearing the link hands the decision to the admin: the
* next SSO sign-in links again under the same rules as a first one, so it still
* needs a verified email and an address the account did not set itself.
*
* Never for the caller themselves, for the same reason as resetUserMfa: a
* stolen admin session must not be able to loosen the login of the account it
* came from.
*/
async unlinkUserSso(id: string, actingUserId: number): Promise<{ email: string; unlinked: boolean }> {
const targetId = Number(id);
// SECURITY: plain JS comparison, kept here like resetUserMfa's (R4).
if (targetId === actingUserId) {
throw new DomainError(400, 'You cannot remove the SSO link of your own account');
}
const target = await this.users.findById(targetId);
if (!target || target.is_guest) throw new DomainError(404, 'User not found');
if (!target.oidc_sub && !target.oidc_issuer) return { email: target.email, unlinked: false };
await this.users.linkOidcIdentity(targetId, null, null);
return { email: target.email, unlinked: true };
}
// ── Stats ──────────────────────────────────────────────────────────────────
async getStats() {
@@ -54,6 +54,15 @@ export interface AiChatRequest {
cacheKey?: string;
/** Lets the model search the web with its provider's own search; a client without one ignores it. */
webSearch?: boolean;
/**
* `none` asks for text only: the tools are still sent, so the tool calls
* already in the history stay valid, but the model may not call one.
* Absent means the model chooses (#3016). `required` makes it call one: the
* parsing read on a ChatGPT plan asks for that, through the Responses API;
* the Anthropic adapter, whose extended thinking refuses a forced tool,
* treats it as absent.
*/
toolChoice?: 'auto' | 'none' | 'required';
}
/** A page the model's answer draws on, from its web search. */
@@ -45,17 +45,7 @@ export class AiChatClientService {
create(connection: ResolvedAiConnection): AiChatClient {
const { credential, kind, model } = connection;
if (credential.type === 'chatgpt') {
let current = credential.accessToken;
return new OpenAiResponsesClient(
model,
async (rejected) => {
if (rejected) current = await this.chatgpt.refresh(connection.id, rejected);
return current;
},
cloudFetch,
);
}
if (credential.type === 'chatgpt') return this.chatgptPlan(connection.id, model, credential.accessToken);
if (kind === 'openai' && openAiWantsResponses(model)) {
const apiKey = credential.apiKey ?? '';
return new OpenAiResponsesClient(model, () => Promise.resolve(apiKey), cloudFetch, 'openai');
@@ -75,4 +65,21 @@ export class AiChatClientService {
return new OpenAiChatClient(kind, model, AI_BASE_URLS[kind], credential.apiKey, cloudFetch);
}
}
/**
* The Responses client on a ChatGPT plan: a token the provider refuses is
* refreshed once for this connection, and the fresh one is kept for the
* client's following calls.
*/
chatgptPlan(connectionId: number, model: string, accessToken: string): AiChatClient {
let current = accessToken;
return new OpenAiResponsesClient(
model,
async (rejected) => {
if (rejected) current = await this.chatgpt.refresh(connectionId, rejected);
return current;
},
cloudFetch,
);
}
}
@@ -16,6 +16,7 @@ import { AiRefreshLock, InMemoryAiRefreshLock } from './ai-refresh-lock';
import { AiSettingsService } from './ai-settings.service';
import { AiUsageService } from './ai-usage.service';
import { ChatgptAuthService } from './chatgpt-auth.service';
import { ChatgptParsingReader } from './chatgpt-parsing.reader';
import { ModelCatalogService } from './model-catalog.service';
import { OpenrouterAuthService } from './openrouter-auth.service';
import { MikroOrmModule } from '@mikro-orm/nestjs';
@@ -47,6 +48,7 @@ import { Module } from '@nestjs/common';
AiUsageService,
AiChatClientService,
ChatgptAuthService,
ChatgptParsingReader,
OpenrouterAuthService,
ModelCatalogService,
{ provide: AiAuthFlowStore, useClass: InMemoryAiAuthFlowStore },
@@ -23,6 +23,7 @@ import { maskHint, openSecrets, sealSecrets } from './ai-secrets';
import { AiSettingsService } from './ai-settings.service';
import { AiUsageService } from './ai-usage.service';
import { ChatgptAuthService } from './chatgpt-auth.service';
import { ChatgptParsingReader } from './chatgpt-parsing.reader';
import { ModelCatalogService, modelReadsImages, pickDefaultModel, type CatalogTarget } from './model-catalog.service';
import { AiProviderError } from './providers/provider-errors';
import { InjectRepository } from '@mikro-orm/nestjs';
@@ -87,6 +88,7 @@ export class AiConnectionsService implements SharedAiConnectionPort {
private readonly rateLimit: RateLimitService,
private readonly llmParse: LlmParseService,
private readonly audit: AuditService,
private readonly chatgptParsing: ChatgptParsingReader,
) {}
/** `kind:model` of the models a provider refused an image for, while this process runs. */
@@ -411,10 +413,10 @@ export class AiConnectionsService implements SharedAiConnectionPort {
/**
* The connection a user's assistant would run on, as AI parsing reads it, so
* an instance sets up one provider for both. The admin's policy decides it
* the same way. A ChatGPT plan is left out: its sign-in only reaches the chat
* models, not the completions endpoint parsing calls. Gemini and OpenRouter
* go through their OpenAI-compatible endpoints; a custom server is the
* admin's own address, read like parsing's local provider (#1772).
* the same way. A ChatGPT plan has no key for parsing's clients, so it is
* lent as a reader that holds the sign-in (chatgpt-parsing.reader.ts). Gemini
* and OpenRouter go through their OpenAI-compatible endpoints; a custom server
* is the admin's own address, read like parsing's local provider (#1772).
*/
async forParsing(userId: number): Promise<SharedParsingConnection | null> {
if (readEnv().managed.enabled) return null;
@@ -438,6 +440,8 @@ export class AiConnectionsService implements SharedAiConnectionPort {
return apiKey ? { provider: 'openai', model: row.model, apiKey, baseUrl: AI_BASE_URLS[kind] } : null;
case 'custom':
return row.base_url ? { provider: 'local', model: row.model, apiKey, baseUrl: row.base_url } : null;
case 'chatgpt':
return { provider: 'chatgpt', model: row.model, read: this.chatgptParsing.readerFor(row, row.model) };
default:
return null;
}
@@ -0,0 +1,60 @@
import type { AiConnectionRow } from '../../db/repositories/AiConnections.repository';
import type {
StructuredReadAnswer,
StructuredReader,
StructuredReadRequest,
} from '../llm-parse/llm-provider.interface';
import { AiChatClientService } from './ai-chat-client.service';
import { ChatgptAuthService } from './chatgpt-auth.service';
import { Injectable } from '@nestjs/common';
/**
* Lends a ChatGPT plan to AI parsing. Parsing's own clients call an endpoint
* with a key; a plan signs in instead, and only the Responses client here
* holds its token. So parsing gets a reader: one document per call, the shape
* to fill offered as the single tool the model has to call. The token is read
* per call, refreshed when it is about to expire or refused; a sign-in that
* cannot be renewed is marked by ChatgptAuthService and the next resolve lends
* nothing.
*/
@Injectable()
export class ChatgptParsingReader {
constructor(
private readonly chatgpt: ChatgptAuthService,
private readonly clients: AiChatClientService,
) {}
/** The reader for one ChatGPT connection, with the model it is saved with. */
readerFor(row: AiConnectionRow, model: string): StructuredReader {
return (request) => this.read(row, model, request);
}
/**
* @txIndependent writes nothing itself: a token refresh is stored by ChatgptAuthService under its own
* lock, complete on its own, with the model call (network I/O) after it.
*/
private async read(
row: AiConnectionRow,
model: string,
request: StructuredReadRequest,
): Promise<StructuredReadAnswer> {
const client = this.clients.chatgptPlan(row.id, model, await this.chatgpt.accessToken(row));
let filled: Record<string, unknown> | null = null;
let text = '';
const turn = client.stream(
{
system: request.system,
messages: [{ role: 'user', text: request.text, images: request.images }],
tools: [request.shape],
toolChoice: 'required',
},
request.signal,
);
for await (const event of turn) {
// The one tool offered is the shape; its first call is the answer.
if (event.type === 'tool-call' && filled === null) filled = event.call.args;
else if (event.type === 'text-delta') text += event.delta;
}
return filled ? { filled } : { text };
}
}
@@ -60,10 +60,10 @@ export function toAnthropicMessages(messages: AiChatMessage[]): AnthropicMessage
if (message.role === 'user') {
push('user', [...(message.images ?? []).map(imageBlock), { type: 'text', text: message.text }]);
} else if (message.role === 'assistant' && message.providerState?.adapter === 'anthropic-messages') {
push('assistant', message.providerState.blocks.map(replayed));
push('assistant', message.providerState.blocks.filter((block) => !isBlankText(block)).map(replayed));
} else if (message.role === 'assistant') {
push('assistant', [
...(message.text ? [{ type: 'text', text: message.text }] : []),
...(message.text.trim() ? [{ type: 'text', text: message.text }] : []),
...message.toolCalls.map((call) => ({ type: 'tool_use', id: call.id, name: call.name, input: call.args })),
]);
} else {
@@ -82,6 +82,15 @@ export function toAnthropicMessages(messages: AiChatMessage[]): AnthropicMessage
return out;
}
/**
* A text block of nothing but whitespace, which the Messages API refuses in a
* request. A model that came back blank streams one now and then; it is left
* out of the replay, which changes nothing the model said.
*/
function isBlankText(block: ContentBlock): boolean {
return block.type === 'text' && !asText(block.text).trim();
}
/**
* A block of an answer as it goes back to the provider: as it came, but a text
* block without the citations it carried, which the provider checks against
@@ -168,6 +177,8 @@ export class AnthropicMessagesClient implements AiChatClient {
})),
...(request.webSearch ? [WEB_SEARCH_TOOL] : []),
],
// `none` is allowed with extended thinking, unlike forcing a tool.
...(request.toolChoice === 'none' ? { tool_choice: { type: 'none' } } : {}),
}
: {}),
};
@@ -1,3 +1,4 @@
import { logWarn } from '../../audit/audit-log.logger';
import type {
AiChatClient,
AiChatEvent,
@@ -8,7 +9,7 @@ import type {
AiSource,
AiToolCall,
} from '../ai-chat-client.interface';
import { AiProviderError, errorCodeOf, providerFailure } from './provider-errors';
import { AiProviderError, errorCodeOf, isReasoningEffortRefusal, providerFailure } from './provider-errors';
import {
asCount,
asRecord,
@@ -107,6 +108,13 @@ const FINISH_REASONS: Record<string, AiFinishReason> = { tool_calls: 'tool-calls
* schemas, also when an admin enters its endpoint as their own server, and
* OpenAI the prompt cache key, which the others do not know. Gemini's thought
* signatures come back as the turn's provider state.
*
* No request names a reasoning effort, since a server that does not reason may
* refuse the field. A reasoning model that takes function tools only without
* reasoning (Azure AI Foundry's deployments, #3043) refuses the default effort
* by naming `reasoning_effort`; the request then goes out once more with
* `reasoning_effort: "none"`, and so does every later request with tools on
* this client, which lives for one turn.
*/
export class OpenAiChatClient implements AiChatClient {
/** OpenRouter searches the web for any model through its web plugin; the others have no search here. */
@@ -117,6 +125,9 @@ export class OpenAiChatClient implements AiChatClient {
/** The kind whose tool schemas the endpoint takes. */
private readonly schemaKind: AiConnectionKind;
/** Whether the server refused function tools with the model's reasoning, so they go out without it. */
private toolsWithoutReasoning = false;
constructor(
readonly kind: AiConnectionKind,
readonly model: string,
@@ -128,42 +139,7 @@ export class OpenAiChatClient implements AiChatClient {
}
async *stream(request: AiChatRequest, signal: AbortSignal): AsyncGenerator<AiChatEvent> {
const body = {
model: this.model,
stream: true,
stream_options: { include_usage: true },
messages: toChatMessages(request, this.model),
...(request.tools.length > 0
? {
tools: request.tools.map((tool) => ({
type: 'function',
function: {
name: tool.name,
description: tool.description,
parameters: toProviderSchema(tool.parameters, this.schemaKind),
},
})),
tool_choice: 'auto',
}
: {}),
...(this.kind === 'openai' && request.cacheKey ? { prompt_cache_key: request.cacheKey } : {}),
...(request.webSearch && this.webSearch ? { plugins: [{ id: 'web', max_results: 5 }] } : {}),
};
const events = await openProviderStream(
this.fetcher,
this.kind,
`${this.baseUrl}/chat/completions`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'text/event-stream',
...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
},
body: JSON.stringify(body),
},
signal,
);
const events = await this.open(request, signal);
const turn = new ChatTurn();
for await (const sse of readSseEvents(events)) {
@@ -192,6 +168,61 @@ export class OpenAiChatClient implements AiChatClient {
}
yield { type: 'finish', reason: turn.finishReason() };
}
/**
* Opens the request's stream. A refusal of the model's reasoning effort for
* a request with tools is answered once by asking again without reasoning.
*/
private async open(request: AiChatRequest, signal: AbortSignal): Promise<ReadableStream<Uint8Array>> {
try {
return await this.send(request, signal);
} catch (err) {
if (this.toolsWithoutReasoning || request.tools.length === 0 || !isReasoningEffortRefusal(err)) throw err;
this.toolsWithoutReasoning = true;
logWarn(`[ai] ${this.kind} takes function tools only without reasoning: asking again with reasoning_effort none`);
return this.send(request, signal);
}
}
private send(request: AiChatRequest, signal: AbortSignal): Promise<ReadableStream<Uint8Array>> {
const body = {
model: this.model,
stream: true,
stream_options: { include_usage: true },
messages: toChatMessages(request, this.model),
...(request.tools.length > 0
? {
tools: request.tools.map((tool) => ({
type: 'function',
function: {
name: tool.name,
description: tool.description,
parameters: toProviderSchema(tool.parameters, this.schemaKind),
},
})),
tool_choice: request.toolChoice ?? 'auto',
}
: {}),
...(this.toolsWithoutReasoning && request.tools.length > 0 ? { reasoning_effort: 'none' } : {}),
...(this.kind === 'openai' && request.cacheKey ? { prompt_cache_key: request.cacheKey } : {}),
...(request.webSearch && this.webSearch ? { plugins: [{ id: 'web', max_results: 5 }] } : {}),
};
return openProviderStream(
this.fetcher,
this.kind,
`${this.baseUrl}/chat/completions`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'text/event-stream',
...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
},
body: JSON.stringify(body),
},
signal,
);
}
}
/** A tool call while it streams in: its id, name and arguments so far, and Gemini's signature. */
@@ -111,7 +111,7 @@ function toolsOf(request: AiChatRequest, kind: AiConnectionKind): Record<string,
});
}
if (request.webSearch) tools.push({ type: 'web_search' });
return tools.length > 0 ? { tools, tool_choice: 'auto', parallel_tool_calls: true } : {};
return tools.length > 0 ? { tools, tool_choice: request.toolChoice ?? 'auto', parallel_tool_calls: true } : {};
}
/** The pages an output message cites, as its text's URL annotations name them. */
@@ -10,6 +10,12 @@ export const CHATGPT_USAGE_URL = 'https://chatgpt.com/settings/usage';
* request again later can help.
*/
export class AiProviderError extends Error {
/**
* The request field the provider named as refused (OpenAI's `param`), when
* it named one as a bare path. Set by `refusalOf` for an HTTP refusal.
*/
refusedParam?: string;
constructor(
readonly code: AssistantErrorCode,
readonly status: number,
@@ -120,3 +126,12 @@ export function namesImageInput(status: number, body: unknown): boolean {
export function isImageRefusal(err: unknown): boolean {
return err instanceof AiProviderError && err.providerCode === IMAGE_INPUT_REFUSED;
}
/**
* Whether a provider error is the refusal of the request's reasoning effort:
* a reasoning model behind Chat Completions that takes function tools only
* with `reasoning_effort: "none"`, as Azure AI Foundry's deployments do (#3043).
*/
export function isReasoningEffortRefusal(err: unknown): boolean {
return err instanceof AiProviderError && err.status === 400 && err.refusedParam === 'reasoning_effort';
}
@@ -65,7 +65,9 @@ export async function refusalOf(kind: AiConnectionKind, res: Response): Promise<
logWarn(
`[ai] ${kind} refused the request: HTTP ${res.status}${code ? ` (${code})` : ''}${param ? ` at ${param}` : ''}`,
);
return providerFailure(kind, res.status, code);
const failure = providerFailure(kind, res.status, code);
failure.refusedParam = param;
return failure;
}
/** The field an error body names as refused (OpenAI's `param`, such as `input[12]`), when it is a bare path. */
+11 -1
View File
@@ -38,4 +38,14 @@ export const kitineraryConfig = registerAs('kitinerary', () => BOOT_DERIVERS.kit
*/
export const servicesConfig = registerAs('services', () => BOOT_DERIVERS.services(process.env));
export const BOOT_STABLE_TOKENS = [httpConfig, storageConfig, transitConfig, kitineraryConfig, servicesConfig];
/** How the booking mail import takes mail in (MAIL_INGEST_*), each value over its stored setting. */
export const mailIngestConfig = registerAs('mailIngest', () => BOOT_DERIVERS.mailIngest(process.env));
export const BOOT_STABLE_TOKENS = [
httpConfig,
storageConfig,
transitConfig,
kitineraryConfig,
servicesConfig,
mailIngestConfig,
];
+8
View File
@@ -46,6 +46,7 @@ import { DocSyncModule } from './doc-sync/doc-sync.module';
import { FeedsModule } from './feeds/feeds.module';
import { FerratasModule } from './ferratas/ferratas.module';
import { FilesModule } from './files/files.module';
import { FlightLiveModule } from './flight-live/flight-live.module';
import { GeoModule } from './geo/geo.module';
import { GoogleQuotaModule } from './google-quota/google-quota.module';
import { HealthModule } from './health/health.module';
@@ -57,10 +58,12 @@ import { JourneyAccessModule } from './journey/journey-access.module';
import { JourneyModule } from './journey/journey.module';
import { TripJournalLinkModule } from './journey/trip-journal-link.module';
import { LlmParseModule } from './llm-parse/llm-parse.module';
import { MailIngestModule } from './mail-ingest/mail-ingest.module';
import { ManagedExtModule } from './managed/managed-ext.module';
import { MapsModule } from './maps/maps.module';
import { McpTransportModule } from './mcp-transport/mcp-transport.module';
import { MemoriesModule } from './memories/memories.module';
import { NativeAuthModule } from './native-auth/native-auth.module';
import { NotificationsModule } from './notifications/notifications.module';
import { OauthModule } from './oauth/oauth.module';
import { OidcModule } from './oidc/oidc.module';
@@ -102,6 +105,7 @@ import { TransitModule } from './transit/transit.module';
import { TripInviteModule } from './trip-invite/trip-invite.module';
import { TripsModule } from './trips/trips.module';
import { VacayModule } from './vacay/vacay.module';
import { WalletPassModule } from './wallet-pass/wallet-pass.module';
import { WandererModule } from './wanderer/wanderer.module';
import { WeatherModule } from './weather/weather.module';
import { MikroOrmModule } from '@mikro-orm/nestjs';
@@ -176,6 +180,7 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
PhotosModule,
MemoriesModule,
AirtrailModule,
FlightLiveModule,
DawarichModule,
JourneyModule,
JourneyRecordingsModule,
@@ -188,11 +193,13 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
SettingsModule,
ServiceIdentityModule,
RoutingRelayModule,
WalletPassModule,
StorageModule,
BackupModule,
AuthModule,
SessionsModule,
OidcModule,
NativeAuthModule,
OauthModule,
AdminModule,
AddonsModule,
@@ -203,6 +210,7 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
ReservationImportModule,
ReceiptScanModule,
LlmParseModule,
MailIngestModule,
AiConnectionsModule,
AssistantModule,
ManagedExtModule,
@@ -1,6 +1,7 @@
import type { User } from '../../types';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { ResponseContract } from '../common/response-contract';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import {
AssignmentCreateDto,
@@ -8,6 +9,7 @@ import {
AssignmentMoveDto,
AssignmentTimeDto,
AssignmentEndDayDto,
AssignmentStayDto,
AssignmentNotesDto,
AssignmentTransportDto,
AssignmentRouteDto,
@@ -15,6 +17,7 @@ import {
} from './assignments.dto';
import { AssignmentsService } from './assignments.service';
import { Body, Controller, Delete, Get, Headers, HttpException, Param, Post, Put, UseGuards } from '@nestjs/common';
import { assignmentResponseSchema } from '@trek/shared';
/**
* /api/trips/:tripId/days/:dayId/assignments — the day's ordered itinerary items.
@@ -221,6 +224,25 @@ export class AssignmentOpsController {
return { assignment };
}
// #3092: the stay of one visit, so two visits of a place can last differently.
// Same guard shape and 404 body as end-day; no reconcile, the stay touches
// neither the day's order nor the journey skeleton.
@RequirePermission('day_edit')
@Put(':id/stay')
@ResponseContract(assignmentResponseSchema)
async stay(
@Param('tripId') tripId: string,
@Param('id') id: string,
@Body() body: AssignmentStayDto,
@Headers('x-socket-id') socketId?: string,
) {
const visit = await this.assignments.getAssignmentForTrip(id, tripId);
if (!visit) throw new HttpException({ error: 'Assignment not found' }, 404);
const assignment = await this.assignments.setStay(visit.id, body.stay_minutes);
this.assignments.broadcast(tripId, 'assignment:updated', { assignment }, socketId);
return { assignment };
}
// #2163: the per-assignment note was write-once (create bodies, MCP, plugin
// RPC) with no edit path anywhere. Same guard shape and 404 body as its
// neighbours; no reconcile — the note doesn't touch the journey skeleton.
@@ -4,6 +4,7 @@ import {
assignmentMoveRequestSchema,
assignmentTimeRequestSchema,
assignmentEndDayRequestSchema,
assignmentStayRequestSchema,
assignmentNotesRequestSchema,
assignmentTransportRequestSchema,
assignmentRouteRequestSchema,
@@ -24,6 +25,7 @@ export class AssignmentReorderDto extends createZodDto(assignmentReorderRequestS
export class AssignmentMoveDto extends createZodDto(assignmentMoveRequestSchema) {}
export class AssignmentTimeDto extends createZodDto(assignmentTimeRequestSchema) {}
export class AssignmentEndDayDto extends createZodDto(assignmentEndDayRequestSchema) {}
export class AssignmentStayDto extends createZodDto(assignmentStayRequestSchema) {}
export class AssignmentNotesDto extends createZodDto(assignmentNotesRequestSchema) {}
export class AssignmentTransportDto extends createZodDto(assignmentTransportRequestSchema) {}
export class AssignmentRouteDto extends createZodDto(assignmentRouteRequestSchema) {}
@@ -17,7 +17,9 @@ import {
idSchema,
assignmentCreateRequestSchema,
assignmentEndDayRequestSchema,
assignmentStayRequestSchema,
type AssignmentEndDayRequest,
type AssignmentStayRequest,
} from '@trek/shared';
import { z } from 'zod';
@@ -102,6 +104,31 @@ export class AssignmentsMcp {
return ok({ assignment });
}
@Tool({
name: 'set_assignment_stay',
description:
'Set how long the road trip stands at this one visit, in minutes. Two visits of the same place keep their own stays: 0 drives past without stopping. Pass null to use the stay of the place (its duration_minutes) again.',
inputSchema: {
tripId: idSchema,
assignmentId: idSchema,
...assignmentStayRequestSchema.shape,
},
annotations: TOOL_ANNOTATIONS_WRITE,
access: { group: 'places', mode: 'write' },
})
async setAssignmentStay(
{ tripId, assignmentId, stay_minutes }: AssignmentStayRequest & { tripId: number; assignmentId: number },
ctx: McpContext,
) {
if (!(await this.assignments.verifyTripAccess(tripId, ctx.userId))) return noAccess();
if (!(await this.guards.hasTripPermission('day_edit', tripId, ctx.userId))) return permissionDenied();
if (!(await this.assignments.getAssignmentForTrip(assignmentId, tripId)))
return errorResult('Assignment not found.');
const assignment = await this.assignments.setStay(assignmentId, stay_minutes);
this.guards.safeBroadcast(tripId, 'assignment:updated', { assignment });
return ok({ assignment });
}
@Tool({
name: 'unassign_place',
description: 'Remove a place assignment from a day.',
@@ -566,6 +566,18 @@ export class AssignmentsService {
return await this.getAssignmentWithPlace(idNum);
}
/**
* How long the drive stands at one visit (#3092). The stay used to be the place's
* alone, so the same place planned twice on a day could not be stopped at once and
* only driven past the other time. Null hands the visit back to the place's own
* `duration_minutes`. No auto-sort and no journey reconcile: a stay moves neither
* the day's order nor the skeleton mirror.
*/
async setStay(id: number, stayMinutes: number | null) {
await this.dayAssignmentsRepo.setStayMinutes(id, stayMinutes);
return await this.getAssignmentWithPlace(id);
}
/**
* Edit the per-assignment note after creation (#2163) — until now the note
* was write-once via the create paths (REST body, MCP tools, plugin RPC) and
+112 -6
View File
@@ -13,6 +13,8 @@ import { activityOf, foundPlacesOf, googleIdsOf, weatherDayOf, weatherOf } from
import type { TripCard } from './assistant-context';
import { FOLLOW_UPS_ANSWER, followUpsOf } from './assistant-follow-ups';
import { outputText } from './assistant-history';
import { OPEN_TRIP_CHAT_TOOL } from './assistant-home';
import { OPEN_JOURNEY_CHAT_TOOL } from './assistant-home-journeys';
import { toolOutputForProvider } from './assistant-output';
import { MAX_CARDS_PER_STEP, MAX_READS_PER_STEP, MAX_STEPS, MAX_WRITES_PER_STEP } from './assistant-tools.constants';
import type { AssistantToolKind, ToolRun } from './assistant-tools.service';
@@ -197,6 +199,10 @@ const GO_ON =
'Your last reply came back empty or was cut off. Carry on from where you were: answer, or make the calls now. ' +
'If the plan is large, propose part of it now; after the user decided you continue with the rest.';
/** The push for an empty step after the ask for the answer: no calls are open to it any more. */
const ANSWER_NOW =
'Your last reply came back empty, and the user still sees no answer. Write your answer to them now, as text.';
/**
* Runs a read at once and records it: where it looks (streamed only), its
* call and answer, and the cards its answer makes (its package's card first,
@@ -255,6 +261,51 @@ function uniqueCallId(call: AiToolCall, used: Set<string>, newId: () => string):
return id;
}
/**
* The provider's own record of a step under the ids its calls go on with. A
* call whose id the thread had used already is renamed (uniqueCallId), and
* the record has to follow: Gemini finds a thought signature by the call's
* id, Anthropic and the Responses API pair each answer with the call of the
* same id, and a record left on the old id was refused on the next request
* that replayed it, which for the last step of an answer is every later
* message of the thread (#3094).
*/
function withRenamedCalls(state: AiProviderState | null, renamed: ReadonlyMap<string, string>): AiProviderState | null {
if (!state || renamed.size === 0) return state;
const idOf = (id: unknown) => (typeof id === 'string' ? (renamed.get(id) ?? id) : id);
if (state.adapter === 'anthropic-messages') {
return {
...state,
blocks: state.blocks.map((block) => (block.type === 'tool_use' ? { ...block, id: idOf(block.id) } : block)),
};
}
if (state.adapter === 'openai-responses') {
return {
...state,
items: state.items.map((item) =>
item.type === 'function_call' ? { ...item, call_id: idOf(item.call_id) } : item,
),
};
}
return {
...state,
signatures: Object.fromEntries(
Object.entries(state.signatures).map(([id, signature]) => [renamed.get(id) ?? id, signature]),
),
};
}
/** The ids the step's calls were renamed from, to the ones they go on with; an id one of its calls kept stays. */
function renamedCalls(received: readonly AiToolCall[], kept: readonly AiToolCall[]): Map<string, string> {
const keptIds = new Set(kept.map((call) => call.id));
const renamed = new Map<string, string>();
received.forEach((call, index) => {
const id = kept[index]?.id;
if (id && call.id !== id && !keptIds.has(call.id) && !renamed.has(call.id)) renamed.set(call.id, id);
});
return renamed;
}
/** What every step of one turn shares. */
interface Turn {
deps: AgentDeps;
@@ -264,8 +315,20 @@ interface Turn {
history: AiChatMessage[];
usage: { inputTokens: number; cachedInputTokens: number; outputTokens: number };
knownPlaceIds: Set<string>;
/** Whether the turn used its one push (an empty, cut-off, announcing or wordless answer). */
/** Whether the turn used its one push (an empty, cut-off or announcing answer). */
nudged: boolean;
/**
* Whether the turn already asked for the answer after a step that only
* offered follow-ups. Apart from the push, so a turn that was pushed once
* still gets its answer written (#2789); each is asked at most once.
*/
askedForAnswer: boolean;
/**
* The next request may only be answered with text: the step after the ask
* for the answer. Claude Sonnet 5.5 offered its follow-ups again instead of
* writing, and the turn ended without an answer (#3016).
*/
textOnlyNext: boolean;
/** The chips of follow-ups offered before any answer, shown under the answer once it is written. */
followUps: AssistantFollowUps | null;
}
@@ -354,6 +417,9 @@ function searchParts(
/** Streams one answer of the model; a failure or a stop ends the turn here, saved as far as it got. */
async function streamAnswer(turn: Turn, rowId: number): Promise<StepAnswer | AgentOutcome> {
const { deps, input, w, signal } = turn;
// Taken once: only the step right after the ask for the answer is held to text.
const textOnly = turn.textOnlyNext;
turn.textOnlyNext = false;
const answer: StepAnswer = {
text: '',
received: [],
@@ -371,6 +437,7 @@ async function streamAnswer(turn: Turn, rowId: number): Promise<StepAnswer | Age
tools: deps.tools.specs(),
cacheKey: input.cacheKey,
webSearch: deps.webSearch === true && deps.client.webSearch,
...(textOnly ? { toolChoice: 'none' as const } : {}),
},
signal,
);
@@ -528,7 +595,8 @@ function answeredAsk(call: AiToolCall): AssistantMessagePart {
*/
async function askForTheAnswer(turn: Turn, rowId: number, step: StepRecord & { asks: AiToolCall[] }): Promise<null> {
const { asks, providerState } = step;
turn.nudged = true;
turn.askedForAnswer = true;
turn.textOnlyNext = true;
turn.followUps = followUpsOf(asks[asks.length - 1].args);
const parts = [...step.parts, ...asks.map((call) => ({ ...answeredAsk(call), result: WRITE_THE_ANSWER }))];
await turn.deps.store.saveStep(rowId, { parts, providerState }, 'complete');
@@ -540,6 +608,12 @@ async function askForTheAnswer(turn: Turn, rowId: number, step: StepRecord & { a
return null;
}
/**
* The calls that only hand the conversation on, to a trip's or a journey's own
* chat: a card for the user, nothing the model reads further.
*/
const HANDOFF_TOOLS: ReadonlySet<string> = new Set([OPEN_TRIP_CHAT_TOOL, OPEN_JOURNEY_CHAT_TOOL]);
/** One step of the turn: the outcome when the turn ends with it, null when the model is asked again. */
async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
const { deps, input, w, signal, history } = turn;
@@ -547,8 +621,9 @@ async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
w.startStep();
const answer = await streamAnswer(turn, rowId);
if ('end' in answer) return answer;
const { text, providerState, cutOff } = answer;
const { text, cutOff } = answer;
const all = answer.received.map((call) => ({ ...call, id: uniqueCallId(call, input.usedCallIds, deps.newId) }));
const providerState = withRenamedCalls(answer.providerState, renamedCalls(answer.received, all));
// The follow-ups the model offers are the server's to answer: they never run, and alone they end the turn.
const asks = all.filter((call) => call.name === ASSISTANT_FOLLOW_UPS_TOOL);
const received = all.filter((call) => call.name !== ASSISTANT_FOLLOW_UPS_TOOL);
@@ -560,18 +635,34 @@ async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
...searched.tail,
];
const empty = received.length === 0 && !text.trim();
if (empty && asks.length > 0 && !turn.nudged) return askForTheAnswer(turn, rowId, { parts, providerState, asks });
if (empty && asks.length > 0 && !turn.askedForAnswer)
return askForTheAnswer(turn, rowId, { parts, providerState, asks });
// An answer that offered follow-ups is finished, however it ends.
const settled = asks.length > 0 && !empty;
const announced = !settled && received.length === 0 && step === 0 && ANNOUNCED.test(text.trimEnd());
const push = empty || (!settled && received.length === 0 && cutOff) ? GO_ON : announced ? MAKE_THE_CALLS : null;
if (push && !turn.nudged) {
const push =
empty && turn.askedForAnswer
? ANSWER_NOW
: empty || (!settled && received.length === 0 && cutOff)
? GO_ON
: announced
? MAKE_THE_CALLS
: null;
// Follow-ups with no answer a second time end the turn: the model was already asked for the answer.
const wordlessAgain = empty && asks.length > 0;
if (push && !turn.nudged && !wordlessAgain) {
turn.nudged = true;
await deps.store.saveStep(rowId, { parts, providerState }, 'complete');
w.finishStep();
// An empty assistant message is one the providers refuse; only the push goes back.
if (!empty) history.push({ role: 'assistant', text, toolCalls: [], providerState });
history.push({ role: 'user', text: push });
// Once the answer was asked for, the push is held to text as well. Claude
// Sonnet 5.5 hands back the words it writes before a tool call as a hidden
// thinking block: with tools open again it answers, offers follow-ups once
// more, and the answer is lost a second time. The chips of the first offer
// are kept in turn.followUps.
if (turn.askedForAnswer) turn.textOnlyNext = true;
return null;
}
if (empty) {
@@ -618,6 +709,19 @@ async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
return outcomeOf(turn, 'aborted');
}
if (held.length > 0) return holdChanges(turn, rowId, { parts, providerState }, held);
// A step that wrote its answer and only handed the conversation on is the end of
// the turn. Asked once more after the card, the model wrote the same answer again.
if (text.trim() && received.every((call) => HANDOFF_TOOLS.has(call.name))) {
const followUps = asks.length > 0 ? followUpsOf(asks[asks.length - 1].args) : turn.followUps;
if (followUps) {
parts.push({ type: 'data-trek-follow-ups', data: followUps });
w.followUps(followUps);
}
await deps.store.saveStep(rowId, { parts, providerState }, 'complete');
w.finishStep();
finishTurn(turn, 'stop');
return outcomeOf(turn, 'stop');
}
const { images, modelTexts } = collected;
await carryOn(turn, rowId, { parts, providerState }, { text, calls, images, modelTexts });
return null;
@@ -663,6 +767,8 @@ export async function runAgentLoop(
usage: { inputTokens: 0, cachedInputTokens: 0, outputTokens: 0 },
knownPlaceIds,
nudged: false,
askedForAnswer: false,
textOnlyNext: false,
followUps: null,
};
return stepsFrom(turn, 0);

Some files were not shown because too many files have changed in this diff Show More