LogicGoInfotechSpaces commited on
Commit
e4b048c
·
1 Parent(s): cb65d6f

Add logic to handle budget _id in get_recommendation_for_category - extract categories and generate recommendations

Browse files
Files changed (1) hide show
  1. app/smart_recommendation.py +68 -1
app/smart_recommendation.py CHANGED
@@ -327,7 +327,7 @@ class SmartBudgetRecommender:
327
 
328
  Args:
329
  user_id: User identifier
330
- category_id: Category ID to get recommendations for
331
  month: Target month (1-12)
332
  year: Target year
333
  budget_amount: Optional current budget amount to use for recommendations
@@ -335,6 +335,73 @@ class SmartBudgetRecommender:
335
  Returns:
336
  List of budget recommendations for the specific category
337
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
  # Get all recommendations for the user
339
  all_recommendations = self.get_recommendations(user_id, month, year)
340
  print(f"🔍 get_recommendation_for_category: Found {len(all_recommendations)} total recommendations for user {user_id}")
 
327
 
328
  Args:
329
  user_id: User identifier
330
+ category_id: Category ID to get recommendations for (can also be a budget _id)
331
  month: Target month (1-12)
332
  year: Target year
333
  budget_amount: Optional current budget amount to use for recommendations
 
335
  Returns:
336
  List of budget recommendations for the specific category
337
  """
338
+ # FIRST: Check if category_id is actually a budget _id
339
+ # If so, extract the budget's data and categories
340
+ try:
341
+ try:
342
+ budget_id_objid = ObjectId(category_id)
343
+ budget_by_id = self.db.budgets.find_one({"_id": budget_id_objid})
344
+ if budget_by_id:
345
+ budget_created_by = budget_by_id.get("createdBy")
346
+ # Check if this budget belongs to the user
347
+ budget_user_match = False
348
+ if budget_created_by:
349
+ if isinstance(budget_created_by, ObjectId):
350
+ budget_user_match = (str(budget_created_by) == str(user_id) or budget_created_by == ObjectId(user_id))
351
+ else:
352
+ budget_user_match = (str(budget_created_by) == str(user_id))
353
+
354
+ if budget_user_match:
355
+ # Extract budget amount and categories
356
+ budget_max_amount = float(budget_by_id.get("maxAmount", 0) or 0)
357
+ budget_spend_amount = float(budget_by_id.get("spendAmount", 0) or 0)
358
+ budget_amount_from_budget = budget_spend_amount if budget_spend_amount > 0 else budget_max_amount
359
+
360
+ # Extract categories from headCategories
361
+ head_categories = budget_by_id.get("headCategories", [])
362
+ category_ids_in_budget = []
363
+ for hc in head_categories:
364
+ if isinstance(hc, dict):
365
+ hc_id = hc.get("headCategory")
366
+ if hc_id:
367
+ category_ids_in_budget.append(str(hc_id))
368
+ nested_cats = hc.get("categories", [])
369
+ for nc in nested_cats:
370
+ if isinstance(nc, dict):
371
+ nc_id = nc.get("category")
372
+ if nc_id:
373
+ category_ids_in_budget.append(str(nc_id))
374
+
375
+ # If budget has categories and amount, generate recommendation
376
+ if category_ids_in_budget and budget_amount_from_budget > 0:
377
+ # Use the first category ID found in the budget
378
+ actual_category_id = category_ids_in_budget[0]
379
+ category_name = self._get_category_name(actual_category_id)
380
+
381
+ # Use budget amount if no budget_amount provided
382
+ if budget_amount is None:
383
+ budget_amount = budget_amount_from_budget
384
+
385
+ # Generate recommendation for this category using the budget amount
386
+ return self._generate_single_recommendation(
387
+ user_id, actual_category_id, category_name,
388
+ month, year, budget_amount, using_budget_amount_only=False
389
+ )
390
+ elif budget_amount_from_budget > 0:
391
+ # Budget exists but no categories - use budget name and amount
392
+ budget_name = budget_by_id.get("name", "Budget")
393
+ if budget_amount is None:
394
+ budget_amount = budget_amount_from_budget
395
+ # Generate recommendation using budget name
396
+ return self._generate_single_recommendation(
397
+ user_id, category_id, budget_name,
398
+ month, year, budget_amount, using_budget_amount_only=False
399
+ )
400
+ except (ValueError, TypeError):
401
+ pass # category_id is not a valid ObjectId, continue with normal check
402
+ except Exception:
403
+ pass # Silently continue if budget check fails
404
+
405
  # Get all recommendations for the user
406
  all_recommendations = self.get_recommendations(user_id, month, year)
407
  print(f"🔍 get_recommendation_for_category: Found {len(all_recommendations)} total recommendations for user {user_id}")