diff --git a/server/src/nest/places/places.helpers.ts b/server/src/nest/places/places.helpers.ts index 9533a78d0..5b011a61d 100644 --- a/server/src/nest/places/places.helpers.ts +++ b/server/src/nest/places/places.helpers.ts @@ -1,9 +1,26 @@ import type { PlaceWithTagsRow as PlaceWithTags } from '../../db/repositories/Places.repository'; import { haversineMetres } from '../common/geo'; +import { toRowId } from '../common/row-id'; import type { KmlImportSummary } from '../place-import/place-import.types'; import type { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service'; import { externalIdsOf, normalizePlaceName, placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared'; +/** + * The category and tag ids a place list filters by, from their query-string + * values. An absent or empty filter is left out; an id that is not canonical + * (`toRowId`) matches no place, so the list is `null` and the answer is empty + * without a query. The repository binds numbers only. + */ +export function placeListIdFilters(filters: { + category?: string; + tag?: string; +}): { category?: number; tag?: number } | null { + const category = filters.category ? toRowId(filters.category) : undefined; + const tag = filters.tag ? toRowId(filters.tag) : undefined; + if (category === null || tag === null) return null; + return { category, tag }; +} + /** * Pure helpers and module-scope constants of the places domain, moved verbatim * out of the legacy services/placeService.ts when it went DI-native. Same diff --git a/server/src/nest/places/places.service.ts b/server/src/nest/places/places.service.ts index f3aab83b8..11ba066fb 100644 --- a/server/src/nest/places/places.service.ts +++ b/server/src/nest/places/places.service.ts @@ -34,7 +34,7 @@ import { TripMembers } from '../../db/entities/TripMembers.entity'; import { DayAssignments } from '../../db/entities/DayAssignments.entity'; import { Categories } from '../../db/entities/Categories.entity'; import { Trips } from '../../db/entities/Trips.entity'; -import type { PlacesRepository, PlaceListRow, PlaceWithTagsRow as PlaceWithTags } from '../../db/repositories/Places.repository'; +import type { PlacesRepository, PlaceWithTagsRow as PlaceWithTags } from '../../db/repositories/Places.repository'; import type { TagsRepository } from '../../db/repositories/Tags.repository'; import type { PlaceRatingsRepository } from '../../db/repositories/PlaceRatings.repository'; import type { TripMembersRepository } from '../../db/repositories/TripMembers.repository'; @@ -73,6 +73,7 @@ import { type ListImportOptions, type ListImportResult, type PlaceImportResult, + placeListIdFilters, } from './places.helpers'; type Trip = TripAccess; @@ -247,14 +248,13 @@ export class PlacesService { filters: { search?: string; category?: string; tag?: string; assignment?: 'all' | 'unassigned' | 'assigned' }, ) { // ESCAPE so a `%` or `_` the user typed matches literally instead of - // acting as a LIKE wildcard (a bare '%' used to return the whole trip) — - // the coercion stays here (the service), the repository binds the + // acting as a LIKE wildcard (a bare `%` would match every place in the + // trip). The coercion stays here (the service); the repository binds the // already-built pattern verbatim (PL3). const searchPattern = filters.search ? `%${escapeLikePattern(filters.search)}%` : undefined; - // A category or tag that is no canonical id (toRowId) matches nothing; bound as text, Postgres would refuse it. - const [category, tag] = [filters.category, filters.tag].map((id) => (id ? toRowId(id) : undefined)); - if (category === null || tag === null) return []; - const places: PlaceListRow[] = await this.placesRepo.listForTrip(tripId, { searchPattern, category, tag, assignment: filters.assignment }); + const ids = placeListIdFilters(filters); + if (!ids) return []; + const places = await this.placesRepo.listForTrip(tripId, { searchPattern, ...ids, assignment: filters.assignment }); const placeIds = places.map(p => p.id); const tagsByPlaceId = await this.queryHelpers.loadTagsByPlaceIds(placeIds);