LogicGoInfotechSpaces commited on
Commit
4a08c54
·
1 Parent(s): bd8f603

Add check for budget _id when category_id is provided - handle case where user passes budget ID instead of category ID

Browse files
Files changed (1) hide show
  1. app/smart_recommendation.py +51 -0
app/smart_recommendation.py CHANGED
@@ -127,6 +127,57 @@ class SmartBudgetRecommender:
127
  """
128
  print(f"🔍 check_user_has_category_data: Checking for user_id={user_id}, category_id={category_id}")
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  # Build comprehensive user query
131
  user_conditions = []
132
  try:
 
127
  """
128
  print(f"🔍 check_user_has_category_data: Checking for user_id={user_id}, category_id={category_id}")
129
 
130
+ # FIRST: Check if category_id is actually a budget _id
131
+ # If so, find the budget and check if it belongs to the user and has categories
132
+ try:
133
+ try:
134
+ budget_id_objid = ObjectId(category_id)
135
+ # Try to find a budget with this _id
136
+ budget_by_id = self.db.budgets.find_one({"_id": budget_id_objid})
137
+ if budget_by_id:
138
+ budget_created_by = budget_by_id.get("createdBy")
139
+ # Check if this budget belongs to the user
140
+ budget_user_match = (
141
+ str(budget_created_by) == str(user_id) or
142
+ budget_created_by == ObjectId(user_id) if isinstance(budget_created_by, ObjectId) else False
143
+ )
144
+
145
+ if budget_user_match:
146
+ print(f"✅ category_id {category_id} is a budget _id for user {user_id}")
147
+ # Extract all category IDs from this budget's headCategories
148
+ head_categories = budget_by_id.get("headCategories", [])
149
+ category_ids_in_budget = []
150
+ for hc in head_categories:
151
+ if isinstance(hc, dict):
152
+ # Check headCategory itself
153
+ hc_id = hc.get("headCategory")
154
+ if hc_id:
155
+ category_ids_in_budget.append(str(hc_id))
156
+ # Check nested categories
157
+ nested_cats = hc.get("categories", [])
158
+ for nc in nested_cats:
159
+ if isinstance(nc, dict):
160
+ nc_id = nc.get("category")
161
+ if nc_id:
162
+ category_ids_in_budget.append(str(nc_id))
163
+
164
+ print(f"🔍 Found {len(category_ids_in_budget)} category IDs in budget {category_id}")
165
+ # If budget has categories, consider it as having previous data
166
+ if category_ids_in_budget:
167
+ print(f"✅ Budget {category_id} contains categories: {category_ids_in_budget[:5]}...")
168
+ print(f" Budget name: {budget_by_id.get('name', 'Unknown')}, maxAmount: {budget_by_id.get('maxAmount', 0)}")
169
+ return True
170
+ else:
171
+ print(f"⚠️ Budget {category_id} exists but has no categories")
172
+ else:
173
+ print(f"⚠️ Budget {category_id} exists but belongs to different user")
174
+ except (ValueError, TypeError):
175
+ pass # category_id is not a valid ObjectId, continue with normal check
176
+ except Exception as e:
177
+ print(f"⚠️ Error checking if category_id is a budget _id: {e}")
178
+ import traceback
179
+ traceback.print_exc()
180
+
181
  # Build comprehensive user query
182
  user_conditions = []
183
  try: