Spaces:
Runtime error
Runtime error
| from typing import List | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from backend.app.core.database import get_db | |
| from backend.app.routers.auth import get_current_user | |
| from backend.app.repositories.wallet import WalletRepository | |
| from backend.app.schemas.wallet import WalletTopup, WalletTransactionResponse | |
| from backend.app.models.users import User | |
| router = APIRouter(prefix="/wallet", tags=["wallet"]) | |
| def topup_wallet( | |
| data: WalletTopup, | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """ | |
| Simulate a UPI/Razorpay wallet top-up, crediting the customer's balance. | |
| """ | |
| wallet_repo = WalletRepository(db) | |
| # Increase wallet balance | |
| new_balance = wallet_repo.update_balance(current_user.id, data.amount) | |
| # Record transaction ledger log | |
| wallet_repo.add_transaction( | |
| user_id=current_user.id, | |
| label="Wallet top-up via UPI", | |
| amount=data.amount, | |
| type_="credit" | |
| ) | |
| return { | |
| "message": f"Successfully recharged wallet by {data.amount} INR.", | |
| "wallet_balance": new_balance | |
| } | |
| def get_transactions( | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """ | |
| Retrieve the immutable ledger history for the current user. | |
| """ | |
| wallet_repo = WalletRepository(db) | |
| return wallet_repo.get_user_transactions(current_user.id) | |
| def get_balance( | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """ | |
| Retrieve the current wallet balance of the authenticated user. | |
| """ | |
| wallet_repo = WalletRepository(db) | |
| if current_user.role == "customer": | |
| profile = wallet_repo.get_customer_profile(current_user.id) | |
| balance = profile.wallet_balance if profile else 8200 | |
| elif current_user.role == "worker": | |
| # Workers can have earnings in their profile wallet balance for viva demo convenience | |
| profile = wallet_repo.get_customer_profile(current_user.id) | |
| balance = profile.wallet_balance if profile else 0 | |
| else: | |
| balance = 0 | |
| return {"wallet_balance": balance} | |