From f5562e21352d75dca16d9b7ae73ec4d860e521e2 Mon Sep 17 00:00:00 2001 From: furkanyigit34 <134547018+furkanyigit34@users.noreply.github.com> Date: Fri, 27 Mar 2026 16:05:01 +0300 Subject: [PATCH] fix: collect_scrapable_categories should recurse to leaf categories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ne yaptık: - Fonksiyon artık her zaman çocuk kategorilere iniyor - Sadece leaf (yaprak) kategorileri toplıyor — children'ı olan kategorileri atılıyor - Önceki davranış: trendyol_category_id varsa duruyordu, alt kategorilere inmiyordu Neden yaptık: - "Kadın" kategorisinde 17 üst kategori seçildiğinde sadece 17 kategori taranıyordu - Oysa bu 17 kategorinin altında yüzlerce leaf kategori var (ör: Ayakkabı→10, Aksesuar→35) - Şimdi tüm ağacın en dibine kadar inip tüm leaf kategorileri tarayacak Co-Authored-By: Claude Opus 4.6 (1M context) --- backend/main.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/main.py b/backend/main.py index cfef7aa..563ba3c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1261,8 +1261,8 @@ def get_category_products(category_id: int, db: Session = Depends(get_db)): # Helper: recursively collect scrapable categories (those with trendyol_category_id) def collect_scrapable_categories(db: Session, category_ids: list) -> list: """ - Given a list of category IDs, collect all categories with valid trendyol_category_id. - If a category doesn't have trendyol_category_id, recursively check its children. + Given a list of category IDs, collect ALL leaf categories with valid trendyol_category_id. + Always recurses into children to find every scrapable category in the tree. Returns list of (trendyol_category_id, name) tuples. """ result = [] @@ -1276,13 +1276,15 @@ def collect_scrapable_categories(db: Session, category_ids: list) -> list: if cat.id in seen: continue seen.add(cat.id) - if cat.trendyol_category_id: - result.append((cat.trendyol_category_id, cat.name)) - else: - # No trendyol_category_id — check children - children = db.query(Category).filter(Category.parent_id == cat.id).all() + # Check children first + children = db.query(Category).filter(Category.parent_id == cat.id).all() + if children: + # Has children — recurse deeper child_ids = [c.id for c in children] _collect(child_ids) + elif cat.trendyol_category_id: + # Leaf category with trendyol_category_id — add to results + result.append((cat.trendyol_category_id, cat.name)) _collect(category_ids) return result