feat(dashboard): search every trip by title, date or a place on it (#2190)

This commit is contained in:
Maurice
2026-10-01 13:30:44 +02:00
parent 99c62878f3
commit 8c37597bd7
38 changed files with 372 additions and 10 deletions
+7 -1
View File
@@ -23,7 +23,7 @@ import { RuntimeEnvService } from '../app-config/runtime-env.service';
import type { Request, Response } from 'express';
import type { Options } from 'multer';
import path from 'path';
import { MAX_TRIP_DAYS, type ActiveTripResponse } from '@trek/shared';
import { MAX_TRIP_DAYS, type ActiveTripResponse, type TripSearchResponse } from '@trek/shared';
import { StorageService } from '../storage/storage.service';
import type { User } from '../../types';
import { TripsService } from './trips.service';
@@ -86,6 +86,12 @@ export class TripsController {
return { trip: { id, title, start_date, end_date } };
}
/** Places across the user's trips for the dashboard search (#2190). Above @Get(':id'). */
@Get('search')
search(@CurrentUser() user: User, @Query('q') q?: string): TripSearchResponse {
return { matches: this.trips.searchPlaces(user.id, typeof q === 'string' ? q.slice(0, 100) : '') };
}
@Get('cover-images/search')
async coverImages(@CurrentUser() user: User, @Query('query') query?: string) {
try {
+28
View File
@@ -306,6 +306,34 @@ export class TripsService {
`).get({ userId, today }) as ActiveTrip & { relevance: number } | undefined;
}
/**
* The user's trips, archived ones included, with places whose name or address
* contains `query` (#2190). Up to three names per trip; a query under two
* characters matches nothing, it would match every place there is.
*/
searchPlaces(userId: number, query: string): { trip_id: number; places: string[] }[] {
const q = query.trim();
if (q.length < 2) return [];
const like = `%${q.replace(/[\\%_]/g, ch => `\\${ch}`)}%`;
const rows = this.db.prepare(`
SELECT p.trip_id, p.name
FROM places p
JOIN trips t ON t.id = p.trip_id
LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = :userId
WHERE (t.user_id = :userId OR m.user_id IS NOT NULL)
AND (p.name LIKE :like ESCAPE '\\' OR p.address LIKE :like ESCAPE '\\')
ORDER BY p.trip_id, p.name
LIMIT 500
`).all({ userId, like }) as { trip_id: number; name: string }[];
const byTrip = new Map<number, string[]>();
for (const row of rows) {
const names = byTrip.get(row.trip_id) ?? [];
if (names.length < 3 && !names.includes(row.name)) names.push(row.name);
byTrip.set(row.trip_id, names);
}
return [...byTrip].map(([trip_id, places]) => ({ trip_id, places }));
}
getRaw(tripId: string | number): Trip | undefined {
return this.db.prepare('SELECT * FROM trips WHERE id = ?').get(tripId) as Trip | undefined;
}