Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(places): deleting a place cancels the nights booked at it

Maurice77a16d6f2693
diff --git a/server/src/nest/accommodations/accommodations.module.ts b/server/src/nest/accommodations/accommodations.module.ts
index 4c66f125c..37b0ecab8 100644
--- a/server/src/nest/accommodations/accommodations.module.ts
+++ b/server/src/nest/accommodations/accommodations.module.ts
@@ -4,7 +4,7 @@ import { AccommodationsService } from './accommodations.service';
import { AccommodationsRpc } from './accommodations.rpc';
import { AccommodationsMcp } from './accommodations.mcp';
import { PlacesModule } from '../places/places.module';
-import { AssignmentsDomainModule } from '../assignments/assignments-domain.module';
+import { AccommodationsDomainModule } from './accommodations-domain.module';
import { AuthModule } from '../auth/auth.module';
import { DatabaseModule } from '../database/database.module';
import { PermissionsModule } from '../permissions/permissions.module';
@@ -29,9 +29,9 @@ import { McpSharedModule } from '../mcp-shared/mcp-shared.module';
* which comes near accommodations.
*/
@Module({
- imports: [McpSharedModule, PermissionsModule, RealtimeModule, PluginGuardsModule, DatabaseModule, PlacesModule, AssignmentsDomainModule, AuthModule],
+ imports: [McpSharedModule, PermissionsModule, RealtimeModule, PluginGuardsModule, DatabaseModule, PlacesModule, AccommodationsDomainModule, AuthModule],
controllers: [AccommodationsController],
- providers: [AccommodationsService, AccommodationsRpc, AccommodationsMcp],
+ providers: [AccommodationsRpc, AccommodationsMcp],
exports: [AccommodationsService],
})
export class AccommodationsModule {}
diff --git a/server/src/nest/places/places.module.ts b/server/src/nest/places/places.module.ts
index 5a45a9586..85980dc6d 100644
--- a/server/src/nest/places/places.module.ts
+++ b/server/src/nest/places/places.module.ts
@@ -8,6 +8,7 @@ import { RealtimeModule } from '../realtime/realtime.module';
import { PlacesMcp } from './places.mcp';
import { PermissionsModule } from '../permissions/permissions.module';
import { AssignmentsDomainModule } from '../assignments/assignments-domain.module';
+import { AccommodationsDomainModule } from '../accommodations/accommodations-domain.module';
import { AppConfigModule } from '../app-config/app-config.module';
import { UnsplashModule } from '../unsplash/unsplash.module';
import { PlacePhotosModule } from '../place-photos/place-photos.module';
@@ -42,7 +43,9 @@ import { MAX_PLACE_IMAGE_SIZE } from '../common/place-image-upload';
buildStorageUploadOptions(storage, { category: 'places', maxSize: MAX_PLACE_IMAGE_SIZE }),
}),
StorageModule,
- McpSharedModule, PermissionsModule, QueryHelpersModule, MapsModule, AuthModule, AppConfigModule, UnsplashModule, PlacePhotosModule, JourneyDomainModule, RealtimeModule, PluginGuardsModule, AssignmentsDomainModule],
+ // 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],
controllers: [PlacesController],
providers: [PlacesService, PlacesMcp, PlacesRpc],
exports: [PlacesService],
diff --git a/server/src/nest/places/places.service.ts b/server/src/nest/places/places.service.ts
index 644614f48..d39fe94ac 100644
--- a/server/src/nest/places/places.service.ts
+++ b/server/src/nest/places/places.service.ts
@@ -28,6 +28,7 @@ import { type UpdateConflict, isUpdateConflict } from '../common/conflictResult'
import { reclaimPlaceImage } from './place-image';
import { JourneyDomainService } from '../journey/journey-domain.service';
import { StorageService } from '../storage/storage.service';
+import { AccommodationsService } from '../accommodations/accommodations.service';
import {
ENRICH_CONCURRENCY,
ADDRESS_BACKFILL_MAX_PLACES,
@@ -123,6 +124,7 @@ export class PlacesService {
private readonly photoCache: PlacePhotoCacheService,
private readonly journey: JourneyDomainService,
private readonly storage: StorageService,
+ private readonly accommodations: AccommodationsService,
) {}
verifyTripAccess(tripId: string, userId: number) {
@@ -425,6 +427,25 @@ export class PlacesService {
return rows.map(r => r.id);
}
+
+ /**
+ * The nights booked at a place, cancelled because the place is going.
+ *
+ * A stay whose place is deleted used to be left behind with place_id NULL: still
+ * drawn in the day header, still naming a hotel through its partner booking, and
+ * pointing nowhere. Through the accommodations domain rather than a DELETE here,
+ * because a stay takes its partner booking, that booking's expense and the day stop
+ * it wrote with it, and none of that is places' business to know.
+ *
+ * Runs inside the caller's transaction.
+ */
+ private cancelStaysAt(tripId: string | number, placeId: string | number): void {
+ const stays = this.dbs.all<{ id: number }>(
+ 'SELECT id FROM day_accommodations WHERE trip_id = ? AND place_id = ?', tripId, placeId,
+ );
+ for (const stay of stays) this.accommodations.deleteAccommodation(stay.id);
+ }
+
async remove(tripId: string, placeId: string): Promise<boolean> {
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,
@@ -434,6 +455,7 @@ export class PlacesService {
// expense with it (#1298). One transaction, so a place can never survive
// half-detached from its money.
this.dbs.transaction(() => {
+ this.cancelStaysAt(tripId, placeId);
this.dbs.run('DELETE FROM budget_items WHERE trip_id = ? AND place_id = ?', tripId, placeId);
this.dbs.run('DELETE FROM places WHERE id = ?', placeId);
});
@@ -453,6 +475,7 @@ export class PlacesService {
for (const id of ids) {
const row = selectStmt.get(id, tripId) as { google_place_id: string | null; image_url: string | null } | undefined;
if (!row) continue;
+ this.cancelStaysAt(tripId, id);
deleteExpenseStmt.run(tripId, id);
deleteStmt.run(id);
deleted.push(id);
diff --git a/server/tests/helpers/mcp-test-controllers.ts b/server/tests/helpers/mcp-test-controllers.ts
index 2cad811ab..dae74fe57 100644
--- a/server/tests/helpers/mcp-test-controllers.ts
+++ b/server/tests/helpers/mcp-test-controllers.ts
@@ -160,17 +160,19 @@ export function createMcpTestRegistry(): McpRegistry {
// the journey skeleton hooks landed on the place write paths. tsconfig.tests.json
// covers `tests` now and CI runs it (npm run typecheck:tests), so a missed
// dependency fails the build — pass them for real regardless of the gate.
+ // One instance, four consumers: AssignmentsMcp, ReservationsMcp, PlacesMcp and
+ // AccommodationsService, which writes the day stop a booked night implies.
+ const assignmentsService = new AssignmentsService(dbService, permissionsService, realtimeService, queryHelpersService, journeyDomain);
+ const accommodationsService = new AccommodationsService(dbService, permissionsService, realtimeService, assignmentsService);
+ // Built after it: deleting a place cancels the nights booked at it through this one.
const placesService = new PlacesService(
dbService, permissionsService, realtimeService, mapsService, queryHelpersService,
new UnsplashService(dbService, new RuntimeEnvService(), generalStorage),
placePhotoCache,
journeyDomain,
generalStorage,
+ accommodationsService,
);
- // One instance, four consumers: AssignmentsMcp, ReservationsMcp, PlacesMcp and
- // AccommodationsService, which writes the day stop a booked night implies.
- const assignmentsService = new AssignmentsService(dbService, permissionsService, realtimeService, queryHelpersService, journeyDomain);
- const accommodationsService = new AccommodationsService(dbService, permissionsService, realtimeService, assignmentsService);
// Built after it: a hotel booking writes the stay's day stop through this one.
const reservationsService = new ReservationsService(dbService, permissionsService, budgetService, realtimeService, notificationsStub(), new ReservationsReadRepository(dbService), accommodationsService);
const membersService = new TripMembersService(dbService, budgetService, new UserCleanupService(dbService, budgetService), permissionsService, realtimeService, notificationsStub());
diff --git a/server/tests/helpers/plugin-host.ts b/server/tests/helpers/plugin-host.ts
index afef3c31e..bc3ef6703 100644
--- a/server/tests/helpers/plugin-host.ts
+++ b/server/tests/helpers/plugin-host.ts
@@ -100,7 +100,6 @@ export function createPluginRpcHostFactory(dbs: DatabaseService): PluginRpcHostF
const photoCache = new PlacePhotoCacheService(dbs, makeStorageFixture('photos/google/').storage);
const unsplash = new UnsplashService(dbs, new RuntimeEnvService(), generalStorage);
const journey = new JourneyDomainService(dbs, realtime, new TrekPhotosRepository(dbs));
- const places = new PlacesService(dbs, permissions, realtime, new MapsService(dbs, photoCache), queryHelpers, unsplash, photoCache, journey, generalStorage);
const collections = new CollectionsService(dbs, permissions, realtime, notificationsStub(), generalStorage);
const atlas = new AtlasService(dbs);
const dayNotes = new DayNotesService(dbs, permissions, realtime);
@@ -110,6 +109,8 @@ export function createPluginRpcHostFactory(dbs: DatabaseService): PluginRpcHostF
const llmConfig = new LlmConfigResolver(new SettingsService(dbs), dbs, addons);
const oauth = new PluginOAuthService(dbs);
const accommodations = new AccommodationsService(dbs, permissions, realtime, assignments);
+ // After it: deleting a place cancels the nights booked at it through this one.
+ const places = new PlacesService(dbs, permissions, realtime, new MapsService(dbs, photoCache), queryHelpers, unsplash, photoCache, journey, generalStorage, accommodations);
// After accommodations: a hotel booking writes the stay's day stop through it.
const reservations = new ReservationsService(dbs, permissions, budget, realtime, notificationsStub(), new ReservationsReadRepository(dbs), accommodations);
const trips = new TripsService(dbs, reservations, days, permissions, budget, vacay, realtime, unsplash, generalStorage);
diff --git a/server/tests/unit/nest/accommodations.service.test.ts b/server/tests/unit/nest/accommodations.service.test.ts
index 62cf19052..7fcebc3fd 100644
--- a/server/tests/unit/nest/accommodations.service.test.ts
+++ b/server/tests/unit/nest/accommodations.service.test.ts
@@ -46,6 +46,7 @@ import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
import { makeAccommodationsService } from '../../helpers/accommodations-service';
import { AccommodationsModule } from '../../../src/nest/accommodations/accommodations.module';
+import { AccommodationsDomainModule } from '../../../src/nest/accommodations/accommodations-domain.module';
import { AccommodationsController } from '../../../src/nest/accommodations/accommodations.controller';
import { expectRegisteredProvider, expectRegisteredController } from '../../helpers/module-providers';
@@ -830,14 +831,27 @@ describe('the day stop a booking implies', () => {
});
describe('AccommodationsService wiring', () => {
- it('ACC-001: the module carries the controller, the service and the RPC surface', () => {
+ it('ACC-001: the module carries the controller and the RPC surface, and re-exports the service', () => {
expectRegisteredController(AccommodationsModule, AccommodationsController);
- expectRegisteredProvider(AccommodationsModule, AccommodationsService);
+ // The service itself lives in the domain module now, so places can delete the
+ // nights booked at a place without the surfaces coming along and closing a loop
+ // back through PlacesModule. Still exported from here: everything that imported
+ // AccommodationsModule for the service keeps working.
+ expectRegisteredProvider(AccommodationsDomainModule, AccommodationsService);
+ const imports = Reflect.getMetadata('imports', AccommodationsModule) as unknown[];
+ expect(imports).toEqual(expect.arrayContaining([AccommodationsDomainModule]));
const exports = Reflect.getMetadata('exports', AccommodationsModule) as unknown[];
expect(Array.isArray(exports)).toBe(true);
expect(exports).toEqual(expect.arrayContaining([AccommodationsService]));
});
+ it('ACC-001b: the domain module reaches neither places nor the surfaces', () => {
+ // The whole point of the split: PlacesModule imports this one, so a path back to
+ // places from here would be the loop it exists to avoid.
+ const imports = (Reflect.getMetadata('imports', AccommodationsDomainModule) as { name?: string }[]) ?? [];
+ expect(imports.map(m => m?.name)).not.toContain('PlacesModule');
+ });
+
it('ACC-002: it does not import the days or reservations modules', () => {
const imports = (Reflect.getMetadata('imports', AccommodationsModule) as { name?: string }[]) ?? [];
expect(Array.isArray(imports)).toBe(true);
diff --git a/server/tests/unit/nest/places.directions.test.ts b/server/tests/unit/nest/places.directions.test.ts
index aa5e9fd53..d67e0a62c 100644
--- a/server/tests/unit/nest/places.directions.test.ts
+++ b/server/tests/unit/nest/places.directions.test.ts
@@ -41,6 +41,7 @@ vi.mock('../../../src/utils/ssrfGuard', () => ({
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { createUser, createTrip, createPlace } from '../../helpers/factories';
+import { accommodationsOver } from '../../helpers/accommodations-service';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
@@ -89,6 +90,7 @@ function svc(searchNominatim: MapsService['searchNominatim']): PlacesService {
photoCacheStub,
new JourneyDomainService(dbs, new RealtimeService(), new TrekPhotosRepository(dbs)),
storageFx.storage,
+ accommodationsOver(dbs),
);
}
diff --git a/server/tests/unit/nest/places.service.test.ts b/server/tests/unit/nest/places.service.test.ts
index b2d04c61d..8b410cd2e 100644
--- a/server/tests/unit/nest/places.service.test.ts
+++ b/server/tests/unit/nest/places.service.test.ts
@@ -65,7 +65,8 @@ const photoCacheStub = { removeIfUnreferenced: removeIfUnreferencedSpy } as unkn
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
-import { createUser, createTrip, createPlace, createCategory, createTag, addTripMember } from '../../helpers/factories';
+import { accommodationsOver } from '../../helpers/accommodations-service';
+import { createUser, createTrip, createPlace, createDay, createCategory, createTag, addTripMember } from '../../helpers/factories';
import path from 'path';
import fs from 'fs';
import { DatabaseService } from '../../../src/nest/database/database.service';
@@ -105,9 +106,11 @@ function makePlacesService(maps: MapsService = new MapsService(dbs, photoCacheSt
photoCacheStub,
new JourneyDomainService(dbs, new RealtimeService(), new TrekPhotosRepository(dbs)),
placesStorageFx.storage,
+ accommodationsOver(dbs),
);
}
+const accommodations = accommodationsOver(dbs);
const svc = makePlacesService();
beforeAll(() => {
@@ -424,6 +427,42 @@ describe('remove', () => {
expect(remaining[0].id).toBe(p1.id);
});
+ it('PLACE-SVC-019d — the night booked at a place goes with the place', async () => {
+ // Left behind, a stay keeps its place_id as NULL: still drawn in the day header,
+ // still naming a hotel through its partner booking, and pointing nowhere. Its day
+ // stop and that booking go too, which is the accommodations cascade doing its job
+ // rather than a second copy of it here.
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const place = createPlace(testDb, trip.id, { name: 'Hotel Adlon' }) as any;
+ const { accommodation } = accommodations.createAccommodation(trip.id, {
+ place_id: place.id, start_day_id: day.id, end_day_id: day.id,
+ }) as any;
+ expect(testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').get(accommodation.id)).toBeTruthy();
+
+ await svc.remove(String(trip.id), String(place.id));
+
+ expect(testDb.prepare('SELECT id FROM day_accommodations WHERE id = ?').get(accommodation.id)).toBeUndefined();
+ expect(testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').get(accommodation.id)).toBeUndefined();
+ expect(testDb.prepare('SELECT id FROM day_assignments WHERE day_id = ?').all(day.id)).toEqual([]);
+ });
+
+ it('PLACE-SVC-019e — a place with no booking is untouched by that', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const hotel = createPlace(testDb, trip.id, { name: 'Hotel Adlon' }) as any;
+ const museum = createPlace(testDb, trip.id, { name: 'Pergamon' }) as any;
+ const { accommodation } = accommodations.createAccommodation(trip.id, {
+ place_id: hotel.id, start_day_id: day.id, end_day_id: day.id,
+ }) as any;
+
+ await svc.remove(String(trip.id), String(museum.id));
+
+ expect(testDb.prepare('SELECT id FROM day_accommodations WHERE id = ?').get(accommodation.id)).toBeTruthy();
+ });
+
it('PLACE-SVC-019c — the linked expense goes with the place (#1298)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
diff --git a/server/tests/unit/nest/trip-read-model.service.test.ts b/server/tests/unit/nest/trip-read-model.service.test.ts
index 747b629b8..f00cbab23 100644
--- a/server/tests/unit/nest/trip-read-model.service.test.ts
+++ b/server/tests/unit/nest/trip-read-model.service.test.ts
@@ -94,6 +94,7 @@ const placesSvc = new PlacesService(
new UnsplashService(dbs(), new RuntimeEnvService(), makeStorageFixture('').storage), photoCache,
new JourneyDomainService(dbs(), new RealtimeService(), new TrekPhotosRepository(dbs())),
makeStorageFixture('').storage,
+ accommodationsOver(dbs()),
);
const accommodationsSvc = makeAccommodationsService(testDb);
const membersSvc = new TripMembersService(dbs(), budgetSvc, new UserCleanupService(dbs(), budgetSvc), new PermissionsService(dbs()), new RealtimeService(), notificationsStub());
diff --git a/server/tests/unit/nest/trips.service.test.ts b/server/tests/unit/nest/trips.service.test.ts
index 88b431ed0..77c56fc0a 100644
--- a/server/tests/unit/nest/trips.service.test.ts
+++ b/server/tests/unit/nest/trips.service.test.ts
@@ -74,7 +74,7 @@ import { UserCleanupService } from '../../../src/nest/auth/user-cleanup.service'
import { TripMembersService } from '../../../src/nest/trip-members/trip-members.service';
import { TripReadModelService } from '../../../src/nest/trip-read-model/trip-read-model.service';
import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
-import { makeAccommodationsService } from '../../helpers/accommodations-service';
+import { accommodationsOver, makeAccommodationsService } from '../../helpers/accommodations-service';
import { MapsService } from '../../../src/nest/maps/maps.service';
import { UnsplashService } from '../../../src/nest/unsplash/unsplash.service';
import { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
@@ -112,6 +112,7 @@ const placesSvc = new PlacesService(
photoCache,
new JourneyDomainService(dbs(), new RealtimeService(), new TrekPhotosRepository(dbs())),
makeStorageFixture('').storage,
+ accommodationsOver(dbs()),
);
const accommodationsSvc = makeAccommodationsService(testDb);
const createAccommodation = accommodationsSvc.createAccommodation.bind(accommodationsSvc);
diff --git a/server/tests/unit/services/conflictUpdate.test.ts b/server/tests/unit/services/conflictUpdate.test.ts
index d3589e311..8559292ad 100644
--- a/server/tests/unit/services/conflictUpdate.test.ts
+++ b/server/tests/unit/services/conflictUpdate.test.ts
@@ -38,6 +38,7 @@ import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
import { createUser, createTrip } from '../../helpers/factories';
+import { accommodationsOver } from '../../helpers/accommodations-service';
import { isUpdateConflict } from '../../../src/nest/common/conflictResult';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { PackingService } from '../../../src/nest/packing/packing.service';
@@ -72,6 +73,7 @@ const places = new PlacesService(
photoCache,
new JourneyDomainService(dbs, realtime, new TrekPhotosRepository(dbs)),
makeStorageFixture('').storage,
+ accommodationsOver(dbs),
);
beforeAll(() => {