mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
refactor(places): places, tags, ratings and place images through repositories
This commit is contained in:
@@ -99,6 +99,27 @@ export class AssignmentParticipantsRepository extends TrekRepository<AssignmentP
|
||||
* for a relation-backed `onConflictFields` list). The caller only ever
|
||||
* passes a non-empty, already-roster-scoped array; the empty-array guard
|
||||
* here is defensive, matching the service's own guard.
|
||||
*
|
||||
* **Two differences from the legacy per-row loop (Task 3 review M1,
|
||||
* absorbed in Task 4), both benign today, documented so they stay that
|
||||
* way on purpose:**
|
||||
* 1. **Narrower failure mode.** `OR IGNORE` swallowed *any* constraint
|
||||
* violation on a row; `ON CONFLICT (assignment_id, user_id) DO
|
||||
* NOTHING` swallows only THAT unique index, and one row that violates
|
||||
* a DIFFERENT constraint aborts the whole multi-row statement rather
|
||||
* than being skipped. Unreachable today — AS28 roster-scopes the ids
|
||||
* before this call, and the caller has already gated the assignment
|
||||
* itself — but a concurrent delete of the assignment mid-request would
|
||||
* now 500 where the legacy loop silently no-opped that one row.
|
||||
* 2. **One extra statement.** `upsertMany` issues a reload `SELECT` after
|
||||
* the insert (`generated SQL, captured: insert … on conflict (…) do
|
||||
* nothing returning id` then `select … from assignment_participants
|
||||
* as a0 where (…)`) that the legacy loop never ran. Harmless — the
|
||||
* reloaded rows are fully hydrated and unchanged, so the enclosing
|
||||
* `em.transactional`'s closing flush emits nothing for them — but it
|
||||
* is a genuine extra round-trip and briefly populates the identity map
|
||||
* inside the write transaction, worth knowing if a future change here
|
||||
* ever needs to reason about statement counts.
|
||||
*/
|
||||
async insertIgnore(assignment_id: number, user_ids: number[]): Promise<void> {
|
||||
if (user_ids.length === 0) return;
|
||||
|
||||
@@ -300,13 +300,20 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
|
||||
/**
|
||||
* AS9 (`assignmentExistsInDay`) — `SELECT da.id FROM day_assignments da
|
||||
* JOIN days d ON da.day_id = d.id WHERE da.id = ? AND da.day_id = ? AND
|
||||
* d.trip_id = ?`. Raw-bind (`number | string`, D4's T5 escape hatch,
|
||||
* `TripsRepository.findAccessible`'s precedent): the legacy guard binds
|
||||
* the route's raw params straight into the statement with no `Number()`/
|
||||
* `toRowId` conversion of its own, so this must accept and bind exactly
|
||||
* what it's handed, not coerce it first.
|
||||
* d.trip_id = ?`.
|
||||
*
|
||||
* **`id: number`, not `number | string` (Task 3 review H1, absorbed in
|
||||
* Task 4 — `task-3-review.md` §9.1):** this method's ONLY caller
|
||||
* (`AssignmentsService.assignmentExistsInDay`) now runs `toRowId` on the
|
||||
* route's raw `id` BEFORE calling this — a non-canonical id (`"3 "`,
|
||||
* `"3.0"`) must resolve to "not found" in the SERVICE gate, not pass this
|
||||
* statement's own SQLite text/integer affinity match and then disagree
|
||||
* with `deleteAssignment`'s `toRowId(id)!` downstream (the H1 regression).
|
||||
* `day_id`/`trip_id` keep their `number | string` raw-bind scoping —
|
||||
* `deleteAssignment` never reads either back, so there is no gate-vs-write
|
||||
* id to protect there.
|
||||
*/
|
||||
async existsInDay(id: number | string, day_id: number | string, trip_id: number | string): Promise<boolean> {
|
||||
async existsInDay(id: number, day_id: number | string, trip_id: number | string): Promise<boolean> {
|
||||
const row = await this.qb('da')
|
||||
.join('da.day', 'd')
|
||||
.select(['da.id'])
|
||||
@@ -318,13 +325,21 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
|
||||
/**
|
||||
* AS12 (`getAssignmentForTrip`) — `SELECT da.* FROM day_assignments da
|
||||
* JOIN days d ON da.day_id = d.id WHERE da.id = ? AND d.trip_id = ?`.
|
||||
* Same raw-bind seam as `existsInDay` above — every real caller
|
||||
* (`AssignmentOpsController`, `AssignmentsMcp`, `ItineraryRpc`) hands this
|
||||
* the route/tool id verbatim, and `ItineraryRpc.unassign` reads
|
||||
* `existing.day_id` off the result, so this is a genuine re-select, not
|
||||
* only an existence guard.
|
||||
*
|
||||
* **`id: number`, not `number | string` (Task 3 review H1, absorbed in
|
||||
* Task 4 — `task-3-review.md` §9.1):** this method's ONLY caller
|
||||
* (`AssignmentsService.getAssignmentForTrip`) now runs `toRowId` on the
|
||||
* route/tool's raw `id` BEFORE calling this, the same H1 fix
|
||||
* `existsInDay` above applies — every mutation behind this gate
|
||||
* (`updateTime`/`setEndDay`/`updateNotes`/`setLegTransportMode`/
|
||||
* `setIncomingLegTransportMode`/`setParticipants`/`moveAssignment`)
|
||||
* converts `id` with its own `toRowId(id)!`, and `ItineraryRpc.unassign`
|
||||
* reads `existing.day_id` off THIS result (a genuine re-select, not only
|
||||
* an existence guard) — so the id this read resolves must be the same one
|
||||
* every one of those writes uses. `trip_id` keeps its `number | string`
|
||||
* raw-bind scoping, unchanged.
|
||||
*/
|
||||
async findInTrip(id: number | string, trip_id: number | string): Promise<DayAssignmentRow | undefined> {
|
||||
async findInTrip(id: number, trip_id: number | string): Promise<DayAssignmentRow | undefined> {
|
||||
const row = await this.qb('da')
|
||||
.join('da.day', 'd')
|
||||
.select(['da.*'])
|
||||
@@ -509,4 +524,39 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
|
||||
async setIncomingLegMode(id: number, mode: string | null): Promise<void> {
|
||||
await this.nativeUpdate({ id }, { incoming_leg_transport_mode: mode });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan 3c Task 4 (`PlacesService.exportGpx`) — appended per the task-4
|
||||
// brief's "task-3-report.md" pointer ("DayAssignmentsRepository for the
|
||||
// PL30 GPX itinerary read if it fits there"). Unlike AS16/AS18, this join
|
||||
// (`day_assignments` -> `days`, `day_assignments` -> `places`) is fully
|
||||
// expressible through the entity's own declared relations (`da.day`,
|
||||
// `da.place`) — no `day_accommodations`-shaped gap — so the QueryBuilder is
|
||||
// used directly rather than Kysely.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* PL30 (`places.service.ts::exportGpx`) — `SELECT d.day_number, d.date,
|
||||
* d.title, p.name, p.lat, p.lng FROM days d JOIN day_assignments da ON
|
||||
* da.day_id = d.id JOIN places p ON p.id = da.place_id WHERE d.trip_id = ?
|
||||
* AND p.lat IS NOT NULL AND p.lng IS NOT NULL ORDER BY d.day_number,
|
||||
* da.order_index` — rooted here on `day_assignments` instead of `days`
|
||||
* (the legacy statement's own `FROM` root) because both joins the legacy
|
||||
* statement needs (`day_assignments -> days`, `day_assignments -> places`)
|
||||
* are single hops FROM this entity, while `days` has no direct relation to
|
||||
* `places`. Same result set, same ordering; the root alias choice is not
|
||||
* observable from the row shape.
|
||||
*/
|
||||
async listItineraryForGpx(trip_id: string | number): Promise<{
|
||||
day_number: number; date: string | null; title: string | null;
|
||||
name: string; lat: number; lng: number;
|
||||
}[]> {
|
||||
return this.qb('da')
|
||||
.join('da.day', 'd')
|
||||
.join('da.place', 'p')
|
||||
.select(['d.day_number', 'd.date', 'd.title', 'p.name', 'p.lat', 'p.lng'])
|
||||
.where('d.trip_id = ? AND p.lat IS NOT NULL AND p.lng IS NOT NULL', [trip_id])
|
||||
.orderBy({ 'd.day_number': 'asc', 'da.order_index': 'asc' })
|
||||
.execute<{ day_number: number; date: string | null; title: string | null; name: string; lat: number; lng: number }[]>('all', false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,4 +40,37 @@ export class PlaceRatingsRepository extends TrekRepository<PlaceRatings> {
|
||||
.orderBy({ 'pr.created_at': 'asc' })
|
||||
.execute<PlaceRatingForPlaceRow[]>('all', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* PL51 (`places.service.ts::rate`) — `INSERT INTO place_ratings (place_id,
|
||||
* user_id, rating) VALUES (?, ?, ?) ON CONFLICT(place_id, user_id) DO
|
||||
* UPDATE SET rating = excluded.rating` — the only explicit `ON CONFLICT …
|
||||
* DO UPDATE` in the Plan 3c cluster (#1435). `onConflictFields: ['place',
|
||||
* 'user']` on the entity's own declared composite unique
|
||||
* (`PlaceRatingsSchema`'s `uniques: [{ properties: ['place', 'user'] }]`,
|
||||
* itself generated from the migration's `UNIQUE(place_id, user_id)`
|
||||
* — verified against both, not assumed). `onConflictMergeFields: ['rating']`
|
||||
* is load-bearing, not decorative: without it `em.upsert`'s default merge
|
||||
* set is every non-key column, which on THIS entity is only `rating`
|
||||
* anyway (`created_at` is the sole other column and is excluded because it
|
||||
* is the primary key's sibling default, not a mergeable field) — named
|
||||
* explicitly so a future column added to this entity does not silently
|
||||
* join the merge set and start touching a column the legacy statement
|
||||
* never wrote on conflict. The ruling this method exists to satisfy: a
|
||||
* vote must never touch `places.updated_at` (a separate table this
|
||||
* statement doesn't reference at all) so it can't 409 another member's
|
||||
* `If-Match` — proven by a repository test that reads `places.updated_at`
|
||||
* unchanged across a rating write.
|
||||
*/
|
||||
async upsertRating(place_id: number, user_id: number, rating: number): Promise<void> {
|
||||
await this.upsert(
|
||||
{ place: place_id, user: user_id, rating },
|
||||
{ onConflictFields: ['place', 'user'], onConflictAction: 'merge', onConflictMergeFields: ['rating'] },
|
||||
);
|
||||
}
|
||||
|
||||
/** PL50 — `DELETE FROM place_ratings WHERE place_id = ? AND user_id = ?` (a null rating clears the vote). */
|
||||
async deleteRating(place_id: number, user_id: number): Promise<void> {
|
||||
await this.nativeDelete({ place: place_id, user: user_id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Places } from '../entities/Places.entity';
|
||||
import type { Tags } from '../entities/Tags.entity';
|
||||
import { columnRef } from '../dialect/sql-functions';
|
||||
import { absDifference, columnRef, currentTimestamp, lowerTrim } from '../dialect/sql-functions';
|
||||
import { type AssertRowKeys } from './_shared/rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
@@ -53,7 +53,8 @@ export interface TagRow {
|
||||
|
||||
const _tagRowKeys: AssertRowKeys<TagRow, Tags> = true;
|
||||
|
||||
interface PlaceWithCategoryRow extends PlaceRow {
|
||||
/** PL3 (`PlacesService.list`) — a `places` row joined with its category's flat columns. */
|
||||
export interface PlaceWithCategoryRow extends PlaceRow {
|
||||
category_name: string | null;
|
||||
category_color: string | null;
|
||||
category_icon: string | null;
|
||||
@@ -181,4 +182,416 @@ export class PlacesRepository extends TrekRepository<Places> {
|
||||
.execute<{ id: number } | undefined>('get', false);
|
||||
return !!row;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan 3c Task 4 (`PlacesService` core) — every method below is additive;
|
||||
// `findWithTagsAndRatings`/`existsByGoogleIdOrImageUrl` above are Task 0b's
|
||||
// and Task 1's, unchanged.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* PL0/PL7/PL49/AS5 — `SELECT id FROM places WHERE id = ? AND trip_id = ?`.
|
||||
*
|
||||
* **`id: number`, not `number | string` (Task 3 review H1, absorbed
|
||||
* here — `task-3-review.md` §9.1):** the legacy raw-bind seam other
|
||||
* guard-shaped methods in this cluster preserve (`TripsRepository
|
||||
* .findAccessible`'s precedent) is the WRONG shape for a method whose
|
||||
* caller then writes with `toRowId(placeId)!`. A non-canonical id
|
||||
* (`"3 "`, `"3.0"`, `"+3"`) matches this statement via SQLite's own
|
||||
* loose text/integer affinity — `existsInTrip` would answer `true` — while
|
||||
* `toRowId` (`/^\d+$/` + `Number.isSafeInteger`, program rule 15) rejects
|
||||
* the same string and returns `null`, so the write downstream runs
|
||||
* `toRowId(placeId)!` on a value the gate just vouched for and gets
|
||||
* `null` — exactly Task 3's H1 (`AssignmentsService.dayExists`/
|
||||
* `placeExists`/`assignmentExistsInDay`/`getAssignmentForTrip`, all four
|
||||
* kept on this seam, all four reproducing it). This method's only two
|
||||
* callers (`PlacesService.get`/`applyUpdate`'s existence half — via
|
||||
* `applyUpdate` calling `findInTrip` below — and `AssignmentsService
|
||||
* .placeExists`, once Task 4 repoints it) now run `toRowId` FIRST and
|
||||
* pass the validated `number` in, matching `DaysService.getDay`'s shape
|
||||
* (the review's cited correct precedent) — so `id` here is narrowed to
|
||||
* match what every caller actually has after its own gate, and a second
|
||||
* `toRowId(...)!` downstream can never disagree with this one.
|
||||
*/
|
||||
async existsInTrip(id: number, trip_id: number): Promise<boolean> {
|
||||
const row = await this.qb('p')
|
||||
.select(['p.id'])
|
||||
.where('p.id = ? AND p.trip_id = ?', [id, trip_id])
|
||||
.execute<{ id: number } | undefined>('get', false);
|
||||
return !!row;
|
||||
}
|
||||
|
||||
/**
|
||||
* PL9/PL48 — `SELECT * FROM places WHERE id = ? AND trip_id = ?`, byte-
|
||||
* identical statements at both legacy sites (`applyUpdate`'s pre-image
|
||||
* read and `searchImage`'s existence-plus-name read) — one method for
|
||||
* both rather than two identically-bodied ones.
|
||||
*
|
||||
* `id: number`, same H1 fix and reasoning as `existsInTrip` above: both
|
||||
* callers write with the SAME already-`toRowId`'d id this read returns
|
||||
* (`applyUpdate` feeds it straight into `updatePlace`/`tagsRepo`;
|
||||
* `searchImage` only reads, but takes the same gate shape for
|
||||
* consistency — one rule for every place-id gate in this service, not
|
||||
* one per call site).
|
||||
*/
|
||||
async findInTrip(id: number, trip_id: number): Promise<PlaceRow | undefined> {
|
||||
return this.qb('p')
|
||||
.select(['p.*'])
|
||||
.where('p.id = ? AND p.trip_id = ?', [id, trip_id])
|
||||
.execute<PlaceRow | undefined>('get', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* PL17/PL20 — `SELECT google_place_id, image_url FROM places WHERE id = ?
|
||||
* AND trip_id = ?`, the reclaim-candidate projection `remove`/`removeMany`
|
||||
* read BEFORE their delete transaction. `id: number`, same H1 fix: both
|
||||
* callers immediately follow a `true` result with `deleteById(id)` on the
|
||||
* SAME numeric id.
|
||||
*/
|
||||
async reclaimInputs(id: number, trip_id: number): Promise<{ google_place_id: string | null; image_url: string | null } | undefined> {
|
||||
return this.qb('p')
|
||||
.select(['p.google_place_id', 'p.image_url'])
|
||||
.where('p.id = ? AND p.trip_id = ?', [id, trip_id])
|
||||
.execute<{ google_place_id: string | null; image_url: string | null } | undefined>('get', false);
|
||||
}
|
||||
|
||||
/** PL19/PL21 — `DELETE FROM places WHERE id = ?`, unscoped: every caller proved trip access first. */
|
||||
async deleteById(id: number): Promise<void> {
|
||||
await this.nativeDelete({ id });
|
||||
}
|
||||
|
||||
/**
|
||||
* PL23 — `` SELECT id FROM places WHERE trip_id = ? AND id IN (${…}) ``,
|
||||
* narrowed to the caller's own INPUT ORDER (not SQL result order — SQLite
|
||||
* makes no ordering promise for an `IN (...)` scan and the legacy code
|
||||
* re-filters the original `ids` array through a `Set` built from the row
|
||||
* set for exactly this reason). `$in` for the dynamic list (rule 17b, "the
|
||||
* good kind").
|
||||
*/
|
||||
async scopedIds(trip_id: string | number, ids: number[]): Promise<number[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const rows = await this.qb('p')
|
||||
.select(['p.id'])
|
||||
.where('p.trip_id = ?', [trip_id])
|
||||
.andWhere({ id: { $in: ids } })
|
||||
.execute<{ id: number }[]>('all', false);
|
||||
const owned = new Set(rows.map((r) => r.id));
|
||||
return ids.filter((id) => owned.has(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* PL4 — the 25-column `INSERT INTO places (...)` the service's `create`
|
||||
* builds. Every value here is already fully coerced by the caller (the
|
||||
* `?? null` vs `|| null` split PL4's own legacy comment documents — 0 is
|
||||
* legitimate for `lat`/`lng`/`price`/`fill_percent`, and
|
||||
* `duration_minutes` defaults to 60 — stays the service's decision, this
|
||||
* writes exactly what it is handed, `DaysRepository.createDay`'s split).
|
||||
* `em.insert()` returns the generated PK (R6's `lastInsertRowid`
|
||||
* replacement).
|
||||
*/
|
||||
async insertPlace(input: {
|
||||
trip_id: number;
|
||||
name: string;
|
||||
description: string | null;
|
||||
lat: number | null;
|
||||
lng: number | null;
|
||||
address: string | null;
|
||||
category_id: number | null;
|
||||
price: number | null;
|
||||
currency: string | null;
|
||||
place_time: string | null;
|
||||
end_time: string | null;
|
||||
duration_minutes: number;
|
||||
notes: string | null;
|
||||
image_url: string | null;
|
||||
google_place_id: string | null;
|
||||
google_ftid: string | null;
|
||||
osm_id: string | null;
|
||||
amap_poi_id: string | null;
|
||||
website: string | null;
|
||||
phone: string | null;
|
||||
transport_mode: string;
|
||||
route_geometry: string | null;
|
||||
route_color: string | null;
|
||||
stop_type: string | null;
|
||||
fill_percent: number | null;
|
||||
}): Promise<number> {
|
||||
return await this.insert({
|
||||
trip: input.trip_id,
|
||||
name: input.name,
|
||||
description: input.description,
|
||||
lat: input.lat,
|
||||
lng: input.lng,
|
||||
address: input.address,
|
||||
category: input.category_id,
|
||||
price: input.price,
|
||||
currency: input.currency,
|
||||
place_time: input.place_time,
|
||||
end_time: input.end_time,
|
||||
duration_minutes: input.duration_minutes,
|
||||
notes: input.notes,
|
||||
image_url: input.image_url,
|
||||
google_place_id: input.google_place_id,
|
||||
google_ftid: input.google_ftid,
|
||||
osm_id: input.osm_id,
|
||||
amap_poi_id: input.amap_poi_id,
|
||||
website: input.website,
|
||||
phone: input.phone,
|
||||
transport_mode: input.transport_mode,
|
||||
route_geometry: input.route_geometry,
|
||||
route_color: input.route_color,
|
||||
stop_type: input.stop_type,
|
||||
fill_percent: input.fill_percent,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PL11 — `UPDATE places SET name = COALESCE(?, name), description = ?,
|
||||
* lat = ?, lng = ?, address = ?, category_id = ?, price = ?, currency =
|
||||
* COALESCE(?, currency), place_time = ?, end_time = ?, duration_minutes =
|
||||
* ?, notes = ?, image_url = ?, google_place_id = ?, google_ftid = ?,
|
||||
* osm_id = ?, amap_poi_id = ?, website = ?, phone = ?, transport_mode =
|
||||
* COALESCE(?, transport_mode), route_color = ?, stop_type = ?,
|
||||
* fill_percent = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?` — ONE
|
||||
* `nativeUpdate` with a typed full partial (the ruling): the SQL
|
||||
* `COALESCE(?, col)` keep-if-null semantics (`name`/`currency`/
|
||||
* `transport_mode`) and the twenty `!== undefined ? x : existing.x`
|
||||
* pre-image fallbacks are both already resolved to their FINAL value by
|
||||
* the caller (`PlacesService.applyUpdate`, which reads the pre-image via
|
||||
* `findInTrip` first) — this method writes exactly what it is handed,
|
||||
* plus the timestamp stamp every write gets.
|
||||
*/
|
||||
async updatePlace(id: number, write: {
|
||||
name: string | null;
|
||||
description: string | null;
|
||||
lat: number | null;
|
||||
lng: number | null;
|
||||
address: string | null;
|
||||
category_id: number | null;
|
||||
price: number | null;
|
||||
currency: string | null;
|
||||
place_time: string | null;
|
||||
end_time: string | null;
|
||||
duration_minutes: number | null;
|
||||
notes: string | null;
|
||||
image_url: string | null;
|
||||
google_place_id: string | null;
|
||||
google_ftid: string | null;
|
||||
osm_id: string | null;
|
||||
amap_poi_id: string | null;
|
||||
website: string | null;
|
||||
phone: string | null;
|
||||
transport_mode: string | null;
|
||||
route_color: string | null;
|
||||
stop_type: string | null;
|
||||
fill_percent: number | null;
|
||||
}): Promise<void> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
await this.nativeUpdate({ id }, {
|
||||
name: write.name,
|
||||
description: write.description,
|
||||
lat: write.lat,
|
||||
lng: write.lng,
|
||||
address: write.address,
|
||||
category: write.category_id,
|
||||
price: write.price,
|
||||
currency: write.currency,
|
||||
place_time: write.place_time,
|
||||
end_time: write.end_time,
|
||||
duration_minutes: write.duration_minutes,
|
||||
notes: write.notes,
|
||||
image_url: write.image_url,
|
||||
google_place_id: write.google_place_id,
|
||||
google_ftid: write.google_ftid,
|
||||
osm_id: write.osm_id,
|
||||
amap_poi_id: write.amap_poi_id,
|
||||
website: write.website,
|
||||
phone: write.phone,
|
||||
transport_mode: write.transport_mode,
|
||||
route_color: write.route_color,
|
||||
stop_type: write.stop_type,
|
||||
fill_percent: write.fill_percent,
|
||||
updated_at: currentTimestamp(platform),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PL25 (`findDuplicatePlace`'s `externalId` strategy) — `SELECT id,
|
||||
* google_ftid FROM places WHERE trip_id = ? AND (google_place_id = ? OR
|
||||
* google_ftid = ? OR osm_id = ? OR amap_poi_id = ?) ORDER BY id ASC LIMIT
|
||||
* 1` — the same candidate id bound in all four positions, exactly as the
|
||||
* legacy statement does.
|
||||
*/
|
||||
async findDuplicateByExternalId(trip_id: string, external_id: string): Promise<{ id: number; google_ftid: string | null } | undefined> {
|
||||
return this.qb('p')
|
||||
.select(['p.id', 'p.google_ftid'])
|
||||
.where('p.trip_id = ?', [trip_id])
|
||||
.andWhere({
|
||||
$or: [
|
||||
{ google_place_id: external_id },
|
||||
{ google_ftid: external_id },
|
||||
{ osm_id: external_id },
|
||||
{ amap_poi_id: external_id },
|
||||
],
|
||||
})
|
||||
.orderBy({ 'p.id': 'asc' })
|
||||
.limit(1)
|
||||
.execute<{ id: number; google_ftid: string | null } | undefined>('get', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* PL26 (`findDuplicatePlace`'s `name` strategy) — `SELECT id, google_ftid
|
||||
* FROM places WHERE trip_id = ? AND lower(trim(name)) = ? ORDER BY id ASC
|
||||
* LIMIT 1`. Rule 18's documented-disagreement exception clause: the legacy
|
||||
* statement's OWN bound value is already JS-lowered+trimmed
|
||||
* (`normalizePlaceName`, `@trek/shared/place/place-match.ts`, Unicode-
|
||||
* aware), so `lowered_trimmed_name` here must be passed in ALREADY
|
||||
* lowered — never re-lowered through `lowerParam` (that would apply
|
||||
* SQLite's ASCII-only `LOWER()` a second time, on a value the caller
|
||||
* already folded fully). `lowerTrim(platform, 'p.name')` renders
|
||||
* `LOWER(TRIM(name))` (SQLite `LOWER()` is ASCII-only, program rule 18 —
|
||||
* the documented divergence PL26's legacy comment (`places.service.ts
|
||||
* :609-619`) already accepts and this repository reproduces literally,
|
||||
* not fixes).
|
||||
*/
|
||||
async findDuplicateByName(trip_id: string, lowered_trimmed_name: string): Promise<{ id: number; google_ftid: string | null } | undefined> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('p')
|
||||
.select(['p.id', 'p.google_ftid'])
|
||||
.where('p.trip_id = ?', [trip_id])
|
||||
.andWhere({ [lowerTrim(platform, 'p.name')]: lowered_trimmed_name })
|
||||
.orderBy({ 'p.id': 'asc' })
|
||||
.limit(1)
|
||||
.execute<{ id: number; google_ftid: string | null } | undefined>('get', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* PL27 (`findDuplicatePlace`'s `coords` strategy) — `SELECT id,
|
||||
* google_ftid FROM places WHERE trip_id = ? AND lat IS NOT NULL AND lng IS
|
||||
* NOT NULL AND abs(lat - ?) <= ? AND abs(lng - ?) <= ? ORDER BY id ASC
|
||||
* LIMIT 1`. `absDifference(platform, ref, value)` renders `ABS(<col> - ?)`
|
||||
* with `value` already bound inside the fragment (`sql-functions.ts`'s own
|
||||
* docstring) — used as a filter KEY paired with `{ $lte: tolerance }`, so
|
||||
* the emitted SQL and bind order match the legacy statement exactly:
|
||||
* `ABS(lat - ?) <= ?` then `ABS(lng - ?) <= ?`, the SAME `tolerance` value
|
||||
* on both.
|
||||
*/
|
||||
async findDuplicateByCoords(trip_id: string, lat: number, lng: number, tolerance: number): Promise<{ id: number; google_ftid: string | null } | undefined> {
|
||||
const platform = this.getEntityManager().getPlatform();
|
||||
return this.qb('p')
|
||||
.select(['p.id', 'p.google_ftid'])
|
||||
.where('p.trip_id = ?', [trip_id])
|
||||
.andWhere({
|
||||
lat: { $ne: null },
|
||||
lng: { $ne: null },
|
||||
[absDifference(platform, 'p.lat', lat)]: { $lte: tolerance },
|
||||
[absDifference(platform, 'p.lng', lng)]: { $lte: tolerance },
|
||||
})
|
||||
.orderBy({ 'p.id': 'asc' })
|
||||
.limit(1)
|
||||
.execute<{ id: number; google_ftid: string | null } | undefined>('get', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* PL29 (`exportGpx`'s waypoint read) — `SELECT p.name, p.description,
|
||||
* p.address, p.lat, p.lng, p.route_geometry, c.name AS category FROM
|
||||
* places p LEFT JOIN categories c ON c.id = p.category_id WHERE p.trip_id
|
||||
* = ? ORDER BY p.id`. PL30 (the `days`/`day_assignments` itinerary half)
|
||||
* is `DayAssignmentsRepository.listItineraryForGpx` — a different table
|
||||
* root, kept there per the task-4 brief's own pointer.
|
||||
*/
|
||||
async listForGpx(trip_id: string): Promise<{
|
||||
name: string; description: string | null; address: string | null;
|
||||
lat: number | null; lng: number | null; route_geometry: string | null;
|
||||
category: string | null;
|
||||
}[]> {
|
||||
return this.qb('p')
|
||||
.leftJoin('p.category', 'c')
|
||||
.select(['p.name', 'p.description', 'p.address', 'p.lat', 'p.lng', 'p.route_geometry', 'c.name as category'])
|
||||
.where('p.trip_id = ?', [trip_id])
|
||||
.orderBy({ 'p.id': 'asc' })
|
||||
.execute<{ name: string; description: string | null; address: string | null; lat: number | null; lng: number | null; route_geometry: string | null; category: string | null }[]>('all', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* PI1 (`place-image.ts:27`) — `SELECT 1 FROM places WHERE image_url = ?
|
||||
* LIMIT 1`. Trip-agnostic on purpose (an uploaded image is ref-counted
|
||||
* across every trip, not just the one it was uploaded from — a place
|
||||
* copied or shared across trips can share the file).
|
||||
*/
|
||||
async existsByImageUrl(imageUrl: string): Promise<boolean> {
|
||||
const row = await this.qb('p')
|
||||
.select(['p.id'])
|
||||
.where({ image_url: imageUrl })
|
||||
.limit(1)
|
||||
.execute<{ id: number } | undefined>('get', false);
|
||||
return !!row;
|
||||
}
|
||||
|
||||
/**
|
||||
* PL3 (`PlacesService.list`) — the trip's places, joined with their
|
||||
* category's flat columns, filtered by up to four optional fragments
|
||||
* built as conditional `andWhere`s rather than a hand-assembled SQL
|
||||
* string (§18.3's surprise: the legacy statement's own text is not
|
||||
* knowable from the source without tracing four `if`s — this makes each
|
||||
* fragment a single, separately readable `andWhere` call instead).
|
||||
*
|
||||
* `filters.searchPattern` arrives PRE-BUILT (`%${escapeLikePattern(term)}%`,
|
||||
* the service's job, matching every other "coercions stay in the service"
|
||||
* split in this program) — this binds it verbatim into all three `LIKE …
|
||||
* ESCAPE '\'` fragments, matching the legacy's one-param-three-times bind.
|
||||
* The `ESCAPE '\'` clause is written into the raw fragment text because
|
||||
* MikroORM's `$like` operator does not add one (§18.3).
|
||||
*
|
||||
* `filters.category`/`filters.tag` bind their raw string values with no
|
||||
* `Number()` conversion (matching the legacy statement's own untyped
|
||||
* bind); the two `assignment` fragments reference `place_tags`/
|
||||
* `day_assignments`/`days` by physical table name in a subquery (neither
|
||||
* has, or needs, an entity-relation path for this shape) — the same
|
||||
* raw-SQL-subquery shape `PlacesService.list`'s own legacy string used.
|
||||
*
|
||||
* `DISTINCT` kept via `.select([...], true)` even though a place has at
|
||||
* most one category row (so it changes nothing about THIS join) — parity
|
||||
* with the legacy statement's own `SELECT DISTINCT`, not a functional
|
||||
* requirement.
|
||||
*/
|
||||
async listForTrip(trip_id: string, filters: {
|
||||
searchPattern?: string;
|
||||
category?: string;
|
||||
tag?: string;
|
||||
assignment?: 'all' | 'unassigned' | 'assigned';
|
||||
}): Promise<PlaceWithCategoryRow[]> {
|
||||
const qb = this.qb('p')
|
||||
.leftJoin('p.category', 'c')
|
||||
.select(['p.*', 'c.name as category_name', 'c.color as category_color', 'c.icon as category_icon'], true)
|
||||
.where('p.trip_id = ?', [trip_id]);
|
||||
|
||||
if (filters.searchPattern) {
|
||||
qb.andWhere(
|
||||
"(p.name LIKE ? ESCAPE '\\' OR p.address LIKE ? ESCAPE '\\' OR p.description LIKE ? ESCAPE '\\')",
|
||||
[filters.searchPattern, filters.searchPattern, filters.searchPattern],
|
||||
);
|
||||
}
|
||||
if (filters.category) {
|
||||
qb.andWhere('p.category_id = ?', [filters.category]);
|
||||
}
|
||||
if (filters.tag) {
|
||||
qb.andWhere('p.id IN (SELECT place_id FROM place_tags WHERE tag_id = ?)', [filters.tag]);
|
||||
}
|
||||
if (filters.assignment === 'unassigned') {
|
||||
qb.andWhere(
|
||||
'p.id NOT IN (SELECT da.place_id FROM day_assignments da JOIN days d ON da.day_id = d.id WHERE d.trip_id = ?)',
|
||||
[trip_id],
|
||||
);
|
||||
} else if (filters.assignment === 'assigned') {
|
||||
qb.andWhere(
|
||||
'p.id IN (SELECT da.place_id FROM day_assignments da JOIN days d ON da.day_id = d.id WHERE d.trip_id = ?)',
|
||||
[trip_id],
|
||||
);
|
||||
}
|
||||
|
||||
qb.orderBy({ 'p.created_at': 'desc' });
|
||||
|
||||
return qb.execute<PlaceWithCategoryRow[]>('all', false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,22 @@ export interface TagForPlaceRow {
|
||||
created_at: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* `place_tags` (`place_id`, `tag_id`, `PRIMARY KEY (place_id, tag_id)`,
|
||||
* `Migration20200101000000_baseline_schema.ts:195-199` — verified against
|
||||
* the migration, not assumed) as `Places.entity.ts`'s own `place_tags`
|
||||
* many-to-many relation declares it — a PLAIN pivot table
|
||||
* (`p.manyToMany(Tags).pivotTable('place_tags')...`) with no owning entity of
|
||||
* its own (unlike `PackingItems`' `packing_item_contributors`, which has a
|
||||
* `pivotEntity`). Neither `em.upsert` nor `qb()` can target a table with no
|
||||
* entity mapping — both are bound to exactly one mapped entity — so
|
||||
* `TrekRepository.kysely()` (D3's sanctioned next escape hatch) is the only
|
||||
* API that reaches it.
|
||||
*/
|
||||
interface PlaceTagsKyselyDB {
|
||||
place_tags: { place_id: number; tag_id: number };
|
||||
}
|
||||
|
||||
export class TagsRepository extends TrekRepository<Tags> {
|
||||
/**
|
||||
* `SELECT * FROM tags WHERE user_id = ? ORDER BY name ASC`, via the shared
|
||||
@@ -163,4 +179,63 @@ export class TagsRepository extends TrekRepository<Tags> {
|
||||
.where({ 'p.id': { $in: placeIds } })
|
||||
.execute<TagForPlaceRow[]>('all', false);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan 3c Task 4 (`PlacesService`) — additive methods.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* PL2 (`PlacesService.tagsOnTrip`) — `` SELECT id, user_id FROM tags WHERE
|
||||
* id IN (${…}) ``, the ownership half of tag scoping: the caller
|
||||
* (`tagsOnTrip`) intersects these rows' `user_id` against the trip's
|
||||
* roster, and an id belonging to nobody on the roster drops silently.
|
||||
* `$in` for the dynamic list (rule 17b).
|
||||
*/
|
||||
async findByIds(ids: number[]): Promise<{ id: number; user_id: number }[]> {
|
||||
if (ids.length === 0) return [];
|
||||
// `t.user` (unaliased, not separately joined) resolves to the physical
|
||||
// `user_id` column and comes back keyed as `user_id` on the raw driver
|
||||
// row — `TripsRepository.findAccessible`'s `.select(['t.id', 't.user',
|
||||
// 't.currency'])` establishes the same shape.
|
||||
return this.qb('t')
|
||||
.select(['t.id', 't.user'])
|
||||
.where({ id: { $in: ids } })
|
||||
.execute<{ id: number; user_id: number }[]>('all', false);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan 3c Task 4 (`PlacesService.create`/`applyUpdate`, PL5/PL12/PL13) —
|
||||
// `place_tags` writes. `PlaceTagsKyselyDB` above documents why Kysely.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* PL5/PL13 — `INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES
|
||||
* (?, ?)`, the legacy statement run once per tag id. One batched Kysely
|
||||
* insert with `onConflict((oc) => oc.columns(['place_id', 'tag_id'])
|
||||
* .doNothing())` on the pivot's real composite `PRIMARY KEY (place_id,
|
||||
* tag_id)` — SQLite's `ON CONFLICT (...) DO NOTHING` (supported since
|
||||
* SQLite 3.24, well within better-sqlite3's bundled version) is the
|
||||
* upsert-API-less equivalent of `INSERT OR IGNORE` for a table with no
|
||||
* entity mapping. Same end state as the legacy per-row loop (every
|
||||
* genuinely new pair inserted, every duplicate silently skipped); the
|
||||
* caller (`PlacesService.tagsOnTrip`) always hands an already
|
||||
* roster-scoped, deduplicated tag id list — the empty-array guard here is
|
||||
* defensive, matching every other batch-write method in this program.
|
||||
*/
|
||||
async insertIgnore(place_id: number, tag_ids: number[]): Promise<void> {
|
||||
if (tag_ids.length === 0) return;
|
||||
await this.kysely<PlaceTagsKyselyDB>()
|
||||
.insertInto('place_tags')
|
||||
.values(tag_ids.map((tag_id) => ({ place_id, tag_id })))
|
||||
.onConflict((oc) => oc.columns(['place_id', 'tag_id']).doNothing())
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** PL12 — `DELETE FROM place_tags WHERE place_id = ?` (the replace-all half of an update). */
|
||||
async deleteForPlace(place_id: number): Promise<void> {
|
||||
await this.kysely<PlaceTagsKyselyDB>()
|
||||
.deleteFrom('place_tags')
|
||||
.where('place_id', '=', place_id)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,21 +65,26 @@ function sortMinutes(time: string | null): number | null {
|
||||
* `AssignmentParticipantsRepository` (Plan 3c Task 3, consuming Task 2's
|
||||
* `findWithPlaceAndCategory`/`listForDay` projection unchanged for AS1/AS3);
|
||||
* AS20–AS23 (`roadtrip_vias`) stay raw on `DatabaseService`, `// ASn — Plan
|
||||
* 3d` marked — that table belongs to Plan 3d. AS5 (`placeExists`) is the one
|
||||
* exception to "SQL lives in a repository method": `Places.repository.ts`
|
||||
* was another task's exclusive, actively-edited file this session (never
|
||||
* touched here), so the equivalent one-line `qb()` raw-condition read is
|
||||
* inlined below rather than added as a `PlacesRepository` method — flagged
|
||||
* in the task report for a follow-up to move it once that file is free.
|
||||
* 3d` marked — that table belongs to Plan 3d. AS5 (`placeExists`) called
|
||||
* `PlacesRepository.existsInTrip` through Task 4's follow-up, once that file
|
||||
* was free (it moved off an inlined `qb()` read this class carried for one
|
||||
* session while `Places.repository.ts` was another task's exclusive file).
|
||||
*
|
||||
* Assignment ids arriving from a route/tool are an **affinity seam** for the
|
||||
* mutation methods (`toRowId(id)!`, program rule 15) — every real call site
|
||||
* only ever reaches a mutation downstream of its own gate check in the same
|
||||
* request (`assignmentExistsInDay`/`getAssignmentForTrip`, both of which
|
||||
* stay on the legacy raw-bind seam themselves, matching `TripsRepository
|
||||
* .findAccessible`'s documented `number | string` pass-through), so the
|
||||
* non-null assertion is dead-code-safe, the same shape `DaysService` already
|
||||
* uses for its own single `getDay` gate.
|
||||
* **Assignment/day/place ids arriving from a route/tool are `toRowId`'d
|
||||
* inside each existence gate, not passed through on the affinity seam**
|
||||
* (Task 3 review H1, a live regression fixed by Task 4): `dayExists`/
|
||||
* `placeExists`/`assignmentExistsInDay`/`getAssignmentForTrip` used to bind
|
||||
* the caller's raw id straight into their own read, which SQLite's
|
||||
* text/integer affinity matches for a shape (`"3 "`, `"3.0"`, `"+3"`)
|
||||
* `toRowId` rejects — so a gate could answer "found" for an id the mutation
|
||||
* behind it, converting with its own `toRowId(id)!`, then treated as `null`
|
||||
* (a 500 on `move`, a silent no-write "success" on `notes`/`delete`). Fixed
|
||||
* the way `DaysService.getDay` (`days.service.ts:238-242`, the correct
|
||||
* precedent — NOT the raw pass-through this docstring previously,
|
||||
* incorrectly, cited it for) does it: `toRowId` runs INSIDE the gate, which
|
||||
* returns not-found on `null`, so every mutation's own `toRowId(id)!` below
|
||||
* can never disagree with the gate that authorized it — the non-null
|
||||
* assertion is dead-code-safe for real, not just by convention.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AssignmentsService {
|
||||
@@ -173,25 +178,39 @@ export class AssignmentsService {
|
||||
});
|
||||
}
|
||||
|
||||
/** AS4 — `DaysRepository.existsInTrip`. Raw-bind pass-through, unchanged. */
|
||||
/**
|
||||
* AS4 — `DaysRepository.existsInTrip`, `dayId` `toRowId`'d INSIDE this
|
||||
* gate (Task 3 review H1 fix, absorbed in Task 4): `createAssignment`
|
||||
* (the only write behind this gate) converts `dayId` with its own
|
||||
* `toRowId(dayId)!` — a non-canonical id (`"3 "`, `"3.0"`) must answer
|
||||
* "not found" HERE, not pass `DaysRepository.existsInTrip`'s own raw-bind
|
||||
* affinity match and then hit `toRowId(dayId)!` downstream returning
|
||||
* `null` (the live regression: a `day_id: null` INSERT, 500). `tripId`
|
||||
* keeps the trip-scoping seam every gate in this cluster uses (D4's T5,
|
||||
* `TripsRepository.findAccessible`'s precedent) — trip access is already
|
||||
* verified upstream of every route this class serves, so `Number(tripId)`
|
||||
* is the correct, unchanged shape (`DaysService.getDay`'s own precedent
|
||||
* keeps its `tripId` unconverted too).
|
||||
*/
|
||||
async dayExists(dayId: string | number, tripId: string | number) {
|
||||
return await this.daysRepo.existsInTrip(dayId, tripId);
|
||||
const id = toRowId(dayId);
|
||||
if (id === null) return false;
|
||||
return await this.daysRepo.existsInTrip(id, Number(tripId));
|
||||
}
|
||||
|
||||
/**
|
||||
* AS5 — `SELECT id FROM places WHERE id = ? AND trip_id = ?`. `Places
|
||||
* .repository.ts` was another task's exclusive, actively-edited file this
|
||||
* session (see the class docstring) — inlined here as the one exception to
|
||||
* "SQL lives in a repository method" rather than adding a method there.
|
||||
* Same raw-bind seam as `dayExists`/`DayAssignmentsRepository.existsInDay`.
|
||||
* AS5 — `PlacesRepository.existsInTrip`, same H1 fix and reasoning as
|
||||
* `dayExists` above: `createAssignment`'s own `toRowId(placeId)!` must
|
||||
* never disagree with this gate. `PlacesRepository.existsInTrip` itself
|
||||
* narrowed its `id` parameter to `number` for this exact reason
|
||||
* (`Places.repository.ts`'s own docstring) — Plan 3c Task 1 originally
|
||||
* landed the method, Task 4 repointed this call site and applied the H1
|
||||
* fix in the same change.
|
||||
*/
|
||||
async placeExists(placeId: unknown, tripId: string | number) {
|
||||
const row = await this.placesRepo
|
||||
.qb('p')
|
||||
.select(['p.id'])
|
||||
.where('p.id = ? AND p.trip_id = ?', [placeId, tripId])
|
||||
.execute<{ id: number } | undefined>('get', false);
|
||||
return !!row;
|
||||
const id = toRowId(placeId);
|
||||
if (id === null) return false;
|
||||
return await this.placesRepo.existsInTrip(id, Number(tripId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,9 +252,20 @@ export class AssignmentsService {
|
||||
return await this.getAssignmentWithPlace(insertedId);
|
||||
}
|
||||
|
||||
/** AS9 — `DayAssignmentsRepository.existsInDay`. Raw-bind pass-through, unchanged. */
|
||||
/**
|
||||
* AS9 — `DayAssignmentsRepository.existsInDay`, `id` `toRowId`'d INSIDE
|
||||
* this gate (Task 3 review H1 fix, absorbed in Task 4): `deleteAssignment`
|
||||
* (the only write behind this gate, `AssignmentsController.remove`) converts
|
||||
* `id` with its own `toRowId(id)!` — this must reject exactly what that
|
||||
* rejects. `dayId`/`tripId` keep the legacy's own raw-bind scoping
|
||||
* (`DayAssignmentsRepository.existsInDay`'s own `number | string`
|
||||
* parameters, unchanged): `deleteAssignment` never reads either back, so
|
||||
* there is no gate-vs-write id to disagree on for them.
|
||||
*/
|
||||
async assignmentExistsInDay(id: string | number, dayId: string | number, tripId: string | number) {
|
||||
return await this.dayAssignmentsRepo.existsInDay(id, dayId, tripId);
|
||||
const rowId = toRowId(id);
|
||||
if (rowId === null) return false;
|
||||
return await this.dayAssignmentsRepo.existsInDay(rowId, dayId, tripId);
|
||||
}
|
||||
|
||||
/** AS10 — `DayAssignmentsRepository.deleteById`. */
|
||||
@@ -257,9 +287,20 @@ export class AssignmentsService {
|
||||
});
|
||||
}
|
||||
|
||||
/** AS12 — `DayAssignmentsRepository.findInTrip`. Raw-bind pass-through, unchanged. */
|
||||
/**
|
||||
* AS12 — `DayAssignmentsRepository.findInTrip`, `id` `toRowId`'d INSIDE
|
||||
* this gate (Task 3 review H1 fix, absorbed in Task 4): every mutation
|
||||
* behind this gate (`updateTime`, `setEndDay`, `updateNotes`,
|
||||
* `setLegTransportMode`, `setIncomingLegTransportMode`, `setParticipants`,
|
||||
* and `ItineraryRpc.unassign` via `moveAssignment`) converts `id` with its
|
||||
* own `toRowId(id)!` — this must reject exactly what those reject, the
|
||||
* same shape `assignmentExistsInDay` above uses for `deleteAssignment`.
|
||||
* `tripId` keeps the legacy's own raw-bind scoping, unchanged.
|
||||
*/
|
||||
async getAssignmentForTrip(id: string | number, tripId: string | number) {
|
||||
return await this.dayAssignmentsRepo.findInTrip(id, tripId);
|
||||
const rowId = toRowId(id);
|
||||
if (rowId === null) return undefined;
|
||||
return await this.dayAssignmentsRepo.findInTrip(rowId, tripId);
|
||||
}
|
||||
|
||||
async moveAssignment(id: string | number, newDayId: unknown, orderIndex: number | null | undefined) {
|
||||
|
||||
@@ -20,6 +20,23 @@ export function isUploadedPlaceImage(url: string | null | undefined): url is str
|
||||
* 'places' category, mirroring tripService.deleteOldCover. Best-effort: never
|
||||
* throws (central key validation rejects a hostile stored value; the catch
|
||||
* swallows it exactly like the old unlink guard).
|
||||
*
|
||||
* Plan 3c Task 4: `PlacesService` no longer calls this free function — it has
|
||||
* its own converted private `reclaimPlaceImage` method (`places.service.ts`),
|
||||
* which does the same job through `PlacesRepository.existsByImageUrl` for
|
||||
* PI1 and one raw `// Plan 3h` statement for PI2. This copy, and the raw `db`
|
||||
* proxy import it still needs, stay here UNCHANGED because
|
||||
* `collections.service.ts` (`src/nest/collections/`, Plan 3h's file — the
|
||||
* `collection_places` table is Plan 3h's) also imports and calls this exact
|
||||
* function on four of its own call sites, verified by grep
|
||||
* (`grep -rn "from '.*place-image'" server/src`) — contradicting the
|
||||
* inventory's stated "every caller is this service" premise
|
||||
* (`plan3c-sql-inventory.md` §8d). Converting that caller means wiring a
|
||||
* `PlacesRepository` injection into `CollectionsModule`, a change to an
|
||||
* unowned domain's DI graph out of this task's file ownership and test
|
||||
* budget — flagged for whoever next owns `src/nest/collections/**` (Plan 3h)
|
||||
* to finish, the same `// Plan 3h` carve-out pattern PI2/PP6 already use in
|
||||
* this cluster.
|
||||
*/
|
||||
export async function reclaimPlaceImage(storage: StorageService, url: string | null | undefined): Promise<void> {
|
||||
if (!isUploadedPlaceImage(url)) return;
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
TOOL_ANNOTATIONS_DELETE, TOOL_ANNOTATIONS_NON_IDEMPOTENT,
|
||||
demoDenied, errorResult, ok,
|
||||
} from '../../nest-mcp';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
|
||||
import {
|
||||
mapsSearchRequestSchema,
|
||||
@@ -20,11 +21,12 @@ import { AuthService } from '../auth/auth.service';
|
||||
import { AssignmentsService } from '../assignments/assignments.service';
|
||||
import { JourneyDomainService } from '../journey/journey-domain.service';
|
||||
import { noAccess, permissionDenied } from '../../mcp/tools/_shared';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { UnitOfWork } from '../database/unit-of-work';
|
||||
import { MapsService } from '../maps/maps.service';
|
||||
import { PlacesService } from './places.service';
|
||||
import { isDirectionsUrl } from './maps-dir.helpers';
|
||||
import { Trips } from '../../db/entities/Trips.entity';
|
||||
import type { TripsRepository } from '../../db/repositories/Trips.repository';
|
||||
|
||||
function parseId(value: string | string[]): number | null {
|
||||
const n = Number(Array.isArray(value) ? value[0] : value);
|
||||
@@ -53,7 +55,7 @@ export class PlacesMcp {
|
||||
constructor(
|
||||
private readonly places: PlacesService,
|
||||
private readonly maps: MapsService,
|
||||
private readonly db: DatabaseService,
|
||||
@InjectRepository(Trips) private readonly tripsRepo: TripsRepository,
|
||||
private readonly auth: AuthService,
|
||||
private readonly journey: JourneyDomainService,
|
||||
private readonly assignments: AssignmentsService,
|
||||
@@ -97,7 +99,7 @@ export class PlacesMcp {
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
const place = await this.places.create(String(tripId), { name, description, lat, lng, address, category_id, google_place_id, google_ftid, osm_id, amap_poi_id, notes, website, phone, image_url, price, currency, stop_type });
|
||||
this.guards.safeBroadcast(tripId, 'place:created', { place });
|
||||
@@ -142,7 +144,7 @@ export class PlacesMcp {
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
if (!(await this.assignments.dayExists(dayId, tripId))) return { content: [{ type: 'text' as const, text: 'Day not found.' }], isError: true };
|
||||
try {
|
||||
@@ -205,7 +207,7 @@ export class PlacesMcp {
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
const place = await this.places.update(String(tripId), String(placeId), { name, description, lat, lng, address, category_id, price, currency, place_time, end_time, duration_minutes, notes, website, phone, image_url, transport_mode, osm_id, google_place_id, google_ftid, amap_poi_id, stop_type, fill_percent });
|
||||
if (!place) return { content: [{ type: 'text' as const, text: 'Place not found.' }], isError: true };
|
||||
@@ -230,7 +232,7 @@ export class PlacesMcp {
|
||||
) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
// Rating is a personal vote — any trip member may cast one, place_edit not required.
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
const place = await this.places.rate(String(tripId), String(placeId), ctx.userId, rating ?? null);
|
||||
if (!place) return { content: [{ type: 'text' as const, text: 'Place not found.' }], isError: true };
|
||||
this.guards.safeBroadcast(tripId, 'place:updated', { place });
|
||||
@@ -249,7 +251,7 @@ export class PlacesMcp {
|
||||
})
|
||||
async deletePlace({ tripId, placeId }: { tripId: number; placeId: number }, ctx: McpContext) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
// Scope the id to the trip before the hook: onPlaceDeleted keys on the place
|
||||
// id alone, so a foreign id would detach that trip's journey entries even
|
||||
@@ -291,7 +293,7 @@ export class PlacesMcp {
|
||||
},
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
const places = await this.places.list(String(tripId), { search, category, tag, assignment });
|
||||
return ok({ places });
|
||||
}
|
||||
@@ -343,7 +345,7 @@ export class PlacesMcp {
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
|
||||
// Same opts the REST route builds: the enrichment pass is keyed on the calling
|
||||
@@ -374,7 +376,7 @@ export class PlacesMcp {
|
||||
})
|
||||
async importGpx(input: RoadtripGpxImport, ctx: McpContext) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(input.tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(input.tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', input.tripId, ctx.userId))) return permissionDenied();
|
||||
if (!input.importWaypoints && !input.importRoutes && !input.importTracks) return errorResult('No import types selected.');
|
||||
try {
|
||||
@@ -406,7 +408,7 @@ export class PlacesMcp {
|
||||
ctx: McpContext,
|
||||
) {
|
||||
// A read, like the REST route: seeing the trip is enough, no place_edit.
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!waypoints && !tracks && !dayRoutes) return errorResult('No export types selected.');
|
||||
const result = await this.places.exportGpx(String(tripId), { waypoints, tracks, dayRoutes });
|
||||
if (!result) return errorResult('Nothing to export.');
|
||||
@@ -425,7 +427,7 @@ export class PlacesMcp {
|
||||
})
|
||||
async bulkDeletePlaces({ tripId, placeIds }: { tripId: number; placeIds: number[] }, ctx: McpContext) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
|
||||
// Trip-scoped, and ahead of the DELETE: journey_entries.source_place_id is
|
||||
@@ -482,7 +484,7 @@ export class PlacesMcp {
|
||||
ctx: McpContext,
|
||||
) {
|
||||
if (await this.auth.isDemoUser(ctx.userId)) return demoDenied();
|
||||
if (!(await this.db.canAccessTrip(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.tripsRepo.findAccessible(tripId, ctx.userId))) return noAccess();
|
||||
if (!(await this.guards.hasTripPermission('place_edit', tripId, ctx.userId))) return permissionDenied();
|
||||
|
||||
const fields = { category_id, price, currency, transport_mode, place_time, end_time, duration_minutes, notes, website, phone, image_url, description, stop_type, fill_percent };
|
||||
@@ -504,7 +506,7 @@ export class PlacesMcp {
|
||||
})
|
||||
async tripPlacesResource(uri: URL, { tripId }: { tripId: string | string[] }, ctx: McpContext) {
|
||||
const id = parseId(tripId);
|
||||
if (id === null || !(await this.db.canAccessTrip(id, ctx.userId))) {
|
||||
if (id === null || !(await this.tripsRepo.findAccessible(id, ctx.userId))) {
|
||||
return {
|
||||
contents: [{
|
||||
uri: uri.href,
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { Places } from '../../db/entities/Places.entity';
|
||||
import { Tags } from '../../db/entities/Tags.entity';
|
||||
import { PlaceRatings } from '../../db/entities/PlaceRatings.entity';
|
||||
import { TripMembers } from '../../db/entities/TripMembers.entity';
|
||||
import { DayAssignments } from '../../db/entities/DayAssignments.entity';
|
||||
import { Trips } from '../../db/entities/Trips.entity';
|
||||
import { JourneyDomainModule } from '../journey/journey-domain.module';
|
||||
import { PlacesController } from './places.controller';
|
||||
import { PlacesService } from './places.service';
|
||||
@@ -45,7 +52,17 @@ import { MAX_PLACE_IMAGE_SIZE } from '../common/place-image-upload';
|
||||
StorageModule,
|
||||
// AccommodationsDomainModule: deleting a place takes the nights booked at it with
|
||||
// it, and the cascade behind a stay belongs to the domain that owns it.
|
||||
McpSharedModule, PermissionsModule, QueryHelpersModule, MapsModule, AuthModule, AppConfigModule, UnsplashModule, PlacePhotosModule, JourneyDomainModule, RealtimeModule, PluginGuardsModule, AssignmentsDomainModule, AccommodationsDomainModule],
|
||||
McpSharedModule, PermissionsModule, QueryHelpersModule, MapsModule, AuthModule, AppConfigModule, UnsplashModule, PlacePhotosModule, JourneyDomainModule, RealtimeModule, PluginGuardsModule, AssignmentsDomainModule, AccommodationsDomainModule,
|
||||
// Plan 3c Task 4: `PlacesService`'s and `PlacesMcp`'s own `@InjectRepository`
|
||||
// constructor params (`PlacesRepository`, `TagsRepository`,
|
||||
// `PlaceRatingsRepository`, `TripMembersRepository`,
|
||||
// `DayAssignmentsRepository`, `TripsRepository`) — registered directly on
|
||||
// THIS module (not a sibling domain module's `exports`), matching
|
||||
// `AssignmentsDomainModule`'s own `MikroOrmModule.forFeature(...)`
|
||||
// precedent: a `forFeature` registration only reaches providers declared
|
||||
// in the SAME module.
|
||||
MikroOrmModule.forFeature([Places, Tags, PlaceRatings, TripMembers, DayAssignments, Trips]),
|
||||
],
|
||||
controllers: [PlacesController],
|
||||
providers: [PlacesService, PlacesMcp, PlacesRpc],
|
||||
exports: [PlacesService],
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import path from 'node:path';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { XMLValidator } from 'fast-xml-parser';
|
||||
import { TRACK_COLORS, placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared';
|
||||
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
|
||||
@@ -9,7 +11,8 @@ import type { PlaceWithTags } from '../database/database.service';
|
||||
import { PermissionsService } from '../permissions/permissions.service';
|
||||
import { MapsService, GOOGLE_SHORT_HOSTS, isGoogleMapsHost } from '../maps/maps.service';
|
||||
import { isDirectionsUrl, parseDirectionsUrl } from './maps-dir.helpers';
|
||||
import type { Place, User } from '../../types';
|
||||
import { toRowId } from '../common/row-id';
|
||||
import type { User } from '../../types';
|
||||
import { QueryHelpersService } from '../query-helpers/query-helpers.service';
|
||||
import { ratingAggregate } from '../common/rowShape';
|
||||
import { checkSsrf, safeFetchFollow, SsrfBlockedError } from '../../utils/ssrfGuard';
|
||||
@@ -26,10 +29,20 @@ 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 { isUploadedPlaceImage } from './place-image';
|
||||
import { JourneyDomainService } from '../journey/journey-domain.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { AccommodationsService } from '../accommodations/accommodations.service';
|
||||
import { Places } from '../../db/entities/Places.entity';
|
||||
import { Tags } from '../../db/entities/Tags.entity';
|
||||
import { PlaceRatings } from '../../db/entities/PlaceRatings.entity';
|
||||
import { TripMembers } from '../../db/entities/TripMembers.entity';
|
||||
import { DayAssignments } from '../../db/entities/DayAssignments.entity';
|
||||
import type { PlacesRepository, PlaceWithCategoryRow } 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';
|
||||
import type { DayAssignmentsRepository } from '../../db/repositories/DayAssignments.repository';
|
||||
|
||||
/** Rows a place delete took down with the nights booked there, for the caller to announce. */
|
||||
export interface CancelledStays {
|
||||
@@ -64,7 +77,6 @@ import {
|
||||
type ListImportOptions,
|
||||
type ListImportResult,
|
||||
type PlaceImportResult,
|
||||
type PlaceWithCategory,
|
||||
} from './places.helpers';
|
||||
|
||||
type Trip = TripAccess;
|
||||
@@ -135,6 +147,11 @@ export class PlacesService {
|
||||
private readonly storage: StorageService,
|
||||
private readonly accommodations: AccommodationsService,
|
||||
private readonly uow: UnitOfWork,
|
||||
@InjectRepository(Places) private readonly placesRepo: PlacesRepository,
|
||||
@InjectRepository(Tags) private readonly tagsRepo: TagsRepository,
|
||||
@InjectRepository(PlaceRatings) private readonly placeRatingsRepo: PlaceRatingsRepository,
|
||||
@InjectRepository(TripMembers) private readonly tripMembersRepo: TripMembersRepository,
|
||||
@InjectRepository(DayAssignments) private readonly dayAssignmentsRepo: DayAssignmentsRepository,
|
||||
) {}
|
||||
|
||||
async verifyTripAccess(tripId: string, userId: number) {
|
||||
@@ -160,11 +177,13 @@ export class PlacesService {
|
||||
private async tagsOnTrip(tripId: string | number, tagIds: number[]): Promise<number[]> {
|
||||
const unique = [...new Set(tagIds)];
|
||||
if (unique.length === 0) return [];
|
||||
const roster = await this.dbs.rosterUserIds(tripId);
|
||||
const owned = this.dbs.all<{ id: number; user_id: number }>(
|
||||
`SELECT id, user_id FROM tags WHERE id IN (${unique.map(() => '?').join(',')})`,
|
||||
...unique,
|
||||
);
|
||||
// PL1 — `TripMembersRepository.rosterUserIds`, injected directly (not
|
||||
// through `DatabaseService`), matching `AssignmentsService`'s AS28
|
||||
// precedent: each domain converts its own `DatabaseService`-delegated
|
||||
// callers as it lands.
|
||||
const roster = await this.tripMembersRepo.rosterUserIds(tripId);
|
||||
// PL2 — `SELECT id, user_id FROM tags WHERE id IN (${…})`.
|
||||
const owned = await this.tagsRepo.findByIds(unique);
|
||||
return owned.filter(t => roster.has(t.user_id)).map(t => t.id);
|
||||
}
|
||||
|
||||
@@ -172,6 +191,37 @@ export class PlacesService {
|
||||
this.realtime.broadcast(tripId, event, payload, socketId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a custom place-image object once nothing references it any more
|
||||
* (moved in from `place-image.ts`'s free function `reclaimPlaceImage`,
|
||||
* option (a) — see `place-image.ts`'s own docstring for why that free
|
||||
* function stays, unconverted, for `collections.service.ts`'s own call
|
||||
* sites). A trip place and a collection saved-place can share the same
|
||||
* uploaded file — save-to-collection and copy-to-trip copy `image_url` by
|
||||
* reference — so this ref-counts across both tables before deleting.
|
||||
*
|
||||
* PI1 (`SELECT 1 FROM places WHERE image_url = ? LIMIT 1`) through
|
||||
* `PlacesRepository.existsByImageUrl`; PI2 (`SELECT 1 FROM collection_places
|
||||
* WHERE image_url = ? LIMIT 1`, Plan 3h's table) stays one raw statement,
|
||||
* evaluated only when PI1 is false — reproducing the legacy `UNION ALL …
|
||||
* LIMIT 1`'s short-circuit by evaluation order, the same pattern
|
||||
* `PlacePhotoCacheService.isReferenced` uses for the same two tables
|
||||
* (Plan 3c Task 1's PP6 ruling). `path.basename()` keeps the storage name
|
||||
* confined to the 'places' category. Best-effort: never throws (central
|
||||
* key validation rejects a hostile stored value; the catch swallows it
|
||||
* exactly like the legacy unlink guard).
|
||||
*/
|
||||
private async reclaimPlaceImage(url: string | null | undefined): Promise<void> {
|
||||
if (!isUploadedPlaceImage(url)) return;
|
||||
if (await this.placesRepo.existsByImageUrl(url)) return;
|
||||
// Plan 3h: collection_places is not yet repository-backed.
|
||||
const referenced = this.dbs.get('SELECT 1 FROM collection_places WHERE image_url = ? LIMIT 1', url);
|
||||
if (referenced) return;
|
||||
await this.storage.delete('places', path.basename(url)).catch(() => {
|
||||
/* best-effort */
|
||||
});
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// List places
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -180,43 +230,17 @@ export class PlacesService {
|
||||
tripId: string,
|
||||
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
|
||||
FROM places p
|
||||
LEFT JOIN categories c ON p.category_id = c.id
|
||||
WHERE p.trip_id = ?
|
||||
`;
|
||||
const params: (string | number)[] = [tripId];
|
||||
|
||||
if (filters.search) {
|
||||
// ESCAPE so a `%` or `_` the user typed matches literally instead of
|
||||
// acting as a LIKE wildcard (a bare '%' used to return the whole trip).
|
||||
query += " AND (p.name LIKE ? ESCAPE '\\' OR p.address LIKE ? ESCAPE '\\' OR p.description LIKE ? ESCAPE '\\')";
|
||||
const searchParam = `%${escapeLikePattern(filters.search)}%`;
|
||||
params.push(searchParam, searchParam, searchParam);
|
||||
}
|
||||
|
||||
if (filters.category) {
|
||||
query += ' AND p.category_id = ?';
|
||||
params.push(filters.category);
|
||||
}
|
||||
|
||||
if (filters.tag) {
|
||||
query += ' AND p.id IN (SELECT place_id FROM place_tags WHERE tag_id = ?)';
|
||||
params.push(filters.tag);
|
||||
}
|
||||
|
||||
if (filters.assignment === 'unassigned') {
|
||||
query += ` AND p.id NOT IN (SELECT da.place_id FROM day_assignments da JOIN days d ON da.day_id = d.id WHERE d.trip_id = ?)`;
|
||||
params.push(tripId);
|
||||
} else if (filters.assignment === 'assigned') {
|
||||
query += ` AND p.id IN (SELECT da.place_id FROM day_assignments da JOIN days d ON da.day_id = d.id WHERE d.trip_id = ?)`;
|
||||
params.push(tripId);
|
||||
}
|
||||
|
||||
query += ' ORDER BY p.created_at DESC';
|
||||
|
||||
const places = this.dbs.prepare(query).all(...params) as PlaceWithCategory[];
|
||||
// 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
|
||||
// already-built pattern verbatim (PL3).
|
||||
const searchPattern = filters.search ? `%${escapeLikePattern(filters.search)}%` : undefined;
|
||||
const places: PlaceWithCategoryRow[] = await this.placesRepo.listForTrip(tripId, {
|
||||
searchPattern,
|
||||
category: filters.category,
|
||||
tag: filters.tag,
|
||||
assignment: filters.assignment,
|
||||
});
|
||||
|
||||
const placeIds = places.map(p => p.id);
|
||||
const tagsByPlaceId = await this.queryHelpers.loadTagsByPlaceIds(placeIds);
|
||||
@@ -248,42 +272,51 @@ export class PlacesService {
|
||||
transport_mode, route_geometry, route_color, stop_type, fill_percent, tags = [],
|
||||
} = body;
|
||||
|
||||
const result = this.dbs.run(`
|
||||
INSERT INTO places (trip_id, name, description, lat, lng, address, category_id, price, currency,
|
||||
place_time, end_time,
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, osm_id, amap_poi_id, website, phone, transport_mode,
|
||||
route_geometry, route_color, stop_type, fill_percent)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
// lat/lng/price/duration_minutes use an explicit undefined check, not `||`:
|
||||
// 0 is a legitimate value for all four (Null Island, a free entry, a
|
||||
// drive-by stop) and the falsy coercion silently threw it away.
|
||||
//
|
||||
// The hour stays the default for a place created without one, which is what it
|
||||
// has always been and what the column itself declares. The road trip rail reads
|
||||
// the value as a stay, and a stop added from the corridor search brings its own
|
||||
// figure — ten minutes for fuel, twenty for a rest area — so the kinds that would
|
||||
// be misread as an hour never take the default in the first place.
|
||||
tripId, name, description || null, lat ?? null, lng ?? null, address || null,
|
||||
category_id || null, price ?? null, currency || null,
|
||||
place_time || null, end_time || null, duration_minutes ?? 60, notes || null, image_url || null,
|
||||
google_place_id || null, google_ftid || null, osm_id || null, amap_poi_id || null, website || null, phone || null, transport_mode || 'walking',
|
||||
route_geometry || null, route_color || null, stop_type || null,
|
||||
// `?? null` rather than `|| null`, the same reason lat/lng have it: the column is a
|
||||
// percentage and the falsy check would be a silent floor.
|
||||
fill_percent ?? null,
|
||||
);
|
||||
|
||||
const placeId = result.lastInsertRowid;
|
||||
// PL4 — the 25-column INSERT. lat/lng/price/duration_minutes/fill_percent
|
||||
// use an explicit undefined check, not `||`: 0 is a legitimate value for
|
||||
// all five (Null Island, a free entry, a drive-by stop, an empty tank)
|
||||
// and the falsy coercion silently threw it away.
|
||||
//
|
||||
// The hour stays the default for a place created without one, which is what it
|
||||
// has always been and what the column itself declares. The road trip rail reads
|
||||
// the value as a stay, and a stop added from the corridor search brings its own
|
||||
// figure — ten minutes for fuel, twenty for a rest area — so the kinds that would
|
||||
// be misread as an hour never take the default in the first place.
|
||||
const placeId = await this.placesRepo.insertPlace({
|
||||
trip_id: Number(tripId),
|
||||
name,
|
||||
description: description || null,
|
||||
lat: lat ?? null,
|
||||
lng: lng ?? null,
|
||||
address: address || null,
|
||||
category_id: category_id || null,
|
||||
price: price ?? null,
|
||||
currency: currency || null,
|
||||
place_time: place_time || null,
|
||||
end_time: end_time || null,
|
||||
duration_minutes: duration_minutes ?? 60,
|
||||
notes: notes || null,
|
||||
image_url: image_url || null,
|
||||
google_place_id: google_place_id || null,
|
||||
google_ftid: google_ftid || null,
|
||||
osm_id: osm_id || null,
|
||||
amap_poi_id: amap_poi_id || null,
|
||||
website: website || null,
|
||||
phone: phone || null,
|
||||
transport_mode: transport_mode || 'walking',
|
||||
route_geometry: route_geometry || null,
|
||||
route_color: route_color || null,
|
||||
stop_type: stop_type || null,
|
||||
fill_percent: fill_percent ?? null,
|
||||
});
|
||||
|
||||
// PL5 — `INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)`.
|
||||
if (tags && tags.length > 0) {
|
||||
const insertTag = this.dbs.prepare('INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)');
|
||||
for (const tagId of await this.tagsOnTrip(tripId, tags)) {
|
||||
insertTag.run(placeId, tagId);
|
||||
}
|
||||
await this.tagsRepo.insertIgnore(placeId, await this.tagsOnTrip(tripId, tags));
|
||||
}
|
||||
|
||||
return (await this.dbs.getPlaceWithTags(Number(placeId)))!;
|
||||
// PL6 — `findWithTagsAndRatings` replaces the `getPlaceWithTags` delegation.
|
||||
return (await this.placesRepo.findWithTagsAndRatings(placeId))!;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -291,9 +324,16 @@ export class PlacesService {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
async get(tripId: string, placeId: string) {
|
||||
const placeCheck = this.dbs.get('SELECT id FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
|
||||
if (!placeCheck) return null;
|
||||
return await this.dbs.getPlaceWithTags(placeId);
|
||||
// PL7 — the existence gate. `toRowId` first (Task 3 review H1, absorbed
|
||||
// here): a non-canonical id must resolve to "not found" here, the same
|
||||
// answer `findWithTagsAndRatings` below would give it anyway — this
|
||||
// just skips the trip-scoped existence read for an id that can never
|
||||
// exist.
|
||||
const id = toRowId(placeId);
|
||||
if (id === null) return null;
|
||||
if (!(await this.placesRepo.existsInTrip(id, Number(tripId)))) return null;
|
||||
// PL8 — `findWithTagsAndRatings` replaces the `getPlaceWithTags` delegation.
|
||||
return await this.placesRepo.findWithTagsAndRatings(id);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -307,7 +347,7 @@ export class PlacesService {
|
||||
ifMatch?: string,
|
||||
): Promise<PlaceWithTags | UpdateConflict | null> {
|
||||
const { result, reclaim } = await this.applyUpdate(tripId, placeId, body, ifMatch);
|
||||
if (reclaim !== undefined) await reclaimPlaceImage(this.storage, reclaim);
|
||||
if (reclaim !== undefined) await this.reclaimPlaceImage(reclaim);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -322,14 +362,24 @@ export class PlacesService {
|
||||
body: PlaceUpdateInput,
|
||||
ifMatch?: string,
|
||||
): Promise<{ result: PlaceWithTags | UpdateConflict | null; reclaim?: string | null }> {
|
||||
const existingPlace = this.dbs.get<Place>('SELECT * FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
|
||||
// PL9 — the pre-image read every `!== undefined` fallback below needs.
|
||||
// `toRowId` first (Task 3 review H1, absorbed here): every write in
|
||||
// this method (`updatePlace`, `tagsRepo.deleteForPlace`/`insertIgnore`)
|
||||
// uses this SAME validated `id` — a non-canonical `placeId` (`"3 "`,
|
||||
// `"3.0"`) must resolve to "not found" HERE, not pass a loose SQLite
|
||||
// affinity match and then hit a downstream write keyed on a `toRowId(x)!`
|
||||
// that returns `null`.
|
||||
const id = toRowId(placeId);
|
||||
if (id === null) return { result: null };
|
||||
const existingPlace = await this.placesRepo.findInTrip(id, Number(tripId));
|
||||
if (!existingPlace) return { result: null };
|
||||
|
||||
// Optimistic concurrency (#1135): when the caller sent the version it based its
|
||||
// edit on and the row has moved on since, reject instead of clobbering. Absent
|
||||
// token => unconditional update (back-compat — old clients keep last-write-wins).
|
||||
if (ifMatch !== undefined && existingPlace.updated_at != null && String(existingPlace.updated_at) !== ifMatch) {
|
||||
return { result: { conflict: true, server: await this.dbs.getPlaceWithTags(placeId) } };
|
||||
// PL10 — the `{conflict: true, server}` body when `If-Match` loses (#1135).
|
||||
return { result: { conflict: true, server: await this.placesRepo.findWithTagsAndRatings(id) } };
|
||||
}
|
||||
|
||||
const {
|
||||
@@ -339,75 +389,58 @@ export class PlacesService {
|
||||
transport_mode, route_color, stop_type, fill_percent, tags,
|
||||
} = body;
|
||||
|
||||
this.dbs.run(`
|
||||
UPDATE places SET
|
||||
name = COALESCE(?, name),
|
||||
description = ?,
|
||||
lat = ?,
|
||||
lng = ?,
|
||||
address = ?,
|
||||
category_id = ?,
|
||||
price = ?,
|
||||
currency = COALESCE(?, currency),
|
||||
place_time = ?,
|
||||
end_time = ?,
|
||||
duration_minutes = ?,
|
||||
notes = ?,
|
||||
image_url = ?,
|
||||
google_place_id = ?,
|
||||
google_ftid = ?,
|
||||
osm_id = ?,
|
||||
amap_poi_id = ?,
|
||||
website = ?,
|
||||
phone = ?,
|
||||
transport_mode = COALESCE(?, transport_mode),
|
||||
route_color = ?,
|
||||
stop_type = ?,
|
||||
fill_percent = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
`,
|
||||
name || null,
|
||||
description !== undefined ? description : existingPlace.description,
|
||||
lat !== undefined ? lat : existingPlace.lat,
|
||||
lng !== undefined ? lng : existingPlace.lng,
|
||||
address !== undefined ? address : existingPlace.address,
|
||||
category_id !== undefined ? category_id : existingPlace.category_id,
|
||||
price !== undefined ? price : existingPlace.price,
|
||||
currency || null,
|
||||
place_time !== undefined ? place_time : existingPlace.place_time,
|
||||
end_time !== undefined ? end_time : existingPlace.end_time,
|
||||
// PL11 — ONE `nativeUpdate` with a typed full partial (the ruling): the
|
||||
// SQL `COALESCE(?, col)` keep-if-null semantics (name/currency/
|
||||
// transport_mode, all three bound `x || null`) and the twenty
|
||||
// `!== undefined ? x : existing.x` pre-image fallbacks are resolved to
|
||||
// their final value HERE, in the service — the repository writes
|
||||
// exactly what it is handed.
|
||||
await this.placesRepo.updatePlace(id, {
|
||||
// COALESCE(?, name): the bound value is `name || null` (falsy clears
|
||||
// to null), and `?? existingPlace.name` is the COALESCE half itself —
|
||||
// a `nativeUpdate` writes a literal value, it does not evaluate SQL
|
||||
// COALESCE, so the keep-if-null fold has to be resolved here.
|
||||
name: (name || null) ?? existingPlace.name,
|
||||
description: description !== undefined ? description : existingPlace.description,
|
||||
lat: lat !== undefined ? lat : existingPlace.lat,
|
||||
lng: lng !== undefined ? lng : existingPlace.lng,
|
||||
address: address !== undefined ? address : existingPlace.address,
|
||||
category_id: category_id !== undefined ? category_id : existingPlace.category_id,
|
||||
price: price !== undefined ? price : existingPlace.price,
|
||||
// COALESCE(?, currency) — same fold as `name` above.
|
||||
currency: (currency || null) ?? existingPlace.currency,
|
||||
place_time: place_time !== undefined ? place_time : existingPlace.place_time,
|
||||
end_time: end_time !== undefined ? end_time : existingPlace.end_time,
|
||||
// Not COALESCE, like its neighbours: the contract says an explicit null
|
||||
// clears the planned stay length, and COALESCE made null and absent the
|
||||
// same thing, so the field advertised a reset it never performed. 0 keeps
|
||||
// working, which a `|| null` bind would have swallowed.
|
||||
duration_minutes !== undefined ? duration_minutes : existingPlace.duration_minutes,
|
||||
notes !== undefined ? notes : existingPlace.notes,
|
||||
image_url !== undefined ? image_url : existingPlace.image_url,
|
||||
google_place_id !== undefined ? google_place_id : existingPlace.google_place_id,
|
||||
google_ftid !== undefined ? google_ftid : existingPlace.google_ftid,
|
||||
osm_id !== undefined ? osm_id : existingPlace.osm_id,
|
||||
amap_poi_id !== undefined ? amap_poi_id : existingPlace.amap_poi_id,
|
||||
website !== undefined ? website : existingPlace.website,
|
||||
phone !== undefined ? phone : existingPlace.phone,
|
||||
transport_mode || null,
|
||||
duration_minutes: duration_minutes !== undefined ? duration_minutes : existingPlace.duration_minutes,
|
||||
notes: notes !== undefined ? notes : existingPlace.notes,
|
||||
image_url: image_url !== undefined ? image_url : existingPlace.image_url,
|
||||
google_place_id: google_place_id !== undefined ? google_place_id : existingPlace.google_place_id,
|
||||
google_ftid: google_ftid !== undefined ? google_ftid : existingPlace.google_ftid,
|
||||
osm_id: osm_id !== undefined ? osm_id : existingPlace.osm_id,
|
||||
amap_poi_id: amap_poi_id !== undefined ? amap_poi_id : existingPlace.amap_poi_id,
|
||||
website: website !== undefined ? website : existingPlace.website,
|
||||
phone: phone !== undefined ? phone : existingPlace.phone,
|
||||
// COALESCE(?, transport_mode) — same fold as `name`/`currency` above.
|
||||
transport_mode: (transport_mode || null) ?? existingPlace.transport_mode,
|
||||
// Deliberately not COALESCE: an explicit null is how the picker resets a
|
||||
// track back to its category colour (#776).
|
||||
route_color !== undefined ? route_color : existingPlace.route_color,
|
||||
route_color: route_color !== undefined ? route_color : existingPlace.route_color,
|
||||
// Same shape: an explicit null is how a fuel stop becomes an ordinary place again.
|
||||
stop_type !== undefined ? stop_type : existingPlace.stop_type,
|
||||
stop_type: stop_type !== undefined ? stop_type : existingPlace.stop_type,
|
||||
// And how a stop that had its own fill amount goes back to following the setting.
|
||||
fill_percent !== undefined ? fill_percent : existingPlace.fill_percent,
|
||||
placeId,
|
||||
);
|
||||
fill_percent: fill_percent !== undefined ? fill_percent : existingPlace.fill_percent,
|
||||
});
|
||||
|
||||
if (tags !== undefined) {
|
||||
this.dbs.run('DELETE FROM place_tags WHERE place_id = ?', placeId);
|
||||
// PL12 — `DELETE FROM place_tags WHERE place_id = ?`.
|
||||
await this.tagsRepo.deleteForPlace(id);
|
||||
if (tags.length > 0) {
|
||||
const insertTag = this.dbs.prepare('INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)');
|
||||
for (const tagId of await this.tagsOnTrip(tripId, tags)) {
|
||||
insertTag.run(placeId, tagId);
|
||||
}
|
||||
// PL13 — `INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)`.
|
||||
await this.tagsRepo.insertIgnore(id, await this.tagsOnTrip(tripId, tags));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -418,7 +451,8 @@ export class PlacesService {
|
||||
? existingPlace.image_url
|
||||
: undefined;
|
||||
|
||||
return { result: await this.dbs.getPlaceWithTags(placeId), reclaim };
|
||||
// PL14 — `findWithTagsAndRatings` replaces the `getPlaceWithTags` delegation.
|
||||
return { result: await this.placesRepo.findWithTagsAndRatings(id), reclaim };
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -432,6 +466,7 @@ export class PlacesService {
|
||||
*/
|
||||
async linkedExpenseIds(tripId: string | number, placeIds: Array<string | number>): Promise<number[]> {
|
||||
if (placeIds.length === 0) return [];
|
||||
// PL15 — `budget_items` is Plan 3e's table; stays raw.
|
||||
const rows = this.dbs.all<{ id: number }>(
|
||||
`SELECT id FROM budget_items WHERE trip_id = ? AND place_id IN (${placeIds.map(() => '?').join(',')})`,
|
||||
tripId, ...placeIds,
|
||||
@@ -452,6 +487,9 @@ export class PlacesService {
|
||||
* Runs inside the caller's transaction.
|
||||
*/
|
||||
private async cancelStaysAt(tripId: string | number, placeId: string | number, into: CancelledStays): Promise<void> {
|
||||
// PL16 — `day_accommodations` is Plan 3d's table; stays raw. Runs inside
|
||||
// the caller's own transaction (R4 — Plan 3d must not re-open or
|
||||
// re-scope this transaction when it converts `accommodations`).
|
||||
const stays = this.dbs.all<{ id: number }>(
|
||||
'SELECT id FROM day_accommodations WHERE trip_id = ? AND place_id = ?', tripId, placeId,
|
||||
);
|
||||
@@ -467,39 +505,47 @@ export class PlacesService {
|
||||
}
|
||||
|
||||
async remove(tripId: string, placeId: string): Promise<{ deleted: boolean; cancelled: CancelledStays }> {
|
||||
const place = this.dbs.get<{ google_place_id: string | null; image_url: string | null }>(
|
||||
'SELECT google_place_id, image_url FROM places WHERE id = ? AND trip_id = ?', placeId, tripId,
|
||||
);
|
||||
const cancelled = noCancelledStays();
|
||||
// `toRowId` first (Task 3 review H1, absorbed here): the write below
|
||||
// (`deleteById`) must use the SAME id this gate's existence read used.
|
||||
const id = toRowId(placeId);
|
||||
if (id === null) return { deleted: false, cancelled };
|
||||
// PL17 — the reclaim-candidate projection, read before the delete.
|
||||
const place = await this.placesRepo.reclaimInputs(id, Number(tripId));
|
||||
if (!place) return { deleted: false, cancelled };
|
||||
// The linked expense goes with the place, the same way a booking takes its
|
||||
// expense with it (#1298). One transaction, so a place can never survive
|
||||
// half-detached from its money.
|
||||
await this.uow.transactional(async () => {
|
||||
await this.cancelStaysAt(tripId, placeId, cancelled);
|
||||
this.dbs.run('DELETE FROM budget_items WHERE trip_id = ? AND place_id = ?', tripId, placeId);
|
||||
this.dbs.run('DELETE FROM places WHERE id = ?', placeId);
|
||||
await this.cancelStaysAt(tripId, id, cancelled);
|
||||
// PL18 — `budget_items` is Plan 3e's table; stays raw.
|
||||
this.dbs.run('DELETE FROM budget_items WHERE trip_id = ? AND place_id = ?', tripId, id);
|
||||
// PL19 — `DELETE FROM places WHERE id = ?`.
|
||||
await this.placesRepo.deleteById(id);
|
||||
});
|
||||
await reclaimPhotoCache(this.photoCache, place.google_place_id, place.image_url);
|
||||
await reclaimPlaceImage(this.storage, place.image_url);
|
||||
await this.reclaimPlaceImage(place.image_url);
|
||||
return { deleted: true, cancelled };
|
||||
}
|
||||
|
||||
async removeMany(tripId: string, ids: number[]): Promise<{ deleted: number[]; cancelled: CancelledStays }> {
|
||||
const cancelled = noCancelledStays();
|
||||
if (ids.length === 0) return { deleted: [], cancelled };
|
||||
const selectStmt = this.dbs.prepare('SELECT google_place_id, image_url FROM places WHERE id = ? AND trip_id = ?');
|
||||
const deleteStmt = this.dbs.prepare('DELETE FROM places WHERE id = ?');
|
||||
const deleteExpenseStmt = this.dbs.prepare('DELETE FROM budget_items WHERE trip_id = ? AND place_id = ?');
|
||||
const deleted: number[] = [];
|
||||
const reclaimable: { google_place_id: string | null; image_url: string | null }[] = [];
|
||||
await this.uow.transactional(async () => {
|
||||
for (const id of ids) {
|
||||
const row = selectStmt.get(id, tripId) as { google_place_id: string | null; image_url: string | null } | undefined;
|
||||
// PL20 — the reclaim-candidate projection, read before the delete
|
||||
// (same statement as PL17). `id` is already a genuine `number`
|
||||
// here (a body-validated array, not a route string) — no `toRowId`
|
||||
// gate needed, the H1 class of bug is a route-string problem.
|
||||
const row = await this.placesRepo.reclaimInputs(id, Number(tripId));
|
||||
if (!row) continue;
|
||||
await this.cancelStaysAt(tripId, id, cancelled);
|
||||
deleteExpenseStmt.run(tripId, id);
|
||||
deleteStmt.run(id);
|
||||
// PL22 — `budget_items` is Plan 3e's table; stays raw.
|
||||
this.dbs.run('DELETE FROM budget_items WHERE trip_id = ? AND place_id = ?', tripId, id);
|
||||
// PL21 — `DELETE FROM places WHERE id = ?`.
|
||||
await this.placesRepo.deleteById(id);
|
||||
deleted.push(id);
|
||||
reclaimable.push(row);
|
||||
}
|
||||
@@ -507,7 +553,7 @@ export class PlacesService {
|
||||
// Reclaim after the transaction commits so isReferenced() sees the final place set.
|
||||
for (const row of reclaimable) {
|
||||
await reclaimPhotoCache(this.photoCache, row.google_place_id, row.image_url);
|
||||
await reclaimPlaceImage(this.storage, row.image_url);
|
||||
await this.reclaimPlaceImage(row.image_url);
|
||||
}
|
||||
return { deleted, cancelled };
|
||||
}
|
||||
@@ -519,13 +565,8 @@ export class PlacesService {
|
||||
* that trip's journey entries even though the delete itself refuses it.
|
||||
*/
|
||||
async scopedIds(tripId: string, ids: number[]): Promise<number[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const placeholders = ids.map(() => '?').join(',');
|
||||
const rows = this.dbs.all<{ id: number }>(
|
||||
`SELECT id FROM places WHERE trip_id = ? AND id IN (${placeholders})`, tripId, ...ids,
|
||||
);
|
||||
const owned = new Set(rows.map((r) => r.id));
|
||||
return ids.filter((id) => owned.has(id));
|
||||
// PL23 — input-order preservation lives in the repository method now.
|
||||
return this.placesRepo.scopedIds(tripId, ids);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -553,7 +594,7 @@ export class PlacesService {
|
||||
});
|
||||
// Settle reclaims after the transaction commits, so the refcount sees the
|
||||
// final image_url state.
|
||||
for (const reclaim of reclaims) await reclaimPlaceImage(this.storage, reclaim);
|
||||
for (const reclaim of reclaims) await this.reclaimPlaceImage(reclaim);
|
||||
return updated;
|
||||
}
|
||||
|
||||
@@ -630,29 +671,18 @@ export class PlacesService {
|
||||
for (const strategy of placeMatchStrategies(place)) {
|
||||
let hit: { id: number; google_ftid: string | null } | undefined;
|
||||
if (strategy.by === 'externalId') {
|
||||
hit = this.dbs.get<{ id: number; google_ftid: string | null }>(`
|
||||
SELECT id, google_ftid FROM places
|
||||
WHERE trip_id = ? AND (google_place_id = ? OR google_ftid = ? OR osm_id = ? OR amap_poi_id = ?)
|
||||
ORDER BY id ASC
|
||||
LIMIT 1
|
||||
`, tripId, strategy.id, strategy.id, strategy.id, strategy.id);
|
||||
// PL25 — the same candidate id bound in all four positions.
|
||||
hit = await this.placesRepo.findDuplicateByExternalId(tripId, strategy.id);
|
||||
} else if (strategy.by === 'name') {
|
||||
hit = this.dbs.get<{ id: number; google_ftid: string | null }>(`
|
||||
SELECT id, google_ftid FROM places
|
||||
WHERE trip_id = ? AND lower(trim(name)) = ?
|
||||
ORDER BY id ASC
|
||||
LIMIT 1
|
||||
`, tripId, strategy.name);
|
||||
// PL26 — `strategy.name` is already `.trim().toLowerCase()`'d in JS
|
||||
// (`normalizePlaceName`, `@trek/shared/place/place-match.ts`,
|
||||
// Unicode-aware) — the documented legacy disagreement with SQLite's
|
||||
// ASCII-only `lower()` (rule 18's exception clause: the legacy
|
||||
// statement itself received a JS-lowered value).
|
||||
hit = await this.placesRepo.findDuplicateByName(tripId, strategy.name);
|
||||
} else {
|
||||
hit = this.dbs.get<{ id: number; google_ftid: string | null }>(`
|
||||
SELECT id, google_ftid FROM places
|
||||
WHERE trip_id = ?
|
||||
AND lat IS NOT NULL AND lng IS NOT NULL
|
||||
AND abs(lat - ?) <= ?
|
||||
AND abs(lng - ?) <= ?
|
||||
ORDER BY id ASC
|
||||
LIMIT 1
|
||||
`, tripId, strategy.lat, strategy.tolerance, strategy.lng, strategy.tolerance);
|
||||
// PL27 — `abs(lat - ?) <= ? AND abs(lng - ?) <= ?` through the Task 0b `absDifference` helper.
|
||||
hit = await this.placesRepo.findDuplicateByCoords(tripId, strategy.lat, strategy.lng, strategy.tolerance);
|
||||
}
|
||||
if (hit) return hit;
|
||||
}
|
||||
@@ -679,29 +709,21 @@ export class PlacesService {
|
||||
* than handing over a file that imports as nothing on the other end.
|
||||
*/
|
||||
async exportGpx(tripId: string, opts: GpxExportOptions = {}): Promise<{ gpx: string; filename: string } | null> {
|
||||
// PL28 — `SELECT title FROM trips WHERE id = ?`. `trips` is outside this
|
||||
// task's owned repositories (Plan 3c Task 7's domain) and
|
||||
// `Trips.repository.ts` has a live concurrent editor in this tree this
|
||||
// session (Task 6, adding its own byte-identical `getTitle` for a
|
||||
// different caller) — stays raw here rather than risk a duplicate-method
|
||||
// collision; once Task 6 lands, this call can reuse its `getTitle`.
|
||||
const trip = this.dbs.get<{ title: string }>('SELECT title FROM trips WHERE id = ?', tripId);
|
||||
if (!trip) return null;
|
||||
|
||||
const places = this.dbs.all<GpxExportPlace>(`
|
||||
SELECT p.name, p.description, p.address, p.lat, p.lng, p.route_geometry, c.name AS category
|
||||
FROM places p
|
||||
LEFT JOIN categories c ON c.id = p.category_id
|
||||
WHERE p.trip_id = ?
|
||||
ORDER BY p.id
|
||||
`, tripId);
|
||||
// PL29 — the waypoint projection.
|
||||
const places = await this.placesRepo.listForGpx(tripId) as GpxExportPlace[];
|
||||
|
||||
// One row per stop, ordered the way the day plan draws it, then folded into days.
|
||||
const stops = this.dbs.all<{
|
||||
day_number: number; date: string | null; title: string | null;
|
||||
name: string; lat: number; lng: number;
|
||||
}>(`
|
||||
SELECT d.day_number, d.date, d.title, p.name, p.lat, p.lng
|
||||
FROM days d
|
||||
JOIN day_assignments da ON da.day_id = d.id
|
||||
JOIN places p ON p.id = da.place_id
|
||||
WHERE d.trip_id = ? AND p.lat IS NOT NULL AND p.lng IS NOT NULL
|
||||
ORDER BY d.day_number, da.order_index
|
||||
`, tripId);
|
||||
// One row per stop, ordered the way the day plan draws it, then folded
|
||||
// into days. PL30 — `DayAssignmentsRepository.listItineraryForGpx`.
|
||||
const stops = await this.dayAssignmentsRepo.listItineraryForGpx(tripId);
|
||||
|
||||
const days = new Map<number, GpxExportDay>();
|
||||
for (const stop of stops) {
|
||||
@@ -1562,7 +1584,12 @@ export class PlacesService {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
async searchImage(tripId: string, placeId: string, userId: number) {
|
||||
const place = this.dbs.get<Place>('SELECT * FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
|
||||
// `toRowId` first (Task 3 review H1, absorbed here) — same gate shape
|
||||
// as every other place-id route in this service.
|
||||
const id = toRowId(placeId);
|
||||
if (id === null) return { error: 'Place not found', status: 404 };
|
||||
// PL48 — same statement as PL9 (`applyUpdate`'s pre-image read).
|
||||
const place = await this.placesRepo.findInTrip(id, Number(tripId));
|
||||
if (!place) return { error: 'Place not found', status: 404 };
|
||||
|
||||
return this.unsplash.searchUnsplashPhotos(place.name + (place.address ? ' ' + place.address : ''), 5, await this.unsplash.getUnsplashKey(userId));
|
||||
@@ -1579,17 +1606,23 @@ export class PlacesService {
|
||||
* place (with the new aggregate) or null when the place isn't in the trip.
|
||||
*/
|
||||
async rate(tripId: string, placeId: string, userId: number, rating: number | null): Promise<PlaceWithTags | null> {
|
||||
const place = this.dbs.get('SELECT id FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
|
||||
if (!place) return null;
|
||||
// `toRowId` first (Task 3 review H1, absorbed here): PL50/PL51 write
|
||||
// with this SAME id — a non-canonical `placeId` must resolve to "not
|
||||
// found" here rather than pass a loose affinity match and then write a
|
||||
// rating against an id `place_ratings`'s own FK never actually named.
|
||||
const id = toRowId(placeId);
|
||||
if (id === null) return null;
|
||||
// PL49 — the existence gate.
|
||||
if (!(await this.placesRepo.existsInTrip(id, Number(tripId)))) return null;
|
||||
if (rating === null) {
|
||||
this.dbs.run('DELETE FROM place_ratings WHERE place_id = ? AND user_id = ?', placeId, userId);
|
||||
// PL50 — `DELETE FROM place_ratings WHERE place_id = ? AND user_id = ?`.
|
||||
await this.placeRatingsRepo.deleteRating(id, userId);
|
||||
} else {
|
||||
this.dbs.run(`
|
||||
INSERT INTO place_ratings (place_id, user_id, rating) VALUES (?, ?, ?)
|
||||
ON CONFLICT(place_id, user_id) DO UPDATE SET rating = excluded.rating
|
||||
`, placeId, userId, rating);
|
||||
// PL51 — the only explicit `ON CONFLICT … DO UPDATE` in the cluster; never touches `places.updated_at`.
|
||||
await this.placeRatingsRepo.upsertRating(id, userId, rating);
|
||||
}
|
||||
return await this.dbs.getPlaceWithTags(placeId);
|
||||
// PL52 — `findWithTagsAndRatings` replaces the `getPlaceWithTags` delegation.
|
||||
return await this.placesRepo.findWithTagsAndRatings(id);
|
||||
}
|
||||
|
||||
// Journey hooks — non-fatal, mirroring the route's try/catch wrappers.
|
||||
|
||||
Reference in New Issue
Block a user