Commit
·
c45fa23
1
Parent(s):
caeed61
Match budgets by createdBy ObjectId
Browse files- app/smart_recommendation.py +10 -8
app/smart_recommendation.py
CHANGED
|
@@ -7,6 +7,7 @@ from typing import Dict, List
|
|
| 7 |
|
| 8 |
import requests
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
| 11 |
from app.models import BudgetRecommendation, CategoryExpense
|
| 12 |
|
|
@@ -255,14 +256,15 @@ class SmartBudgetRecommender:
|
|
| 255 |
We treat each budget document (e.g. \"Office Maintenance\", \"LOGICGO\")
|
| 256 |
as a spending category and derive an \"average\" from its amounts.
|
| 257 |
"""
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
| 266 |
|
| 267 |
if not budgets:
|
| 268 |
return {}
|
|
|
|
| 7 |
|
| 8 |
import requests
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
from bson import ObjectId
|
| 11 |
|
| 12 |
from app.models import BudgetRecommendation, CategoryExpense
|
| 13 |
|
|
|
|
| 256 |
We treat each budget document (e.g. \"Office Maintenance\", \"LOGICGO\")
|
| 257 |
as a spending category and derive an \"average\" from its amounts.
|
| 258 |
"""
|
| 259 |
+
# createdBy is stored as ObjectId in WalletSync, while user_id is a string.
|
| 260 |
+
# Try to cast to ObjectId; if it fails, fall back to matching the raw string.
|
| 261 |
+
query: Dict = {"status": "OPEN"}
|
| 262 |
+
try:
|
| 263 |
+
query["createdBy"] = ObjectId(user_id)
|
| 264 |
+
except Exception:
|
| 265 |
+
query["createdBy"] = user_id
|
| 266 |
+
|
| 267 |
+
budgets = list(self.db.budgets.find(query))
|
| 268 |
|
| 269 |
if not budgets:
|
| 270 |
return {}
|