Spaces:
Build error
Build error
from app.db.database import db | |
from app.schemas.user import TransactionResponse | |
from bson.objectid import ObjectId | |
from datetime import datetime | |
async def record_transaction(user_id: str, amount: float, description: str): | |
transaction = { | |
"user_id": ObjectId(user_id), | |
"amount": amount, | |
"description": description, | |
"timestamp": datetime.utcnow() | |
} | |
result = await db.transactions.insert_one(transaction) | |
transaction["_id"] = result.inserted_id | |
transaction["id"] = str(transaction.pop("_id")) | |
return transaction | |
async def get_user_transactions(user_id: str): | |
cursor = db.transactions.find({"user_id": ObjectId(user_id)}) | |
transactions = [] | |
async for transaction in cursor: | |
transaction["id"] = str(transaction.pop("_id")) | |
transactions.append(transaction) | |
return transactions | |