feat: quality of life sweep from the feature discussions

Small requests from the discussions, each built end to end: planner and
map (cycling mode, date first headings, clear a day, stay times per day,
off-route stops, map lock, compact unplanned markers, pending stays,
cable car, transit quick picks), places (location filter, category from
OSM, open in maps, geo: link, image from an attachment, enrichment on
file import, places near a pin, place name language, Amap first in
China), lists and costs (packed counter, inherited sharing, Markdown
to-dos, payment notes, bookings CSV, my plan PDF), sharing (unplanned
places, travel only, without photos), journey (photo zoom, drafts,
photo order, trip suggestion, day jump), atlas (region counter, region
bucket list, places per country), vacay half company holidays, studio
copy and paste plus the book's own layouts, and RESTORE_FROM_BACKUP for
a first start restore.
This commit is contained in:
Maurice
2026-09-30 23:00:12 +02:00
parent 91dfab3f4b
commit 8fd46c4c18
691 changed files with 9491 additions and 927 deletions
+1
View File
@@ -42,6 +42,7 @@ OIDC_SCOPE=openid email profile # Fully overrides the default. Add extra scopes
DEMO_MODE=false # Demo mode - resets data hourly
# RESTORE_FROM_BACKUP=/app/data/backup.zip # Restore this backup on the first start, before any setup. Ignored once a database exists.
# BACKUP_UPLOAD_LIMIT_MB=500 # Max size (MB) of a backup archive you can upload when restoring. Raise it if your backup exceeds 500 MB. If you sit behind a reverse proxy, raise its upload limit too (e.g. nginx client_max_body_size).
# TREK_DB_JOURNAL_MODE=WAL # SQLite journal mode (DELETE, TRUNCATE, PERSIST, MEMORY, WAL, OFF). WAL is fastest and the right choice on local disk. Set DELETE if the data directory lives on network storage (Azure App Service, an SMB/NFS volume on a NAS) — WAL coordinates writers through a shared-memory file that those filesystems do not implement reliably. The mode is stored inside the database file, so it survives a restart; changing this applies on the next boot.
+2
View File
@@ -304,6 +304,8 @@ export function deriveBackup(raw: RawEnv) {
maxDecompressedMb: positiveNumberOr(raw.BACKUP_MAX_DECOMPRESSED_MB, 5 * 1024),
/** backupService only bundles data/.encryption_key into archives when the key does NOT come from env. */
encryptionKeyFromEnv: !!raw.ENCRYPTION_KEY,
/** Archive restored on the first start, while no database exists yet (#1089). Null when unset or blank. */
restoreFromBackup: raw.RESTORE_FROM_BACKUP?.trim() || null,
};
}
+2
View File
@@ -161,6 +161,8 @@ export const envSchema = z.object({
TREK_PLACE_PHOTO_DIR: anyString,
BACKUP_UPLOAD_LIMIT_MB: positiveNumber,
BACKUP_MAX_DECOMPRESSED_MB: positiveNumber,
// A backup archive to restore on the very first start, before any setup (#1089).
RESTORE_FROM_BACKUP: anyString,
// Admin / demo
ADMIN_EMAIL: anyString,
+66
View File
@@ -5336,6 +5336,72 @@ function runMigrations(db: Database.Database): void {
* is a no-op.
*/
() => createPlaceRegionsFollowPlaceTrigger(db),
/*
* A stop kept on the day but left out of its route (#2532): shown as a pin, not
* driven to. 0 routes it like every stop before this existed.
*/
() => {
const has = db.prepare("SELECT 1 FROM pragma_table_info('day_assignments') WHERE name = 'route_excluded'").get();
if (!has) db.exec('ALTER TABLE day_assignments ADD COLUMN route_excluded INTEGER NOT NULL DEFAULT 0');
},
/*
* How many of an item are packed so far (#2296), for "7 of 10 shirts". NULL is the
* plain checkbox every item has had, and `checked` stays the answer to "done?".
*/
() => {
const has = db.prepare("SELECT 1 FROM pragma_table_info('packing_items') WHERE name = 'packed_quantity'").get();
if (!has) db.exec('ALTER TABLE packing_items ADD COLUMN packed_quantity INTEGER');
},
/*
* A bucket-list wish for one state or province rather than the whole country
* (#1901). NULL keeps every existing wish a country wish.
*/
() => {
const has = db.prepare("SELECT 1 FROM pragma_table_info('bucket_list') WHERE name = 'region_code'").get();
if (!has) db.exec('ALTER TABLE bucket_list ADD COLUMN region_code TEXT');
},
/*
* Half company holidays (#2439): 0.5 covers the morning or afternoon and leaves
* the other half open for a half vacation day. 1 is what every holiday was.
*/
() => {
const has = db.prepare("SELECT 1 FROM pragma_table_info('vacay_company_holidays') WHERE name = 'fraction'").get();
if (!has) db.exec('ALTER TABLE vacay_company_holidays ADD COLUMN fraction REAL NOT NULL DEFAULT 1');
},
/*
* A journey entry still being written (#696): contributors see it, the public
* share page does not. Its own flag, since `visibility` defaults to 'private' on
* every entry and filtering on it would empty every shared journey.
*/
() => {
const has = db.prepare("SELECT 1 FROM pragma_table_info('journey_entries') WHERE name = 'is_draft'").get();
if (!has) db.exec('ALTER TABLE journey_entries ADD COLUMN is_draft INTEGER NOT NULL DEFAULT 0');
},
/*
* Two narrower ways to share a trip (#1712): only the travel (flights, trains,
* stays) without the day's activities, and without the place photos. Both off
* keeps every existing link exactly as it was.
*/
() => {
const hasTravel = db.prepare("SELECT 1 FROM pragma_table_info('share_tokens') WHERE name = 'share_travel_only'").get();
if (!hasTravel) db.exec('ALTER TABLE share_tokens ADD COLUMN share_travel_only INTEGER NOT NULL DEFAULT 0');
const hasImages = db.prepare("SELECT 1 FROM pragma_table_info('share_tokens') WHERE name = 'share_hide_images'").get();
if (!hasImages) db.exec('ALTER TABLE share_tokens ADD COLUMN share_hide_images INTEGER NOT NULL DEFAULT 0');
},
/**
* A note on a settle-up payment (#2340), as expenses already have one:
* "paid back in cash at the airport". Null on every existing payment.
*/
() => {
const has = db.prepare("SELECT 1 FROM pragma_table_info('budget_settlements') WHERE name = 'note'").get();
if (!has) db.exec('ALTER TABLE budget_settlements ADD COLUMN note TEXT');
},
];
if (currentVersion < migrations.length) {
+44 -1
View File
@@ -8,7 +8,10 @@ import path from 'node:path';
import fs from 'node:fs';
import http from 'node:http';
import type { INestApplication } from '@nestjs/common';
import { buildApp, getHttpServer } from './bootstrap';
// bootstrap is required inside bootstrap() below, not imported here: importing
// it opens the database, and a first-start restore (#1089) has to put the
// backup's database in place before that happens.
import type * as Bootstrap from './bootstrap';
// data/tmp is the driver-agnostic global scratch dir (restore-upload spool,
// mirror stream staging) and stays boot-created here. Driver-owned roots — the
@@ -87,7 +90,10 @@ async function bootstrap(): Promise<void> {
// global pipeline + /uploads + every /api domain + the platform/transport routes
// (/mcp, /.well-known, OAuth SDK, SPA catch-all). buildApp() owns the composition
// order; it is shared with the integration-test harness so they can't drift.
const restore = await restoreBeforeTheDatabaseOpens();
const { buildApp, getHttpServer } = require('./bootstrap') as typeof Bootstrap;
nestApp = await buildApp();
if (restore.restored) await finishFirstBootRestore(nestApp, restore);
// The server buildApp created and bound /ws to. Creating a second one here
// would serve the REST API fine and leave the gateway attached to a socket
// nobody listens on.
@@ -110,6 +116,43 @@ async function bootstrap(): Promise<void> {
else server.listen(PORT, onListen);
}
/**
* RESTORE_FROM_BACKUP on a first start (#1089), before anything opens the
* database. See nest/backup/boot-restore.ts for when it runs and why it fails
* the start rather than coming up empty.
*/
async function restoreBeforeTheDatabaseOpens() {
const { restoreOnFirstBoot } = require('./nest/backup/boot-restore') as typeof import('./nest/backup/boot-restore');
const env = readEnv();
const dataDir = path.join(__dirname, '../data');
return restoreOnFirstBoot({
archive: env.backup.restoreFromBackup,
dbFile: env.db.trekDbFile || path.join(dataDir, 'travel.db'),
dataDir,
});
}
/**
* The uploads go in once storage is up, through the same driver every upload
* uses, so an instance on S3 gets them in S3. A failure here leaves the restored
* database running and says where the files are, rather than stopping a start
* whose data is already in place.
*/
async function finishFirstBootRestore(
app: INestApplication,
restore: { uploads: string | null; staging: string },
): Promise<void> {
const { StorageService } = require('./nest/storage/storage.service') as typeof import('./nest/storage/storage.service');
const { rehydrateUploads } = require('./nest/backup/backup.impl') as typeof import('./nest/backup/backup.impl');
try {
if (restore.uploads) await rehydrateUploads(app.get(StorageService), restore.uploads);
fs.rmSync(restore.staging, { recursive: true, force: true });
console.log('[restore] Backup restored. Sign in with an account from the backup.');
} catch (err) {
console.error(`[restore] The database was restored, but the uploads could not be copied (${err instanceof Error ? err.message : String(err)}). They are still in ${restore.staging}.`);
}
}
bootstrap().catch((err) => {
console.error('Fatal: failed to bootstrap server', err);
process.exit(1);
@@ -20,6 +20,7 @@ import {
AssignmentEndDayDto,
AssignmentNotesDto,
AssignmentTransportDto,
AssignmentRouteDto,
AssignmentParticipantsDto,
} from './assignments.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
@@ -93,6 +94,26 @@ export class DayAssignmentsController {
return { success: true };
}
// Declared before ':id' so the bare collection path is never read as an id (#2470).
@RequirePermission('day_edit')
@Delete()
clear(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('dayId') dayId: string,
@Headers('x-socket-id') socketId?: string,
) {
if (!this.assignments.dayExists(dayId, tripId)) {
throw new HttpException({ error: 'Day not found' }, 404);
}
const removedIds = this.assignments.clearDay(dayId);
for (const assignmentId of removedIds) {
this.assignments.broadcast(tripId, 'assignment:deleted', { assignmentId, dayId: Number(dayId) }, socketId);
}
if (removedIds.length > 0) this.assignments.reconcile(tripId, socketId);
return { success: true, removedIds };
}
@RequirePermission('day_edit')
@Delete(':id')
remove(
@@ -237,6 +258,23 @@ export class AssignmentOpsController {
return { assignment };
}
@RequirePermission('day_edit')
@Put(':id/route')
route(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('id') id: string,
@Body() body: AssignmentRouteDto,
@Headers('x-socket-id') socketId?: string,
) {
if (!this.assignments.getAssignmentForTrip(id, tripId)) {
throw new HttpException({ error: 'Assignment not found' }, 404);
}
const assignment = this.assignments.setRouteExcluded(id, body.excluded);
this.assignments.broadcast(tripId, 'assignment:updated', { assignment }, socketId);
return { assignment };
}
@RequirePermission('day_edit')
@Put(':id/participants')
setParticipants(
@@ -7,6 +7,7 @@ import {
assignmentEndDayRequestSchema,
assignmentNotesRequestSchema,
assignmentTransportRequestSchema,
assignmentRouteRequestSchema,
assignmentParticipantsRequestSchema,
} from '@trek/shared';
@@ -24,4 +25,5 @@ export class AssignmentTimeDto extends createZodDto(assignmentTimeRequestSchema)
export class AssignmentEndDayDto extends createZodDto(assignmentEndDayRequestSchema) {}
export class AssignmentNotesDto extends createZodDto(assignmentNotesRequestSchema) {}
export class AssignmentTransportDto extends createZodDto(assignmentTransportRequestSchema) {}
export class AssignmentRouteDto extends createZodDto(assignmentRouteRequestSchema) {}
export class AssignmentParticipantsDto extends createZodDto(assignmentParticipantsRequestSchema) {}
@@ -107,6 +107,30 @@ export class AssignmentsMcp {
return ok({ success: true });
}
@Tool({
name: 'clear_day_assignments',
description: 'Remove every place from one day in a single step. The day itself, its notes and its bookings stay; the places stay in the trip and can be planned again. Returns the removed assignment ids.',
inputSchema: {
tripId: z.number().int().positive(),
dayId: z.number().int().positive(),
},
annotations: TOOL_ANNOTATIONS_DELETE,
access: { group: 'places', mode: 'write' },
})
async clearDayAssignments(
{ tripId, dayId }: { tripId: number; dayId: number },
ctx: McpContext,
) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
if (!this.assignments.verifyTripAccess(tripId, ctx.userId)) return noAccess();
if (!this.guards.hasTripPermission('day_edit', tripId, ctx.userId)) return permissionDenied();
if (!this.assignments.dayExists(dayId, tripId)) return errorResult('Day not found.');
const removedIds = this.assignments.clearDay(dayId);
for (const assignmentId of removedIds) this.guards.safeBroadcast(tripId, 'assignment:deleted', { assignmentId, dayId });
if (removedIds.length > 0) this.assignments.reconcile(tripId);
return ok({ success: true, removedIds });
}
@Tool({
name: 'update_assignment_time',
description: 'Set the start and/or end time for a place assignment on a day (e.g. "09:00", "11:30"). Pass null to clear a time.',
@@ -199,6 +223,30 @@ export class AssignmentsMcp {
return ok({ assignment });
}
@Tool({
name: 'set_assignment_route_excluded',
description: 'Keep a place on its day but leave it out of that day route, or put it back. An excluded stop still shows in the day plan and on the map, and the route runs from the stop before it straight to the one after. Use it for a place only visited on foot from somewhere nearby, or one that is just a point of interest.',
inputSchema: {
tripId: z.number().int().positive(),
assignmentId: z.number().int().positive(),
excluded: z.boolean().describe('true leaves the stop out of the route, false routes it again'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
access: { group: 'places', mode: 'write' },
})
async setAssignmentRouteExcluded(
{ tripId, assignmentId, excluded }: { tripId: number; assignmentId: number; excluded: boolean },
ctx: McpContext,
) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
if (!this.assignments.verifyTripAccess(tripId, ctx.userId)) return noAccess();
if (!this.guards.hasTripPermission('day_edit', tripId, ctx.userId)) return permissionDenied();
if (!this.assignments.getAssignmentForTrip(assignmentId, tripId)) return errorResult('Assignment not found.');
const assignment = this.assignments.setRouteExcluded(assignmentId, excluded);
this.guards.safeBroadcast(tripId, 'assignment:updated', { assignment });
return ok({ assignment });
}
@Tool({
name: 'move_assignment',
description: 'Move a place assignment to a different day.',
@@ -202,6 +202,19 @@ export class AssignmentsService {
this.dbs.run('DELETE FROM day_assignments WHERE id = ?', id);
}
/**
* Empties a day: every place comes off it in one transaction, while the day, its
* notes and its bookings stay (#2470). Returns the removed ids so each can be
* announced the way a single unassign is.
*/
clearDay(dayId: string | number): number[] {
return this.dbs.transaction(() => {
const ids = this.dbs.all<{ id: number }>('SELECT id FROM day_assignments WHERE day_id = ?', dayId).map(r => r.id);
this.dbs.run('DELETE FROM day_assignments WHERE day_id = ?', dayId);
return ids;
});
}
reorderAssignments(dayId: string | number, orderedIds: number[]): void {
const update = this.dbs.prepare('UPDATE day_assignments SET order_index = ? WHERE id = ? AND day_id = ?');
this.dbs.transaction(() => {
@@ -411,6 +424,15 @@ export class AssignmentsService {
return this.getAssignmentWithPlace(Number(id));
}
/**
* Keep a stop on the day but out of its route (#2532): it stays in the list and on
* the map, and the route runs from the stop before it straight to the one after.
*/
setRouteExcluded(id: string | number, excluded: boolean) {
this.dbs.run('UPDATE day_assignments SET route_excluded = ? WHERE id = ?', excluded ? 1 : 0, id);
return this.getAssignmentWithPlace(Number(id));
}
/**
* Both callers have already proven the assignment sits on `tripId`; this
* settles the other half, that the ids in the body do too. Off-roster ids drop
+2 -2
View File
@@ -152,9 +152,9 @@ export class AtlasController {
if (!body.name?.trim()) {
throw new HttpException({ error: 'Name is required' }, 400);
}
const { name, lat, lng, country_code, notes, target_date } = body;
const { name, lat, lng, country_code, notes, target_date, region_code } = body;
try {
return { item: this.atlas.createBucketItem(user.id, { name, lat, lng, country_code, notes, target_date }) };
return { item: this.atlas.createBucketItem(user.id, { name, lat, lng, country_code, notes, target_date, region_code }) };
} catch (err) {
// #1898: the same wish twice is a conflict, not a server error. Bespoke
// { error } body like the neighbouring 400/404s.
+8 -3
View File
@@ -2,7 +2,7 @@ import {
McpController, Tool, Resource, type McpContext,
TOOL_ANNOTATIONS_READONLY, TOOL_ANNOTATIONS_WRITE,
TOOL_ANNOTATIONS_DELETE, TOOL_ANNOTATIONS_NON_IDEMPOTENT,
demoDenied, ok,
demoDenied, errorResult, ok,
} from '../../nest-mcp';
import { z } from 'zod';
import { ADDON_IDS } from '../../addons';
@@ -77,18 +77,23 @@ export class AtlasMcp {
// "same place, different date" case the report calls for was unreachable.
// update_bucket_list_item has had the field all along.
target_date: z.string().nullable().optional().describe('When you plan to go, e.g. "2027-05"'),
region_code: z.string().regex(/^[A-Za-z]{2}-[A-Za-z0-9]{1,8}$/).optional().describe('ISO 3166-2 code of a state or province on the wish list, e.g. "US-CA"; needs country_code and has to belong to it. Hatches that region on the Atlas map'),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: atlasAddonOn,
access: { group: 'atlas', mode: 'write' },
})
async createBucketListItem(
{ name, lat, lng, country_code, notes, target_date }: { name: string; lat?: number; lng?: number; country_code?: string; notes?: string; target_date?: string | null },
{ name, lat, lng, country_code, notes, target_date, region_code }: { name: string; lat?: number; lng?: number; country_code?: string; notes?: string; target_date?: string | null; region_code?: string },
ctx: McpContext,
) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
// The same rule the REST contract enforces: a region comes with its own country.
if (region_code && (!country_code || !region_code.toUpperCase().startsWith(`${country_code.toUpperCase()}-`))) {
return errorResult('region_code must belong to country_code.');
}
try {
const item = this.atlas.createBucketItem(ctx.userId, { name, lat, lng, country_code, notes, target_date });
const item = this.atlas.createBucketItem(ctx.userId, { name, lat, lng, country_code, notes, target_date, region_code });
return ok({ item });
} catch (err) {
if (err instanceof BucketItemExistsError) return bucketDuplicateResult();
+15 -1
View File
@@ -47,6 +47,7 @@ export type CreateBucketData = {
country_code?: string | null;
notes?: string | null;
target_date?: string | null;
region_code?: string | null;
};
export type UpdateBucketData = {
@@ -95,6 +96,18 @@ function blankToNull(value: string | null | undefined): string | null {
return value === undefined || value === null || value === '' ? null : value;
}
/**
* A wished-for region as stored (#1901): ISO 3166-2, upper case as the region
* boundaries carry it, and only when it lies in the item's own country. Anything
* else is no region, which keeps a caller that skips the contract (the plugin RPC)
* from pinning a wish to somebody else's map.
*/
export function bucketRegionCode(region: string | null | undefined, country: string | null): string | null {
const code = blankToNull(region)?.toUpperCase() ?? null;
if (!code || !country || !/^[A-Z]{2}-[A-Z0-9]{1,8}$/.test(code)) return null;
return code.startsWith(`${country.toUpperCase()}-`) ? code : null;
}
/**
* Who ticked a country off, for the detail sheet.
*
@@ -986,7 +999,7 @@ export class AtlasService {
if (this.findDuplicateBucketItem(userId, identity)) throw new BucketItemExistsError();
const result = this.db
.prepare(
'INSERT INTO bucket_list (user_id, name, lat, lng, country_code, notes, target_date) VALUES (?, ?, ?, ?, ?, ?, ?)',
'INSERT INTO bucket_list (user_id, name, lat, lng, country_code, notes, target_date, region_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
)
.run(
userId,
@@ -996,6 +1009,7 @@ export class AtlasService {
identity.country_code,
data.notes ?? null,
identity.target_date,
bucketRegionCode(data.region_code, identity.country_code),
);
return this.db.prepare('SELECT * FROM bucket_list WHERE id = ?').get(result.lastInsertRowid);
}
+123
View File
@@ -0,0 +1,123 @@
import fs from 'fs';
import path from 'path';
import unzipper from 'unzipper';
import type Database from 'better-sqlite3';
import { readEnv } from '../../app-config';
import { openDatabase } from '../../db/connection';
/**
* Reading a backup archive, without touching the live database.
*
* Split out of backup.impl.ts so the restore that runs before the database is
* opened (boot-restore.ts, #1089) can use the same guards as the one in the
* admin panel: importing backup.impl.ts opens the database as a side effect,
* which is exactly what a first-boot restore has to happen before.
*/
// Frozen at import on purpose (legacy timing, see backup.impl.ts).
const backupEnv = readEnv().backup;
export const MAX_BACKUP_UPLOAD_SIZE = backupEnv.uploadLimitMb * 1024 * 1024; // compressed
export const MAX_BACKUP_DECOMPRESSED_SIZE = backupEnv.maxDecompressedMb * 1024 * 1024;
/** A refusal in the restore route's own words and status. */
export interface ArchiveRefusal {
error: string;
status: number;
}
/**
* Unpack an archive into `extractDir`, or say why not.
*
* Fast reject on the central-directory's declared size, then extract entry-by-entry
* enforcing the ACTUAL decompressed bytes. The declared uncompressedSize is
* attacker-declarable — a zip bomb can under-report it and expand past the cap during
* extraction — so the real guard counts bytes as they are written and aborts once the
* running total crosses the cap. Each entry's resolved path is also confined to
* extractDir (a `../` entry that escaped the root — zip-slip — is refused).
*
* A refusal leaves nothing behind. Any other failure is thrown, also with the
* directory removed.
*/
export async function extractBackupArchive(zipPath: string, extractDir: string): Promise<ArchiveRefusal | null> {
const directory = await unzipper.Open.file(zipPath);
const claimedSize = directory.files.reduce((sum, f) => sum + (f.uncompressedSize || 0), 0);
if (claimedSize > MAX_BACKUP_DECOMPRESSED_SIZE) {
return { error: 'Backup exceeds the maximum decompressed size.', status: 400 };
}
fs.mkdirSync(extractDir, { recursive: true });
let decompressedBytes = 0;
for (const entry of directory.files) {
if (entry.type === 'Directory') continue;
const dest = path.join(extractDir, entry.path);
const rel = path.relative(extractDir, dest);
if (rel.startsWith('..') || path.isAbsolute(rel)) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { error: 'Invalid backup: an entry path escapes the archive root.', status: 400 };
}
fs.mkdirSync(path.dirname(dest), { recursive: true });
try {
await new Promise<void>((resolve, reject) => {
const source = entry.stream();
const out = fs.createWriteStream(dest);
source.on('data', (chunk: Buffer) => {
decompressedBytes += chunk.length;
if (decompressedBytes > MAX_BACKUP_DECOMPRESSED_SIZE) {
source.destroy();
out.destroy();
reject(new Error('DECOMPRESSED_CAP_EXCEEDED'));
}
});
source.on('error', reject);
out.on('error', reject);
out.on('finish', resolve);
source.pipe(out);
});
} catch (err) {
fs.rmSync(extractDir, { recursive: true, force: true });
if (err instanceof Error && err.message === 'DECOMPRESSED_CAP_EXCEEDED') {
return { error: 'Backup exceeds the maximum decompressed size.', status: 400 };
}
throw err;
}
}
return null;
}
/**
* Whether an unpacked archive holds a TREK database that can be put in place:
* present, intact, and carrying the tables every TREK has. Read-only; the file
* is closed again before this returns.
*/
export function checkBackupDatabase(extractDir: string): ArchiveRefusal | null {
const extractedDb = path.join(extractDir, 'travel.db');
if (!fs.existsSync(extractedDb)) {
return { error: 'Invalid backup: travel.db not found', status: 400 };
}
let uploadedDb: InstanceType<typeof Database> | null = null;
try {
uploadedDb = openDatabase(extractedDb, { readonly: true });
const integrityResult = uploadedDb.prepare('PRAGMA integrity_check').get() as { integrity_check: string };
if (integrityResult.integrity_check !== 'ok') {
return { error: `Uploaded database failed integrity check: ${integrityResult.integrity_check}`, status: 400 };
}
const requiredTables = ['users', 'trips', 'trip_members', 'places', 'days'];
const existingTables = uploadedDb
.prepare("SELECT name FROM sqlite_master WHERE type='table'")
.all() as { name: string }[];
const tableNames = new Set(existingTables.map(t => t.name));
for (const table of requiredTables) {
if (!tableNames.has(table)) {
return { error: `Uploaded database is missing required table: ${table}. This does not appear to be a TREK backup.`, status: 400 };
}
}
return null;
} catch {
return { error: 'Uploaded file is not a valid SQLite database', status: 400 };
} finally {
uploadedDb?.close();
}
}
+11 -93
View File
@@ -1,13 +1,11 @@
import archiver from 'archiver';
import unzipper from 'unzipper';
import path from 'path';
import { pipeline } from 'node:stream/promises';
import { readEnv } from '../../app-config';
import fs from 'fs';
import type Database from 'better-sqlite3';
import { openDatabase } from '../../db/connection';
import { db, closeDb, reinitialize } from '../../db/database';
import { VALID_INTERVALS } from './auto-backup.settings';
import { checkBackupDatabase, extractBackupArchive } from './backup-archive';
import { invalidatePermissionsCache } from '../permissions/permissions-cache';
import { pluginsCodeRoot, pluginsDataRoot } from '../plugins/paths';
import { stageExtractedPluginTrees, applyStagedRestoreNow } from '../plugins/plugin-backup';
@@ -24,16 +22,9 @@ const dataDir = path.join(__dirname, '../../../data');
// Compressed upload cap for restore archives. Defaults to 500 MB, raisable via
// BACKUP_UPLOAD_LIMIT_MB for instances whose backups (uploads/ included) grow
// past that. Malformed values abort boot (app-config fail-fast validation);
// frozen at import on purpose (legacy timing).
const backupEnv = readEnv().backup;
export const MAX_BACKUP_UPLOAD_SIZE = backupEnv.uploadLimitMb * 1024 * 1024; // compressed
// Upper bound on the TOTAL decompressed size of a restore archive (the upload
// limit only caps the compressed bytes). Default 5 GB, raisable via
// BACKUP_MAX_DECOMPRESSED_MB for an instance whose own backups (now including the
// plugin trees) legitimately grow past it — otherwise its own backups become
// unrestorable.
export const MAX_BACKUP_DECOMPRESSED_SIZE = backupEnv.maxDecompressedMb * 1024 * 1024;
// past that. Upper bound on the TOTAL decompressed size: default 5 GB, raisable
// via BACKUP_MAX_DECOMPRESSED_MB. Both live beside the archive reader now.
export { MAX_BACKUP_UPLOAD_SIZE, MAX_BACKUP_DECOMPRESSED_SIZE } from './backup-archive';
// ---------------------------------------------------------------------------
// Helpers
@@ -350,7 +341,7 @@ const isBackupCategory = (dir: string): dir is (typeof BACKUP_UPLOAD_CATEGORIES)
* with a warning (2026-08-17 decision): new archives never contain them, and
* the category mapping stays structural in both directions.
*/
async function rehydrateUploads(storage: StorageService, extractedUploads: string): Promise<void> {
export async function rehydrateUploads(storage: StorageService, extractedUploads: string): Promise<void> {
const walk = (dir: string): string[] =>
fs.readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
const p = path.join(dir, e.name);
@@ -380,88 +371,15 @@ export async function restoreFromZip(storage: StorageService, zipPath: string):
const extractDir = path.join(dataDir, `restore-${Date.now()}`);
let reinitFailed: unknown = null;
try {
// Fast reject on the central-directory's declared size, then extract entry-by-entry
// enforcing the ACTUAL decompressed bytes. The declared uncompressedSize is
// attacker-declarable — a zip bomb can under-report it and expand past the cap during
// extraction — so the real guard counts bytes as they are written and aborts once the
// running total crosses the cap. Each entry's resolved path is also confined to
// extractDir (a `../` entry that escaped the root — zip-slip — is refused).
const directory = await unzipper.Open.file(zipPath);
const claimedSize = directory.files.reduce((sum, f) => sum + (f.uncompressedSize || 0), 0);
if (claimedSize > MAX_BACKUP_DECOMPRESSED_SIZE) {
return { success: false, error: 'Backup exceeds the maximum decompressed size.', status: 400 };
}
const refused = await extractBackupArchive(zipPath, extractDir);
if (refused) return { success: false, ...refused };
fs.mkdirSync(extractDir, { recursive: true });
let decompressedBytes = 0;
for (const entry of directory.files) {
if (entry.type === 'Directory') continue;
const dest = path.join(extractDir, entry.path);
const rel = path.relative(extractDir, dest);
if (rel.startsWith('..') || path.isAbsolute(rel)) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, error: 'Invalid backup: an entry path escapes the archive root.', status: 400 };
}
fs.mkdirSync(path.dirname(dest), { recursive: true });
try {
await new Promise<void>((resolve, reject) => {
const source = entry.stream();
const out = fs.createWriteStream(dest);
source.on('data', (chunk: Buffer) => {
decompressedBytes += chunk.length;
if (decompressedBytes > MAX_BACKUP_DECOMPRESSED_SIZE) {
source.destroy();
out.destroy();
reject(new Error('DECOMPRESSED_CAP_EXCEEDED'));
}
});
source.on('error', reject);
out.on('error', reject);
out.on('finish', resolve);
source.pipe(out);
});
} catch (err) {
fs.rmSync(extractDir, { recursive: true, force: true });
if (err instanceof Error && err.message === 'DECOMPRESSED_CAP_EXCEEDED') {
return { success: false, error: 'Backup exceeds the maximum decompressed size.', status: 400 };
}
throw err;
}
const unreadable = checkBackupDatabase(extractDir);
if (unreadable) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, ...unreadable };
}
const extractedDb = path.join(extractDir, 'travel.db');
if (!fs.existsSync(extractedDb)) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, error: 'Invalid backup: travel.db not found', status: 400 };
}
let uploadedDb: InstanceType<typeof Database> | null = null;
try {
uploadedDb = openDatabase(extractedDb, { readonly: true });
const integrityResult = uploadedDb.prepare('PRAGMA integrity_check').get() as { integrity_check: string };
if (integrityResult.integrity_check !== 'ok') {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, error: `Uploaded database failed integrity check: ${integrityResult.integrity_check}`, status: 400 };
}
const requiredTables = ['users', 'trips', 'trip_members', 'places', 'days'];
const existingTables = uploadedDb
.prepare("SELECT name FROM sqlite_master WHERE type='table'")
.all() as { name: string }[];
const tableNames = new Set(existingTables.map(t => t.name));
for (const table of requiredTables) {
if (!tableNames.has(table)) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, error: `Uploaded database is missing required table: ${table}. This does not appear to be a TREK backup.`, status: 400 };
}
}
} catch (err) {
fs.rmSync(extractDir, { recursive: true, force: true });
return { success: false, error: 'Uploaded file is not a valid SQLite database', status: 400 };
} finally {
uploadedDb?.close();
}
closeDb();
+93
View File
@@ -0,0 +1,93 @@
import fs from 'fs';
import path from 'path';
import { stageExtractedPluginTrees } from '../plugins/plugin-backup';
import { checkBackupDatabase, extractBackupArchive } from './backup-archive';
/**
* Restoring a backup on the very first start (#1089).
*
* Moving an instance used to mean setting up a new one first: fetch the admin
* password from the logs, sign in, click through the welcome, then restore in
* the admin panel and throw the fresh admin away again. With
* RESTORE_FROM_BACKUP pointing at an archive, the first start restores it
* instead, and the instance comes up as the one the backup was taken from.
*
* ── When it runs ─────────────────────────────────────────────────────────
*
* Only while there is no database yet. The variable is typically left in the
* compose file after the move, and a restart must not roll the instance back to
* the day it moved; with a database present it is ignored with a warning.
*
* It runs before the database is opened, not after: the archive's database is
* migrated on the normal open, the admin seed finds its users and adds none, and
* the at-rest key the archive carries is in place before anything reads it. A
* restore after startup would need a restart for that key, which is the setup
* step this exists to remove.
*
* ── Why not an upload on the setup screen ────────────────────────────────
*
* A restore that anyone may run on an empty instance is a takeover: whoever
* reaches it first installs their own admins. The environment belongs to
* whoever runs the container, so that is where the switch lives.
*
* ── When it fails ────────────────────────────────────────────────────────
*
* Loudly, and without starting. An empty instance coming up instead would be
* worse than no instance: its new database would then stop the restore from
* ever running on the next start.
*/
export interface BootRestorePlan {
/** The archive to restore, or null when the variable is not set. */
archive: string | null;
/** Where the database lives: TREK_DB_FILE, or data/travel.db. */
dbFile: string;
/** The data directory, for the scratch space and the at-rest key. */
dataDir: string;
}
export type BootRestoreOutcome =
| { restored: false; reason: 'unset' | 'database-exists' }
/** `uploads` is the unpacked uploads tree, to hand to storage once it is up. Null when the archive had none. */
| { restored: true; uploads: string | null; staging: string };
export class BootRestoreError extends Error {}
export async function restoreOnFirstBoot(plan: BootRestorePlan): Promise<BootRestoreOutcome> {
if (!plan.archive) return { restored: false, reason: 'unset' };
if (fs.existsSync(plan.dbFile)) {
console.warn(`[restore] RESTORE_FROM_BACKUP is set, but ${plan.dbFile} already exists. The backup is only restored on a first start; remove the variable to silence this.`);
return { restored: false, reason: 'database-exists' };
}
if (!fs.existsSync(plan.archive) || !fs.statSync(plan.archive).isFile()) {
throw new BootRestoreError(`RESTORE_FROM_BACKUP: no backup archive at ${plan.archive}`);
}
console.log(`[restore] Restoring ${plan.archive} before the first start`);
const staging = path.join(plan.dataDir, `restore-boot-${Date.now()}`);
const refused = await extractBackupArchive(plan.archive, staging);
if (refused) throw new BootRestoreError(`RESTORE_FROM_BACKUP: ${refused.error}`);
const unreadable = checkBackupDatabase(staging);
if (unreadable) {
fs.rmSync(staging, { recursive: true, force: true });
throw new BootRestoreError(`RESTORE_FROM_BACKUP: ${unreadable.error}`);
}
fs.mkdirSync(path.dirname(plan.dbFile), { recursive: true });
// Copied in beside the target and renamed, so a crash leaves either nothing or a whole file.
const tmp = `${plan.dbFile}.restore-tmp`;
fs.copyFileSync(path.join(staging, 'travel.db'), tmp);
fs.renameSync(tmp, plan.dbFile);
// The key the archive's secrets were encrypted with. An ENCRYPTION_KEY in the
// environment still wins over the file, exactly as on any other start.
const key = path.join(staging, '.encryption_key');
if (fs.existsSync(key)) fs.copyFileSync(key, path.join(plan.dataDir, '.encryption_key'));
// Plugin trees are swapped in by the runtime's own boot reconcile.
stageExtractedPluginTrees(staging);
const uploads = path.join(staging, 'uploads');
return { restored: true, uploads: fs.existsSync(uploads) ? uploads : null, staging };
}
+2 -1
View File
@@ -99,7 +99,7 @@ export class BudgetController {
tripId,
{
from_user_id: body.from_user_id, to_user_id: body.to_user_id, amount: body.amount,
currency: body.currency, settled_at: body.settled_at, fallback_fx: body.fallback_fx,
currency: body.currency, settled_at: body.settled_at, note: body.note, fallback_fx: body.fallback_fx,
},
user.id,
);
@@ -127,6 +127,7 @@ export class BudgetController {
amount: body.amount,
currency: body.currency,
settled_at: body.settled_at,
note: body.note,
fallback_fx: body.fallback_fx,
});
if (!settlement) {
+8 -6
View File
@@ -432,7 +432,7 @@ export class BudgetMcp {
@Tool({
name: 'create_settlement',
description: "Record a settle-up payment: from_user_id paid to_user_id the given amount to settle shared expenses. The amount is in the trip's base currency unless `currency` says otherwise. Use get_settlement_summary first to find who owes whom and how much. When the server has no exchange rate for a foreign `currency`, the payment counts in no balance and is listed under `unconverted` in get_settlement_summary until freeze_budget_rates pins one.",
description: "Record a settle-up payment: from_user_id paid to_user_id the given amount to settle shared expenses, optionally with a note. The amount is in the trip's base currency unless `currency` says otherwise. Use get_settlement_summary first to find who owes whom and how much. When the server has no exchange rate for a foreign `currency`, the payment counts in no balance and is listed under `unconverted` in get_settlement_summary until freeze_budget_rates pins one.",
inputSchema: {
tripId: z.number().int().positive(),
from_user_id: z.number().int().positive().describe('User ID of the member who paid'),
@@ -440,13 +440,14 @@ export class BudgetMcp {
amount: z.number().positive().describe('Amount paid, in `currency`'),
currency: z.string().max(10).nullable().optional().describe("ISO currency code the payment was made in (e.g. \"USD\"); defaults to the trip currency. Its FX rate is frozen now, so the transfer keeps cancelling its expense when live rates drift."),
settled_at: z.string().max(40).nullable().optional().describe('Day the payment actually happened, YYYY-MM-DD. Omitted means no day is stored and the ledger files the payment under the day it was recorded.'),
note: z.string().max(500).nullable().optional().describe('A short note on the payment, such as how or where it was paid'),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: budgetAddonOn,
access: { group: 'budget', mode: 'write' },
})
async createSettlement(
{ tripId, from_user_id, to_user_id, amount, currency, settled_at }: { tripId: number; from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null },
{ tripId, from_user_id, to_user_id, amount, currency, settled_at, note }: { tripId: number; from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null; note?: string | null },
ctx: McpContext,
) {
if (this.isDemoUser(ctx.userId)) return demoDenied();
@@ -454,7 +455,7 @@ export class BudgetMcp {
if (!this.guards.hasTripPermission('budget_edit', tripId, ctx.userId)) return permissionDenied();
// Freeze-then-write composite, same as the REST path: the rate for the display
// currency is frozen at entry time (#1445).
const settlement = await this.budget.createSettlement(tripId, { from_user_id, to_user_id, amount, currency, settled_at }, ctx.userId);
const settlement = await this.budget.createSettlement(tripId, { from_user_id, to_user_id, amount, currency, settled_at, note }, ctx.userId);
if (!settlement) return errorResult('Settlement not found.');
this.guards.safeBroadcast(tripId, 'budget:settlement-created', { settlement });
return ok({ settlement });
@@ -462,7 +463,7 @@ export class BudgetMcp {
@Tool({
name: 'update_settlement',
description: 'Update a recorded settle-up payment (who paid, who received, the amount and the currency it was made in). Every field is a full replace, so restate the ones that stay the same. When the server has no exchange rate for a foreign `currency`, the payment counts in no balance and is listed under `unconverted` in get_settlement_summary until freeze_budget_rates pins one.',
description: 'Update a recorded settle-up payment (who paid, who received, the amount, the currency it was made in and its note). Every field is a full replace, so restate the ones that stay the same. When the server has no exchange rate for a foreign `currency`, the payment counts in no balance and is listed under `unconverted` in get_settlement_summary until freeze_budget_rates pins one.',
inputSchema: {
tripId: z.number().int().positive(),
settlementId: z.number().int().positive(),
@@ -471,13 +472,14 @@ export class BudgetMcp {
amount: z.number().positive().describe('Amount paid, in `currency`'),
currency: z.string().max(10).nullable().optional().describe('ISO currency code the payment was made in (e.g. "USD"); null puts it back in the trip currency. Omit to leave it as recorded, which also keeps the rate frozen at settle time.'),
settled_at: z.string().max(40).nullable().optional().describe('Date the payment actually happened, YYYY-MM-DD. Omit to leave it as recorded.'),
note: z.string().max(500).nullable().optional().describe('A short note on the payment; null or blank clears it. Omit to leave it as recorded.'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: budgetAddonOn,
access: { group: 'budget', mode: 'write' },
})
async updateSettlement(
{ tripId, settlementId, from_user_id, to_user_id, amount, currency, settled_at }: { tripId: number; settlementId: number; from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null },
{ tripId, settlementId, from_user_id, to_user_id, amount, currency, settled_at, note }: { tripId: number; settlementId: number; from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null; note?: string | null },
ctx: McpContext,
) {
if (this.isDemoUser(ctx.userId)) return demoDenied();
@@ -485,7 +487,7 @@ export class BudgetMcp {
if (!this.guards.hasTripPermission('budget_edit', tripId, ctx.userId)) return permissionDenied();
// Freeze-then-write composite, same as the REST path: an edit that leaves the
// currency alone keeps the rate frozen at settle time.
const settlement = await this.budget.updateSettlement(settlementId, tripId, { from_user_id, to_user_id, amount, currency, settled_at });
const settlement = await this.budget.updateSettlement(settlementId, tripId, { from_user_id, to_user_id, amount, currency, settled_at, note });
if (!settlement) return errorResult('Settlement not found.');
this.guards.safeBroadcast(tripId, 'budget:settlement-updated', { settlement });
return ok({ settlement });
+18 -9
View File
@@ -13,11 +13,17 @@ type Trip = TripAccess;
type SettlementRow = {
id: number; trip_id: string; from_user_id: number; to_user_id: number;
amount: number; currency: string | null; exchange_rate: number | null;
created_at: string; settled_at: string | null; created_by_user_id: number | null;
created_at: string; settled_at: string | null; note?: string | null; created_by_user_id: number | null;
from_username: string; from_avatar: string | null;
to_username: string; to_avatar: string | null;
};
/** A settle-up note as stored (#2340): trimmed, and nothing at all when blank. */
function settlementNote(note: string | null | undefined): string | null {
const trimmed = (note ?? '').trim();
return trimmed ? trimmed : null;
}
/** How the costs UI used to smuggle an itemized receipt through the note field. */
const LEGACY_TICKET_PREFIX = 'TICKETJSON:';
@@ -1481,7 +1487,7 @@ export class BudgetService {
// Settlement usernames use COALESCE(display_name, username) like every item
// query (the legacy raw fu.username was the odd one out).
private static readonly SETTLEMENT_SELECT = `
SELECT s.id, s.trip_id, s.from_user_id, s.to_user_id, s.amount, s.currency, s.exchange_rate, s.created_at, s.settled_at, s.created_by_user_id,
SELECT s.id, s.trip_id, s.from_user_id, s.to_user_id, s.amount, s.currency, s.exchange_rate, s.created_at, s.settled_at, s.note, s.created_by_user_id,
COALESCE(fu.display_name, fu.username) AS from_username, fu.avatar AS from_avatar,
COALESCE(tu.display_name, tu.username) AS to_username, tu.avatar AS to_avatar
FROM budget_settlements s
@@ -1494,7 +1500,7 @@ export class BudgetService {
id: r.id, trip_id: r.trip_id,
from_user_id: r.from_user_id, to_user_id: r.to_user_id,
amount: r.amount, currency: r.currency ?? null, exchange_rate: r.exchange_rate ?? 1,
created_at: r.created_at, settled_at: r.settled_at ?? null, created_by_user_id: r.created_by_user_id,
created_at: r.created_at, settled_at: r.settled_at ?? null, note: r.note ?? null, created_by_user_id: r.created_by_user_id,
from_username: r.from_username, from_avatar_url: avatarUrl({ avatar: r.from_avatar }),
to_username: r.to_username, to_avatar_url: avatarUrl({ avatar: r.to_avatar }),
};
@@ -1521,15 +1527,16 @@ export class BudgetService {
/** Raw settlement insert (no FX freeze) — the REST path wraps it in createSettlement. */
insertSettlement(
tripId: string | number,
data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; exchange_rate?: number; settled_at?: string | null },
data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; exchange_rate?: number; settled_at?: string | null; note?: string | null },
createdByUserId?: number,
) {
const result = this.db.run(
'INSERT INTO budget_settlements (trip_id, from_user_id, to_user_id, amount, currency, exchange_rate, settled_at, created_by_user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
'INSERT INTO budget_settlements (trip_id, from_user_id, to_user_id, amount, currency, exchange_rate, settled_at, note, created_by_user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
tripId, data.from_user_id, data.to_user_id, Math.round(data.amount * 100) / 100,
data.currency ? data.currency.toUpperCase() : null,
data.exchange_rate != null ? data.exchange_rate : 1,
data.settled_at || null,
settlementNote(data.note),
createdByUserId ?? null,
);
return this.getSettlement(Number(result.lastInsertRowid), tripId);
@@ -1539,7 +1546,7 @@ export class BudgetService {
applySettlementUpdate(
id: string | number,
tripId: string | number,
data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; exchange_rate?: number; settled_at?: string | null },
data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; exchange_rate?: number; settled_at?: string | null; note?: string | null },
) {
const row = this.db.get('SELECT id FROM budget_settlements WHERE id = ? AND trip_id = ?', id, tripId);
if (!row) return null;
@@ -1548,13 +1555,15 @@ export class BudgetService {
from_user_id = ?, to_user_id = ?, amount = ?,
currency = CASE WHEN ? THEN ? ELSE currency END,
exchange_rate = CASE WHEN ? IS NOT NULL THEN ? ELSE exchange_rate END,
settled_at = CASE WHEN ? THEN ? ELSE settled_at END
settled_at = CASE WHEN ? THEN ? ELSE settled_at END,
note = CASE WHEN ? THEN ? ELSE note END
WHERE id = ?
`,
data.from_user_id, data.to_user_id, Math.round(data.amount * 100) / 100,
data.currency !== undefined ? 1 : 0, data.currency ? data.currency.toUpperCase() : null,
data.exchange_rate !== undefined ? 1 : null, data.exchange_rate !== undefined ? data.exchange_rate : 1,
data.settled_at !== undefined ? 1 : 0, data.settled_at || null,
data.note !== undefined ? 1 : 0, settlementNote(data.note),
id,
);
return this.getSettlement(id, tripId);
@@ -1629,7 +1638,7 @@ export class BudgetService {
return roster.has(data.from_user_id) && roster.has(data.to_user_id);
}
async createSettlement(tripId: string | number, data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null; fallback_fx?: BudgetFallbackFx }, userId: number) {
async createSettlement(tripId: string | number, data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null; note?: string | null; fallback_fx?: BudgetFallbackFx }, userId: number) {
if (!this.settlementPartiesOnTrip(tripId, data)) return null;
// Freeze the FX rate for the display currency the amount was entered in so the
// transfer keeps cancelling its expense when live rates drift (#1445).
@@ -1637,7 +1646,7 @@ export class BudgetService {
return this.insertSettlement(tripId, data, userId);
}
async updateSettlement(id: string | number, tripId: string | number, data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null; fallback_fx?: BudgetFallbackFx }) {
async updateSettlement(id: string | number, tripId: string | number, data: { from_user_id: number; to_user_id: number; amount: number; currency?: string | null; settled_at?: string | null; note?: string | null; fallback_fx?: BudgetFallbackFx }) {
// Pass the settlement's stored currency so an edit that doesn't change it keeps
// the already-frozen rate (#1445) — otherwise a live-rate drift would re-open a
// settled position on an unrelated edit.
+3 -2
View File
@@ -2,6 +2,8 @@ import path from 'path';
import type { Options } from 'multer';
export const MAX_PLACE_IMAGE_SIZE = 20 * 1024 * 1024; // 20 MB — same cap as covers.
/** The picture types a place image may be, whether uploaded or taken from an attachment. */
export const PLACE_IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
/**
* fileFilter for the custom place-image upload (#1136), shared by the
@@ -11,8 +13,7 @@ export const MAX_PLACE_IMAGE_SIZE = 20 * 1024 * 1024; // 20 MB — same cap as c
*/
export const PLACE_IMAGE_FILE_FILTER: Options['fileFilter'] = (_req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase();
const allowed = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
if (file.mimetype.startsWith('image/') && !file.mimetype.includes('svg') && allowed.includes(ext)) {
if (file.mimetype.startsWith('image/') && !file.mimetype.includes('svg') && PLACE_IMAGE_EXTENSIONS.includes(ext)) {
cb(null, true);
} else {
// Carry statusCode so TrekExceptionFilter maps the rejection to a 400 rather
+2
View File
@@ -21,6 +21,8 @@ export function formatAssignmentWithPlace(a: AssignmentRow, tags: Partial<Tag>[]
end_day: a.end_day === 1,
leg_transport_mode: a.leg_transport_mode ?? null,
incoming_leg_transport_mode: a.incoming_leg_transport_mode ?? null,
// Kept on the day but not driven to (#2532).
route_excluded: a.route_excluded === 1,
// Which booking put this stop here, if a booking did. The day list has nothing
// else to tell it from a place the traveller added, and it must not draw the
// hotel a second time under the overnight block that already names it.
+1
View File
@@ -130,6 +130,7 @@ export class DaysService {
day_id: a.day_id,
order_index: a.order_index,
end_day: a.end_day === 1,
route_excluded: a.route_excluded === 1,
notes: a.notes,
// Repeated here for the same reason stop_type is: the day list hides the stop
// a booking wrote, and this copy is one of the paths that feed it.
@@ -1291,6 +1291,7 @@ export class JourneyDomainService {
pros_cons?: { pros: string[]; cons: string[] };
visibility?: string;
sort_order?: number;
is_draft?: boolean;
},
sid?: string,
): JourneyEntryWire | null {
@@ -1309,8 +1310,8 @@ export class JourneyDomainService {
const res = this.db
.prepare(
`
INSERT INTO journey_entries (journey_id, author_id, type, title, story, entry_date, entry_time, location_name, location_lat, location_lng, country_code, mood, weather, tags, pros_cons, visibility, sort_order, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INSERT INTO journey_entries (journey_id, author_id, type, title, story, entry_date, entry_time, location_name, location_lat, location_lng, country_code, mood, weather, tags, pros_cons, visibility, sort_order, is_draft, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
)
.run(
@@ -1331,6 +1332,7 @@ export class JourneyDomainService {
prosConsJson,
data.visibility || 'private',
(maxOrder?.m ?? -1) + 1,
data.is_draft ? 1 : 0,
now,
now,
);
@@ -1364,6 +1366,7 @@ export class JourneyDomainService {
sort_order: number;
stats_excluded: boolean;
dismissed: boolean;
is_draft: boolean;
}>,
sid?: string,
): JourneyEntryWire | null {
@@ -1394,6 +1397,7 @@ export class JourneyDomainService {
'sort_order',
'stats_excluded',
'dismissed',
'is_draft',
]);
for (const [key, val] of Object.entries(data)) {
@@ -1405,7 +1409,7 @@ export class JourneyDomainService {
} else if (key === 'pros_cons') {
fields.push('pros_cons = ?');
values.push(val && typeof val === 'object' ? JSON.stringify(val) : val);
} else if (key === 'stats_excluded' || key === 'dismissed') {
} else if (key === 'stats_excluded' || key === 'dismissed' || key === 'is_draft') {
// INTEGER columns, and better-sqlite3 refuses to bind a boolean.
fields.push(`${key} = ?`);
values.push(val ? 1 : 0);
@@ -1474,6 +1478,30 @@ export class JourneyDomainService {
return true;
}
/**
* The photos of one entry in a new order (#824), in one transaction. The list has
* to be exactly the entry's photos, each once, so a stale or foreign id cannot
* slip in. The order is kept on this entry's own links: a photo that also sits on
* another entry keeps its place there.
*/
reorderEntryPhotos(entryId: number, userId: number, orderedIds: number[], sid?: string): boolean {
const entry = this.db.prepare('SELECT id, journey_id FROM journey_entries WHERE id = ?').get(entryId) as { id: number; journey_id: number } | undefined;
if (!entry || !this.canEdit(entry.journey_id, userId)) return false;
const held = (this.db.prepare('SELECT journey_photo_id FROM journey_entry_photos WHERE entry_id = ?').all(entryId) as { journey_photo_id: number }[])
.map(r => r.journey_photo_id);
const asked = new Set(orderedIds);
if (asked.size !== orderedIds.length || held.length !== orderedIds.length || held.some(id => !asked.has(id))) return false;
const update = this.db.prepare('UPDATE journey_entry_photos SET sort_order = ? WHERE entry_id = ? AND journey_photo_id = ?');
this.db.connection.transaction(() => {
orderedIds.forEach((id, index) => update.run(index, entryId, id));
this.db.prepare('UPDATE journey_entries SET updated_at = ? WHERE id = ?').run(this.ts(), entryId);
})();
const updated = decodeEntryRow(this.db.prepare('SELECT * FROM journey_entries WHERE id = ?').get(entryId) as JourneyEntry);
this.broadcastJourneyEvent(entry.journey_id, 'journey:entry:updated', { entry: updated }, sid);
return true;
}
deleteEntry(entryId: number, userId: number, sid?: string): boolean {
const entry = this.db.prepare('SELECT * FROM journey_entries WHERE id = ?').get(entryId) as JourneyEntry | undefined;
if (!entry) return false;
+4 -2
View File
@@ -26,21 +26,23 @@ import type { JourneyEntry } from '../../types';
* the reason the JSON does, so that a client comparing it with `=== true` is
* comparing against something that can be true on every path.
*/
export interface JourneyEntryWire extends Omit<JourneyEntry, 'tags' | 'pros_cons' | 'stats_excluded' | 'dismissed'> {
export interface JourneyEntryWire extends Omit<JourneyEntry, 'tags' | 'pros_cons' | 'stats_excluded' | 'dismissed' | 'is_draft'> {
tags: string[];
pros_cons: { pros: string[]; cons: string[] } | null;
stats_excluded: boolean;
dismissed: boolean;
is_draft: boolean;
}
/** Decode a row's JSON columns and its flags. The one place that knows how they are stored. */
export function decodeEntryRow(row: JourneyEntry): JourneyEntryWire {
const { tags, pros_cons, stats_excluded, dismissed, ...rest } = row;
const { tags, pros_cons, stats_excluded, dismissed, is_draft, ...rest } = row;
return {
...rest,
tags: tags ? JSON.parse(tags) : [],
pros_cons: pros_cons ? JSON.parse(pros_cons) : null,
stats_excluded: !!stats_excluded,
dismissed: !!dismissed,
is_draft: !!is_draft,
};
}
@@ -6,6 +6,16 @@ import { decodeEntryRow } from './journey-entry-row';
import { GALLERY_CHRONOLOGICAL_ORDER } from './journey-gallery-order';
import { SettingsService } from '../settings/settings.service';
/**
* A photo whose every entry is a draft (#696) stays off the public page, and so do
* its bytes. One that also sits on a published entry, or on no entry at all (a
* plain gallery photo), is shown. `gp` is the journey_photos row.
*/
const NOT_DRAFT_ONLY_PHOTO = `NOT (
EXISTS (SELECT 1 FROM journey_entry_photos d JOIN journey_entries de ON de.id = d.entry_id WHERE d.journey_photo_id = gp.id AND de.is_draft = 1)
AND NOT EXISTS (SELECT 1 FROM journey_entry_photos p JOIN journey_entries pe ON pe.id = p.entry_id WHERE p.journey_photo_id = gp.id AND pe.is_draft = 0)
)`;
interface JourneySharePermissions {
share_timeline?: boolean;
share_gallery?: boolean;
@@ -126,7 +136,7 @@ export class JourneyShareService {
SELECT gp.photo_id, tkp.owner_id, gp.journey_id
FROM journey_photos gp
JOIN trek_photos tkp ON tkp.id = gp.photo_id
WHERE gp.photo_id = ? AND gp.journey_id = ?
WHERE gp.photo_id = ? AND gp.journey_id = ? AND ${NOT_DRAFT_ONLY_PHOTO}
`).get(photoId, row.journey_id) as any;
if (!photo) return null;
const journey = this.db.prepare('SELECT user_id FROM journeys WHERE id = ?').get(row.journey_id) as any;
@@ -144,7 +154,7 @@ export class JourneyShareService {
FROM journey_photos gp
JOIN trek_photos tkp ON tkp.id = gp.photo_id
JOIN journeys j ON j.id = gp.journey_id
WHERE tkp.asset_id = ? AND gp.journey_id = ?
WHERE tkp.asset_id = ? AND gp.journey_id = ? AND ${NOT_DRAFT_ONLY_PHOTO}
`).get(assetId, row.journey_id) as any;
// Only resolve assets that actually belong to this shared journey.
if (!photo) return null;
@@ -164,7 +174,7 @@ export class JourneyShareService {
// Entries with photos
const entries = this.db.prepare(`
SELECT je.* FROM journey_entries je
WHERE je.journey_id = ? AND je.type != 'skeleton' AND je.dismissed = 0
WHERE je.journey_id = ? AND je.type != 'skeleton' AND je.dismissed = 0 AND je.is_draft = 0
ORDER BY je.entry_date, je.sort_order
`).all(row.journey_id) as any[];
@@ -190,7 +200,7 @@ export class JourneyShareService {
tp.media_type, tp.duration_ms, tp.taken_at, tp.lat, tp.lng
FROM journey_photos gp
JOIN trek_photos tp ON tp.id = gp.photo_id
WHERE gp.journey_id = ?
WHERE gp.journey_id = ? AND ${NOT_DRAFT_ONLY_PHOTO}
${GALLERY_CHRONOLOGICAL_ORDER}
`).all(row.journey_id) as any[];
+10 -1
View File
@@ -36,7 +36,8 @@ import {
JourneyAddTripDto, JourneyContributorAddDto, JourneyContributorUpdateDto, JourneyCreateDto,
JourneyEntryCreateDto, JourneyEntryPhotoUploadDto, JourneyEntryUpdateDto, JourneyGalleryVideoDto,
JourneyLinkPhotoDto, JourneyPhotoUpdateDto, JourneyPreferencesDto, JourneyProviderPhotosDto,
JourneyReorderEntriesDto, JourneyShareLinkDto, JourneyUpdateDto,
JourneyReorderEntriesDto,
JourneyReorderEntryPhotosDto, JourneyShareLinkDto, JourneyUpdateDto,
BookSaveDto,
} from './journey.dto';
import { isVideoMime, isVideoExtension, MAX_VIDEO_SIZE } from '../files/files.constants';
@@ -206,6 +207,14 @@ export class JourneyController {
return result;
}
@Put('entries/:entryId/photos/reorder')
reorderEntryPhotos(@CurrentUser() user: User, @Param('entryId') entryId: string, @Body() body: JourneyReorderEntryPhotosDto, @Headers('x-socket-id') socketId?: string) {
if (!this.journey.reorderEntryPhotos(Number(entryId), user.id, body.orderedIds, socketId)) {
throw new HttpException({ error: 'Entry not found' }, 404);
}
return { success: true };
}
@Delete('entries/:entryId')
deleteEntry(@CurrentUser() user: User, @Param('entryId') entryId: string, @Headers('x-socket-id') socketId?: string) {
if (!this.journey.deleteEntry(Number(entryId), user.id, socketId)) {
+2
View File
@@ -13,6 +13,7 @@ import {
journeyPreferencesRequestSchema,
journeyProviderPhotosRequestSchema,
journeyReorderEntriesRequestSchema,
journeyReorderEntryPhotosRequestSchema,
journeyShareLinkRequestSchema,
journeyUpdateRequestSchema,
bookSaveRequestSchema,
@@ -41,6 +42,7 @@ export class JourneyAddTripDto extends createZodDto(journeyAddTripRequestSchema)
export class JourneyEntryCreateDto extends createZodDto(journeyEntryCreateRequestSchema) {}
export class JourneyEntryUpdateDto extends createZodDto(journeyEntryUpdateRequestSchema) {}
export class JourneyReorderEntriesDto extends createZodDto(journeyReorderEntriesRequestSchema) {}
export class JourneyReorderEntryPhotosDto extends createZodDto(journeyReorderEntryPhotosRequestSchema) {}
export class JourneyContributorAddDto extends createZodDto(journeyContributorRequestSchema) {}
export class JourneyContributorUpdateDto extends createZodDto(journeyContributorUpdateRequestSchema) {}
export class JourneyPreferencesDto extends createZodDto(journeyPreferencesRequestSchema) {}
+24 -3
View File
@@ -347,6 +347,7 @@ export class JourneyMcp {
visibility: ENTRY_VISIBILITY.optional().describe('Defaults to private; "shared" and "public" expose the entry through the journey share link'),
type: ENTRY_TYPE.optional().describe('Defaults to "entry"; "skeleton" is the stub TREK derives from a trip place and hides behind the hide-skeletons preference'),
sort_order: z.number().int().min(0).optional(),
is_draft: z.boolean().optional().describe('True keeps the entry a draft: contributors see it, the journey share link does not'),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: journeyAddonOn,
@@ -357,7 +358,7 @@ export class JourneyMcp {
journeyId: number; entry_date: string; title?: string; story?: string; entry_time?: string;
location_name?: string; location_lat?: number; location_lng?: number; mood?: string; weather?: string;
tags?: string[]; pros_cons?: { pros: string[]; cons: string[] }; visibility?: EntryVisibility;
type?: EntryType; sort_order?: number;
type?: EntryType; sort_order?: number; is_draft?: boolean;
},
ctx: McpContext,
) {
@@ -371,7 +372,7 @@ export class JourneyMcp {
@Tool({
name: 'update_journey_entry',
description: 'Update an existing journey entry: its text, date, place, coordinates, weather, tags, verdict, visibility, or whether it counts as a stop. Fields left out keep their value; pass null to clear one. To move an entry within its day use reorder_journey_entries rather than setting sort_order here.',
description: 'Update an existing journey entry: its text, date, place, coordinates, weather, tags, verdict, visibility, whether it is a draft, or whether it counts as a stop. Fields left out keep their value; pass null to clear one. To move an entry within its day use reorder_journey_entries rather than setting sort_order here.',
inputSchema: {
entryId: z.number().int().positive(),
title: z.string().max(300).nullable().optional(),
@@ -390,6 +391,7 @@ export class JourneyMcp {
sort_order: z.number().int().min(0).optional(),
stats_excluded: z.boolean().optional().describe('True leaves the entry in the journal but takes it off the journey route and out of its distance, countries and step count (see get_journey_stats); false puts it back. For the home airport, a stopover, the place the trip was planned from'),
dismissed: z.boolean().optional().describe('True waves a trip-derived suggestion away: it leaves the journey without being deleted, so the trip sync does not offer it again. restore_journey_suggestions brings every dismissed one back'),
is_draft: z.boolean().optional().describe('True turns the entry into a draft that the journey share link leaves out; false publishes it'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: journeyAddonOn,
@@ -402,7 +404,7 @@ export class JourneyMcp {
location_lng?: number | null; mood?: string | null; weather?: string | null;
tags?: string[] | null; pros_cons?: { pros: string[]; cons: string[] } | null;
visibility?: EntryVisibility; type?: EntryType; sort_order?: number; stats_excluded?: boolean;
dismissed?: boolean;
dismissed?: boolean; is_draft?: boolean;
},
ctx: McpContext,
) {
@@ -446,6 +448,25 @@ export class JourneyMcp {
return ok({ success: true });
}
@Tool({
name: 'reorder_journey_entry_photos',
description: 'Put the photos of one journey entry in a new order. Pass every photo id the entry holds (the journey photo ids list_journey_entries returns), each once, first photo first. The first one is the entry cover.',
inputSchema: {
entryId: z.number().int().positive(),
orderedIds: z.array(z.number().int().positive()).min(1).max(500),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: journeyAddonOn,
access: { group: 'journey', mode: 'write' },
})
reorderJourneyEntryPhotos({ entryId, orderedIds }: { entryId: number; orderedIds: number[] }, ctx: McpContext) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
if (!this.journey.reorderEntryPhotos(entryId, ctx.userId, orderedIds, undefined)) {
return notFound('Entry not found, access denied, or the ids are not exactly the photos of this entry.');
}
return ok({ success: true });
}
@Tool({
name: 'add_journey_contributor',
description: 'Add a contributor to a journey. Owner only.',
@@ -60,6 +60,7 @@ export class JourneyService {
updateEntry(entryId: number, userId: number, data: Parameters<typeof this.journey.updateEntry>[2], sid?: string) { return this.journey.updateEntry(entryId, userId, data, sid); }
deleteEntry(entryId: number, userId: number, sid?: string) { return this.journey.deleteEntry(entryId, userId, sid); }
reorderEntries(id: number, userId: number, orderedIds: number[], sid?: string) { return this.journey.reorderEntries(id, userId, orderedIds, sid); }
reorderEntryPhotos(entryId: number, userId: number, orderedIds: number[], sid?: string) { return this.journey.reorderEntryPhotos(entryId, userId, orderedIds, sid); }
// Photos
addPhoto(entryId: number, userId: number, filePath: string, thumbnailPath: string | undefined, caption: string | undefined, media?: { mediaType?: string; durationMs?: number | null }) { return this.journey.addPhoto(entryId, userId, filePath, thumbnailPath, caption, media); }
+100
View File
@@ -0,0 +1,100 @@
import { normalizePlaceWebsite } from '@trek/shared';
import { haversineMetres, OVERPASS_QUERY_TIMEOUT_S } from './maps.helpers';
/** What "near" means when the caller does not say: a short walk. */
export const NEARBY_DEFAULT_RADIUS_M = 500;
/** Google's own ceiling for one nearby answer, and the most a picker lists. */
export const NEARBY_DEFAULT_LIMIT = 20;
/** The OSM keys a place worth visiting is tagged with. Street furniture is not among them. */
const NEARBY_OSM_KEYS = 'amenity|tourism|shop|leisure|historic';
export interface NearbyOsmElement {
type: string;
id: number;
lat?: number;
lon?: number;
center?: { lat: number; lon: number };
tags?: Record<string, string>;
}
/**
* Named places of any kind around a point (#976). Without a category the answer
* can be large in a city centre, so the circle is the caller's and the row count
* is capped at a few times the limit: enough to sort by distance, not the square
* kilometre.
*/
export function nearbyOverpassQuery(lat: number, lng: number, radius: number, limit: number): string {
const around = `(around:${Math.round(radius)},${lat},${lng})`;
return `[out:json][timeout:${OVERPASS_QUERY_TIMEOUT_S}];\nnwr${around}[name][~"^(${NEARBY_OSM_KEYS})$"~"."];\nout center tags ${limit * 3};`;
}
/** The OSM kind of a place, as `category` in the other search answers: a shop reads `shop_<type>`. */
function osmKind(tags: Record<string, string>): string | null {
for (const key of NEARBY_OSM_KEYS.split('|')) {
const value = tags[key];
if (!value || value === 'yes') continue;
return key === 'shop' ? `shop_${value}` : value;
}
return null;
}
/** Overpass elements as the place records a search answers with, nearest first. */
export function overpassNearbyRecords(
elements: NearbyOsmElement[],
origin: { lat: number; lng: number },
lang: string,
limit: number,
): Record<string, unknown>[] {
const out: Record<string, unknown>[] = [];
for (const el of elements) {
const tags = el.tags || {};
const name = tags[`name:${lang}`] || tags.int_name || tags.name;
const lat = el.lat ?? el.center?.lat;
const lng = el.lon ?? el.center?.lon;
if (!name || lat == null || lng == null) continue;
// Shut for good is not somewhere to go, the same rule the POI search keeps.
if (tags.disused === 'yes' || tags.abandoned === 'yes') continue;
const address = [tags['addr:street'], tags['addr:housenumber'], tags['addr:postcode'], tags['addr:city']]
.filter(Boolean)
.join(' ');
out.push({
google_place_id: null,
osm_id: `${el.type}:${el.id}`,
name,
address,
lat,
lng,
rating: null,
website: normalizePlaceWebsite(tags.website) ?? normalizePlaceWebsite(tags['contact:website']),
phone: tags.phone || tags['contact:phone'] || null,
category: osmKind(tags),
source: 'openstreetmap',
});
}
return nearestFirst(out, origin, limit);
}
/**
* Stamps each record with its distance from the point and keeps the nearest.
* Every source goes through here, so the answer reads the same whoever gave it.
*/
export function nearestFirst(
records: Record<string, unknown>[],
origin: { lat: number; lng: number },
limit: number,
): Record<string, unknown>[] {
return records
.filter(r => typeof r.lat === 'number' && typeof r.lng === 'number')
.map(r => ({ ...r, distance_m: Math.round(haversineMetres(origin.lat, origin.lng, r.lat as number, r.lng as number)) }))
.sort((a, b) => a.distance_m - b.distance_m)
.slice(0, limit);
}
/**
* The cache key for one nearby question. Rounded to about ten metres, so the same
* photo location asked twice (an import of a whole album) costs one lookup.
*/
export function nearbyCacheKey(lat: number, lng: number, radius: number, limit: number, lang: string): string {
return `${lat.toFixed(4)},${lng.toFixed(4)}|${radius}|${limit}|${lang}`;
}
+16 -1
View File
@@ -27,7 +27,7 @@ import { StorageService } from '../storage/storage.service';
import { isClientAbortError } from '../storage/storage.types';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { MapsSearchDto, MapsAutocompleteDto, MapsResolveUrlDto } from './maps.dto';
import { MapsSearchDto, MapsNearbyDto, MapsAutocompleteDto, MapsResolveUrlDto } from './maps.dto';
/** Google's session-token shape: URL-safe ASCII, at most 36 characters. The
* autocomplete body is validated by the Zod pipe; the details query is not,
@@ -83,6 +83,21 @@ export class MapsController {
}
}
// Places of any kind around a point, nearest first (#976).
@Post('nearby')
@HttpCode(200)
async nearby(
@CurrentUser() user: User,
@Body() body: MapsNearbyDto,
@Query('lang') lang?: string,
): Promise<MapsSearchResult> {
try {
return await this.maps.nearbyPlaces(user.id, body.lat, body.lng, { radius: body.radius, limit: body.limit, lang });
} catch (err: unknown) {
throw toHttpException(err, 'Nearby search error', 500);
}
}
// OSM-only POI explore: places of a category within the current map viewport.
@Get('pois')
async pois(
+2
View File
@@ -1,6 +1,7 @@
import { createZodDto } from 'nestjs-zod';
import {
mapsSearchRequestSchema,
mapsNearbyRequestSchema,
mapsAutocompleteRequestSchema,
mapsResolveUrlRequestSchema,
} from '@trek/shared';
@@ -12,5 +13,6 @@ import {
* shared/ remain the single source of truth for the wire contract.
*/
export class MapsSearchDto extends createZodDto(mapsSearchRequestSchema) {}
export class MapsNearbyDto extends createZodDto(mapsNearbyRequestSchema) {}
export class MapsAutocompleteDto extends createZodDto(mapsAutocompleteRequestSchema) {}
export class MapsResolveUrlDto extends createZodDto(mapsResolveUrlRequestSchema) {}
+25
View File
@@ -82,6 +82,31 @@ export class MapsMcp {
return ok(result);
}
/** The MCP side of POST /api/maps/nearby (#976), with the same caps. */
@Tool({
name: 'search_nearby_places',
description: 'List named places of any kind around a coordinate, nearest first, each with its distance in metres (`distance_m`). No category and no name needed: use it for "what is at this spot", e.g. where a photo was taken. Answers from the TREK place index first, from Google Places only when the instance has a key and the index found nothing, and from OpenStreetMap otherwise; `source` says which. Prefer search_pois for one kind of place across a whole area, and search_place when the user named the place.',
inputSchema: {
lat: z.number().min(-90).max(90),
lng: z.number().min(-180).max(180),
radius: z.number().int().min(50).max(5000).optional().describe('Search radius in metres, 500 when left out'),
limit: z.number().int().min(1).max(20).optional().describe('How many places, 20 when left out'),
lang: z.string().max(35).optional().describe('Language for the place names, e.g. "de" or "ja"'),
},
annotations: TOOL_ANNOTATIONS_READONLY,
access: { group: 'geo', mode: 'read' },
})
async searchNearbyPlaces(
{ lat, lng, radius, limit, lang }: { lat: number; lng: number; radius?: number; limit?: number; lang?: string },
ctx: McpContext,
) {
try {
return ok(await this.maps.nearbyPlaces(ctx.userId, lat, lng, { radius, limit, lang }));
} catch {
return errorResult('Nearby search failed.');
}
}
/**
* The MCP side of GET /api/maps/pois, the "explore" pill on the trip map. Not
* part of the 1:1 move above: the route had no tool at all, which left the
+160 -18
View File
@@ -64,6 +64,14 @@ import {
type GoogleOpeningHours,
type OverpassPoi,
} from './maps.helpers';
import {
NEARBY_DEFAULT_LIMIT,
NEARBY_DEFAULT_RADIUS_M,
nearbyCacheKey,
nearbyOverpassQuery,
nearestFirst,
overpassNearbyRecords,
} from './maps-nearby.helpers';
// ── Google API call counter ───────────────────────────────────────────────────
@@ -125,9 +133,17 @@ function googleFetch(rawEndpoint: string, label: string, init?: RequestInit): Pr
// ── Interfaces ───────────────────────────────────────────────────────────────
function nominatimCategory(item: { class?: string; type?: string }): string | null {
if (!item.class || !item.type || item.type === 'yes') return null;
return item.class === 'shop' ? `shop_${item.type}` : item.type;
}
interface NominatimResult {
osm_type: string;
osm_id: string;
/** The main OSM tag, key and value: `class` "tourism" with `type` "hotel". */
class?: string;
type?: string;
name?: string;
display_name?: string;
lat: string;
@@ -345,6 +361,27 @@ interface GooglePlaceResult {
googleMapsUri?: string;
}
/** A Google place as the record every search answers with. */
function googlePlaceRecord(p: GooglePlaceResult): Record<string, unknown> {
return {
google_place_id: p.id,
google_ftid: googleFtidFromMapsUrl(p.googleMapsUri),
name: p.displayName?.text || '',
address: p.formattedAddress || '',
// `?? null`, not `|| null`: 0 is a real coordinate (equator / prime meridian).
lat: p.location?.latitude ?? null,
lng: p.location?.longitude ?? null,
rating: p.rating || null,
website: normalizePlaceWebsite(p.websiteUri),
phone: p.nationalPhoneNumber || null,
types: p.types || [],
source: 'google',
};
}
/** A place that has shut down for good is never the answer to "where should we go" (#1341). */
const isOpenGooglePlace = (p: GooglePlaceResult) => p.businessStatus !== 'CLOSED_PERMANENTLY';
interface GoogleAutocompleteSuggestion {
placePrediction?: {
placeId: string;
@@ -587,6 +624,12 @@ const POI_CACHE_MAX = 500;
* nobody reads and every mirror pays for.
*/
const POI_RESULT_CAP = 240;
// Places near a point (#976): cached longer than the POI boxes, because the
// likely caller is an import asking the same photo location again, and Google
// bills every one of those.
const NEARBY_CACHE = new Map<string, { at: number; value: { places: Record<string, unknown>[]; source: string } }>();
const NEARBY_CACHE_TTL_MS = 30 * 60 * 1000;
const NEARBY_CACHE_MAX = 500;
// POST the query to all mirrors at once and return the first one that answers with
// valid JSON. Throws {status:502} only if every mirror fails. Racing (rather than
@@ -1116,6 +1159,18 @@ export class MapsService {
return row?.value === 'true';
}
/**
* Whether Amap answers before the index and OpenStreetMap (#1636): only when
* the admin picked Amap outright, not when it holds the slot by default, and
* only for a search centred inside China. Outside it Amap still answers, with
* the wrong place (the Eiffel Tower lands in Macau), so the gate is the point,
* not the provider.
*/
private amapAnswersFirst(point?: { lat: number; lng: number }): boolean {
if (!point || !Number.isFinite(point.lat) || !Number.isFinite(point.lng)) return false;
return this.placesProviderChoice() === 'amap' && !isOutsideChina(point.lat, point.lng);
}
/** The Amap provider, when Amap holds the keyed slot; null otherwise. */
resolvePlacesProvider(userId: number): AmapPlacesProvider | null {
const keyed = this.keyedProvider(userId);
@@ -1235,6 +1290,9 @@ export class MapsService {
rating: null,
website: null,
phone: null,
// What the place is, so the add form can preselect a category (#2282). A shop's
// value is the ware ("bakery"), so the key goes first and says it is a shop.
category: nominatimCategory(item),
source: 'openstreetmap',
...readWikiIdentity(item.extratags),
};
@@ -2088,6 +2146,19 @@ export class MapsService {
// the same question twice. `null` means it never ran.
let osmAnswer: Record<string, unknown>[] | null = null;
// Amap first where the admin picked it and the search is about China (#1636):
// there the index and OpenStreetMap are thin and Amap is the map people use.
// Its answer is kept, so the Amap slot further down never pays for the same
// question twice. A failure drops through to the usual order.
let amapAnswer: Record<string, unknown>[] | null = null;
if (keyed?.id === 'amap' && this.amapAnswersFirst(locationBias)) {
amapAnswer = await keyed.provider.searchText(query, lang, locationBias).catch((err: unknown) => {
console.warn('Amap search failed, falling back:', (err as Error).message);
return null;
});
if (amapAnswer && amapAnswer.length > 0) return { places: amapAnswer, source: 'amap' };
}
// A search sent to Google on purpose, or the admin's "Google only" switch,
// skips the pair the same way: the search then reads exactly as it did
// before 4.3.0 on an install with a key.
@@ -2139,7 +2210,7 @@ export class MapsService {
// Amap in the slot Google otherwise holds: asked only once the index and
// OpenStreetMap came back empty, exactly like the Google call below.
if (keyed?.id === 'amap') {
const places = await keyed.provider.searchText(query, lang, locationBias);
const places = amapAnswer ?? (await keyed.provider.searchText(query, lang, locationBias));
return { places, source: 'amap' };
}
@@ -2193,26 +2264,84 @@ export class MapsService {
// go" (#1341). Temporarily closed stays: a restaurant on holiday next month is
// still worth planning around. Anything without the field is a non-business
// result (a park, a viewpoint) and is kept.
const places = (data.places || [])
.filter((p: GooglePlaceResult) => p.businessStatus !== 'CLOSED_PERMANENTLY')
.map((p: GooglePlaceResult) => ({
google_place_id: p.id,
google_ftid: googleFtidFromMapsUrl(p.googleMapsUri),
name: p.displayName?.text || '',
address: p.formattedAddress || '',
// `?? null`, not `|| null`: 0 is a real coordinate (equator / prime meridian).
lat: p.location?.latitude ?? null,
lng: p.location?.longitude ?? null,
rating: p.rating || null,
website: normalizePlaceWebsite(p.websiteUri),
phone: p.nationalPhoneNumber || null,
types: p.types || [],
source: 'google',
}));
const places = (data.places || []).filter(isOpenGooglePlace).map(googlePlaceRecord);
return { places, source: 'google' };
}
// ── Places near a point (#976) ─────────────────────────────────────────────
/**
* Places of any kind around a coordinate, nearest first, in the shape a search
* answers with plus `distance_m`. For "what is here" where no name was typed:
* a photo's location, a pin on the map.
*
* Same order as the search: the TREK index first, free and storable; Google
* only with a key and only when the index has nothing (or the admin set Google
* only); OpenStreetMap otherwise. Google bills per call, so every answer is
* cached for a while and the circle and count are capped by the contract.
*/
async nearbyPlaces(
userId: number,
lat: number,
lng: number,
opts: { radius?: number; limit?: number; lang?: string } = {},
): Promise<{ places: Record<string, unknown>[]; source: string }> {
const radius = opts.radius ?? NEARBY_DEFAULT_RADIUS_M;
const limit = opts.limit ?? NEARBY_DEFAULT_LIMIT;
const lang = toApiLang(opts.lang);
const key = nearbyCacheKey(lat, lng, radius, limit, lang);
const cached = NEARBY_CACHE.get(key);
if (cached && Date.now() - cached.at < NEARBY_CACHE_TTL_MS) return cached.value;
const value = await this.lookUpNearby(userId, { lat, lng }, radius, limit, lang);
if (NEARBY_CACHE.size >= NEARBY_CACHE_MAX) NEARBY_CACHE.delete(NEARBY_CACHE.keys().next().value as string);
NEARBY_CACHE.set(key, { at: Date.now(), value });
return value;
}
private async lookUpNearby(
userId: number,
origin: { lat: number; lng: number },
radius: number,
limit: number,
lang: string,
): Promise<{ places: Record<string, unknown>[]; source: string }> {
const keyed = this.keyedProvider(userId);
if (this.trekPlacesEnabled() && !this.googleOnly(keyed)) {
// Never throws upward, like the search: a slow index drops to the next source.
const found = await trekPlacesNearby(origin.lat, origin.lng, { radius, limit }).catch((err: unknown) => {
console.warn('TREK Places nearby failed, falling back:', (err as Error).message);
return [];
});
if (found.length > 0) return { places: nearestFirst(found.map(toPlaceRecord), origin, limit), source: 'trek-places' };
}
if (keyed?.id === 'google') {
const response = await googleFetch('https://places.googleapis.com/v1/places:searchNearby', 'searchNearby', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Goog-Api-Key': keyed.key, 'X-Goog-FieldMask': SEARCH_TEXT_FIELD_MASK },
body: JSON.stringify({
maxResultCount: limit,
rankPreference: 'DISTANCE',
languageCode: lang,
locationRestriction: { circle: { center: { latitude: origin.lat, longitude: origin.lng }, radius } },
}),
});
const data = (await response.json()) as { places?: GooglePlaceResult[]; error?: { message?: string } };
if (!response.ok) {
logKeyFailure('searchNearby', response.status, userId, keyed.source);
throw Object.assign(new Error(data.error?.message || 'Google Places API error'), { status: response.status });
}
const places = (data.places || []).filter(isOpenGooglePlace).map(googlePlaceRecord);
return { places: nearestFirst(places, origin, limit), source: 'google' };
}
const osmLang = lang.split('-')[0].toLowerCase();
const elements = await overpassFetch(nearbyOverpassQuery(origin.lat, origin.lng, radius, limit));
return { places: overpassNearbyRecords(elements, origin, osmLang, limit), source: 'openstreetmap' };
}
// ── Autocomplete (Google or Nominatim fallback) ────────────────────────────
async autocompletePlaces(
@@ -2233,6 +2362,19 @@ export class MapsService {
// Same contract as search: never throws upward, falls through to what this
// method did before. The admin's "Google only" switch skips the index here
// too, so the suggestions and the search agree on where they come from.
// Amap first inside China when the admin picked it, as in the search (#1636).
let amapTips: MapsAutocompleteResult['suggestions'] | null = null;
const boxCentre = locationBias
? { lat: (locationBias.low.lat + locationBias.high.lat) / 2, lng: (locationBias.low.lng + locationBias.high.lng) / 2 }
: undefined;
if (keyed?.id === 'amap' && this.amapAnswersFirst(boxCentre)) {
amapTips = await keyed.provider.autocomplete(input, lang, locationBias).catch((err: unknown) => {
console.warn('Amap autocomplete failed, falling back:', (err as Error).message);
return null;
});
if (amapTips && amapTips.length > 0) return { suggestions: amapTips, source: 'amap' };
}
if (this.trekPlacesEnabled() && !this.googleOnly(keyed)) {
try {
const centre = locationBias
@@ -2303,7 +2445,7 @@ export class MapsService {
}
if (keyed?.id === 'amap') {
const suggestions = await keyed.provider.autocomplete(input, lang, locationBias);
const suggestions = amapTips ?? (await keyed.provider.autocomplete(input, lang, locationBias));
return { suggestions, source: 'amap' };
}
@@ -134,11 +134,11 @@ export class PackingController {
// Privacy state before the change, so a public↔private toggle (#858) can route
// the broadcast correctly instead of leaking a freshly-privatized item.
const before = this.packing.getItemPrivacy(tripId, id);
const { name, checked, category, weight_grams, bag_id, quantity, is_private } = body;
const { name, checked, category, weight_grams, bag_id, quantity, packed_quantity, is_private } = body;
// bodyKeys carries which keys the request actually provided (the presence-
// sentinel protocol); the parsed body only ever holds known schema keys.
// checked arrives as boolean or legacy 0/1 — normalize to the 0/1 the SQL binds.
const updated = this.packing.updateItem(tripId, id, { name, checked: checked === undefined ? undefined : checked ? 1 : 0, category, weight_grams, bag_id, quantity, is_private }, Object.keys(body), ifMatch, user.id);
const updated = this.packing.updateItem(tripId, id, { name, checked: checked === undefined ? undefined : checked ? 1 : 0, category, weight_grams, bag_id, quantity, packed_quantity, is_private }, Object.keys(body), ifMatch, user.id);
if (!updated) {
throw new HttpException({ error: 'Item not found' }, 404);
}
+4 -3
View File
@@ -155,7 +155,7 @@ export class PackingMcp {
@Tool({
name: 'update_packing_item',
description: 'Change a packing item: rename it, recategorise it, move it into a bag, set how many are needed, record its weight, or flip it between the common list and your own. Ticking it off is toggle_packing_item; choosing who a private item is shared with is set_packing_item_sharing.',
description: 'Change a packing item: rename it, recategorise it, move it into a bag, set how many are needed, count how many are already packed, record its weight, or flip it between the common list and your own. Ticking it off is toggle_packing_item; choosing who a private item is shared with is set_packing_item_sharing.',
inputSchema: {
tripId: z.number().int().positive(),
itemId: z.number().int().positive(),
@@ -163,6 +163,7 @@ export class PackingMcp {
category: z.string().max(100).optional(),
bag_id: packingUpdateItemRequestSchema.shape.bag_id.describe('Bag to pack the item into (ids come from list_packing_bags); null takes it out of its bag'),
quantity: packingUpdateItemRequestSchema.shape.quantity.describe('How many to pack, clamped to 1-999'),
packed_quantity: packingUpdateItemRequestSchema.shape.packed_quantity.describe('How many of the quantity are already packed; reaching the quantity ticks the item off, 0 or null clears the count'),
weight_grams: packingUpdateItemRequestSchema.shape.weight_grams.describe('Weight in grams, which feeds the bag fill bar; null clears it'),
is_private: packingUpdateItemRequestSchema.shape.is_private.describe('true takes the item off the common list and onto the caller\'s own'),
},
@@ -171,13 +172,13 @@ export class PackingMcp {
access: { group: 'packing', mode: 'write' },
})
async updatePackingItem(
{ tripId, itemId, name, category, bag_id, quantity, weight_grams, is_private }: { tripId: number; itemId: number; name?: string; category?: string; bag_id?: number | null; quantity?: number; weight_grams?: number | null; is_private?: boolean },
{ tripId, itemId, name, category, bag_id, quantity, packed_quantity, weight_grams, is_private }: { tripId: number; itemId: number; name?: string; category?: string; bag_id?: number | null; quantity?: number; packed_quantity?: number | null; weight_grams?: number | null; is_private?: boolean },
ctx: McpContext,
) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
if (!this.packing.verifyTripAccess(tripId, ctx.userId)) return noAccess();
if (!this.guards.hasTripPermission('packing_edit', tripId, ctx.userId)) return permissionDenied();
const fields = { name, category, bag_id, quantity, weight_grams, is_private };
const fields = { name, category, bag_id, quantity, packed_quantity, weight_grams, is_private };
// The service reads presence from bodyKeys, so a field has to be named there
// for an explicit null to clear it rather than read as "leave it alone".
const bodyKeys = Object.keys(fields).filter(k => fields[k as keyof typeof fields] !== undefined);
+17 -9
View File
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
import { resolvePackedState, type TrekWsPayload, type TrekWsTripEventName } from '@trek/shared';
import { RealtimeService } from '../realtime/realtime.service';
import { PermissionsService } from '../permissions/permissions.service';
import { avatarUrl } from '../common/avatarUrl';
@@ -276,7 +276,7 @@ export class PackingService {
updateItem(
tripId: string | number,
id: string | number,
data: { name?: string; checked?: number; category?: string; weight_grams?: number | null; bag_id?: number | null; quantity?: number; is_private?: boolean },
data: { name?: string; checked?: number; category?: string; weight_grams?: number | null; bag_id?: number | null; quantity?: number; packed_quantity?: number | null; is_private?: boolean },
bodyKeys: string[],
ifMatch?: string,
actingUserId?: number,
@@ -302,29 +302,37 @@ export class PackingService {
// the visibility filter still has someone to match (#858).
const claimOwner = bodyKeys.includes('is_private') && !!data.is_private && item.owner_id == null && actingUserId != null;
// The box and the packed count (#2296) are settled together, so a count
// that reaches the quantity ticks the item and a tick clears the count.
const quantity = bodyKeys.includes('quantity') ? Math.max(1, Math.min(999, Number(data.quantity) || 1)) : (item.quantity || 1);
const packed = resolvePackedState(
{ checked: item.checked ? 1 : 0, packed_quantity: item.packed_quantity ?? null },
{ bodyKeys, checked: data.checked, packed_quantity: data.packed_quantity, quantity },
);
this.db.run(`
UPDATE packing_items SET
name = COALESCE(?, name),
checked = CASE WHEN ? IS NOT NULL THEN ? ELSE checked END,
checked = ?,
packed_quantity = ?,
category = COALESCE(?, category),
weight_grams = CASE WHEN ? THEN ? ELSE weight_grams END,
bag_id = CASE WHEN ? THEN ? ELSE bag_id END,
quantity = CASE WHEN ? THEN ? ELSE quantity END,
quantity = ?,
is_private = CASE WHEN ? THEN ? ELSE is_private END,
owner_id = CASE WHEN ? THEN ? ELSE owner_id END,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
`,
data.name || null,
data.checked !== undefined ? 1 : null,
data.checked ? 1 : 0,
packed.checked,
packed.packed_quantity,
data.category || null,
bodyKeys.includes('weight_grams') ? 1 : 0,
data.weight_grams ?? null,
bodyKeys.includes('bag_id') ? 1 : 0,
data.bag_id ?? null,
bodyKeys.includes('quantity') ? 1 : 0,
Math.max(1, Math.min(999, Number(data.quantity) || 1)),
quantity,
bodyKeys.includes('is_private') ? 1 : 0,
data.is_private ? 1 : 0,
claimOwner ? 1 : 0,
@@ -361,7 +369,7 @@ export class PackingService {
*/
private getItemInTrip(tripId: string | number, id: string | number, actorId: number | undefined) {
if (actorId == null) return undefined;
return this.db.get<{ id: number; owner_id: number | null; is_private: number; name: string; category: string | null; quantity: number; weight_grams: number | null; bag_id: number | null; updated_at?: string | null }>(
return this.db.get<{ id: number; owner_id: number | null; is_private: number; name: string; category: string | null; checked: number; quantity: number; packed_quantity?: number | null; weight_grams: number | null; bag_id: number | null; updated_at?: string | null }>(
`SELECT * FROM packing_items WHERE id = ? AND trip_id = ? AND ${PackingService.VISIBLE_TO_ACTOR}`,
id, tripId, actorId, actorId,
);
@@ -41,6 +41,7 @@ import {
PlaceImportListDto,
PlaceImportMapDto,
PlaceRatingDto,
PlaceImageFromFileDto,
PlaceUpdateDto,
} from './places.dto';
@@ -193,6 +194,7 @@ export class PlacesController {
for (const place of result.places) {
this.places.broadcast(tripId, 'place:created', { place }, socketId);
}
if (parseBool(body.enrich, false)) this.places.enrichImportedFilePlaces(tripId, user.id, result.places);
return { places: result.places, count: result.count, skipped: result.skipped };
}
@@ -251,6 +253,7 @@ export class PlacesController {
for (const place of result.places) {
this.places.broadcast(tripId, 'place:created', { place }, socketId);
}
if (parseBool(body.enrich, false)) this.places.enrichImportedFilePlaces(tripId, user.id, result.places);
return result;
} catch (err: unknown) {
if (err instanceof HttpException) throw err;
@@ -418,6 +421,29 @@ export class PlacesController {
return { place };
}
@Put(':id/image/from-file')
async imageFromFile(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('id') id: string,
@Body() body: PlaceImageFromFileDto,
@Headers('x-socket-id') socketId?: string,
) {
const trip = this.requireTrip(tripId, user);
this.requireEdit(trip, user);
if (isDemoWriteBlocked(this.env, user.email)) {
throw new HttpException(DEMO_WRITE_ERROR, 403);
}
const result = await this.places.setImageFromFile(tripId, id, body.file_id);
if (result === 'not_found') throw new HttpException({ error: 'File not found' }, 404);
if (result === 'not_image') throw new HttpException({ error: 'Only jpg, png, gif, webp images allowed' }, 400);
if (result === 'too_large') throw new HttpException({ error: 'Image too large' }, 400);
if (!result || isUpdateConflict(result)) throw new HttpException({ error: 'Place not found' }, 404);
this.places.broadcast(tripId, 'place:updated', { place: result }, socketId);
this.places.onUpdated(result.id);
return { place: result };
}
@Put(':id/rating')
rate(
@CurrentUser() user: User,
+2
View File
@@ -9,6 +9,7 @@ import {
placeExportGpxRequestSchema,
placeRatingRequestSchema,
placeUpdateRequestSchema,
placeImageFromFileRequestSchema,
} from '@trek/shared';
/**
@@ -28,6 +29,7 @@ import {
export class PlaceCreateDto extends createZodDto(placeCreateRequestSchema) {}
export class PlaceUpdateDto extends createZodDto(placeUpdateRequestSchema) {}
export class PlaceRatingDto extends createZodDto(placeRatingRequestSchema) {}
export class PlaceImageFromFileDto extends createZodDto(placeImageFromFileRequestSchema) {}
export class PlaceBulkDeleteDto extends createZodDto(placeBulkDeleteRequestSchema) {}
export class PlaceBulkUpdateDto extends createZodDto(placeBulkUpdateRequestSchema) {}
export class PlaceImportListDto extends createZodDto(placeImportListRequestSchema) {}
+29
View File
@@ -23,6 +23,7 @@ import { noAccess, permissionDenied } from '../../mcp/tools/_shared';
import { DatabaseService } from '../database/database.service';
import { MapsService } from '../maps/maps.service';
import { PlacesService } from './places.service';
import { isUpdateConflict } from '../common/conflictResult';
import { isDirectionsUrl } from './maps-dir.helpers';
function parseId(value: string | string[]): number | null {
@@ -211,6 +212,33 @@ export class PlacesMcp {
return ok({ place });
}
@Tool({
name: 'set_place_image_from_file',
description: 'Use a picture already attached in the trip (a jpg, png, gif or webp from list_files) as the image of the place. The file is copied, so deleting the attachment later keeps the image.',
inputSchema: {
tripId: z.number().int().positive(),
placeId: z.number().int().positive(),
fileId: z.number().int().positive().describe('Id of a trip file that is an image'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
access: { group: 'places', mode: 'write' },
})
async setPlaceImageFromFile(
{ tripId, placeId, fileId }: { tripId: number; placeId: number; fileId: number },
ctx: McpContext,
) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
if (!this.db.canAccessTrip(tripId, ctx.userId)) return noAccess();
if (!this.guards.hasTripPermission('place_edit', tripId, ctx.userId)) return permissionDenied();
const result = await this.places.setImageFromFile(String(tripId), String(placeId), fileId);
if (result === 'not_found') return errorResult('File not found.');
if (result === 'not_image') return errorResult('That file is not a jpg, png, gif or webp image.');
if (result === 'too_large') return errorResult('That image is too large.');
if (!result || isUpdateConflict(result)) return errorResult('Place not found.');
this.guards.safeBroadcast(tripId, 'place:updated', { place: result });
return ok({ place: result });
}
@Tool({
name: 'rate_place',
description: "Set or clear the current user's 1-5 star rating on a trip place (#1435). Every trip member rates independently; the place shows the average. Omit rating (or pass null) to remove the user's vote. Use the ratings to capture the user's preferences and shape the itinerary around highly-rated places.",
@@ -380,6 +408,7 @@ export class PlacesMcp {
const imported = this.places.importGpx(String(input.tripId), Buffer.from(input.gpx, 'utf8'), { importWaypoints: input.importWaypoints, importRoutes: input.importRoutes, importTracks: input.importTracks, defaultName: input.name });
if (!imported) return errorResult('No matching places found in GPX.');
for (const place of imported.places) this.guards.safeBroadcast(input.tripId, 'place:created', { place });
if (input.enrich) this.places.enrichImportedFilePlaces(String(input.tripId), ctx.userId, imported.places);
return ok(imported);
} catch {
return errorResult('Could not import GPX. Check the XML and coordinates.');
+45 -2
View File
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import { resolveCountryCodeSync } from '../atlas/atlas-geo';
import { XMLValidator } from 'fast-xml-parser';
import { TRACK_COLORS, placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared';
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
@@ -25,7 +26,10 @@ import type { GpxExportDay, GpxExportOptions, GpxExportPlace } from './gpx-expor
import { UnsplashService } from '../unsplash/unsplash.service';
import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
import { type UpdateConflict, isUpdateConflict } from '../common/conflictResult';
import { reclaimPlaceImage } from './place-image';
import { placeImageUrl, reclaimPlaceImage } from './place-image';
import { MAX_PLACE_IMAGE_SIZE, PLACE_IMAGE_EXTENSIONS } from '../common/place-image-upload';
import { randomUUID } from 'node:crypto';
import path from 'node:path';
import { JourneyDomainService } from '../journey/journey-domain.service';
import { StorageService } from '../storage/storage.service';
import { AccommodationsService } from '../accommodations/accommodations.service';
@@ -179,9 +183,11 @@ export class PlacesService {
filters: { search?: string; category?: string; tag?: string; assignment?: 'all' | 'unassigned' | 'assigned' },
) {
let query = `
SELECT DISTINCT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon
SELECT DISTINCT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon,
pr.country_code as country_code, pr.region_name as region_name
FROM places p
LEFT JOIN categories c ON p.category_id = c.id
LEFT JOIN place_regions pr ON pr.place_id = p.id
WHERE p.trip_id = ?
`;
const params: (string | number)[] = [tripId];
@@ -222,6 +228,9 @@ export class PlacesService {
return places.map(p => ({
...p,
// The cached region row when the atlas resolved one, else the country the bundled
// borders place it in, which needs no network (#2537).
country_code: (p as { country_code?: string | null }).country_code ?? resolveCountryCodeSync(p),
category: p.category_id ? {
id: p.category_id,
name: p.category_name,
@@ -234,6 +243,30 @@ export class PlacesService {
}));
}
/**
* Makes a picture already attached in the trip the place's own image (#1242). The file
* is copied, not pointed at: the place keeps its picture when the attachment is deleted,
* and the update path reclaims the copy like any other uploaded image. Returns a reason
* string when the file cannot serve, the updated place otherwise.
*/
async setImageFromFile(tripId: string, placeId: string, fileId: number): Promise<'not_found' | 'not_image' | 'too_large' | Awaited<ReturnType<PlacesService['update']>>> {
const file = this.dbs.get<{ filename: string; original_name: string; mime_type: string | null; file_size: number | null }>(
'SELECT filename, original_name, mime_type, file_size FROM trip_files WHERE id = ? AND trip_id = ? AND deleted_at IS NULL',
fileId, tripId,
);
if (!file) return 'not_found';
const ext = path.extname(file.original_name || file.filename).toLowerCase();
const mime = file.mime_type ?? '';
if (!mime.startsWith('image/') || mime.includes('svg') || !PLACE_IMAGE_EXTENSIONS.includes(ext)) return 'not_image';
if (file.file_size != null && file.file_size > MAX_PLACE_IMAGE_SIZE) return 'too_large';
const name = `${randomUUID()}${ext}`;
const { stream } = await this.storage.getStream('files', path.basename(file.filename));
await this.storage.put('places', name, stream, { contentType: mime });
const updated = await this.update(tripId, placeId, { image_url: placeImageUrl(name) } as never);
if (!updated || isUpdateConflict(updated)) await reclaimPlaceImage(this.storage, placeImageUrl(name));
return updated;
}
// -------------------------------------------------------------------------
// Create place
// -------------------------------------------------------------------------
@@ -1499,6 +1532,16 @@ export class PlacesService {
}
}
/**
* The Google pass for places a file brought in (#2536). Only its points: a track
* or a drawn path is a line, and looking a line up by its name finds a stranger.
* Detached like the list imports, and just as quietly a no-op without a key.
*/
enrichImportedFilePlaces(tripId: string, userId: number, places: ImportedPlace[]): void {
const points = (places as (ImportedPlace & EnrichablePlace)[]).filter(p => !p.route_geometry);
void this.enrichImportedPlaces(tripId, userId, points);
}
/**
* Everything a just-imported list gets in the background, in one place so the
* Google and Naver call sites cannot drift apart.
@@ -19,7 +19,7 @@ import { transportLegInputSchema, reservationUrlSchema, type TransportLegInput }
// What counts as a transport booking, for the update_transport gate. Every value
// ReservationsPanel renders with a transport icon, so a stored `transit` row is
// editable through the transport tools like any other.
const TRANSPORT_TYPES = ['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'transit', 'transport_other'] as const;
const TRANSPORT_TYPES = ['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'cable_car', 'transit', 'transport_other'] as const;
// What a caller may ASK for, which is the transport form's own picker
// (client/src/components/Planner/TransportModal.tsx), in its order. The tools
// below used to accept four of these nine, so a bus or a ferry could be planned
@@ -29,7 +29,7 @@ const TRANSPORT_TYPES = ['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cr
// booking carries a provider itinerary in metadata.transit, and create_transit_journey
// is what writes one. A hand-made `transit` row would be a shape the transit UI
// does not expect.
const CREATABLE_TRANSPORT_TYPES = ['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'transport_other'] as const;
const CREATABLE_TRANSPORT_TYPES = ['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'cable_car', 'transport_other'] as const;
/** Only these two carry per-segment detail: the transport form writes metadata.legs for a flight or a train and for nothing else. */
const LEG_TRANSPORT_TYPES = ['flight', 'train'] as const;
/** Everything the picker offers that is not a transport: create_reservation's half. */
+5
View File
@@ -44,6 +44,11 @@ const DISPLAY_PREFERENCES = {
(value) => SUPPORTED_LANGUAGE_CODES.includes(value),
{ message: `Expected one of: ${SUPPORTED_LANGUAGE_CODES.join(', ')}` },
),
// Place names and addresses in search (#1799); '' follows `language`.
place_language: z.union([
z.literal(''),
z.string().refine((value) => SUPPORTED_LANGUAGE_CODES.includes(value), { message: `Expected "" or one of: ${SUPPORTED_LANGUAGE_CODES.join(', ')}` }),
]),
temperature_unit: z.enum(['celsius', 'fahrenheit']),
distance_unit: z.enum(['metric', 'imperial']),
time_format: z.enum(['12h', '24h']),
@@ -0,0 +1,51 @@
/**
* The two narrowing options of a public share link (#1712), as pure functions over
* the snapshot the service has already read, so they can be tested apart from SQL.
*/
/** Getting there and sleeping there: every transport type, and the hotel. */
const TRAVEL_BOOKING_TYPES = new Set([
'flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'cable_car', 'transit', 'transport_other',
'hotel',
]);
export function isTravelBooking(type: unknown): boolean {
return typeof type === 'string' && TRAVEL_BOOKING_TYPES.has(type);
}
interface ShareSnapshot {
assignments: Record<number, Array<{ accommodation_id?: number | null; place?: { id: number; image_url?: string | null } }>>;
dayNotes: Record<number, unknown[]>;
places: Array<{ id: number; image_url?: string | null }>;
reservations: Array<{ type?: unknown }>;
}
/**
* "Only travel and stays": what family or an emergency contact needs to know,
* without the museum and the restaurant. A day keeps only the stop its night's
* booking wrote, the notes go, the place pool shrinks to where the trip sleeps,
* and the bookings to transport and hotels.
*/
export function travelOnly<T extends ShareSnapshot>(data: T, stayPlaceIds: ReadonlySet<number>): T {
const assignments: T['assignments'] = {};
for (const [dayId, rows] of Object.entries(data.assignments)) {
const kept = rows.filter(a => a.accommodation_id != null);
if (kept.length) assignments[Number(dayId)] = kept;
}
return {
...data,
assignments,
dayNotes: {},
places: data.places.filter(p => stayPlaceIds.has(p.id)),
reservations: data.reservations.filter(r => isTravelBooking(r.type)),
};
}
/** Without the place photos: a link to pass around without the preview images. */
export function withoutImages<T extends ShareSnapshot>(data: T): T {
const assignments: T['assignments'] = {};
for (const [dayId, rows] of Object.entries(data.assignments)) {
assignments[Number(dayId)] = rows.map(a => (a.place ? { ...a, place: { ...a.place, image_url: null } } : a));
}
return { ...data, assignments, places: data.places.map(p => ({ ...p, image_url: null })) };
}
@@ -48,6 +48,8 @@ export class TripShareController {
share_packing: body.share_packing,
share_budget: body.share_budget,
share_collab: body.share_collab,
share_travel_only: body.share_travel_only,
share_hide_images: body.share_hide_images,
});
// 201 only on first creation; an update answers 200, mirroring the legacy route.
res.status(result.created ? 201 : 200);
+5 -2
View File
@@ -56,13 +56,16 @@ export class ShareMcp {
share_packing: z.boolean().optional().default(false).describe('Share packing list'),
share_budget: z.boolean().optional().default(false).describe('Share budget'),
share_collab: z.boolean().optional().default(false).describe('Share collab messages'),
share_travel_only: z.boolean().optional().default(false).describe('Narrow the shared plan to transport and stays: no activities, no day notes, only transport and hotel bookings'),
share_hide_images: z.boolean().optional().default(false).describe('Leave the place photos out of the shared page'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
access: (ctx) => canShareTrips(ctx.scopes),
})
async createShareLink(
{ tripId, share_map, share_bookings, share_packing, share_budget, share_collab }: {
{ tripId, share_map, share_bookings, share_packing, share_budget, share_collab, share_travel_only, share_hide_images }: {
tripId: number; share_map?: boolean; share_bookings?: boolean; share_packing?: boolean; share_budget?: boolean; share_collab?: boolean;
share_travel_only?: boolean; share_hide_images?: boolean;
},
ctx: McpContext,
) {
@@ -72,7 +75,7 @@ export class ShareMcp {
// The zod .default()s above fill omitted flags, and ShareService applies
// the same defaults again for undefined — no re-defaulting needed here.
const { token, created } = this.share.createOrUpdate(String(tripId), ctx.userId, {
share_map, share_bookings, share_packing, share_budget, share_collab,
share_map, share_bookings, share_packing, share_budget, share_collab, share_travel_only, share_hide_images,
});
return ok({ token, created });
}
+32 -8
View File
@@ -8,6 +8,7 @@ import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.servic
import { publicReservationSql, publicStaySql } from '../reservations/reservation-visibility';
import { SettingsService } from '../settings/settings.service';
import type { User } from '../../types';
import { travelOnly, withoutImages } from './share-view.helpers';
type Trip = TripAccess;
@@ -34,6 +35,8 @@ export interface SharePermissions {
share_packing?: boolean;
share_budget?: boolean;
share_collab?: boolean;
share_travel_only?: boolean;
share_hide_images?: boolean;
}
export interface ShareTokenInfo {
@@ -44,6 +47,8 @@ export interface ShareTokenInfo {
share_packing: boolean;
share_budget: boolean;
share_collab: boolean;
share_travel_only: boolean;
share_hide_images: boolean;
}
/**
@@ -181,23 +186,26 @@ export class ShareService {
share_packing = false,
share_budget = false,
share_collab = false,
share_travel_only = false,
share_hide_images = false,
} = permissions;
const flags = [share_map, share_bookings, share_packing, share_budget, share_collab, share_travel_only, share_hide_images].map(f => (f ? 1 : 0));
const expiresAt = new Date(Date.now() + 90 * 24 * 60 * 60 * 1000).toISOString();
return this.dbs.transaction(() => {
const existing = this.dbs.get<{ token: string }>('SELECT token FROM share_tokens WHERE trip_id = ?', tripId);
if (existing) {
this.dbs.run(
'UPDATE share_tokens SET share_map = ?, share_bookings = ?, share_packing = ?, share_budget = ?, share_collab = ?, expires_at = ? WHERE trip_id = ?',
share_map ? 1 : 0, share_bookings ? 1 : 0, share_packing ? 1 : 0, share_budget ? 1 : 0, share_collab ? 1 : 0, expiresAt, tripId,
'UPDATE share_tokens SET share_map = ?, share_bookings = ?, share_packing = ?, share_budget = ?, share_collab = ?, share_travel_only = ?, share_hide_images = ?, expires_at = ? WHERE trip_id = ?',
...flags, expiresAt, tripId,
);
return { token: existing.token, created: false };
}
const token = crypto.randomBytes(24).toString('base64url');
this.dbs.run(
'INSERT INTO share_tokens (trip_id, token, created_by, share_map, share_bookings, share_packing, share_budget, share_collab, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
tripId, token, userId, share_map ? 1 : 0, share_bookings ? 1 : 0, share_packing ? 1 : 0, share_budget ? 1 : 0, share_collab ? 1 : 0, expiresAt,
'INSERT INTO share_tokens (trip_id, token, created_by, share_map, share_bookings, share_packing, share_budget, share_collab, share_travel_only, share_hide_images, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
tripId, token, userId, ...flags, expiresAt,
);
return { token, created: true };
});
@@ -217,6 +225,8 @@ export class ShareService {
share_packing: !!row.share_packing,
share_budget: !!row.share_budget,
share_collab: !!row.share_collab,
share_travel_only: !!row.share_travel_only,
share_hide_images: !!row.share_hide_images,
};
}
@@ -280,6 +290,8 @@ export class ShareService {
share_packing: !!shareRow.share_packing,
share_budget: !!shareRow.share_budget,
share_collab: !!shareRow.share_collab,
share_travel_only: !!shareRow.share_travel_only,
share_hide_images: !!shareRow.share_hide_images,
};
// Itinerary — days with assignments/notes, and the place pool
@@ -449,10 +461,20 @@ export class ShareService {
const ownerCartoKey = ownerSettings['carto_api_key'];
const cartoApiKey = typeof ownerCartoKey === 'string' ? ownerCartoKey.trim() : '';
// The owner's narrowing options (#1712) apply last, over what the flags
// above already let through.
let view = { assignments, dayNotes, places, reservations };
if (permissions.share_travel_only) {
const stayPlaceIds = new Set(this.dbs.all<{ place_id: number }>(
`SELECT DISTINCT a.place_id FROM day_accommodations a WHERE a.trip_id = ? AND ${publicStaySql('a')}`, tripId,
).map(r => r.place_id));
view = travelOnly(view, stayPlaceIds);
}
if (permissions.share_hide_images) view = withoutImages(view);
return {
trip, baseCurrency, cartoApiKey, categories, permissions,
days, assignments, dayNotes, places,
reservations, accommodations,
days, ...view, accommodations,
packing, budget,
collab: collabMessages,
};
@@ -468,11 +490,13 @@ export class ShareService {
* mirroring the authenticated bytes endpoint.
*/
async getSharedPlacePhotoKey(token: string, placeId: string): Promise<string | null> {
const shareRow = this.dbs.get<{ trip_id: string; share_map: number }>(
"SELECT trip_id, share_map FROM share_tokens WHERE token = ? AND (expires_at IS NULL OR expires_at > datetime('now'))",
const shareRow = this.dbs.get<{ trip_id: string; share_map: number; share_hide_images?: number }>(
"SELECT trip_id, share_map, share_hide_images FROM share_tokens WHERE token = ? AND (expires_at IS NULL OR expires_at > datetime('now'))",
token,
);
if (!shareRow) return null;
// A link that leaves the photos out (#1712) does not serve them either.
if (shareRow.share_hide_images) return null;
// Place photos belong to the map/itinerary section — withhold them when the
// owner disabled the map, matching getSharedTripData which no longer returns
// the places (and thus their ids) in that case.
+1 -1
View File
@@ -244,7 +244,7 @@ export class VacayController {
@Headers('x-socket-id') socketId?: string,
) {
const planId = this.vacay.getActivePlanId(user.id);
return this.vacay.toggleCompanyHoliday(planId, body.date, body.note, socketId);
return this.vacay.toggleCompanyHoliday(planId, body.date, body.note, socketId, body.fraction);
}
@Get('stats/:year')
+4 -3
View File
@@ -353,19 +353,20 @@ export class VacayMcp {
@Tool({
name: 'toggle_company_holiday',
description: 'Toggle a date as a company holiday for the whole plan.',
description: 'Toggle a date as a company holiday for the whole plan, whole or half. The same size again removes it; the other size converts it. A half company holiday leaves room for a half vacation day.',
inputSchema: {
date: z.string(),
note: z.string().optional(),
half: z.boolean().optional().describe('True makes it a half company holiday (e.g. a free afternoon)'),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: vacayAddonOn,
access: { group: 'vacay', mode: 'write' },
})
async toggleCompanyHoliday({ date, note }: { date: string; note?: string }, ctx: McpContext) {
async toggleCompanyHoliday({ date, note, half }: { date: string; note?: string; half?: boolean }, ctx: McpContext) {
if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
const planId = this.vacay.getActivePlanId(ctx.userId);
const result = this.vacay.toggleCompanyHoliday(planId, date, note, undefined);
const result = this.vacay.toggleCompanyHoliday(planId, date, note, undefined, half ? 0.5 : 1);
return ok(result);
}
+1 -1
View File
@@ -40,7 +40,7 @@ export class VacayRpc {
const date = this.dateStr(params.date);
const note = typeof params.note === 'string' ? params.note.slice(0, 256) : undefined;
this.requireVacayAddon();
return this.vacay.toggleCompanyHoliday(this.vacay.getActivePlanId(userId), date, note, undefined);
return this.vacay.toggleCompanyHoliday(this.vacay.getActivePlanId(userId), date, note, undefined, params.fraction);
}
private requireVacayUser(ctx: PluginRpcContext, kind: 'reads' | 'writes'): number {
+41 -22
View File
@@ -520,10 +520,8 @@ export class VacayService {
}
if (company_holidays_enabled === true) {
const companyDates = this.db.all<{ date: string }>('SELECT date FROM vacay_company_holidays WHERE plan_id = ?', planId);
for (const { date } of companyDates) {
this.db.run('DELETE FROM vacay_entries WHERE plan_id = ? AND date = ?', planId, date);
}
const companyDates = this.db.all<{ date: string; fraction: number | null }>('SELECT date, fraction FROM vacay_company_holidays WHERE plan_id = ?', planId);
for (const { date, fraction } of companyDates) this.makeRoomForCompanyHoliday(planId, date, fraction ?? 1);
}
const updatedPlan = this.db.get<VacayPlan>('SELECT * FROM vacay_plans WHERE id = ?', planId)!;
@@ -760,7 +758,7 @@ export class VacayService {
const isOwnerFlag = plan.owner_id === userId;
const userIds = this.getPlanUsers(plan.id).map(u => u.id);
const companyHolidays = this.db.all<{ date: string; note: string }>('SELECT date, note FROM vacay_company_holidays WHERE plan_id = ?', plan.id);
const companyHolidays = this.db.all<{ date: string; note: string; fraction: number | null }>('SELECT date, note, fraction FROM vacay_company_holidays WHERE plan_id = ?', plan.id);
if (isOwnerFlag) {
const members = this.db.all<{ user_id: number }>("SELECT user_id FROM vacay_plan_members WHERE plan_id = ? AND status = 'accepted'", plan.id);
@@ -768,7 +766,7 @@ export class VacayService {
const memberPlan = this.getOwnPlan(m.user_id);
this.db.run('UPDATE vacay_entries SET plan_id = ? WHERE plan_id = ? AND user_id = ?', memberPlan.id, plan.id, m.user_id);
for (const ch of companyHolidays) {
this.db.run('INSERT OR IGNORE INTO vacay_company_holidays (plan_id, date, note) VALUES (?, ?, ?)', memberPlan.id, ch.date, ch.note);
this.db.run('INSERT OR IGNORE INTO vacay_company_holidays (plan_id, date, note, fraction) VALUES (?, ?, ?, ?)', memberPlan.id, ch.date, ch.note, ch.fraction ?? 1);
}
}
this.db.run('DELETE FROM vacay_plan_members WHERE plan_id = ?', plan.id);
@@ -776,7 +774,7 @@ export class VacayService {
const ownPlan = this.getOwnPlan(userId);
this.db.run('UPDATE vacay_entries SET plan_id = ? WHERE plan_id = ? AND user_id = ?', ownPlan.id, plan.id, userId);
for (const ch of companyHolidays) {
this.db.run('INSERT OR IGNORE INTO vacay_company_holidays (plan_id, date, note) VALUES (?, ?, ?)', ownPlan.id, ch.date, ch.note);
this.db.run('INSERT OR IGNORE INTO vacay_company_holidays (plan_id, date, note, fraction) VALUES (?, ?, ?, ?)', ownPlan.id, ch.date, ch.note, ch.fraction ?? 1);
}
this.db.run('DELETE FROM vacay_plan_members WHERE plan_id = ? AND user_id = ?', plan.id, userId);
}
@@ -983,7 +981,7 @@ export class VacayService {
// only exposed while the owner has the feature enabled, and dates only —
// the note text may be authored by plan members who aren't part of the share.
const companyHolidays = plan.company_holidays_enabled
? this.db.all('SELECT date FROM vacay_company_holidays WHERE plan_id = ? AND date >= ? AND date < ? ORDER BY date', plan.id, start, end)
? this.db.all('SELECT date, fraction FROM vacay_company_holidays WHERE plan_id = ? AND date >= ? AND date < ? ORDER BY date', plan.id, start, end)
: [];
return { share_id: s.id, owner_id: s.owner_id, owner_name: s.username, color, hidden: !!s.hidden, entries, companyHolidays };
});
@@ -1104,9 +1102,13 @@ export class VacayService {
}
toggleEntry(userId: number, planId: number, date: string, fraction?: unknown, kind?: unknown, socketId?: string): { action?: string; fraction?: number; kind?: string; error?: string } {
const frac = normalizeFraction(fraction);
const knd = normalizeKind(kind);
const plan = this.db.get<VacayPlan>('SELECT * FROM vacay_plans WHERE id = ?', planId);
// Half the day is the company's already (#2439): what is left is half a day.
const company = plan?.company_holidays_enabled
? this.db.get<{ fraction: number | null }>('SELECT fraction FROM vacay_company_holidays WHERE plan_id = ? AND date = ?', planId, date)
: undefined;
const frac = company && (company.fraction ?? 1) < 1 ? 0.5 : normalizeFraction(fraction);
const knd = normalizeKind(kind);
const weekendBlocked = plan ? isBlockedWeekend(plan, date) : false;
const existing = this.db.get<{ id: number; fraction: number; kind: string | null }>('SELECT id, fraction, kind FROM vacay_entries WHERE user_id = ? AND date = ? AND plan_id = ?', userId, date, planId);
if (existing) {
@@ -1130,18 +1132,35 @@ export class VacayService {
return { action: 'added', fraction: frac, kind: knd };
}
toggleCompanyHoliday(planId: number, date: string, note: string | undefined, socketId: string | undefined): { action: string } {
const existing = this.db.get<{ id: number }>('SELECT id FROM vacay_company_holidays WHERE plan_id = ? AND date = ?', planId, date);
if (existing) {
this.db.run('DELETE FROM vacay_company_holidays WHERE id = ?', existing.id);
this.notifyPlanUsers(planId, socketId);
return { action: 'removed' };
} else {
this.db.run('INSERT INTO vacay_company_holidays (plan_id, date, note) VALUES (?, ?, ?)', planId, date, note || '');
this.db.run('DELETE FROM vacay_entries WHERE plan_id = ? AND date = ?', planId, date);
this.notifyPlanUsers(planId, socketId);
return { action: 'added' };
}
/**
* A company holiday on or off, whole or half (#2439). The same click again clears
* it; the other size converts it in place, the way a vacation entry toggles. A
* whole company holiday leaves no room for leave that day; a half one leaves half.
*/
toggleCompanyHoliday(planId: number, date: string, note: string | undefined, socketId: string | undefined, fraction?: unknown): { action: string; fraction?: number } {
const frac = normalizeFraction(fraction);
const result = this.db.transaction(() => {
const existing = this.db.get<{ id: number; fraction: number | null }>('SELECT id, fraction FROM vacay_company_holidays WHERE plan_id = ? AND date = ?', planId, date);
if (existing && (existing.fraction ?? 1) === frac) {
this.db.run('DELETE FROM vacay_company_holidays WHERE id = ?', existing.id);
return { action: 'removed' };
}
if (existing) {
this.db.run('UPDATE vacay_company_holidays SET fraction = ? WHERE id = ?', frac, existing.id);
} else {
this.db.run('INSERT INTO vacay_company_holidays (plan_id, date, note, fraction) VALUES (?, ?, ?, ?)', planId, date, note || '', frac);
}
this.makeRoomForCompanyHoliday(planId, date, frac);
return { action: existing ? 'updated' : 'added', fraction: frac };
});
this.notifyPlanUsers(planId, socketId);
return result;
}
/** What a company holiday leaves of the day's leave: nothing for a whole one, half for a half one. */
private makeRoomForCompanyHoliday(planId: number, date: string, fraction: number): void {
if (fraction >= 1) this.db.run('DELETE FROM vacay_entries WHERE plan_id = ? AND date = ?', planId, date);
else this.db.run('UPDATE vacay_entries SET fraction = 0.5 WHERE plan_id = ? AND date = ? AND fraction > 0.5', planId, date);
}
// -------------------------------------------------------------------------
+3
View File
@@ -119,6 +119,7 @@ export interface DayAssignment {
assignment_end_time?: string | null;
end_day?: number;
leg_transport_mode?: string | null;
route_excluded?: number;
incoming_leg_transport_mode?: string | null;
/** The lodging booking that put this stop on the day, when one did. */
accommodation_id?: number | null;
@@ -425,6 +426,8 @@ export interface JourneyEntry {
* The wire carries a boolean; journey-entry-row.ts is where the two meet.
*/
stats_excluded: number;
/** 1 while the entry is a draft (#696), kept off the public share page. */
is_draft?: number;
created_at: number;
updated_at: number;
}
+34
View File
@@ -155,6 +155,40 @@ describe('Assignments e2e (real auth guard + temp SQLite)', () => {
expect(reconcileTripSkeletons).toHaveBeenCalledWith(5, undefined);
});
it('200 clear day removes every place of the day and keeps the day (#2470)', async () => {
reconcileTripSkeletons.mockClear();
const first = seedAssignment(3, 2, 0);
const second = seedAssignment(3, 2, 1);
const elsewhere = seedAssignment(4, 2, 0);
const res = await request(server).delete('/api/trips/5/days/3/assignments').set('Cookie', sessionCookie(1));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect([...res.body.removedIds].sort()).toEqual([first, second].sort());
expect(db.prepare('SELECT COUNT(*) AS n FROM day_assignments WHERE day_id = 3').get()).toEqual({ n: 0 });
expect(db.prepare('SELECT id FROM day_assignments WHERE id = ?').get(elsewhere)).toEqual({ id: elsewhere });
expect(reconcileTripSkeletons).toHaveBeenCalledWith(5, undefined);
});
it('403 clear day without day_edit, 404 for a foreign day', async () => {
seedAssignment();
checkPermission.mockReturnValue(false);
expect((await request(server).delete('/api/trips/5/days/3/assignments').set('Cookie', sessionCookie(1))).status).toBe(403);
checkPermission.mockReturnValue(true);
const miss = await request(server).delete('/api/trips/5/days/999/assignments').set('Cookie', sessionCookie(1));
expect(miss.status).toBe(404);
expect(miss.body).toEqual({ error: 'Day not found' });
});
it('200 route exclude roundtrip, 400 without a boolean (#2532)', async () => {
const id = seedAssignment();
const res = await request(server).put(`/api/trips/5/assignments/${id}/route`).set('Cookie', sessionCookie(1)).send({ excluded: true });
expect(res.status).toBe(200);
expect(res.body.assignment).toMatchObject({ id, route_excluded: true });
const list = await request(server).get('/api/trips/5/days/3/assignments').set('Cookie', sessionCookie(1));
expect(list.body.assignments[0].route_excluded).toBe(true);
expect((await request(server).put(`/api/trips/5/assignments/${id}/route`).set('Cookie', sessionCookie(1)).send({ excluded: 'yes' })).status).toBe(400);
});
it('200 move assignment reconciles journey skeletons', async () => {
reconcileTripSkeletons.mockClear();
const id = seedAssignment();
+10
View File
@@ -147,6 +147,16 @@ describe('Atlas e2e (real auth guard + real service + temp SQLite)', () => {
expect(row).toEqual({ name: 'Kyoto', user_id: userId });
});
it('keeps a wished-for region, and refuses one outside the item country (#1901)', async () => {
const ok = await request(server).post('/api/addons/atlas/bucket-list').set('Cookie', sessionCookie(userId))
.send({ name: 'Bayern', country_code: 'DE', region_code: 'DE-BY' });
expect(ok.status).toBe(201);
expect(ok.body.item.region_code).toBe('DE-BY');
const bad = await request(server).post('/api/addons/atlas/bucket-list').set('Cookie', sessionCookie(userId))
.send({ name: 'Berlin', country_code: 'FR', region_code: 'DE-BE' });
expect(bad.status).toBe(400);
});
it('404 on delete of a missing bucket item', async () => {
const res = await request(server).delete('/api/addons/atlas/bucket-list/999').set('Cookie', sessionCookie(userId));
expect(res.status).toBe(404);
+17
View File
@@ -293,6 +293,23 @@ describe('Budget e2e (real auth guard + temp SQLite, real budget SQL)', () => {
expect(row).toEqual({ settled_at: null });
});
it('keeps a note on a settlement, leaves it alone when omitted and clears it on blank (#2340)', async () => {
const created = await request(server)
.post(`/api/trips/${tripId}/budget/settlements`)
.set('Cookie', sessionCookie(1))
.send({ from_user_id: 2, to_user_id: 1, amount: 10, note: ' Cash at the airport ' });
expect(created.status).toBe(201);
expect(created.body.settlement.note).toBe('Cash at the airport');
const url = `/api/trips/${tripId}/budget/settlements/${created.body.settlement.id}`;
const kept = await request(server).put(url).set('Cookie', sessionCookie(1)).send({ from_user_id: 2, to_user_id: 1, amount: 12 });
expect(kept.body.settlement.note).toBe('Cash at the airport');
const cleared = await request(server).put(url).set('Cookie', sessionCookie(1)).send({ from_user_id: 2, to_user_id: 1, amount: 12, note: ' ' });
expect(cleared.body.settlement.note).toBeNull();
const tooLong = await request(server).put(url).set('Cookie', sessionCookie(1)).send({ from_user_id: 2, to_user_id: 1, amount: 12, note: 'x'.repeat(501) });
expect(tooLong.status).toBe(400);
});
it('404 on settlement update when it does not exist', async () => {
const res = await request(server)
.put(`/api/trips/${tripId}/budget/settlements/424242`)
+14
View File
@@ -57,6 +57,7 @@ describe('Maps e2e (real auth guard + temp SQLite)', () => {
const maps = app.get(MapsService);
vi.spyOn(maps, 'searchPlaces').mockResolvedValue({ places: [{ name: 'Berlin' }], source: 'osm' });
vi.spyOn(maps, 'reverseGeocode').mockResolvedValue({ name: 'Spot', address: 'Street 1' });
vi.spyOn(maps, 'nearbyPlaces').mockResolvedValue({ places: [{ name: 'Cafe', distance_m: 40 }], source: 'trek-places' });
});
afterAll(async () => {
@@ -84,6 +85,19 @@ describe('Maps e2e (real auth guard + temp SQLite)', () => {
expect(res.body).toEqual({ places: [{ name: 'Berlin' }], source: 'osm' });
});
it('200 with the places near a point (POST stays 200, not 201)', async () => {
const res = await request(server).post('/api/maps/nearby?lang=de').set('Cookie', sessionCookie(1)).send({ lat: 52.5, lng: 13.4 });
expect(res.status).toBe(200);
expect(res.body).toEqual({ places: [{ name: 'Cafe', distance_m: 40 }], source: 'trek-places' });
});
it('400 on nearby with a circle wider than the contract allows, 401 without a session', async () => {
const wide = await request(server).post('/api/maps/nearby').set('Cookie', sessionCookie(1)).send({ lat: 52.5, lng: 13.4, radius: 9000 });
expect(wide.status).toBe(400);
const anon = await request(server).post('/api/maps/nearby').send({ lat: 52.5, lng: 13.4 });
expect(anon.status).toBe(401);
});
it('200 on reverse geocode', async () => {
const res = await request(server).get('/api/maps/reverse').set('Cookie', sessionCookie(1)).query({ lat: '52.5', lng: '13.4' });
expect(res.status).toBe(200);
+12
View File
@@ -353,6 +353,18 @@ describe('Packing e2e (real auth guard + real SQL over temp SQLite)', () => {
expect(res.body.item.checked).toBe(1);
});
it('counts packed pieces and ticks the item once the count is full (#2296)', async () => {
const id = insertItem(tripId, 'Shirts');
await request(server).put(`/api/trips/${tripId}/packing/${id}`).set('Cookie', sessionCookie(1)).send({ quantity: 3 });
const partial = await request(server).put(`/api/trips/${tripId}/packing/${id}`).set('Cookie', sessionCookie(1)).send({ packed_quantity: 2 });
expect(partial.status).toBe(200);
expect(partial.body.item).toMatchObject({ packed_quantity: 2, checked: 0 });
const full = await request(server).put(`/api/trips/${tripId}/packing/${id}`).set('Cookie', sessionCookie(1)).send({ packed_quantity: 3 });
expect(full.body.item).toMatchObject({ packed_quantity: null, checked: 1 });
const negative = await request(server).put(`/api/trips/${tripId}/packing/${id}`).set('Cookie', sessionCookie(1)).send({ packed_quantity: -1 });
expect(negative.status).toBe(400);
});
it('whitespace-only bag name still gets the bespoke 400', async () => {
const res = await request(server).post(`/api/trips/${tripId}/packing/bags`).set('Cookie', sessionCookie(1)).send({ name: ' ' });
expect(res.status).toBe(400);
+12
View File
@@ -151,6 +151,18 @@ describe('Share-link e2e (real auth guard + real SQL over temp SQLite)', () => {
expect(after.body).toEqual({ token: null });
});
it('carries the narrowing options through to the public read (#1712)', async () => {
const created = await request(server).post(`/api/trips/${tripId}/share-link`).set('Cookie', sessionCookie(1))
.send({ share_travel_only: true, share_hide_images: true });
const info = await request(server).get(`/api/trips/${tripId}/share-link`).set('Cookie', sessionCookie(1));
expect(info.body).toEqual(expect.objectContaining({ share_travel_only: true, share_hide_images: true }));
const res = await request(server).get(`/api/shared/${created.body.token}`);
expect(res.body.permissions).toEqual(expect.objectContaining({ share_travel_only: true, share_hide_images: true }));
expect(res.body.dayNotes).toEqual({});
const bad = await request(server).post(`/api/trips/${tripId}/share-link`).set('Cookie', sessionCookie(1)).send({ share_hide_images: 'yes' });
expect(bad.status).toBe(400);
});
it('public shared read is unguarded (200, no cookie)', async () => {
const created = await request(server).post(`/api/trips/${tripId}/share-link`).set('Cookie', sessionCookie(1)).send({});
const res = await request(server).get(`/api/shared/${created.body.token}`);
+13
View File
@@ -438,6 +438,19 @@ describe('Vacay company holidays', () => {
expect(res.status).toBe(200);
});
it('VACAY-032b — POST /entries/company-holiday takes a half day and refuses any other size (#2439)', async () => {
const { user } = createUser(testDb);
await request(app).get('/api/addons/vacay/plan').set('Cookie', authCookie(user.id));
await request(app).post('/api/addons/vacay/years').set('Cookie', authCookie(user.id)).send({ year: 2025 });
const half = await request(app).post('/api/addons/vacay/entries/company-holiday').set('Cookie', authCookie(user.id))
.send({ date: '2025-12-24', fraction: 0.5 });
expect(half.status).toBe(200);
expect(half.body).toMatchObject({ action: 'added', fraction: 0.5 });
const odd = await request(app).post('/api/addons/vacay/entries/company-holiday').set('Cookie', authCookie(user.id))
.send({ date: '2025-12-24', fraction: 0.25 });
expect(odd.status).toBe(400);
});
it('VACAY-033 — POST /entries/toggle with target_user_id not in plan returns 403', async () => {
const { user: owner } = createUser(testDb);
const { user: outsider } = createUser(testDb);
@@ -247,6 +247,12 @@ describe('deriveBackup', () => {
expect(deriveBackup({}).maxDecompressedMb).toBe(5 * 1024);
expect(deriveBackup({ ENCRYPTION_KEY: 'x' }).encryptionKeyFromEnv).toBe(true);
});
it('reads RESTORE_FROM_BACKUP as a trimmed path, and blank as unset (#1089)', () => {
expect(deriveBackup({}).restoreFromBackup).toBeNull();
expect(deriveBackup({ RESTORE_FROM_BACKUP: ' ' }).restoreFromBackup).toBeNull();
expect(deriveBackup({ RESTORE_FROM_BACKUP: ' /app/data/b.zip ' }).restoreFromBackup).toBe('/app/data/b.zip');
});
});
describe('deriveNet', () => {
@@ -285,6 +285,46 @@ describe('Tool: unassign_place', () => {
// reorder_day_assignments
// ---------------------------------------------------------------------------
describe('Tool: clear_day_assignments (#2470)', () => {
it('removes every place from the day and broadcasts each removal', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id);
const a1 = createDayAssignment(testDb, day.id, place.id);
const a2 = createDayAssignment(testDb, day.id, place.id);
await withHarness(user.id, async (h) => {
const data = parseToolResult(await h.client.callTool({ name: 'clear_day_assignments', arguments: { tripId: trip.id, dayId: day.id } })) as any;
expect(data.success).toBe(true);
expect([...data.removedIds].sort()).toEqual([a1.id, a2.id].sort());
expect(testDb.prepare('SELECT COUNT(*) AS n FROM day_assignments WHERE day_id = ?').get(day.id)).toEqual({ n: 0 });
expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'assignment:deleted', { assignmentId: a1.id, dayId: day.id });
});
});
it('returns an error for a day on another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const foreignDay = createDay(testDb, otherTrip.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'clear_day_assignments', arguments: { tripId: trip.id, dayId: foreignDay.id } });
expect(result.isError).toBe(true);
});
});
it('returns access denied for non-member', async () => {
const { user } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, other.id);
const day = createDay(testDb, trip.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'clear_day_assignments', arguments: { tripId: trip.id, dayId: day.id } });
expect(result.isError).toBe(true);
});
});
});
describe('Tool: reorder_day_assignments', () => {
it('reorders assignments by updating order_index', async () => {
const { user } = createUser(testDb);
+14
View File
@@ -138,6 +138,20 @@ describe('Tool: unmark_country_visited', () => {
// ---------------------------------------------------------------------------
describe('Tool: create_bucket_list_item', () => {
it('stores a wished-for region and refuses one outside its country (#1901)', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const ok = parseToolResult(await h.client.callTool({
name: 'create_bucket_list_item', arguments: { name: 'Bayern', country_code: 'DE', region_code: 'DE-BY' },
})) as any;
expect(ok.item.region_code).toBe('DE-BY');
const bad = await h.client.callTool({
name: 'create_bucket_list_item', arguments: { name: 'Berlin', country_code: 'FR', region_code: 'DE-BE' },
});
expect(bad.isError).toBe(true);
});
});
it('creates a bucket list item with all fields', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
@@ -641,6 +641,22 @@ describe('Tool: update_budget_item (currency)', () => {
});
});
it('records and edits a note on the payment (#2340)', async () => {
const { user, other, trip } = tripWithTwo();
await withHarness(user.id, async (h) => {
const created = parseToolResult(await h.client.callTool({
name: 'create_settlement',
arguments: { tripId: trip.id, from_user_id: other.id, to_user_id: user.id, amount: 20, note: 'Cash' },
})) as any;
expect(created.settlement.note).toBe('Cash');
const updated = parseToolResult(await h.client.callTool({
name: 'update_settlement',
arguments: { tripId: trip.id, settlementId: created.settlement.id, from_user_id: other.id, to_user_id: user.id, amount: 20, note: 'Bank transfer' },
})) as any;
expect(updated.settlement.note).toBe('Bank transfer');
});
});
it('refuses a currency that is not a code', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -143,6 +143,21 @@ describe('Tool: update_journey_entry', () => {
expect(Array.isArray(data.entry.photos)).toBe(true);
});
});
it('creates a draft and publishes it, as the REST route does (#696)', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const journey = (parseToolResult(await h.client.callTool({ name: 'create_journey', arguments: { title: 'J' } })) as any).journey;
const draft = (parseToolResult(await h.client.callTool({
name: 'create_journey_entry', arguments: { journeyId: journey.id, entry_date: '2026-07-01', title: 'Rough', is_draft: true },
})) as any).entry;
expect(draft.is_draft).toBe(true);
const published = (parseToolResult(await h.client.callTool({
name: 'update_journey_entry', arguments: { entryId: draft.id, is_draft: false },
})) as any).entry;
expect(published.is_draft).toBe(false);
});
});
});
describe('Tool: update_journey_preferences', () => {
@@ -391,6 +406,19 @@ describe('journey write tools', () => {
});
});
it('reorder_journey_entry_photos refuses ids that are not the entry photos (#824)', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const journey = await seedJourney(h);
const entry = (parseToolResult(await h.client.callTool({
name: 'create_journey_entry', arguments: { journeyId: journey.id, entry_date: '2026-07-01', title: 'A' },
})) as any).entry;
expect((await h.client.callTool({
name: 'reorder_journey_entry_photos', arguments: { entryId: entry.id, orderedIds: [999999] },
})).isError).toBe(true);
});
});
it('contributor tools add, re-role and remove; each refuses a foreign journey', async () => {
const { user } = createUser(testDb);
const { user: guest } = createUser(testDb);
@@ -63,6 +63,37 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>)
// set_leg_transport_mode
// ---------------------------------------------------------------------------
describe('Tool: set_assignment_route_excluded (#2532)', () => {
it('takes a stop out of the route and puts it back, broadcasting each change', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id);
const assignment = createDayAssignment(testDb, day.id, place.id);
await withHarness(user.id, async (h) => {
const out = parseToolResult(await h.client.callTool({ name: 'set_assignment_route_excluded', arguments: { tripId: trip.id, assignmentId: assignment.id, excluded: true } })) as any;
expect(out.assignment.route_excluded).toBe(true);
expect(testDb.prepare('SELECT route_excluded FROM day_assignments WHERE id = ?').get(assignment.id)).toEqual({ route_excluded: 1 });
const back = parseToolResult(await h.client.callTool({ name: 'set_assignment_route_excluded', arguments: { tripId: trip.id, assignmentId: assignment.id, excluded: false } })) as any;
expect(back.assignment.route_excluded).toBe(false);
expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'assignment:updated', expect.any(Object));
});
});
it('rejects an assignment of another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
const day = createDay(testDb, other.id);
const place = createPlace(testDb, other.id);
const assignment = createDayAssignment(testDb, day.id, place.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'set_assignment_route_excluded', arguments: { tripId: trip.id, assignmentId: assignment.id, excluded: true } });
expect(result.isError).toBe(true);
});
});
});
describe('Tool: set_leg_transport_mode', () => {
it('sets the outgoing leg mode (default direction)', async () => {
const { user } = createUser(testDb);
@@ -301,6 +301,19 @@ describe('Tool: update_packing_item', () => {
});
});
it('counts packed pieces like the REST route does (#2296)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createPackingItem(testDb, trip.id, { name: 'Shirts' });
await withHarness(user.id, async (h) => {
await h.client.callTool({ name: 'update_packing_item', arguments: { tripId: trip.id, itemId: item.id, quantity: 4 } });
const partial = parseToolResult(await h.client.callTool({ name: 'update_packing_item', arguments: { tripId: trip.id, itemId: item.id, packed_quantity: 3 } })) as any;
expect(partial.item).toMatchObject({ packed_quantity: 3, checked: 0 });
const full = parseToolResult(await h.client.callTool({ name: 'update_packing_item', arguments: { tripId: trip.id, itemId: item.id, packed_quantity: 4 } })) as any;
expect(full.item).toMatchObject({ packed_quantity: null, checked: 1 });
});
});
it('broadcasts packing:updated event', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -219,6 +219,23 @@ describe('Tool: create_place', () => {
// update_place
// ---------------------------------------------------------------------------
describe('Tool: set_place_image_from_file (#1242)', () => {
it('refuses a file that is no picture, and one from another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id);
const pdf = Number(testDb.prepare("INSERT INTO trip_files (trip_id, filename, original_name, mime_type) VALUES (?, 'a.pdf', 'a.pdf', 'application/pdf')").run(trip.id).lastInsertRowid);
const foreign = Number(testDb.prepare("INSERT INTO trip_files (trip_id, filename, original_name, mime_type) VALUES (?, 'b.jpg', 'b.jpg', 'image/jpeg')").run(other.id).lastInsertRowid);
await withHarness(user.id, async (h) => {
const notImage = await h.client.callTool({ name: 'set_place_image_from_file', arguments: { tripId: trip.id, placeId: place.id, fileId: pdf } });
expect(notImage.isError).toBe(true);
const notFound = await h.client.callTool({ name: 'set_place_image_from_file', arguments: { tripId: trip.id, placeId: place.id, fileId: foreign } });
expect(notFound.isError).toBe(true);
});
});
});
describe('Tool: update_place', () => {
it('updates specific fields and preserves others', async () => {
const { user } = createUser(testDb);
@@ -259,6 +259,19 @@ describe('Tool: update_display_settings', () => {
});
});
it('writes place_language, clears it with "", and refuses a language TREK does not offer (#1799)', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const set = parseToolResult(await update(h, { place_language: 'en' })) as any;
expect(set.settings.place_language).toBe('en');
const refused = await update(h, { place_language: 'klingon' });
expect(refused.isError).toBe(true);
expect(readSetting(user.id, 'place_language')).toBe('en');
const cleared = parseToolResult(await update(h, { place_language: '' })) as any;
expect(cleared.settings.place_language).toBe('');
});
});
it('writes week_start and refuses a day outside the three the app offers (#2029)', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
+10
View File
@@ -71,6 +71,16 @@ describe('Tool: get_share_link', () => {
});
});
it('stores the two narrowing options like the REST route (#1712)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
await withHarness(user.id, async (h) => {
await h.client.callTool({ name: 'create_share_link', arguments: { tripId: trip.id, share_travel_only: true, share_hide_images: true } });
const link = parseToolResult(await h.client.callTool({ name: 'get_share_link', arguments: { tripId: trip.id } })) as any;
expect(link.link).toMatchObject({ share_travel_only: true, share_hide_images: true });
});
});
it('returns access denied for a non-member trip', async () => {
const { user } = createUser(testDb);
const { user: other } = createUser(testDb);
@@ -422,6 +422,44 @@ describe('Tool: get_place_details', () => {
});
});
// ---------------------------------------------------------------------------
// search_nearby_places (#976)
// ---------------------------------------------------------------------------
describe('Tool: search_nearby_places', () => {
it('lists what the index holds around the point, nearest first with the distance', async () => {
const { user } = createUser(testDb);
trekNearbyMock.mockClear();
trekNearbyMock.mockResolvedValueOnce([
{ gers: 'n-2', name: 'Farther', lat: 48.8611, lng: 2.3364, address: null, contact: null },
{ gers: 'n-1', name: 'Closer', lat: 48.8607, lng: 2.3376, address: null, contact: null },
]);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'search_nearby_places',
arguments: { lat: 48.8606, lng: 2.3376, radius: 200, limit: 5 },
});
const data = parseToolResult(result) as any;
expect(data.source).toBe('trek-places');
expect(data.places.map((p: any) => p.name)).toEqual(['Closer', 'Farther']);
expect(data.places[0].distance_m).toBe(11);
expect(trekNearbyMock).toHaveBeenCalledWith(48.8606, 2.3376, { radius: 200, limit: 5 });
});
});
it('refuses a circle wider than the REST route allows', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'search_nearby_places',
arguments: { lat: 48.8606, lng: 2.3376, radius: 5001 },
});
expect(result.isError).toBe(true);
});
});
});
// ---------------------------------------------------------------------------
// search_pois (#30)
// ---------------------------------------------------------------------------
+10
View File
@@ -478,6 +478,16 @@ describe('Tool: toggle_company_holiday', () => {
});
});
it('makes a half company holiday like the REST route (#2439)', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const data = parseToolResult(await h.client.callTool({
name: 'toggle_company_holiday', arguments: { date: '2025-12-24', half: true },
})) as any;
expect(data).toMatchObject({ action: 'added', fraction: 0.5 });
});
});
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
@@ -64,9 +64,39 @@ describe('DayAssignmentsController (parity with the legacy day-assignments route
expect(new DayAssignmentsController(s).remove(user, '5', '3', '9', 'sock')).toEqual({ success: true });
expect(reconcile).toHaveBeenCalledWith('5', 'sock');
});
it('DELETE / (clear day) 404 day, else empties the day and announces every removal (#2470)', () => {
expect(thrown(() => new DayAssignmentsController(svc({ dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).clear(user, '5', '3'))).toEqual({ status: 404, body: { error: 'Day not found' } });
const clearDay = vi.fn().mockReturnValue([7, 8]); const broadcast = vi.fn(); const reconcile = vi.fn();
const s = svc({ clearDay, broadcast, reconcile } as Partial<AssignmentsService>);
expect(new DayAssignmentsController(s).clear(user, '5', '3', 'sock')).toEqual({ success: true, removedIds: [7, 8] });
expect(clearDay).toHaveBeenCalledWith('3');
expect(broadcast).toHaveBeenCalledWith('5', 'assignment:deleted', { assignmentId: 7, dayId: 3 }, 'sock');
expect(broadcast).toHaveBeenCalledWith('5', 'assignment:deleted', { assignmentId: 8, dayId: 3 }, 'sock');
expect(reconcile).toHaveBeenCalledWith('5', 'sock');
});
it('DELETE / on an already empty day succeeds without a journey reconcile', () => {
const reconcile = vi.fn(); const broadcast = vi.fn();
const s = svc({ clearDay: vi.fn().mockReturnValue([]), reconcile, broadcast } as Partial<AssignmentsService>);
expect(new DayAssignmentsController(s).clear(user, '5', '3')).toEqual({ success: true, removedIds: [] });
expect(broadcast).not.toHaveBeenCalled();
expect(reconcile).not.toHaveBeenCalled();
});
});
describe('AssignmentOpsController (parity with the per-assignment op routes)', () => {
it('PUT /:id/route takes a stop out of the route and back, scoped to the trip (#2532)', () => {
const setRouteExcluded = vi.fn().mockReturnValue({ id: 9, route_excluded: true })
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), setRouteExcluded } as Partial<AssignmentsService>)
const controller = new AssignmentOpsController(s)
expect(controller.route(user, '5', '9', { excluded: true }, 'sock')).toEqual({ assignment: { id: 9, route_excluded: true } })
expect(setRouteExcluded).toHaveBeenCalledWith('9', true)
expect(s.broadcast).toHaveBeenCalledWith('5', 'assignment:updated', { assignment: { id: 9, route_excluded: true } }, 'sock')
vi.mocked(s.getAssignmentForTrip).mockReturnValue(undefined)
expect(thrown(() => controller.route(user, '5', '9', { excluded: false }))).toEqual({ status: 404, body: { error: 'Assignment not found' } })
});
it('scopes explicit day ends to the trip and broadcasts the saved visit', () => {
const setEndDay = vi.fn().mockReturnValue({ id: 9, end_day: true });
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), setEndDay });
@@ -249,6 +249,26 @@ describe('deleteAssignment / reorderAssignments', () => {
});
});
describe('clearDay (#2470)', () => {
it('ASG-SVC-013b: removes every place of the day, leaves other days alone, returns the ids', () => {
const { trip, day, place } = fixture();
const otherDay = createDay(testDb, trip.id);
const a1 = createDayAssignment(testDb, day.id, place.id);
const a2 = createDayAssignment(testDb, day.id, place.id);
const kept = createDayAssignment(testDb, otherDay.id, place.id);
expect(svc.clearDay(day.id).sort()).toEqual([a1.id, a2.id].sort());
expect(testDb.prepare('SELECT COUNT(*) AS n FROM day_assignments WHERE day_id = ?').get(day.id)).toEqual({ n: 0 });
expect(testDb.prepare('SELECT id FROM day_assignments WHERE id = ?').get(kept.id)).toEqual({ id: kept.id });
expect(testDb.prepare('SELECT id FROM days WHERE id = ?').get(day.id)).toEqual({ id: day.id });
});
it('ASG-SVC-013c: an empty day clears to an empty list', () => {
const { day } = fixture();
expect(svc.clearDay(day.id)).toEqual([]);
});
});
// ── getAssignmentForTrip / moveAssignment ─────────────────────────────────────
describe('getAssignmentForTrip', () => {
@@ -667,5 +687,15 @@ describe('setLegTransportMode', () => {
});
});
describe('setRouteExcluded (#2532)', () => {
it('ASG-SVC-025b: keeps the stop but flags it out of the route, and back', () => {
const { day, place } = fixture();
const a = createDayAssignment(testDb, day.id, place.id);
expect(svc.getAssignmentWithPlace(a.id)!.route_excluded).toBe(false);
expect(svc.setRouteExcluded(a.id, true)!.route_excluded).toBe(true);
expect(svc.setRouteExcluded(a.id, false)!.route_excluded).toBe(false);
});
});
// ASG-SVC-026 pinned the deleted assignments.bridge and died with it — the
// same paths are covered on the service above and via places.mcp injection.
@@ -128,6 +128,12 @@ describe('AtlasController (parity with the legacy /api/addons/atlas route)', ()
expect(createBucketItem).toHaveBeenCalledWith(8, { name: 'Tokyo', lat: 35, lng: 139, country_code: undefined, notes: undefined, target_date: undefined });
});
it('forwards a wished-for region, as the MCP tool does (#1901)', () => {
const createBucketItem = vi.fn().mockReturnValue({ id: 2 });
makeController({ createBucketItem }).createBucketItem(user, { name: 'Bayern', country_code: 'DE', region_code: 'DE-BY' });
expect(createBucketItem).toHaveBeenCalledWith(8, expect.objectContaining({ country_code: 'DE', region_code: 'DE-BY' }));
});
it('404 on update of a missing item', () => {
const updateBucketItem = vi.fn().mockReturnValue(null);
return thrown(() => makeController({ updateBucketItem }).updateBucketItem(user, '9', { name: 'X' })).then((r) =>
+12 -1
View File
@@ -42,7 +42,7 @@ import { createUser, createTrip, createReservation } from '../../helpers/factori
import { getCountryFromCoords, getCountryFromAddress, isPointInCountryBox, reverseGeocodeCountry, getRegionGeo, getCountryGeo } from '../../../src/nest/atlas/atlas-geo';
import { cacheKeyFor, getCached, setCached } from '../../../src/nest/geo/nominatim.client';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { AtlasService, BucketItemExistsError } from '../../../src/nest/atlas/atlas.service';
import { AtlasService, BucketItemExistsError, bucketRegionCode } from '../../../src/nest/atlas/atlas.service';
// Direct construction over the shared test connection — no TestingModule (repo
// convention for DI-native service unit tests).
@@ -1340,6 +1340,17 @@ describe('atlas quirk fixes', () => {
expect(unmarked.marked_source).toBeNull();
});
it('ATLAS-SVC-031b: keeps a wished-for region of the same country, drops any other (#1901)', () => {
const { user } = createUser(testDb);
const bavaria = atlas.createBucketItem(user.id, { name: 'Bayern', country_code: 'DE', region_code: 'de-by' }) as { region_code: string | null };
expect(bavaria.region_code).toBe('DE-BY');
const foreign = atlas.createBucketItem(user.id, { name: 'Berlin', country_code: 'FR', region_code: 'DE-BE' }) as { region_code: string | null };
expect(foreign.region_code).toBeNull();
const noCountry = atlas.createBucketItem(user.id, { name: 'Somewhere', region_code: 'DE-HH' }) as { region_code: string | null };
expect(noCountry.region_code).toBeNull();
expect(bucketRegionCode('DE-BY; DROP', 'DE')).toBeNull();
});
it('ATLAS-SVC-032: updateBucketItem persists lat/lng of exactly 0 (equator/prime meridian)', () => {
const { user } = createUser(testDb);
const item = atlas.createBucketItem(user.id, { name: 'Null Island', lat: 10, lng: 10 }) as { id: number };
+132
View File
@@ -0,0 +1,132 @@
/**
* BOOT-RESTORE-001..007 — restoring a backup on a first start (#1089).
*
* Real files, a real archive and a real SQLite file: what is being protected is
* the order of things on disk, and a mocked filesystem would only confirm the
* calls this file makes. The plugin staging is the one seam stubbed, because
* it moves trees under the plugin roots of the machine running the tests.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import archiver from 'archiver';
import Database from 'better-sqlite3';
const { stageMock } = vi.hoisted(() => ({ stageMock: vi.fn() }));
vi.mock('../../../src/nest/plugins/plugin-backup', () => ({ stageExtractedPluginTrees: stageMock }));
import { BootRestoreError, restoreOnFirstBoot } from '../../../src/nest/backup/boot-restore';
let root: string;
let dataDir: string;
let dbFile: string;
beforeEach(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-boot-restore-'));
dataDir = path.join(root, 'data');
fs.mkdirSync(dataDir);
dbFile = path.join(dataDir, 'travel.db');
stageMock.mockClear();
vi.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
fs.rmSync(root, { recursive: true, force: true });
});
/** A database with the tables every TREK has, and one user to recognise it by. */
function trekDb(file: string, tables = ['users', 'trips', 'trip_members', 'places', 'days']) {
const db = new Database(file);
for (const t of tables) db.exec(`CREATE TABLE ${t} (id INTEGER PRIMARY KEY, name TEXT)`);
if (tables.includes('users')) db.prepare('INSERT INTO users (name) VALUES (?)').run('from-backup');
db.close();
}
async function archive(entries: Record<string, string | { file: string }>): Promise<string> {
const zipPath = path.join(root, 'backup.zip');
await new Promise<void>((resolve, reject) => {
const out = fs.createWriteStream(zipPath);
const zip = archiver('zip');
out.on('close', () => resolve());
zip.on('error', reject);
zip.pipe(out);
for (const [name, content] of Object.entries(entries)) {
if (typeof content === 'string') zip.append(content, { name });
else zip.file(content.file, { name });
}
void zip.finalize();
});
return zipPath;
}
async function goodArchive(extra: Record<string, string> = {}) {
const src = path.join(root, 'src.db');
trekDb(src);
return archive({ 'travel.db': { file: src }, '.encryption_key': 'a'.repeat(64), 'uploads/covers/c.jpg': 'jpeg', ...extra });
}
describe('restoreOnFirstBoot', () => {
it('BOOT-RESTORE-001: without the variable nothing happens', async () => {
expect(await restoreOnFirstBoot({ archive: null, dbFile, dataDir })).toEqual({ restored: false, reason: 'unset' });
expect(fs.existsSync(dbFile)).toBe(false);
});
it('BOOT-RESTORE-002: on a first start the backup\'s database, key and uploads are put in place', async () => {
const zipPath = await goodArchive();
const out = await restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir });
expect(out.restored).toBe(true);
const db = new Database(dbFile, { readonly: true });
expect(db.prepare('SELECT name FROM users').get()).toEqual({ name: 'from-backup' });
db.close();
expect(fs.readFileSync(path.join(dataDir, '.encryption_key'), 'utf8')).toBe('a'.repeat(64));
if (!out.restored) throw new Error('unreachable');
expect(fs.readFileSync(path.join(out.uploads!, 'covers', 'c.jpg'), 'utf8')).toBe('jpeg');
expect(stageMock).toHaveBeenCalledWith(out.staging);
expect(fs.existsSync(`${dbFile}.restore-tmp`)).toBe(false);
});
it('BOOT-RESTORE-003: with a database already there the backup is left alone, so a restart never rolls back', async () => {
const zipPath = await goodArchive();
trekDb(dbFile, ['users', 'trips', 'trip_members', 'places', 'days']);
const before = fs.readFileSync(dbFile);
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
expect(await restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir })).toEqual({ restored: false, reason: 'database-exists' });
expect(fs.readFileSync(dbFile).equals(before)).toBe(true);
expect(warn).toHaveBeenCalledWith(expect.stringContaining('only restored on a first start'));
});
it('BOOT-RESTORE-004: a missing archive stops the start instead of coming up empty', async () => {
await expect(restoreOnFirstBoot({ archive: path.join(root, 'nope.zip'), dbFile, dataDir }))
.rejects.toBeInstanceOf(BootRestoreError);
expect(fs.existsSync(dbFile)).toBe(false);
});
it('BOOT-RESTORE-005: an archive without a database is refused and leaves no scratch behind', async () => {
const zipPath = await archive({ 'notes.txt': 'hello' });
await expect(restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir }))
.rejects.toThrow('RESTORE_FROM_BACKUP: Invalid backup: travel.db not found');
expect(fs.existsSync(dbFile)).toBe(false);
expect(fs.readdirSync(dataDir).filter(n => n.startsWith('restore-boot-'))).toEqual([]);
});
it('BOOT-RESTORE-006: a database that is not a TREK one is refused', async () => {
const src = path.join(root, 'other.db');
trekDb(src, ['users']);
const zipPath = await archive({ 'travel.db': { file: src } });
await expect(restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir }))
.rejects.toThrow('missing required table: trips');
expect(fs.existsSync(dbFile)).toBe(false);
});
it('BOOT-RESTORE-007: an entry escaping the archive is refused', async () => {
const zipPath = await archive({ '../escape.txt': 'x', 'travel.db': 'not a db' });
await expect(restoreOnFirstBoot({ archive: zipPath, dbFile, dataDir }))
.rejects.toThrow(/escapes the archive root|not a valid SQLite database/);
expect(fs.existsSync(dbFile)).toBe(false);
});
});
@@ -119,6 +119,17 @@ describe('BudgetController (parity with the legacy /api/trips/:tripId/budget rou
expect(updateSettlement).toHaveBeenCalledWith('3', '5', expect.objectContaining({ currency: 'EUR', fallback_fx }));
});
it('POST and PUT /settlements forward the note, as the MCP tools do (#2340)', async () => {
const createSettlement = vi.fn().mockResolvedValue({ id: 3 });
const updateSettlement = vi.fn().mockResolvedValue({ id: 3 });
const svc = makeService({ createSettlement, updateSettlement } as Partial<BudgetService>);
const body = { from_user_id: 1, to_user_id: 2, amount: 10, note: 'Cash' };
await new BudgetController(svc).createSettlement(user, '5', body);
expect(createSettlement).toHaveBeenCalledWith('5', expect.objectContaining({ note: 'Cash' }), user.id);
await new BudgetController(svc).updateSettlement(user, '5', '3', body);
expect(updateSettlement).toHaveBeenCalledWith('3', '5', expect.objectContaining({ note: 'Cash' }));
});
it('DELETE /settlements/:id 404 when missing', () => {
const svc = makeService({ deleteSettlement: vi.fn().mockReturnValue(false) } as Partial<BudgetService>);
expect(thrown(() => new BudgetController(svc).deleteSettlement(user, '5', '7'))).toEqual({
@@ -571,6 +571,20 @@ describe('settlement parties are confined to the trip', () => {
expect(created).toMatchObject({ from_user_id: bob.id, to_user_id: alice.id });
});
it('BUDGET-SVC-DB-030b: keeps a trimmed note on a payment and clears a blank one (#2340)', async () => {
const { user: alice } = createUser(testDb, { username: 'alice' });
const { user: bob } = createUser(testDb, { username: 'bob' });
const trip = createTrip(testDb, alice.id);
addTripMember(testDb, trip.id, bob.id);
const created = await budget.createSettlement(trip.id, { from_user_id: bob.id, to_user_id: alice.id, amount: 10, note: ' Bank transfer ' }, alice.id);
expect(created).toMatchObject({ note: 'Bank transfer' });
const kept = await budget.updateSettlement(created!.id, trip.id, { from_user_id: bob.id, to_user_id: alice.id, amount: 11 });
expect(kept).toMatchObject({ amount: 11, note: 'Bank transfer' });
const cleared = await budget.updateSettlement(created!.id, trip.id, { from_user_id: bob.id, to_user_id: alice.id, amount: 11, note: '' });
expect(cleared).toMatchObject({ note: null });
});
it('BUDGET-SVC-DB-031: drops an off-trip payer from an item instead of refusing it', () => {
const { user: alice } = createUser(testDb, { username: 'alice' });
const { user: outsider } = createUser(testDb, { username: 'outsider' });
@@ -692,6 +692,16 @@ describe('updateEntry', () => {
* be the boolean on the way out as well as the integer on the way in, and
* the broadcast has to say the same thing the answer does.
*/
it('creates and toggles a draft, answering the flag as a boolean (#696)', () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const created = svc.createEntry(journey.id, user.id, { entry_date: '2026-03-01', title: 'Rough', is_draft: true });
expect(created!.is_draft).toBe(true);
const published = svc.updateEntry(created!.id, user.id, { is_draft: false });
expect(published!.is_draft).toBe(false);
expect((testDb.prepare('SELECT is_draft FROM journey_entries WHERE id = ?').get(created!.id) as { is_draft: number }).is_draft).toBe(0);
});
it('switches a stop off and back on, and answers with the flag as a boolean', () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
@@ -1667,6 +1677,45 @@ function insertEntry(journeyId: number, authorId: number, opts: { entry_date: st
return { id: Number(res.lastInsertRowid) };
}
describe('reorderEntryPhotos (#824)', () => {
function photoOn(journeyId: number, entryIds: number[], order = 0): number {
const trek = testDb.prepare("INSERT INTO trek_photos (provider, file_path, created_at) VALUES ('local', '/p.jpg', ?)").run(Date.now()).lastInsertRowid;
const gp = Number(testDb.prepare('INSERT INTO journey_photos (journey_id, photo_id, sort_order, created_at) VALUES (?, ?, 0, ?)').run(journeyId, trek, Date.now()).lastInsertRowid);
for (const entryId of entryIds) testDb.prepare('INSERT INTO journey_entry_photos (entry_id, journey_photo_id, sort_order, created_at) VALUES (?, ?, ?, ?)').run(entryId, gp, order, Date.now());
return gp;
}
const orderOf = (entryId: number) =>
(testDb.prepare('SELECT journey_photo_id FROM journey_entry_photos WHERE entry_id = ? ORDER BY sort_order').all(entryId) as { journey_photo_id: number }[]).map(r => r.journey_photo_id);
it('JOURNEY-SVC-089b: orders the photos of one entry and leaves the same photo elsewhere alone', () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const a = insertEntry(journey.id, user.id, { entry_date: '2026-08-01' });
const b = insertEntry(journey.id, user.id, { entry_date: '2026-08-02' });
const p1 = photoOn(journey.id, [a.id, b.id], 0);
const p2 = photoOn(journey.id, [a.id], 1);
const p3 = photoOn(journey.id, [a.id], 2);
expect(svc.reorderEntryPhotos(a.id, user.id, [p3, p1, p2])).toBe(true);
expect(orderOf(a.id)).toEqual([p3, p1, p2]);
expect(testDb.prepare('SELECT sort_order FROM journey_entry_photos WHERE entry_id = ? AND journey_photo_id = ?').get(b.id, p1)).toEqual({ sort_order: 0 });
});
it('JOURNEY-SVC-089c: refuses a list that is not exactly the photos of the entry, and a stranger', () => {
const { user } = createUser(testDb);
const { user: stranger } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const a = insertEntry(journey.id, user.id, { entry_date: '2026-08-01' });
const p1 = photoOn(journey.id, [a.id]);
const p2 = photoOn(journey.id, [a.id]);
const other = photoOn(journey.id, []);
expect(svc.reorderEntryPhotos(a.id, user.id, [p1])).toBe(false);
expect(svc.reorderEntryPhotos(a.id, user.id, [p1, other])).toBe(false);
expect(svc.reorderEntryPhotos(a.id, user.id, [p1, p1])).toBe(false);
expect(svc.reorderEntryPhotos(a.id, stranger.id, [p2, p1])).toBe(false);
expect(svc.reorderEntryPhotos(999999, user.id, [p2, p1])).toBe(false);
});
});
describe('reorderEntries', () => {
it('JOURNEY-SVC-089: reorder persists and listEntries returns requested order regardless of entry_time', () => {
const { user } = createUser(testDb);
@@ -496,6 +496,29 @@ describe('getPublicJourney', () => {
expect(result!.entries[0].title).toBe('Visible Entry');
});
it('JOURNEY-SHARE-018b: leaves drafts and the photos only they hold off the public page (#696)', () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const published = createJourneyEntry(testDb, journey.id, user.id, { title: 'Published', entry_date: '2026-01-10' });
const draft = createJourneyEntry(testDb, journey.id, user.id, { title: 'Draft', entry_date: '2026-01-11' });
testDb.prepare('UPDATE journey_entries SET is_draft = 1 WHERE id = ?').run(draft.id);
const shownPhoto = insertJourneyPhoto(published.id, { ownerId: user.id });
const draftPhoto = insertJourneyPhoto(draft.id, { ownerId: user.id });
// A photo on both a draft and a published entry is published.
const sharedPhoto = insertJourneyPhoto(draft.id, { ownerId: user.id, filePath: '/photos/both.jpg' });
const sharedRow = testDb.prepare('SELECT id FROM journey_photos WHERE photo_id = ?').get(sharedPhoto) as { id: number };
testDb.prepare('INSERT INTO journey_entry_photos (entry_id, journey_photo_id, sort_order, created_at) VALUES (?, ?, 1, 0)').run(published.id, sharedRow.id);
const { token } = svc.createOrUpdateJourneyShareLink(journey.id, user.id, { share_timeline: true, share_gallery: true });
const result = svc.getPublicJourney(token)!;
expect(result.entries.map((e: any) => e.title)).toEqual(['Published']);
expect(result.stats.entries).toBe(1);
const galleryIds = (result as any).gallery.map((p: any) => p.photo_id).sort();
expect(galleryIds).toEqual([shownPhoto, sharedPhoto].sort());
expect(svc.validateShareTokenForPhoto(token, draftPhoto)).toBeNull();
expect(svc.validateShareTokenForPhoto(token, shownPhoto)).not.toBeNull();
});
it('JOURNEY-SHARE-019: enriches entries with parsed tags and photos', () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
@@ -397,6 +397,14 @@ describe('JourneyController', () => {
expect(ctl(svc({ reorderEntries: vi.fn().mockReturnValue(true) } as Partial<JourneyService>)).reorderEntries(user, '9', { orderedIds: [3, 1, 2] })).toEqual({ success: true });
});
it('reorderEntryPhotos forwards the order and answers 404 when refused (#824)', () => {
const reorderEntryPhotos = vi.fn().mockReturnValue(true);
expect(ctl(svc({ reorderEntryPhotos } as Partial<JourneyService>)).reorderEntryPhotos(user, '12', { orderedIds: [3, 1] }, 'sock')).toEqual({ success: true });
expect(reorderEntryPhotos).toHaveBeenCalledWith(12, user.id, [3, 1], 'sock');
expect(thrown(() => ctl(svc({ reorderEntryPhotos: vi.fn().mockReturnValue(false) } as Partial<JourneyService>)).reorderEntryPhotos(user, '12', { orderedIds: [3] })))
.toEqual({ status: 404, body: { error: 'Entry not found' } });
});
it('restoreSuggestions answers with the count, and refuses a viewer the same way its siblings do', () => {
const restore = vi.fn().mockReturnValue({ restored: 3 });
expect(ctl(svc({ restoreDismissedSuggestions: restore } as Partial<JourneyService>)).restoreSuggestions(user, '9')).toEqual({ restored: 3 });
+117
View File
@@ -63,6 +63,7 @@ vi.mock('../../../src/nest/maps/trek-places.client', async (importOriginal) => (
import { db } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { MapsService } from '../../../src/nest/maps/maps.service';
import { trekPlacesSearch } from '../../../src/nest/maps/trek-places.client';
import {
AmapPlacesProvider,
AmapTipStash,
@@ -894,6 +895,122 @@ describe('MapsService with Amap in the keyed slot', () => {
});
});
describe('MapsService: Amap first inside China when picked outright (#1636)', () => {
const BEIJING = { lat: 39.9087, lng: 116.3975, radius: 20000 };
const PARIS = { lat: 48.8584, lng: 2.2945, radius: 20000 };
const BEIJING_BOX = { low: { lat: 39.8, lng: 116.3 }, high: { lat: 40.0, lng: 116.5 } };
let spies: { mockRestore: () => void }[] = [];
afterEach(() => {
spies.forEach(s => s.mockRestore());
spies = [];
vi.mocked(trekPlacesSearch).mockClear();
});
function osmAnswers(places: Record<string, unknown>[]) {
const spy = vi.spyOn(svc, 'searchNominatim').mockResolvedValue(places as never);
spies.push(spy);
return spy;
}
const amapPoi = () => ok({ pois: [{ id: 'B1', name: '天安门', location: TIANANMEN_LOCATION }] });
it('AMAP-095: picked outright and searching around Beijing, Amap answers before the index and OpenStreetMap', async () => {
mockProviderGet.mockReturnValue({ value: 'amap' });
keys({ amap: 'akey' });
const osm = osmAnswers([{ name: 'Tiananmen (OSM)' }]);
const fetchSpy = vi.fn().mockResolvedValue(amapPoi());
vi.stubGlobal('fetch', fetchSpy);
const result = await svc.searchPlaces(1, '天安门', 'zh', BEIJING);
expect(result.source).toBe('amap');
expect(result.places[0].amap_poi_id).toBe('amap:B1');
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(osm).not.toHaveBeenCalled();
expect(trekPlacesSearch).not.toHaveBeenCalled();
});
it('AMAP-096: an empty Amap answer hands the search on, and Amap is not asked a second time', async () => {
mockProviderGet.mockReturnValue({ value: 'amap' });
keys({ amap: 'akey' });
osmAnswers([]);
const fetchSpy = vi.fn().mockResolvedValue(ok({ count: '0', pois: [] }));
vi.stubGlobal('fetch', fetchSpy);
const result = await svc.searchPlaces(1, 'nothing here', 'zh', BEIJING);
expect(result).toEqual({ places: [], source: 'amap' });
expect(trekPlacesSearch).toHaveBeenCalled();
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it('AMAP-097: a search centred outside China keeps the usual order, so the Eiffel Tower does not land in Macau', async () => {
mockProviderGet.mockReturnValue({ value: 'amap' });
keys({ amap: 'akey' });
osmAnswers([{ name: 'Tour Eiffel' }]);
const fetchSpy = vi.fn().mockResolvedValue(amapPoi());
vi.stubGlobal('fetch', fetchSpy);
const result = await svc.searchPlaces(1, 'Eiffel Tower', 'en', PARIS);
expect(result.places[0].name).toBe('Tour Eiffel');
expect(fetchSpy).not.toHaveBeenCalled();
});
it('AMAP-098: Amap holding the slot only by default (auto) changes nothing', async () => {
mockProviderGet.mockReturnValue(undefined);
keys({ amap: 'akey' });
osmAnswers([{ name: 'Tiananmen (OSM)' }]);
const fetchSpy = vi.fn().mockResolvedValue(amapPoi());
vi.stubGlobal('fetch', fetchSpy);
const result = await svc.searchPlaces(1, '天安门', 'zh', BEIJING);
expect(result.places[0].name).toBe('Tiananmen (OSM)');
expect(fetchSpy).not.toHaveBeenCalled();
});
it('AMAP-099: a failing Amap drops through to the index and OpenStreetMap with a warning', async () => {
mockProviderGet.mockReturnValue({ value: 'amap' });
keys({ amap: 'akey' });
osmAnswers([{ name: 'Tiananmen (OSM)' }]);
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
spies.push(warn);
spies.push(vi.spyOn(console, 'error').mockImplementation(() => {}));
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(amapError('10003')));
const result = await svc.searchPlaces(1, '天安门', 'zh', BEIJING);
expect(result.places[0].name).toBe('Tiananmen (OSM)');
expect(warn).toHaveBeenCalledWith('Amap search failed, falling back:', expect.any(String));
});
it('AMAP-100: suggestions around Beijing come from Amap first, once', async () => {
mockProviderGet.mockReturnValue({ value: 'amap' });
keys({ amap: 'akey' });
const fetchSpy = vi.fn().mockResolvedValue(ok({ tips: [{ id: 'T1', name: '天安门', district: '北京市东城区' }] }));
vi.stubGlobal('fetch', fetchSpy);
const result = await svc.autocompletePlaces(1, '天安', 'zh', BEIJING_BOX);
expect(result.source).toBe('amap');
expect(result.suggestions[0].placeId).toBe('amap:T1');
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(trekPlacesSearch).not.toHaveBeenCalled();
});
it('AMAP-101: empty Amap suggestions hand on to the index and are not asked for again', async () => {
mockProviderGet.mockReturnValue({ value: 'amap' });
keys({ amap: 'akey' });
const fetchSpy = vi.fn().mockResolvedValue(ok({ tips: [] }));
vi.stubGlobal('fetch', fetchSpy);
const result = await svc.autocompletePlaces(1, 'zzz', 'zh', BEIJING_BOX);
expect(result).toEqual({ suggestions: [], source: 'amap' });
expect(trekPlacesSearch).toHaveBeenCalled();
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
});
describe('AmapTipStash', () => {
it('AMAP-090: a tip is forgotten once its ten minutes are up', () => {
const tips = new AmapTipStash();
@@ -80,6 +80,26 @@ describe('MapsController (parity with the legacy /api/maps route)', () => {
});
});
describe('POST /nearby', () => {
it('forwards the point, the circle, the count and the language', async () => {
const nearbyPlaces = vi.fn().mockResolvedValue({ places: [], source: 'trek-places' });
const res = await makeController({ nearbyPlaces }).nearby(user, { lat: 52.5, lng: 13.4, radius: 300, limit: 5 }, 'de');
expect(res).toEqual({ places: [], source: 'trek-places' });
expect(nearbyPlaces).toHaveBeenCalledWith(3, 52.5, 13.4, { radius: 300, limit: 5, lang: 'de' });
});
it('maps a provider error to its status, and anything else to 500', async () => {
const refused = vi.fn().mockRejectedValue(withError(429, 'Quota exceeded'));
expect(await thrown(() => makeController({ nearbyPlaces: refused }).nearby(user, { lat: 1, lng: 2 }))).toEqual({
status: 429, body: { error: 'Quota exceeded' },
});
const broken = vi.fn().mockRejectedValue('boom');
expect(await thrown(() => makeController({ nearbyPlaces: broken }).nearby(user, { lat: 1, lng: 2 }))).toEqual({
status: 500, body: { error: 'Nearby search error' },
});
});
});
describe('GET /area', () => {
const BOX = { minLat: 54, minLng: 12, maxLat: 54.2, maxLng: 12.3 };
+227
View File
@@ -0,0 +1,227 @@
/**
* MAPS-NEARBY-001..010 — places around a point, nearest first (#976).
*
* Three sources in the search's own order: the index, Google with a key, then
* OpenStreetMap. What matters is that each answer reads the same (the search
* record plus `distance_m`, nearest first), and that Google, which bills per
* call, is asked only when the index had nothing and never twice for one spot.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
const { mockNearby } = vi.hoisted(() => ({
mockNearby: vi.fn(async (_lat: number, _lng: number, _opts?: { radius?: number; limit?: number }): Promise<unknown[]> => []),
}));
vi.mock('../../../src/nest/maps/trek-places.client', async (importOriginal) => ({
...(await importOriginal<typeof import('../../../src/nest/maps/trek-places.client')>()),
trekPlacesNearby: mockNearby,
}));
vi.mock('../../../src/config', () => ({ JWT_SECRET: 'test-secret', ENCRYPTION_KEY: '0'.repeat(64) }));
import { MapsService } from '../../../src/nest/maps/maps.service';
import {
nearbyCacheKey,
nearbyOverpassQuery,
nearestFirst,
overpassNearbyRecords,
} from '../../../src/nest/maps/maps-nearby.helpers';
import type { DatabaseService } from '../../../src/nest/database/database.service';
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
const indexRow = (gers: string, name: string, lat: number, lng: number) => ({
gers,
name,
lat,
lng,
category: 'cafe',
categoryPath: null,
confidence: 0.9,
address: { freeform: 'Pariser Platz 1', locality: 'Berlin', postcode: '10117', region: null, country: 'DE' },
contact: null,
brand: null,
source: 'overture',
hours: null,
distanceMetres: 0,
});
const ORIGINAL_TREK_PLACES = process.env.TREK_PLACES_ENABLED;
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
// Every case asks about its own spot: the answers are cached per process, and a
// shared point would make the second case read the first one's answer.
let spot = 0;
function nextPoint() {
spot += 1;
return { lat: 10 + spot / 10, lng: 20 + spot / 10 };
}
function make(opts: { index?: boolean; google?: boolean } = {}) {
if (opts.index === false) process.env.TREK_PLACES_ENABLED = 'false';
else delete process.env.TREK_PLACES_ENABLED;
const database = { get: vi.fn(() => undefined) } as unknown as DatabaseService;
const svc = new MapsService(database, {} as PlacePhotoCacheService);
vi.spyOn(svc, 'keyedProvider').mockReturnValue(
opts.google ? ({ id: 'google', key: 'test-key', source: 'user-row' } as ReturnType<MapsService['keyedProvider']>) : null,
);
return svc;
}
const jsonResponse = (body: unknown, ok = true, status = 200) => ({ ok, status, json: async () => body });
beforeEach(() => {
mockNearby.mockReset();
mockNearby.mockResolvedValue([]);
warn.mockClear();
error.mockClear();
});
afterEach(() => {
vi.unstubAllGlobals();
if (ORIGINAL_TREK_PLACES === undefined) delete process.env.TREK_PLACES_ENABLED;
else process.env.TREK_PLACES_ENABLED = ORIGINAL_TREK_PLACES;
});
describe('MapsService.nearbyPlaces', () => {
it('MAPS-NEARBY-001: the index answers first, nearest first, with the distance on each record', async () => {
const p = nextPoint();
mockNearby.mockResolvedValue([
indexRow('far', 'Far Cafe', p.lat + 0.003, p.lng),
indexRow('near', 'Near Cafe', p.lat + 0.0005, p.lng),
]);
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
const out = await make({ google: true }).nearbyPlaces(1, p.lat, p.lng, { radius: 800, limit: 5 });
expect(mockNearby).toHaveBeenCalledWith(p.lat, p.lng, { radius: 800, limit: 5 });
expect(out.source).toBe('trek-places');
expect(out.places.map(r => r.name)).toEqual(['Near Cafe', 'Far Cafe']);
expect(out.places[0].distance_m).toBe(56);
expect(out.places[0].osm_id).toBe('gers:near');
// A key is there, and still not spent.
expect(fetchMock).not.toHaveBeenCalled();
});
it('MAPS-NEARBY-002: with nothing in the index, Google is asked by distance inside the circle', async () => {
const p = nextPoint();
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
places: [
{ id: 'g-far', displayName: { text: 'Far' }, location: { latitude: p.lat + 0.002, longitude: p.lng } },
{ id: 'g-shut', displayName: { text: 'Shut' }, location: { latitude: p.lat, longitude: p.lng }, businessStatus: 'CLOSED_PERMANENTLY' },
{ id: 'g-near', displayName: { text: 'Near' }, formattedAddress: 'Somewhere 1', location: { latitude: p.lat + 0.0001, longitude: p.lng } },
],
}));
vi.stubGlobal('fetch', fetchMock);
const out = await make({ google: true }).nearbyPlaces(1, p.lat, p.lng, { lang: 'de' });
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
expect(url).toContain('places:searchNearby');
expect(JSON.parse(init.body as string)).toEqual({
maxResultCount: 20,
rankPreference: 'DISTANCE',
languageCode: 'de',
locationRestriction: { circle: { center: { latitude: p.lat, longitude: p.lng }, radius: 500 } },
});
expect(out.source).toBe('google');
expect(out.places.map(r => r.google_place_id)).toEqual(['g-near', 'g-far']);
expect(out.places[0]).toMatchObject({ name: 'Near', address: 'Somewhere 1', distance_m: 11, source: 'google' });
});
it('MAPS-NEARBY-003: a Google refusal is an error with its status, not an empty list', async () => {
const p = nextPoint();
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ error: { message: 'Quota exceeded' } }, false, 429)));
await expect(make({ google: true }).nearbyPlaces(1, p.lat, p.lng)).rejects.toMatchObject({
message: 'Quota exceeded',
status: 429,
});
expect(error).toHaveBeenCalledWith(expect.stringContaining('searchNearby failed with 429 userId=1 keySource=user-row'));
});
it('MAPS-NEARBY-004: without a key OpenStreetMap answers, in the asked language, nearest first', async () => {
const p = nextPoint();
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
elements: [
{ type: 'node', id: 2, lat: p.lat + 0.001, lon: p.lng, tags: { name: 'Museum', 'name:de': 'Das Museum', tourism: 'museum' } },
{ type: 'way', id: 3, center: { lat: p.lat + 0.0002, lon: p.lng }, tags: { name: 'Bakery', shop: 'bakery' } },
],
}));
vi.stubGlobal('fetch', fetchMock);
const out = await make().nearbyPlaces(1, p.lat, p.lng, { radius: 300, lang: 'de' });
const body = decodeURIComponent(String((fetchMock.mock.calls[0][1] as RequestInit).body));
expect(body).toContain(`around:300,${p.lat},${p.lng}`);
expect(out.source).toBe('openstreetmap');
expect(out.places.map(r => r.name)).toEqual(['Bakery', 'Das Museum']);
expect(out.places[0]).toMatchObject({ osm_id: 'way:3', category: 'shop_bakery', distance_m: 22 });
});
it('MAPS-NEARBY-005: a failing index drops through to the next source and says so', async () => {
const p = nextPoint();
mockNearby.mockRejectedValue(new Error('index down'));
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ elements: [] })));
const out = await make().nearbyPlaces(1, p.lat, p.lng);
expect(out).toEqual({ places: [], source: 'openstreetmap' });
expect(warn).toHaveBeenCalledWith('TREK Places nearby failed, falling back:', 'index down');
});
it('MAPS-NEARBY-006: with the index switched off it is never asked', async () => {
const p = nextPoint();
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ elements: [] })));
await make({ index: false }).nearbyPlaces(1, p.lat, p.lng);
expect(mockNearby).not.toHaveBeenCalled();
});
it('MAPS-NEARBY-007: the same spot asked twice costs one lookup', async () => {
const p = nextPoint();
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ places: [] }));
vi.stubGlobal('fetch', fetchMock);
const svc = make({ google: true });
await svc.nearbyPlaces(1, p.lat, p.lng);
await svc.nearbyPlaces(1, p.lat + 0.00001, p.lng);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(mockNearby).toHaveBeenCalledTimes(1);
});
});
describe('nearby helpers', () => {
it('MAPS-NEARBY-008: the Overpass question is a named place of a visitable kind inside the circle, capped', () => {
const q = nearbyOverpassQuery(52.5, 13.4, 250.4, 10);
expect(q).toContain('nwr(around:250,52.5,13.4)[name][~"^(amenity|tourism|shop|leisure|historic)$"~"."];');
expect(q).toContain('out center tags 30;');
});
it('MAPS-NEARBY-009: unnamed, placeless and shut places are left out; the kind reads like a search category', () => {
const origin = { lat: 0, lng: 0 };
const out = overpassNearbyRecords([
{ type: 'node', id: 1, lat: 0, lon: 0, tags: { amenity: 'bench' } },
{ type: 'node', id: 2, tags: { name: 'Nowhere', amenity: 'cafe' } },
{ type: 'node', id: 3, lat: 0, lon: 0, tags: { name: 'Gone', amenity: 'bar', disused: 'yes' } },
{ type: 'node', id: 4, lat: 0, lon: 0.001, tags: { int_name: 'Park', leisure: 'park', website: 'park.example', 'addr:street': 'Main', 'addr:housenumber': '5' } },
{ type: 'node', id: 5, lat: 0, lon: 0, tags: { name: 'Shopish', shop: 'yes', tourism: 'artwork' } },
], origin, 'en', 10);
expect(out.map(r => r.name)).toEqual(['Shopish', 'Park']);
expect(out[0].category).toBe('artwork');
expect(out[1]).toMatchObject({ address: 'Main 5', website: 'https://park.example', category: 'park', distance_m: 111 });
});
it('MAPS-NEARBY-010: records without a position are dropped and the list keeps to the limit; the cache key rounds to ten metres', () => {
const out = nearestFirst(
[{ name: 'a', lat: 0, lng: 0.002 }, { name: 'b', lat: null, lng: null }, { name: 'c', lat: 0, lng: 0.001 }],
{ lat: 0, lng: 0 },
1,
);
expect(out).toEqual([{ name: 'c', lat: 0, lng: 0.001, distance_m: 111 }]);
expect(nearbyCacheKey(52.516312, 13.377712, 500, 20, 'de')).toBe(nearbyCacheKey(52.51634, 13.37768, 500, 20, 'de'));
expect(nearbyCacheKey(52.5163, 13.3777, 500, 20, 'de')).not.toBe(nearbyCacheKey(52.5163, 13.3777, 500, 20, 'en'));
});
});
@@ -883,6 +883,23 @@ describe('searchNominatim (fetch stubbed)', () => {
expect((results[1] as any).lat).toBeNull();
expect((results[1] as any).lng).toBeNull();
});
it('MAPS-108b: carries what the place is, with a shop named as one (#2282)', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: async () => [
{ osm_type: 'node', osm_id: '5', lat: '52.5', lon: '13.4', name: 'Adlon', class: 'tourism', type: 'hotel' },
{ osm_type: 'node', osm_id: '6', lat: '52.5', lon: '13.4', name: 'Bäcker', class: 'shop', type: 'bakery' },
{ osm_type: 'node', osm_id: '7', lat: '52.5', lon: '13.4', name: 'Hut', class: 'building', type: 'yes' },
{ osm_type: 'node', osm_id: '8', lat: '52.5', lon: '13.4', name: 'Bare' },
],
}),
);
const results = await svc.searchNominatim('x');
expect(results.map((r: any) => r.category)).toEqual(['hotel', 'shop_bakery', null, null]);
});
});
// ── fetchOverpassDetails (fetch stubbed) ─────────────────────────────────────
@@ -209,6 +209,13 @@ describe('PackingController (parity with the legacy /api/trips/:tripId/packing r
expect(broadcast).toHaveBeenCalledWith('5', 'packing:updated', { item: { id: 9, name: 'X' } }, 'sock');
});
it('forwards a packed count to the service, as the MCP tool does (#2296)', () => {
const updateItem = vi.fn().mockReturnValue({ id: 9, packed_quantity: 3 });
const svc = makeService({ updateItem, broadcast: vi.fn() } as Partial<PackingService>);
new PackingController(svc).update(user, '5', '9', { packed_quantity: 3 });
expect(updateItem).toHaveBeenCalledWith('5', '9', expect.objectContaining({ packed_quantity: 3 }), ['packed_quantity'], undefined, user.id);
});
it('keeps a private update scoped to the owner (#858)', () => {
const updateItem = vi.fn().mockReturnValue({ id: 9, name: 'X', is_private: 1, owner_id: 1 });
const broadcast = vi.fn();
@@ -683,6 +683,23 @@ describe('legacy-quirk fixes', () => {
// Omitted key leaves the quantity unchanged.
expect((svc.updateItem(trip.id, item.id, { name: 'Wool socks' }, ['name'], undefined, user.id) as any).quantity).toBe(999);
});
it('PACK-SVC-053b: a packed count keeps the box in step (#2296)', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = svc.createItem(trip.id, { name: 'Shirts', quantity: 10 }, user.id) as any;
const put = (data: Record<string, unknown>) => svc.updateItem(trip.id, item.id, data, Object.keys(data), undefined, user.id) as any;
expect(put({ packed_quantity: 7 })).toMatchObject({ packed_quantity: 7, checked: 0 });
// Reaching the quantity ticks the item off and drops the partial count.
expect(put({ packed_quantity: 12 })).toMatchObject({ packed_quantity: null, checked: 1 });
// Unticking starts over from nothing.
expect(put({ checked: 0 })).toMatchObject({ packed_quantity: null, checked: 0 });
expect(put({ packed_quantity: 6 })).toMatchObject({ packed_quantity: 6, checked: 0 });
// A quantity lowered to the count finishes the item; a rename leaves it alone.
expect(put({ name: 'Tees' })).toMatchObject({ packed_quantity: 6, checked: 0 });
expect(put({ quantity: 5 })).toMatchObject({ quantity: 5, packed_quantity: null, checked: 1 });
});
});
// ── Create-path fields + bag trip scope (#2154) ───────────────────────────────
@@ -97,6 +97,24 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
});
});
describe('file import enrichment (#2536)', () => {
const file = { buffer: Buffer.from('gpx'), originalname: 'r.gpx' } as Express.Multer.File;
it('hands the imported places to the Google pass only when asked', async () => {
const enrichImportedFilePlaces = vi.fn();
const s = svc({
importGpx: vi.fn().mockReturnValue({ places: [{ id: 1 }], count: 1, skipped: 0 }),
importMapFile: vi.fn().mockResolvedValue({ places: [{ id: 2 }], count: 1, summary: { totalPlacemarks: 1 } }),
broadcast: vi.fn(), enrichImportedFilePlaces,
} as Partial<PlacesService>);
const c = new PlacesController(s, new RuntimeEnvService(), storageStub);
c.importGpx(user, '5', file, {});
expect(enrichImportedFilePlaces).not.toHaveBeenCalled();
c.importGpx(user, '5', file, { enrich: 'true' });
await c.importMap(user, '5', file, { enrich: 'true' });
expect(enrichImportedFilePlaces.mock.calls).toEqual([['5', user.id, [{ id: 1 }]], ['5', user.id, [{ id: 2 }]]]);
});
});
describe('POST /import/map', () => {
const file = { buffer: Buffer.from('<kml/>'), originalname: 'm.kml' } as Express.Multer.File;
it('400 without a file', async () => {
@@ -547,3 +565,23 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
});
});
});
describe('PUT /:id/image/from-file (#1242)', () => {
it('maps every refusal to its status and broadcasts a success', async () => {
const make = (result: unknown, extra: Partial<PlacesService> = {}) =>
new PlacesController(svc({ setImageFromFile: vi.fn().mockResolvedValue(result), ...extra } as Partial<PlacesService>), new RuntimeEnvService(), storageStub);
expect(await thrownAsync(() => make('not_found').imageFromFile(user, '5', '9', { file_id: 3 }))).toEqual({ status: 404, body: { error: 'File not found' } });
expect(await thrownAsync(() => make('not_image').imageFromFile(user, '5', '9', { file_id: 3 }))).toEqual({ status: 400, body: { error: 'Only jpg, png, gif, webp images allowed' } });
expect(await thrownAsync(() => make('too_large').imageFromFile(user, '5', '9', { file_id: 3 }))).toEqual({ status: 400, body: { error: 'Image too large' } });
expect(await thrownAsync(() => make(null).imageFromFile(user, '5', '9', { file_id: 3 }))).toEqual({ status: 404, body: { error: 'Place not found' } });
const broadcast = vi.fn(); const onUpdated = vi.fn()
expect(await make({ id: 9 }, { broadcast, onUpdated } as Partial<PlacesService>).imageFromFile(user, '5', '9', { file_id: 3 }, 'sock')).toEqual({ place: { id: 9 } })
expect(broadcast).toHaveBeenCalledWith('5', 'place:updated', { place: { id: 9 } }, 'sock')
expect(onUpdated).toHaveBeenCalledWith(9)
});
it('403 without place_edit', async () => {
expect(await thrownAsync(() => new PlacesController(svc({ canEdit: vi.fn().mockReturnValue(false) }), new RuntimeEnvService(), storageStub).imageFromFile(user, '5', '9', { file_id: 3 }))).toEqual({ status: 403, body: { error: 'No permission' } });
});
});
@@ -165,6 +165,20 @@ describe('list', () => {
expect(places[0].name).toBe('Eiffel Tower');
});
it('PLACE-SVC-005b — carries where each place lies: the cached region, else the bundled borders (#2537)', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const berlin = createPlace(testDb, trip.id, { name: 'Brandenburger Tor', lat: 52.5163, lng: 13.3777 });
const paris = createPlace(testDb, trip.id, { name: 'Louvre', lat: 48.8606, lng: 2.3376 });
const nowhere = createPlace(testDb, trip.id, { name: 'Unplaced' });
testDb.prepare('UPDATE places SET lat = NULL, lng = NULL, address = NULL WHERE id = ?').run(nowhere.id);
testDb.prepare('INSERT INTO place_regions (place_id, country_code, region_code, region_name) VALUES (?, ?, ?, ?)').run(berlin.id, 'DE', 'DE-BE', 'Berlin');
const byId = new Map((svc.list(String(trip.id), {}) as any[]).map(p => [p.id, p]));
expect(byId.get(berlin.id)).toMatchObject({ country_code: 'DE', region_name: 'Berlin' });
expect(byId.get(paris.id)).toMatchObject({ country_code: 'FR', region_name: null });
expect(byId.get(nowhere.id)!.country_code).toBeNull();
});
it('PLACE-SVC-005 — attaches tags array to each place (empty when none)', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -1284,6 +1298,13 @@ describe('enrichImportedPlaces', () => {
expect(searchPlaces).not.toHaveBeenCalled();
});
it('PLACE-SVC-058b — a file import enriches its points, never its paths (#2536)', async () => {
const service = enrichSvc({ getMapsKey: vi.fn(() => null) });
const spy = vi.spyOn(service, 'enrichImportedPlaces').mockResolvedValue();
service.enrichImportedFilePlaces('1', 1, [{ id: 1 }, { id: 2, route_geometry: '[[1,2],[3,4]]' }]);
expect(spy).toHaveBeenCalledWith('1', 1, [{ id: 1 }]);
});
it('PLACE-SVC-059 — no-ops for an empty batch without touching the provider', async () => {
const getMapsKey = vi.fn(() => 'key');
await enrichSvc({ getMapsKey }).enrichImportedPlaces('1', 1, []);
@@ -1439,6 +1460,36 @@ describe('zero-valued numeric fields', () => {
// ── LIKE metacharacter escaping (#1745) ───────────────────────────────────────
describe('setImageFromFile (#1242)', () => {
const attach = (tripId: number, name: string, mime: string, size = 3) => {
fs.writeFileSync(path.join(placesStorageFx.root, name), 'img');
return Number(testDb.prepare('INSERT INTO trip_files (trip_id, filename, original_name, file_size, mime_type) VALUES (?, ?, ?, ?, ?)').run(tripId, name, name, size, mime).lastInsertRowid);
};
it('PLACE-SVC-IMGFILE-001 — copies an attached picture in as the place image', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id);
const fileId = attach(trip.id, 'holiday.jpg', 'image/jpeg');
const updated = await svc.setImageFromFile(String(trip.id), String(place.id), fileId) as any;
expect(updated.image_url).toMatch(/^\/uploads\/places\/[0-9a-f-]+\.jpg$/);
expect(fs.existsSync(path.join(placesStorageFx.root, path.basename(updated.image_url)))).toBe(true);
// A copy: the attachment itself is still there.
expect(fs.existsSync(path.join(placesStorageFx.root, 'holiday.jpg'))).toBe(true);
});
it('PLACE-SVC-IMGFILE-002 — refuses a file of another trip, a document and an oversized image', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id);
expect(await svc.setImageFromFile(String(trip.id), String(place.id), attach(other.id, 'x.jpg', 'image/jpeg'))).toBe('not_found');
expect(await svc.setImageFromFile(String(trip.id), String(place.id), attach(trip.id, 'ticket.pdf', 'application/pdf'))).toBe('not_image');
expect(await svc.setImageFromFile(String(trip.id), String(place.id), attach(trip.id, 'vector.svg', 'image/svg+xml'))).toBe('not_image');
expect(await svc.setImageFromFile(String(trip.id), String(place.id), attach(trip.id, 'huge.jpg', 'image/jpeg', 50 * 1024 * 1024))).toBe('too_large');
});
});
describe('list search escaping', () => {
it('PLACE-SVC-068 — a % or _ in the search term matches literally, not as a wildcard', () => {
const { user } = createUser(testDb);
@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest';
import { isTravelBooking, travelOnly, withoutImages } from '../../../src/nest/share/share-view.helpers';
const snapshot = () => ({
assignments: {
1: [{ accommodation_id: null, place: { id: 10, image_url: 'a.jpg' } }, { accommodation_id: 5, place: { id: 20, image_url: 'b.jpg' } }],
2: [{ accommodation_id: null, place: { id: 10, image_url: 'a.jpg' } }],
},
dayNotes: { 1: [{ id: 1 }] },
places: [{ id: 10, image_url: 'a.jpg' }, { id: 20, image_url: 'b.jpg' }],
reservations: [{ type: 'flight' }, { type: 'restaurant' }, { type: 'hotel' }, { type: 'cable_car' }, { type: null }],
});
describe('share view helpers (#1712)', () => {
it('SHARE-VIEW-001: knows travel bookings from the rest', () => {
expect(['flight', 'train', 'ferry', 'cable_car', 'hotel'].every(isTravelBooking)).toBe(true);
expect(['restaurant', 'tour', 'event', 'parking', undefined, 3].some(isTravelBooking)).toBe(false);
});
it('SHARE-VIEW-002: travel only keeps the stay stops, the stay places and the travel bookings', () => {
const out = travelOnly(snapshot(), new Set([20]));
expect(out.assignments).toEqual({ 1: [{ accommodation_id: 5, place: { id: 20, image_url: 'b.jpg' } }] });
expect(out.dayNotes).toEqual({});
expect(out.places.map(p => p.id)).toEqual([20]);
expect(out.reservations.map(r => r.type)).toEqual(['flight', 'hotel', 'cable_car']);
});
it('SHARE-VIEW-003: without images clears every photo and leaves the rest alone', () => {
const data = snapshot();
const out = withoutImages(data);
expect(out.places.every(p => p.image_url === null)).toBe(true);
expect(Object.values(out.assignments).flat().every(a => a.place?.image_url === null)).toBe(true);
expect(out.reservations).toBe(data.reservations);
expect(data.places[0].image_url).toBe('a.jpg');
});
});
@@ -56,6 +56,12 @@ describe('TripShareController', () => {
expect(updatedRes.statusCode).toBe(200);
});
it('POST forwards the two narrowing options, as the MCP tool does (#1712)', () => {
const createOrUpdate = vi.fn().mockReturnValue({ token: 't', created: true });
new TripShareController(svc({ createOrUpdate } as Partial<ShareService>)).create(user, '5', { share_travel_only: true, share_hide_images: true }, res());
expect(createOrUpdate).toHaveBeenCalledWith('5', user.id, expect.objectContaining({ share_travel_only: true, share_hide_images: true }));
});
it('GET 404 without access, 403 without share_manage, returns info or a null token', () => {
expect(thrown(() => new TripShareController(svc({ verifyTripAccess: vi.fn().mockReturnValue(undefined) })).get(user, '5'))).toEqual({ status: 404, body: { error: 'Trip not found' } });
// Reading returns the token itself, so it needs the same permission as
@@ -53,6 +53,7 @@ import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
import {
createUser, createTrip, addTripMember, createDay, createPlace, createDayAssignment,
createDayAccommodation, createDayNote, createReservation,
} from '../../helpers/factories';
import { DatabaseService } from '../../../src/nest/database/database.service';
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
@@ -202,6 +203,8 @@ describe('get and remove', () => {
share_packing: true,
share_budget: false,
share_collab: false,
share_travel_only: false,
share_hide_images: false,
});
});
@@ -254,6 +257,7 @@ describe('getSharedTripData', () => {
);
expect(data.permissions).toEqual({
share_map: true, share_bookings: true, share_packing: true, share_budget: true, share_collab: true,
share_travel_only: false, share_hide_images: false,
});
expect(data.days).toHaveLength(1);
const entries = data.assignments[day.id];
@@ -352,6 +356,40 @@ describe('getSharedTripData', () => {
expect(data.collab).toEqual([]);
});
it('SHARE-SVC-019b: travel and stays only keeps transport, hotels and the stay stops (#1712)', () => {
const { trip, token } = seedSharedTrip({ share_travel_only: true });
const day = createDay(testDb, trip.id);
const museum = createPlace(testDb, trip.id, { name: 'Museum' });
const hotel = createPlace(testDb, trip.id, { name: 'Hotel' });
const stay = createDayAccommodation(testDb, trip.id, hotel.id, day.id, day.id);
createDayAssignment(testDb, day.id, museum.id);
const stayStop = createDayAssignment(testDb, day.id, hotel.id);
testDb.prepare('UPDATE day_assignments SET accommodation_id = ? WHERE id = ?').run(stay.id, stayStop.id);
createDayNote(testDb, day.id, trip.id);
for (const type of ['flight', 'hotel', 'restaurant', 'tour']) createReservation(testDb, trip.id, { type, title: type });
const data = svc.getSharedTripData(token)!;
expect(data.permissions.share_travel_only).toBe(true);
expect(data.assignments[day.id].map((a: any) => a.place.name)).toEqual(['Hotel']);
expect(data.dayNotes).toEqual({});
expect(data.places.map((p: any) => p.name)).toEqual(['Hotel']);
expect(data.reservations.map((r: any) => r.type).sort()).toEqual(['flight', 'hotel']);
});
it('SHARE-SVC-019c: without photos drops every place image and refuses the photo route (#1712)', async () => {
const { trip, token } = seedSharedTrip({ share_hide_images: true });
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Louvre' });
testDb.prepare("UPDATE places SET image_url = '/api/maps/place-photo/abc/bytes' WHERE id = ?").run(place.id);
createDayAssignment(testDb, day.id, place.id);
const data = svc.getSharedTripData(token)!;
expect(data.places[0].image_url).toBeNull();
expect(data.assignments[day.id][0].place.image_url).toBeNull();
serveKey.mockReturnValue('photos-google/abc');
await expect(svc.getSharedPlacePhotoKey(token, 'abc')).resolves.toBeNull();
});
it('SHARE-SVC-020: baseCurrency falls back trip currency → EUR, with the owner default_currency winning (#1361)', () => {
const { user, trip, token } = seedSharedTrip();
expect(svc.getSharedTripData(token)!.baseCurrency).toBe('EUR');
@@ -697,6 +697,38 @@ describe('toggleCompanyHoliday', () => {
});
});
describe('half company holidays (#2439)', () => {
const entryOf = (planId: number, date: string) =>
testDb.prepare('SELECT fraction FROM vacay_entries WHERE plan_id = ? AND date = ?').get(planId, date) as { fraction: number } | undefined;
const holidayOf = (planId: number, date: string) =>
testDb.prepare('SELECT fraction FROM vacay_company_holidays WHERE plan_id = ? AND date = ?').get(planId, date) as { fraction: number } | undefined;
it('VACAY-SVC-036b: a half company holiday halves a whole vacation day instead of wiping it', () => {
const { user, plan } = setupUserWithPlan();
svc.toggleEntry(user.id, plan.id, '2025-12-24', 1);
expect(svc.toggleCompanyHoliday(plan.id, '2025-12-24', 'Christmas Eve', undefined, 0.5)).toEqual({ action: 'added', fraction: 0.5 });
expect(entryOf(plan.id, '2025-12-24')?.fraction).toBe(0.5);
expect(holidayOf(plan.id, '2025-12-24')?.fraction).toBe(0.5);
});
it('VACAY-SVC-036c: the other size converts the holiday, the same size clears it', () => {
const { user, plan } = setupUserWithPlan();
svc.toggleCompanyHoliday(plan.id, '2025-12-31', undefined, undefined, 0.5);
svc.toggleEntry(user.id, plan.id, '2025-12-31', 0.5);
expect(svc.toggleCompanyHoliday(plan.id, '2025-12-31', undefined, undefined, 1)).toEqual({ action: 'updated', fraction: 1 });
expect(entryOf(plan.id, '2025-12-31')).toBeUndefined();
expect(svc.toggleCompanyHoliday(plan.id, '2025-12-31', undefined, undefined, 1)).toEqual({ action: 'removed' });
expect(holidayOf(plan.id, '2025-12-31')).toBeUndefined();
});
it('VACAY-SVC-036d: leave on a half company holiday is half a day, whatever was asked', () => {
const { user, plan } = setupUserWithPlan();
testDb.prepare('UPDATE vacay_plans SET company_holidays_enabled = 1 WHERE id = ?').run(plan.id);
svc.toggleCompanyHoliday(plan.id, '2025-12-24', undefined, undefined, 0.5);
expect(svc.toggleEntry(user.id, plan.id, '2025-12-24', 1)).toMatchObject({ action: 'added', fraction: 0.5 });
});
});
// ── acceptInvite / declineInvite / cancelInvite ───────────────────────────────
describe('acceptInvite', () => {