refactor(places): read the list's category and tag ids through one helper

The list filter turned its two query-string ids with a destructured
[...].map(), and the null-means-no-match convention (a non-canonical id
matches no place) was carried by a comment and two checks on the
result. placeListIdFilters in places.helpers.ts owns that convention and
returns null when the list must be empty, so the service reads as three
lines. The MCP tool passes the same strings to the same service method,
so it is unchanged.
This commit is contained in:
Isaias Tavares
2026-11-05 01:15:10 -03:00
parent c0bae97a14
commit 783ad5c0f1
2 changed files with 24 additions and 7 deletions
+17
View File
@@ -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
+7 -7
View File
@@ -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);