From a691fb94c4c69995baef1408dd13f4b5089191a3 Mon Sep 17 00:00:00 2001 From: Julian Tabel Date: Sat, 14 Feb 2026 16:09:50 +0100 Subject: [PATCH] Fix route filtering to keep parent routes with encountered children In flat mode, parent routes with no direct encounters were being filtered out even when their children had encounters. Now we pre-compute which parents have encountered children so they're retained in both flat and hierarchical modes. Co-Authored-By: Claude Opus 4.6 --- backend/src/app/api/games.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/backend/src/app/api/games.py b/backend/src/app/api/games.py index 67d747c..573f99e 100644 --- a/backend/src/app/api/games.py +++ b/backend/src/app/api/games.py @@ -168,11 +168,21 @@ async def list_game_routes( "encounter_methods": methods, } + # Determine which routes have encounters for this game + def has_encounters(route: Route) -> bool: + return any(re.game_id == game_id for re in route.route_encounters) + + # Collect IDs of parent routes that have at least one child with encounters + parents_with_children = set() + for route in all_routes: + if route.parent_route_id is not None and has_encounters(route): + parents_with_children.add(route.parent_route_id) + if flat: return [ route_to_dict(r) for r in all_routes - if any(re.game_id == game_id for re in r.route_encounters) + if has_encounters(r) or r.id in parents_with_children ] # Build hierarchical structure @@ -183,22 +193,17 @@ async def list_game_routes( for route in all_routes: if route.parent_route_id is None: top_level_routes.append(route) - else: - # Only include children that have encounters for this game - if any(re.game_id == game_id for re in route.route_encounters): - children_by_parent.setdefault(route.parent_route_id, []).append( - route_to_dict(route) - ) + elif has_encounters(route): + children_by_parent.setdefault(route.parent_route_id, []).append( + route_to_dict(route) + ) # Build response with nested children # Only include top-level routes that have their own encounters or remaining children response = [] for route in top_level_routes: children = children_by_parent.get(route.id, []) - has_own_encounters = any( - re.game_id == game_id for re in route.route_encounters - ) - if has_own_encounters or children: + if has_encounters(route) or children: route_dict = route_to_dict(route) route_dict["children"] = children response.append(route_dict)