Spaces:
Build error
Build error
File size: 860 Bytes
07fbc67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
|