Commit
·
88106f3
1
Parent(s):
5c831e5
Prioritize nested category structure check in check_user_has_category_data
Browse files- app/smart_recommendation.py +39 -29
app/smart_recommendation.py
CHANGED
|
@@ -171,33 +171,9 @@ class SmartBudgetRecommender:
|
|
| 171 |
{"headCategories.categories.category": category_id},
|
| 172 |
]
|
| 173 |
|
| 174 |
-
#
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
budget_query = {**user_cond, **cat_cond}
|
| 178 |
-
budget_count = self.db.budgets.count_documents(budget_query)
|
| 179 |
-
if budget_count > 0:
|
| 180 |
-
print(f"✅ Found {budget_count} budget(s) for user {user_id} with category_id {category_id}")
|
| 181 |
-
print(f" Query that matched: {budget_query}")
|
| 182 |
-
return True
|
| 183 |
-
|
| 184 |
-
# Also try a more comprehensive query using $and and $or
|
| 185 |
-
try:
|
| 186 |
-
comprehensive_query = {
|
| 187 |
-
"$and": [
|
| 188 |
-
{"$or": user_conditions},
|
| 189 |
-
{"$or": category_conditions}
|
| 190 |
-
]
|
| 191 |
-
}
|
| 192 |
-
budget_count = self.db.budgets.count_documents(comprehensive_query)
|
| 193 |
-
if budget_count > 0:
|
| 194 |
-
print(f"✅ Found {budget_count} budget(s) using comprehensive query for user {user_id} with category_id {category_id}")
|
| 195 |
-
return True
|
| 196 |
-
except Exception as e:
|
| 197 |
-
print(f"⚠️ Error with comprehensive query: {e}")
|
| 198 |
-
|
| 199 |
-
# Additional check: Look for budgets with nested category arrays
|
| 200 |
-
# Some budgets might have categories nested in arrays like headCategories[].categories[]
|
| 201 |
try:
|
| 202 |
try:
|
| 203 |
category_objid = ObjectId(category_id)
|
|
@@ -205,15 +181,18 @@ class SmartBudgetRecommender:
|
|
| 205 |
category_objid = category_id
|
| 206 |
|
| 207 |
# Try to find budgets with nested category structures
|
|
|
|
| 208 |
nested_query = {
|
| 209 |
"$and": [
|
| 210 |
{"$or": user_conditions},
|
| 211 |
{
|
| 212 |
"$or": [
|
| 213 |
-
|
| 214 |
-
{"headCategories": {"$elemMatch": {"headCategory": category_id}}},
|
| 215 |
{"headCategories": {"$elemMatch": {"categories": {"$elemMatch": {"category": category_objid}}}}},
|
| 216 |
{"headCategories": {"$elemMatch": {"categories": {"$elemMatch": {"category": category_id}}}}},
|
|
|
|
|
|
|
|
|
|
| 217 |
]
|
| 218 |
}
|
| 219 |
]
|
|
@@ -221,9 +200,40 @@ class SmartBudgetRecommender:
|
|
| 221 |
nested_count = self.db.budgets.count_documents(nested_query)
|
| 222 |
if nested_count > 0:
|
| 223 |
print(f"✅ Found {nested_count} budget(s) with nested category structure for user {user_id} with category_id {category_id}")
|
|
|
|
| 224 |
return True
|
|
|
|
|
|
|
| 225 |
except Exception as e:
|
| 226 |
print(f"⚠️ Error checking nested categories: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# Debug: Check if user has ANY budgets at all and what categories they have
|
| 229 |
try:
|
|
|
|
| 171 |
{"headCategories.categories.category": category_id},
|
| 172 |
]
|
| 173 |
|
| 174 |
+
# FIRST: Check nested structure (headCategories[].categories[].category) - most common case
|
| 175 |
+
# Budgets have structure: headCategories[].categories[].category (ObjectId)
|
| 176 |
+
print(f"🔍 Checking nested structure: headCategories[].categories[].category for category_id {category_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
try:
|
| 178 |
try:
|
| 179 |
category_objid = ObjectId(category_id)
|
|
|
|
| 181 |
category_objid = category_id
|
| 182 |
|
| 183 |
# Try to find budgets with nested category structures
|
| 184 |
+
# Structure: headCategories[].categories[].category
|
| 185 |
nested_query = {
|
| 186 |
"$and": [
|
| 187 |
{"$or": user_conditions},
|
| 188 |
{
|
| 189 |
"$or": [
|
| 190 |
+
# Check if category_id is in headCategories[].categories[].category
|
|
|
|
| 191 |
{"headCategories": {"$elemMatch": {"categories": {"$elemMatch": {"category": category_objid}}}}},
|
| 192 |
{"headCategories": {"$elemMatch": {"categories": {"$elemMatch": {"category": category_id}}}}},
|
| 193 |
+
# Also check if category_id is a headCategory itself
|
| 194 |
+
{"headCategories": {"$elemMatch": {"headCategory": category_objid}}},
|
| 195 |
+
{"headCategories": {"$elemMatch": {"headCategory": category_id}}},
|
| 196 |
]
|
| 197 |
}
|
| 198 |
]
|
|
|
|
| 200 |
nested_count = self.db.budgets.count_documents(nested_query)
|
| 201 |
if nested_count > 0:
|
| 202 |
print(f"✅ Found {nested_count} budget(s) with nested category structure for user {user_id} with category_id {category_id}")
|
| 203 |
+
print(f" Nested query matched: headCategories[].categories[].category = {category_id}")
|
| 204 |
return True
|
| 205 |
+
else:
|
| 206 |
+
print(f"🔍 Nested query found 0 budgets for category_id {category_id}")
|
| 207 |
except Exception as e:
|
| 208 |
print(f"⚠️ Error checking nested categories: {e}")
|
| 209 |
+
import traceback
|
| 210 |
+
traceback.print_exc()
|
| 211 |
+
|
| 212 |
+
# SECOND: Combine user and category conditions (direct fields)
|
| 213 |
+
print(f"🔍 Checking direct category fields for category_id {category_id}")
|
| 214 |
+
for user_cond in user_conditions:
|
| 215 |
+
for cat_cond in category_conditions:
|
| 216 |
+
budget_query = {**user_cond, **cat_cond}
|
| 217 |
+
budget_count = self.db.budgets.count_documents(budget_query)
|
| 218 |
+
if budget_count > 0:
|
| 219 |
+
print(f"✅ Found {budget_count} budget(s) for user {user_id} with category_id {category_id}")
|
| 220 |
+
print(f" Query that matched: {budget_query}")
|
| 221 |
+
return True
|
| 222 |
+
|
| 223 |
+
# THIRD: Also try a more comprehensive query using $and and $or
|
| 224 |
+
try:
|
| 225 |
+
comprehensive_query = {
|
| 226 |
+
"$and": [
|
| 227 |
+
{"$or": user_conditions},
|
| 228 |
+
{"$or": category_conditions}
|
| 229 |
+
]
|
| 230 |
+
}
|
| 231 |
+
budget_count = self.db.budgets.count_documents(comprehensive_query)
|
| 232 |
+
if budget_count > 0:
|
| 233 |
+
print(f"✅ Found {budget_count} budget(s) using comprehensive query for user {user_id} with category_id {category_id}")
|
| 234 |
+
return True
|
| 235 |
+
except Exception as e:
|
| 236 |
+
print(f"⚠️ Error with comprehensive query: {e}")
|
| 237 |
|
| 238 |
# Debug: Check if user has ANY budgets at all and what categories they have
|
| 239 |
try:
|