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
@@ -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);
});
});