From 4d3f7fd355111b78140d5c87ee4738bdd83db266 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 30 Nov 2026 22:10:18 +0100 Subject: [PATCH] fix(memories): scope album link cleanup to its owner Confirm trip access and the scoped album link inside the transaction before handling related photos. Treat a missing link as a completed removal. --- .../db/repositories/TripPhotos.repository.ts | 2 +- .../nest/memories/unified-memories.service.ts | 22 ++++++---------- .../nest/unified-memories.service.test.ts | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/server/src/db/repositories/TripPhotos.repository.ts b/server/src/db/repositories/TripPhotos.repository.ts index 3619578f2..3f810ae3c 100644 --- a/server/src/db/repositories/TripPhotos.repository.ts +++ b/server/src/db/repositories/TripPhotos.repository.ts @@ -146,7 +146,7 @@ export class TripPhotosRepository extends TrekRepository { .execute(); } - /** UM8 (`UnifiedMemoriesService.removeAlbumLink`'s pre-tx orphan-candidate list) — `SELECT photo_id FROM trip_photos WHERE trip_id = ? AND album_link_id = ?`. */ + /** UM8 (`UnifiedMemoriesService.removeAlbumLink`, inside its transaction). */ async listPhotoIdsForAlbumLink(trip_id: number | string, album_link_id: number | string): Promise { const rows = await this.kysely() .selectFrom('trip_photos') diff --git a/server/src/nest/memories/unified-memories.service.ts b/server/src/nest/memories/unified-memories.service.ts index 58a974cc1..e2b387b2d 100644 --- a/server/src/nest/memories/unified-memories.service.ts +++ b/server/src/nest/memories/unified-memories.service.ts @@ -304,25 +304,19 @@ export class UnifiedMemoriesService { } async removeAlbumLink(tripId: string, linkId: string, userId: number): Promise> { - const access = await this.trips.findAccessible(tripId, userId); - if (!access) { - return fail('Trip not found or access denied', 404); - } - // A link id toRowId refuses names no link, so there is nothing to remove, - // as for an id no link has; bound raw, Postgres refused it with an error. const link = toRowId(linkId); - if (link === null) return success(true); - try { - const linkedPhotoIds = await this.tripPhotos.listPhotoIdsForAlbumLink(tripId, link); + return await this.uow.transactional>(async () => { + const trip = await this.trips.findAccessible(tripId, userId); + if (!trip) return fail('Trip not found or access denied', 404); + if (link === null || !(await this.tripAlbumLinks.findScoped(link, trip.id, userId))) return success(true); - await this.uow.transactional(async () => { - await this.tripPhotos.deleteForAlbumLink(tripId, link); - await this.tripAlbumLinks.deleteScoped(link, tripId, userId); + const linkedPhotoIds = await this.tripPhotos.listPhotoIdsForAlbumLink(trip.id, link); + await this.tripPhotos.deleteForAlbumLink(trip.id, link); + await this.tripAlbumLinks.deleteScoped(link, trip.id, userId); for (const photo_id of linkedPhotoIds) await this.photos.deleteIfOrphan(photo_id); + return success(true); }); - - return success(true); } catch (error) { return mapDbError(error, 'Failed to remove album link'); } diff --git a/server/tests/unit/nest/unified-memories.service.test.ts b/server/tests/unit/nest/unified-memories.service.test.ts index f4fa301ee..9676931ec 100644 --- a/server/tests/unit/nest/unified-memories.service.test.ts +++ b/server/tests/unit/nest/unified-memories.service.test.ts @@ -354,6 +354,30 @@ describe('removeAlbumLink', () => { expect((result as any).error.status).toBe(404); }); + it('MEM-UNIFIED-020a: treats an absent scoped link as a no-op inside the transaction', async () => { + const { user } = createUser(testDb); + const trip = createTrip(testDb, user.id); + const findScoped = vi.spyOn(tripAlbumLinksRepo, 'findScoped').mockImplementationOnce(async () => { + expect(tripAlbumLinksRepo.getEntityManager().isInTransaction()).toBe(true); + return null; + }); + const listPhotos = vi.spyOn(tripPhotosRepo, 'listPhotoIdsForAlbumLink'); + const deletePhotos = vi.spyOn(tripPhotosRepo, 'deleteForAlbumLink'); + const deleteLink = vi.spyOn(tripAlbumLinksRepo, 'deleteScoped'); + try { + expect(expectSuccess(await removeAlbumLink(String(trip.id), '17', user.id))).toBe(true); + expect(findScoped).toHaveBeenCalledWith(17, trip.id, user.id); + expect(listPhotos).not.toHaveBeenCalled(); + expect(deletePhotos).not.toHaveBeenCalled(); + expect(deleteLink).not.toHaveBeenCalled(); + } finally { + findScoped.mockRestore(); + listPhotos.mockRestore(); + deletePhotos.mockRestore(); + deleteLink.mockRestore(); + } + }); + it('MEM-UNIFIED-020: UM8/UM9/UM10 — the transaction removes the link and every trip_photos row it owned', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); @@ -384,6 +408,7 @@ describe('removeAlbumLink', () => { // The orphaned trek_photos row is reclaimed too (TrekPhotoRegistrationService.deleteIfOrphan, PH10/PH11). const orphan = await findRow(orm, TrekPhotos, { id: photoRow?.photo_id }); expect(orphan).toBeNull(); + expect(expectSuccess(await removeAlbumLink(String(trip.id), String(linkRow.id), user.id))).toBe(true); }); });