From f0e4c9a33d9501b1a9b6968f7360804d08ca6722 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 13 Sep 2026 22:45:23 +0200 Subject: [PATCH] fix: five small defects the audit turned up in the release An explicit null on a place's duration_minutes advertised a reset it never performed: the column sat behind COALESCE, so null and absent were the same thing and the day plan kept budgeting the old stay length. It clears now, and 0 still writes 0. update_trip_file and link_trip_file never learned the budget_item_id target the REST routes gained, so a receipt could not be attached to an expense from MCP. Added to both, along with the cross-trip check that goes with it: adding the field without it would have opened the hole the same change closed for REST. The roadtrip preferences PUT dropped the X-Socket-Id it was given, so the tab that saved a setting received its own change back and spent a second store commit and a route recompute on an answer it already had. Its two siblings on the same feature forward it. A shared journey did not carry the three "this journey does not use that field" switches, so the phone card read undefined, decided everything was switched on, and showed mood and weather chips on a journey whose owner had turned them off. An entry's country flag followed the pin when the entry was edited and not when the trip sync moved the place underneath it, so a stop dragged across a border kept the old flag with nothing to ever correct it. Also: dawarich.helpers.ts held a raw NUL byte as a hash separator, which made every search tool treat the file as binary and skip it. Same character, written as an escape. --- server/src/nest/files/files.mcp.ts | 7 ++- .../src/nest/integrations/dawarich.helpers.ts | Bin 19897 -> 19902 bytes .../nest/journey/journey-domain.service.ts | 20 +++++--- .../src/nest/journey/journey-share.service.ts | 7 +++ server/src/nest/places/places.service.ts | 10 ++-- .../roadtrip-preferences.controller.ts | 6 +-- .../roadtrip/roadtrip-preferences.service.ts | 8 ++- .../unit/nest/journey-domain.service.test.ts | 46 ++++++++++++++++++ .../unit/nest/journey-share.service.test.ts | 18 +++++++ server/tests/unit/nest/places.service.test.ts | 15 +++++- 10 files changed, 118 insertions(+), 19 deletions(-) diff --git a/server/src/nest/files/files.mcp.ts b/server/src/nest/files/files.mcp.ts index 27399cda3..3858c2485 100644 --- a/server/src/nest/files/files.mcp.ts +++ b/server/src/nest/files/files.mcp.ts @@ -112,6 +112,7 @@ export class FilesMcp { description: fileUpdateRequestSchema.shape.description.describe('Free-text description, or an empty string to clear it'), place_id: fileUpdateRequestSchema.shape.place_id.describe('Place on the same trip to attach the file to, or null to detach it'), reservation_id: fileUpdateRequestSchema.shape.reservation_id.describe('Booking on the same trip to attach the file to, or null to detach it'), + budget_item_id: fileUpdateRequestSchema.shape.budget_item_id.describe('Expense on the same trip to attach the file to (a receipt), or null to detach it'), }, annotations: TOOL_ANNOTATIONS_WRITE, access: { group: 'files', mode: 'write' }, @@ -131,6 +132,7 @@ export class FilesMcp { const foreign = this.files.findForeignLinkTarget(tripId, { reservation_id: fields.reservation_id, place_id: fields.place_id, + budget_item_id: fields.budget_item_id, }); if (foreign) return errorResult(`Linked item does not belong to this trip (${foreign}).`); // The rest spread carries only the keys the caller actually sent, which is what @@ -150,6 +152,7 @@ export class FilesMcp { reservation_id: fileLinkRequestSchema.shape.reservation_id.describe('Booking on the same trip'), assignment_id: fileLinkRequestSchema.shape.assignment_id.describe('Day assignment (a place scheduled on a specific day) on the same trip'), place_id: fileLinkRequestSchema.shape.place_id.describe('Place on the same trip'), + budget_item_id: fileLinkRequestSchema.shape.budget_item_id.describe('Expense on the same trip, for a receipt'), }, annotations: TOOL_ANNOTATIONS_WRITE, access: { group: 'files', mode: 'write' }, @@ -165,8 +168,8 @@ export class FilesMcp { // The REST body allows all three to be absent and stores a link row pointing at // nothing. A tool caller that gets here with no target made a mistake, and saying // so is more useful than a success that attached the file to nothing. - if (!targets.reservation_id && !targets.assignment_id && !targets.place_id) { - return errorResult('Pass at least one of reservation_id, assignment_id or place_id.'); + if (!targets.reservation_id && !targets.assignment_id && !targets.place_id && !targets.budget_item_id) { + return errorResult('Pass at least one of reservation_id, assignment_id, place_id or budget_item_id.'); } const foreign = this.files.findForeignLinkTarget(tripId, targets); if (foreign) return errorResult(`Linked item does not belong to this trip (${foreign}).`); diff --git a/server/src/nest/integrations/dawarich.helpers.ts b/server/src/nest/integrations/dawarich.helpers.ts index f00218024d75dd32988c747e24ec36c0d499428b..e93ca2250deab13cd856bd7b62c2c9273bfa0b45 100644 GIT binary patch delta 21 bcmdlvn{nT4#tjpM*kVczfMD}PAq{H)TQmn! delta 16 Ycmdltn{nrC#tjpM7#TKC6Vk8-0669a!2kdN diff --git a/server/src/nest/journey/journey-domain.service.ts b/server/src/nest/journey/journey-domain.service.ts index 772a17d42..7b7bf018e 100644 --- a/server/src/nest/journey/journey-domain.service.ts +++ b/server/src/nest/journey/journey-domain.service.ts @@ -608,7 +608,7 @@ export class JourneyDomainService { // update everything on skeletons this.db.prepare( ` - UPDATE journey_entries SET title = ?, entry_date = ?, entry_time = ?, location_name = ?, location_lat = ?, location_lng = ?, updated_at = ? + UPDATE journey_entries SET title = ?, entry_date = ?, entry_time = ?, location_name = ?, location_lat = ?, location_lng = ?, country_code = ?, updated_at = ? WHERE id = ? `, ).run( @@ -618,6 +618,9 @@ export class JourneyDomainService { place.address || place.name, place.lat || null, place.lng || null, + // The pin moved, so the flag has to follow it — the same rule updateEntry + // states, and the one every sync write here used to skip. + this.countryFor(place.lat ?? null, place.lng ?? null), now, entry.id, ); @@ -625,10 +628,13 @@ export class JourneyDomainService { // for filled entries, only update location silently this.db.prepare( ` - UPDATE journey_entries SET location_name = ?, location_lat = ?, location_lng = ?, updated_at = ? + UPDATE journey_entries SET location_name = ?, location_lat = ?, location_lng = ?, country_code = ?, updated_at = ? WHERE id = ? `, - ).run(place.address || place.name, place.lat || null, place.lng || null, now, entry.id); + ).run( + place.address || place.name, place.lat || null, place.lng || null, + this.countryFor(place.lat ?? null, place.lng ?? null), now, entry.id, + ); } } } @@ -815,8 +821,8 @@ export class JourneyDomainService { found.location_lng !== lng; if (stale) { this.db.prepare( - `UPDATE journey_entries SET title = ?, entry_date = ?, entry_time = ?, location_name = ?, location_lat = ?, location_lng = ?, updated_at = ? WHERE id = ?`, - ).run(place.name, entryDate, entryTime, locationName, lat, lng, now, found.id); + `UPDATE journey_entries SET title = ?, entry_date = ?, entry_time = ?, location_name = ?, location_lat = ?, location_lng = ?, country_code = ?, updated_at = ? WHERE id = ?`, + ).run(place.name, entryDate, entryTime, locationName, lat, lng, this.countryFor(lat, lng), now, found.id); changed = true; } } else { @@ -825,8 +831,8 @@ export class JourneyDomainService { found.location_name !== locationName || found.location_lat !== lat || found.location_lng !== lng; if (stale) { this.db.prepare( - `UPDATE journey_entries SET location_name = ?, location_lat = ?, location_lng = ?, updated_at = ? WHERE id = ?`, - ).run(locationName, lat, lng, now, found.id); + `UPDATE journey_entries SET location_name = ?, location_lat = ?, location_lng = ?, country_code = ?, updated_at = ? WHERE id = ?`, + ).run(locationName, lat, lng, this.countryFor(lat, lng), now, found.id); changed = true; } } diff --git a/server/src/nest/journey/journey-share.service.ts b/server/src/nest/journey/journey-share.service.ts index 2c048a4a7..1a0ad5477 100644 --- a/server/src/nest/journey/journey-share.service.ts +++ b/server/src/nest/journey/journey-share.service.ts @@ -256,6 +256,13 @@ export class JourneyShareService { subtitle: journey.subtitle, cover_image: journey.cover_image, status: journey.status, + // The three "this journey does not use that field" switches. A public + // reader was getting mood and weather chips on a journey that had them + // turned off, because the phone card reads these and they were not here + // to read: undefined never equals 0, so both always looked switched on. + show_verdict: journey.show_verdict ? 1 : 0, + show_mood: journey.show_mood ? 1 : 0, + show_weather: journey.show_weather ? 1 : 0, }, entries: publicEntries, // A photo now carries the coordinates it was taken at, which is a location the diff --git a/server/src/nest/places/places.service.ts b/server/src/nest/places/places.service.ts index 8aa0752b4..6bc360182 100644 --- a/server/src/nest/places/places.service.ts +++ b/server/src/nest/places/places.service.ts @@ -349,7 +349,7 @@ export class PlacesService { currency = COALESCE(?, currency), place_time = ?, end_time = ?, - duration_minutes = COALESCE(?, duration_minutes), + duration_minutes = ?, notes = ?, image_url = ?, google_place_id = ?, @@ -375,9 +375,11 @@ export class PlacesService { currency || null, place_time !== undefined ? place_time : existingPlace.place_time, end_time !== undefined ? end_time : existingPlace.end_time, - // `?? null` rather than `|| null`: with COALESCE(?, duration_minutes) a - // falsy-coerced 0 read as "absent" and silently kept the old duration. - duration_minutes ?? null, + // 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, diff --git a/server/src/nest/roadtrip/roadtrip-preferences.controller.ts b/server/src/nest/roadtrip/roadtrip-preferences.controller.ts index 2755f6c55..90a4cb761 100644 --- a/server/src/nest/roadtrip/roadtrip-preferences.controller.ts +++ b/server/src/nest/roadtrip/roadtrip-preferences.controller.ts @@ -4,7 +4,7 @@ import { RequireAddon } from '../addons/require-addon.decorator'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard'; import { RoadtripPreferencesService } from './roadtrip-preferences.service'; -import { Body, Controller, Get, Param, Put, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Headers, Param, Put, UseGuards } from '@nestjs/common'; import { roadtripPreferencesUpdateSchema } from '@trek/shared'; import { createZodDto } from 'nestjs-zod'; @@ -24,7 +24,7 @@ export class RoadtripPreferencesController { @Put() @RequirePermission('day_edit') - update(@Param('tripId') tripId: string, @Body() patch: PreferencesDto) { - return { tripId: Number(tripId), preferences: this.preferences.update(Number(tripId), patch) }; + update(@Param('tripId') tripId: string, @Body() patch: PreferencesDto, @Headers('x-socket-id') socketId?: string) { + return { tripId: Number(tripId), preferences: this.preferences.update(Number(tripId), patch, socketId) }; } } diff --git a/server/src/nest/roadtrip/roadtrip-preferences.service.ts b/server/src/nest/roadtrip/roadtrip-preferences.service.ts index 87051decb..b612dbe7f 100644 --- a/server/src/nest/roadtrip/roadtrip-preferences.service.ts +++ b/server/src/nest/roadtrip/roadtrip-preferences.service.ts @@ -35,7 +35,7 @@ export class RoadtripPreferencesService { return roadtripPreferencesSchema.parse(preferences); } - update(tripId: number, patch: RoadtripPreferences): RoadtripPreferences { + update(tripId: number, patch: RoadtripPreferences, socketId?: string): RoadtripPreferences { const validated = roadtripPreferencesUpdateSchema.parse(patch); const saved = this.db.transaction(() => { const next = { ...this.read(tripId), ...validated }; @@ -52,7 +52,11 @@ export class RoadtripPreferencesService { } return this.read(tripId); }); - this.realtime.broadcast(String(tripId), 'roadtripPreferences:changed', { preferences: saved }); + // The saving tab is left out, like every other trip mutation: it already has + // the answer, and its own echo costs it a second store commit and the route + // recompute that follows. The MCP tool passes none, which is right — nobody + // there is holding the result already. + this.realtime.broadcast(String(tripId), 'roadtripPreferences:changed', { preferences: saved }, socketId); return saved; } } diff --git a/server/tests/unit/nest/journey-domain.service.test.ts b/server/tests/unit/nest/journey-domain.service.test.ts index eca64bc1e..19a9b7eb6 100644 --- a/server/tests/unit/nest/journey-domain.service.test.ts +++ b/server/tests/unit/nest/journey-domain.service.test.ts @@ -2471,6 +2471,52 @@ describe('country_code', () => { expect(cleared.country_code).toBeNull(); }); + it('JOURNEY-SVC-114: a place that moves across a border moves its skeleton entry country too', () => { + // The pin can also move without anybody editing the entry: the place it came + // from is dragged, and the trip sync writes the new coordinates onto the + // skeleton. The flag has to follow that write like it follows a manual one. + const { user } = createUser(testDb); + const journey = createJourney(testDb, user.id); + const trip = createTrip(testDb, user.id, { title: 'Border', start_date: '2026-08-01', end_date: '2026-08-03' }); + const place = createPlace(testDb, trip.id, { name: 'Grenzstein', lat: 48.8584, lng: 2.2945 }); + const day = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY date ASC LIMIT 1').get(trip.id) as { id: number }; + createDayAssignment(testDb, day.id, place.id); + svc.addTripToJourney(journey.id, trip.id, user.id); + + const before = testDb.prepare( + "SELECT country_code FROM journey_entries WHERE journey_id = ? AND source_place_id = ? AND type = 'skeleton'" + ).get(journey.id, place.id) as { country_code: string | null }; + expect(before.country_code).toBe('FR'); + + testDb.prepare('UPDATE places SET lat = ?, lng = ? WHERE id = ?').run(52.52, 13.405, place.id); + svc.onPlaceUpdated(place.id); + + const after = testDb.prepare( + "SELECT country_code FROM journey_entries WHERE journey_id = ? AND source_place_id = ? AND type = 'skeleton'" + ).get(journey.id, place.id) as { country_code: string | null }; + expect(after.country_code).toBe('DE'); + }); + + it('JOURNEY-SVC-115: and so does a filled entry, whose location follows the place silently', () => { + const { user } = createUser(testDb); + const journey = createJourney(testDb, user.id); + const trip = createTrip(testDb, user.id, { title: 'Border 2', start_date: '2026-08-01', end_date: '2026-08-03' }); + const place = createPlace(testDb, trip.id, { name: 'Grenzstein', lat: 48.8584, lng: 2.2945 }); + const day = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY date ASC LIMIT 1').get(trip.id) as { id: number }; + createDayAssignment(testDb, day.id, place.id); + svc.addTripToJourney(journey.id, trip.id, user.id); + // Writing a story turns the skeleton into a filled entry. + testDb.prepare("UPDATE journey_entries SET type = 'entry' WHERE source_place_id = ?").run(place.id); + + testDb.prepare('UPDATE places SET lat = ?, lng = ? WHERE id = ?').run(52.52, 13.405, place.id); + svc.onPlaceUpdated(place.id); + + const after = testDb.prepare( + 'SELECT country_code, location_lat FROM journey_entries WHERE source_place_id = ?' + ).get(place.id) as { country_code: string | null; location_lat: number }; + expect(after).toMatchObject({ country_code: 'DE', location_lat: 52.52 }); + }); + it('JOURNEY-SVC-113: an edit that does not touch the pin leaves the country alone', () => { const { user } = createUser(testDb); const journey = createJourney(testDb, user.id); diff --git a/server/tests/unit/nest/journey-share.service.test.ts b/server/tests/unit/nest/journey-share.service.test.ts index 1da104750..09630647c 100644 --- a/server/tests/unit/nest/journey-share.service.test.ts +++ b/server/tests/unit/nest/journey-share.service.test.ts @@ -447,6 +447,10 @@ describe('getPublicJourney', () => { expect(result).not.toBeNull(); expect(result!.journey.title).toBe('Japan 2026'); expect(result!.journey.subtitle).toBe('Cherry blossom season'); + // The "this journey does not use that field" switches travel with the share: + // the phone card reads them, and reading an absent one made every field look + // switched on, so a reader saw chips the owner had turned off. + expect(result!.journey).toMatchObject({ show_verdict: 1, show_mood: 1, show_weather: 1 }); expect(result!.entries).toHaveLength(2); expect(result!.stats.entries).toBe(2); expect(result!.stats.photos).toBe(1); @@ -456,6 +460,20 @@ describe('getPublicJourney', () => { expect(result!.permissions.share_map).toBe(false); }); + it('JOURNEY-SHARE-017b: a field the owner switched off is switched off for the reader too', () => { + const { user } = createUser(testDb); + const journey = createJourney(testDb, user.id); + testDb.prepare('UPDATE journeys SET show_mood = 0, show_weather = 0 WHERE id = ?').run(journey.id); + createJourneyEntry(testDb, journey.id, user.id, { type: 'entry', title: 'Tag 1', entry_date: '2026-03-20' }); + const { token } = svc.createOrUpdateJourneyShareLink(journey.id, user.id, { + share_timeline: true, share_gallery: false, share_map: true, + }); + + const result = svc.getPublicJourney(token)!; + + expect(result.journey).toMatchObject({ show_mood: 0, show_weather: 0, show_verdict: 1 }); + }); + it('JOURNEY-SHARE-018: excludes skeleton entries from public view', () => { const { user } = createUser(testDb); const journey = createJourney(testDb, user.id); diff --git a/server/tests/unit/nest/places.service.test.ts b/server/tests/unit/nest/places.service.test.ts index 2462f38c2..0989b3712 100644 --- a/server/tests/unit/nest/places.service.test.ts +++ b/server/tests/unit/nest/places.service.test.ts @@ -1418,10 +1418,23 @@ describe('zero-valued numeric fields', () => { const zeroed = await svc.update(String(trip.id), String(place.id), { duration_minutes: 0 }) as any; expect(zeroed.duration_minutes).toBe(0); - // An omitted duration still leaves the stored value alone (COALESCE). + // An omitted duration still leaves the stored value alone. const untouched = await svc.update(String(trip.id), String(place.id), { name: 'Stop 2' }) as any; expect(untouched.duration_minutes).toBe(0); }); + + it('PLACE-SVC-067b — an explicit null clears the planned stay length', async () => { + // What the write contract and the MCP tool both advertise. Behind COALESCE, + // null and absent were the same thing, so the field promised a reset it never + // performed and the day plan kept budgeting the old ninety minutes. + const { user } = createUser(testDb); + const trip = createTrip(testDb, user.id); + const place = createPlace(testDb, trip.id, { name: 'Stop' }) as any; + testDb.prepare('UPDATE places SET duration_minutes = 90 WHERE id = ?').run(place.id); + + const cleared = await svc.update(String(trip.id), String(place.id), { duration_minutes: null }) as any; + expect(cleared.duration_minutes).toBeNull(); + }); }); // ── LIKE metacharacter escaping (#1745) ───────────────────────────────────────