Spaces:
Running
Running
Commit ·
7deddcc
1
Parent(s): 0d04bdc
fix the member not found check
Browse files- app/constants/categories.py +10 -1
- app/routers/categorybudget.py +25 -8
- app/routers/expense.py +18 -40
- app/utils/member_utils.py +29 -0
app/constants/categories.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
{"name": "Groceries", "icon": "🛒", "budget": 0, "spent": 0, "remaining": 0},
|
| 3 |
{"name": "Food", "icon": "🍽", "budget": 0, "spent": 0, "remaining": 0},
|
| 4 |
{"name": "Transport", "icon": "🚌", "budget": 0, "spent": 0, "remaining": 0},
|
|
@@ -11,4 +11,13 @@ CATEGORIES = [
|
|
| 11 |
{"name": "Insurance", "icon": "🛡", "budget": 0, "spent": 0, "remaining": 0},
|
| 12 |
]
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 1 |
+
HEAD_CATEGORIES = [
|
| 2 |
{"name": "Groceries", "icon": "🛒", "budget": 0, "spent": 0, "remaining": 0},
|
| 3 |
{"name": "Food", "icon": "🍽", "budget": 0, "spent": 0, "remaining": 0},
|
| 4 |
{"name": "Transport", "icon": "🚌", "budget": 0, "spent": 0, "remaining": 0},
|
|
|
|
| 11 |
{"name": "Insurance", "icon": "🛡", "budget": 0, "spent": 0, "remaining": 0},
|
| 12 |
]
|
| 13 |
|
| 14 |
+
MEMBER_CATEGORIES = [
|
| 15 |
+
{"name": "Food", "icon": "🍽", "budget": 0, "spent": 0, "remaining": 0},
|
| 16 |
+
{"name": "Transport", "icon": "🚌", "budget": 0, "spent": 0, "remaining": 0},
|
| 17 |
+
{"name": "Entertainment", "icon": "🎉", "budget": 0, "spent": 0, "remaining": 0},
|
| 18 |
+
{"name": "Education", "icon": "📚", "budget": 0, "spent": 0, "remaining": 0},
|
| 19 |
+
{"name": "Gifts", "icon": "🎁", "budget": 0, "spent": 0, "remaining": 0},
|
| 20 |
+
{"name": "Rent", "icon": "🏠", "budget": 0, "spent": 0, "remaining": 0},
|
| 21 |
+
|
| 22 |
+
]
|
| 23 |
|
app/routers/categorybudget.py
CHANGED
|
@@ -7,10 +7,12 @@ from app.db.categories_budget import CategoryBudget # <-- import
|
|
| 7 |
from app.deps.deps import get_current_user
|
| 8 |
from app.schemas.schemas import UpdateCategoryBudgetRequest # <-- import
|
| 9 |
from app.db.models_family import Family, FamilyMember
|
| 10 |
-
|
|
|
|
| 11 |
router = APIRouter(prefix="/categories", tags=["categories"])
|
| 12 |
|
| 13 |
|
|
|
|
| 14 |
@router.post("/update-budget")
|
| 15 |
def update_category_budget(
|
| 16 |
payload: UpdateCategoryBudgetRequest,
|
|
@@ -23,24 +25,39 @@ def update_category_budget(
|
|
| 23 |
if current_user.role == "head":
|
| 24 |
scope = "family"
|
| 25 |
owner_id = None
|
|
|
|
| 26 |
|
| 27 |
elif current_user.role == "member":
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
raise HTTPException(400, "Member record not found")
|
| 35 |
|
| 36 |
scope = "member"
|
| 37 |
owner_id = fm.id
|
|
|
|
| 38 |
|
| 39 |
else:
|
| 40 |
raise HTTPException(403, "Invalid role")
|
| 41 |
|
| 42 |
# ---------------- UPDATE BUDGETS ----------------
|
| 43 |
for item in payload.budgets:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
row = db.query(CategoryBudget).filter(
|
| 45 |
CategoryBudget.family_code == family_code,
|
| 46 |
CategoryBudget.category_name == item.category,
|
|
|
|
| 7 |
from app.deps.deps import get_current_user
|
| 8 |
from app.schemas.schemas import UpdateCategoryBudgetRequest # <-- import
|
| 9 |
from app.db.models_family import Family, FamilyMember
|
| 10 |
+
from app.utils.member_utils import get_or_assign_member
|
| 11 |
+
from app.constants.categories import HEAD_CATEGORIES, MEMBER_CATEGORIES
|
| 12 |
router = APIRouter(prefix="/categories", tags=["categories"])
|
| 13 |
|
| 14 |
|
| 15 |
+
@router.post("/update-budget")
|
| 16 |
@router.post("/update-budget")
|
| 17 |
def update_category_budget(
|
| 18 |
payload: UpdateCategoryBudgetRequest,
|
|
|
|
| 25 |
if current_user.role == "head":
|
| 26 |
scope = "family"
|
| 27 |
owner_id = None
|
| 28 |
+
allowed_categories = [c["name"] for c in HEAD_CATEGORIES]
|
| 29 |
|
| 30 |
elif current_user.role == "member":
|
| 31 |
+
# 🔥 auto-link or fetch member slot
|
| 32 |
+
fm = get_or_assign_member(
|
| 33 |
+
db=db,
|
| 34 |
+
family_code=family_code,
|
| 35 |
+
user_id=current_user.id
|
| 36 |
+
)
|
|
|
|
| 37 |
|
| 38 |
scope = "member"
|
| 39 |
owner_id = fm.id
|
| 40 |
+
allowed_categories = [c["name"] for c in MEMBER_CATEGORIES]
|
| 41 |
|
| 42 |
else:
|
| 43 |
raise HTTPException(403, "Invalid role")
|
| 44 |
|
| 45 |
# ---------------- UPDATE BUDGETS ----------------
|
| 46 |
for item in payload.budgets:
|
| 47 |
+
|
| 48 |
+
# 🔒 category-level security
|
| 49 |
+
if item.category not in allowed_categories:
|
| 50 |
+
raise HTTPException(
|
| 51 |
+
status_code=400,
|
| 52 |
+
detail=f"Category '{item.category}' not allowed for this role"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if item.budget < 0:
|
| 56 |
+
raise HTTPException(
|
| 57 |
+
status_code=400,
|
| 58 |
+
detail="Budget must be >= 0"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
row = db.query(CategoryBudget).filter(
|
| 62 |
CategoryBudget.family_code == family_code,
|
| 63 |
CategoryBudget.category_name == item.category,
|
app/routers/expense.py
CHANGED
|
@@ -9,33 +9,12 @@ from app.db.models_expenses import ExpenseDB
|
|
| 9 |
from app.deps.deps import get_current_user
|
| 10 |
from app.schemas.schemas import AddExpenseRequest
|
| 11 |
from app.db.categories_budget import CategoryBudget
|
|
|
|
|
|
|
| 12 |
# from app.constants import CATEGORIES
|
| 13 |
|
| 14 |
router = APIRouter(prefix="/expense", tags=["expense"])
|
| 15 |
|
| 16 |
-
HEAD_CATEGORIES = [
|
| 17 |
-
{"name": "Groceries", "icon": "🛒", "budget": 0, "spent": 0, "remaining": 0},
|
| 18 |
-
{"name": "Food", "icon": "🍽", "budget": 0, "spent": 0, "remaining": 0},
|
| 19 |
-
{"name": "Transport", "icon": "🚌", "budget": 0, "spent": 0, "remaining": 0},
|
| 20 |
-
{"name": "Health", "icon": "💊", "budget": 0, "spent": 0, "remaining": 0},
|
| 21 |
-
{"name": "Gifts", "icon": "🎁", "budget": 0, "spent": 0, "remaining": 0},
|
| 22 |
-
{"name": "Rent", "icon": "🏠", "budget": 0, "spent": 0, "remaining": 0},
|
| 23 |
-
{"name": "Utilities", "icon": "⚡", "budget": 0, "spent": 0, "remaining": 0},
|
| 24 |
-
{"name": "Entertainment", "icon": "🎉", "budget": 0, "spent": 0, "remaining": 0},
|
| 25 |
-
{"name": "Education", "icon": "📚", "budget": 0, "spent": 0, "remaining": 0},
|
| 26 |
-
{"name": "Insurance", "icon": "🛡", "budget": 0, "spent": 0, "remaining": 0},
|
| 27 |
-
]
|
| 28 |
-
|
| 29 |
-
MEMBER_CATEGORIES = [
|
| 30 |
-
{"name": "Food", "icon": "🍽", "budget": 0, "spent": 0, "remaining": 0},
|
| 31 |
-
{"name": "Transport", "icon": "🚌", "budget": 0, "spent": 0, "remaining": 0},
|
| 32 |
-
{"name": "Entertainment", "icon": "🎉", "budget": 0, "spent": 0, "remaining": 0},
|
| 33 |
-
{"name": "Education", "icon": "📚", "budget": 0, "spent": 0, "remaining": 0},
|
| 34 |
-
{"name": "Gifts", "icon": "🎁", "budget": 0, "spent": 0, "remaining": 0},
|
| 35 |
-
{"name": "Rent", "icon": "🏠", "budget": 0, "spent": 0, "remaining": 0},
|
| 36 |
-
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
|
| 40 |
@router.get("/categories")
|
| 41 |
def get_categories(
|
|
@@ -46,13 +25,12 @@ def get_categories(
|
|
| 46 |
|
| 47 |
# ---------------- ROLE CHECK ----------------
|
| 48 |
if current_user.role == "member":
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
raise HTTPException(400, "Member record not found")
|
| 56 |
|
| 57 |
scope = "member"
|
| 58 |
owner_id = fm.id
|
|
@@ -70,7 +48,6 @@ def get_categories(
|
|
| 70 |
CategoryBudget.owner_id == owner_id
|
| 71 |
).all()
|
| 72 |
|
| 73 |
-
# Safe lookup
|
| 74 |
db_map = {row.category_name: row for row in rows}
|
| 75 |
|
| 76 |
# ---------------- BUILD RESPONSE ----------------
|
|
@@ -81,6 +58,7 @@ def get_categories(
|
|
| 81 |
icon = cat["icon"]
|
| 82 |
|
| 83 |
row = db_map.get(name)
|
|
|
|
| 84 |
budget = row.budget if row else 0
|
| 85 |
spent = row.spent if row else 0
|
| 86 |
|
|
@@ -98,6 +76,7 @@ def get_categories(
|
|
| 98 |
"categories": result
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
@router.post("/add")
|
| 102 |
def add_expense(
|
| 103 |
payload: AddExpenseRequest,
|
|
@@ -117,7 +96,7 @@ def add_expense(
|
|
| 117 |
allowed_categories = [c["name"] for c in MEMBER_CATEGORIES]
|
| 118 |
else:
|
| 119 |
allowed_categories = [c["name"] for c in HEAD_CATEGORIES]
|
| 120 |
-
|
| 121 |
if payload.category not in allowed_categories:
|
| 122 |
raise HTTPException(400, "Invalid category")
|
| 123 |
|
|
@@ -130,13 +109,12 @@ def add_expense(
|
|
| 130 |
|
| 131 |
# ---------------- MEMBER ----------------
|
| 132 |
if current_user.role == "member":
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
raise HTTPException(400, "Member record not found")
|
| 140 |
|
| 141 |
member_id = fm.id
|
| 142 |
scope = "member"
|
|
@@ -155,6 +133,7 @@ def add_expense(
|
|
| 155 |
|
| 156 |
member_id = chosen.id
|
| 157 |
# scope stays "family"
|
|
|
|
| 158 |
|
| 159 |
# ---------------- CREATE EXPENSE ----------------
|
| 160 |
exp = ExpenseDB(
|
|
@@ -203,7 +182,6 @@ def add_expense(
|
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
-
|
| 207 |
@router.get("/list")
|
| 208 |
def list_expenses(
|
| 209 |
current_user: UserDB = Depends(get_current_user),
|
|
|
|
| 9 |
from app.deps.deps import get_current_user
|
| 10 |
from app.schemas.schemas import AddExpenseRequest
|
| 11 |
from app.db.categories_budget import CategoryBudget
|
| 12 |
+
from app.utils.member_utils import get_or_assign_member
|
| 13 |
+
from app.constants.categories import HEAD_CATEGORIES, MEMBER_CATEGORIES
|
| 14 |
# from app.constants import CATEGORIES
|
| 15 |
|
| 16 |
router = APIRouter(prefix="/expense", tags=["expense"])
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@router.get("/categories")
|
| 20 |
def get_categories(
|
|
|
|
| 25 |
|
| 26 |
# ---------------- ROLE CHECK ----------------
|
| 27 |
if current_user.role == "member":
|
| 28 |
+
# 🔥 Auto-assign or fetch member slot
|
| 29 |
+
fm = get_or_assign_member(
|
| 30 |
+
db=db,
|
| 31 |
+
family_code=family_code,
|
| 32 |
+
user_id=current_user.id
|
| 33 |
+
)
|
|
|
|
| 34 |
|
| 35 |
scope = "member"
|
| 36 |
owner_id = fm.id
|
|
|
|
| 48 |
CategoryBudget.owner_id == owner_id
|
| 49 |
).all()
|
| 50 |
|
|
|
|
| 51 |
db_map = {row.category_name: row for row in rows}
|
| 52 |
|
| 53 |
# ---------------- BUILD RESPONSE ----------------
|
|
|
|
| 58 |
icon = cat["icon"]
|
| 59 |
|
| 60 |
row = db_map.get(name)
|
| 61 |
+
|
| 62 |
budget = row.budget if row else 0
|
| 63 |
spent = row.spent if row else 0
|
| 64 |
|
|
|
|
| 76 |
"categories": result
|
| 77 |
}
|
| 78 |
|
| 79 |
+
|
| 80 |
@router.post("/add")
|
| 81 |
def add_expense(
|
| 82 |
payload: AddExpenseRequest,
|
|
|
|
| 96 |
allowed_categories = [c["name"] for c in MEMBER_CATEGORIES]
|
| 97 |
else:
|
| 98 |
allowed_categories = [c["name"] for c in HEAD_CATEGORIES]
|
| 99 |
+
|
| 100 |
if payload.category not in allowed_categories:
|
| 101 |
raise HTTPException(400, "Invalid category")
|
| 102 |
|
|
|
|
| 109 |
|
| 110 |
# ---------------- MEMBER ----------------
|
| 111 |
if current_user.role == "member":
|
| 112 |
+
# 🔥 Auto-link or fetch member slot
|
| 113 |
+
fm = get_or_assign_member(
|
| 114 |
+
db=db,
|
| 115 |
+
family_code=family.family_code,
|
| 116 |
+
user_id=current_user.id
|
| 117 |
+
)
|
|
|
|
| 118 |
|
| 119 |
member_id = fm.id
|
| 120 |
scope = "member"
|
|
|
|
| 133 |
|
| 134 |
member_id = chosen.id
|
| 135 |
# scope stays "family"
|
| 136 |
+
# owner_id stays None
|
| 137 |
|
| 138 |
# ---------------- CREATE EXPENSE ----------------
|
| 139 |
exp = ExpenseDB(
|
|
|
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
|
|
|
| 185 |
@router.get("/list")
|
| 186 |
def list_expenses(
|
| 187 |
current_user: UserDB = Depends(get_current_user),
|
app/utils/member_utils.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import HTTPException
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
|
| 4 |
+
from app.models.family_member import FamilyMember
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def get_or_assign_member(db: Session, family_code: str, user_id: int) -> FamilyMember:
|
| 8 |
+
# Try already linked member
|
| 9 |
+
fm = db.query(FamilyMember).filter(
|
| 10 |
+
FamilyMember.family_code == family_code,
|
| 11 |
+
FamilyMember.user_id == user_id
|
| 12 |
+
).first()
|
| 13 |
+
|
| 14 |
+
if fm:
|
| 15 |
+
return fm
|
| 16 |
+
|
| 17 |
+
# Assign to empty member slot
|
| 18 |
+
fm = db.query(FamilyMember).filter(
|
| 19 |
+
FamilyMember.family_code == family_code,
|
| 20 |
+
FamilyMember.user_id.is_(None)
|
| 21 |
+
).first()
|
| 22 |
+
|
| 23 |
+
if not fm:
|
| 24 |
+
raise HTTPException(400, "No available member slot")
|
| 25 |
+
|
| 26 |
+
fm.user_id = user_id
|
| 27 |
+
db.commit()
|
| 28 |
+
db.refresh(fm)
|
| 29 |
+
return fm
|