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.
This commit is contained in:
Maurice
2026-11-30 22:10:18 +01:00
parent 8335ac45f9
commit 4d3f7fd355
3 changed files with 34 additions and 15 deletions
@@ -146,7 +146,7 @@ export class TripPhotosRepository extends TrekRepository<TripPhotos> {
.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<number[]> {
const rows = await this.kysely<TripPhotosKyselyDB>()
.selectFrom('trip_photos')
@@ -304,25 +304,19 @@ export class UnifiedMemoriesService {
}
async removeAlbumLink(tripId: string, linkId: string, userId: number): Promise<ServiceResult<true>> {
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<ServiceResult<true>>(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');
}
@@ -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);
});
});