41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
from flask import request
|
|
from flask_restful import Resource
|
|
from models import Transaction
|
|
from config import transaction_collection
|
|
from modules.helpers import missing_fields, get_own_account, to_iso
|
|
|
|
|
|
def create_transaction(acc_num, transaction_type, amount, related_acc_num=None):
|
|
"""یه تراکنش ثبت میکنه (توسط accountModuls صدا زده میشه)."""
|
|
newTransaction = Transaction()
|
|
newTransaction.acc_num = acc_num
|
|
newTransaction.transaction_type = transaction_type
|
|
newTransaction.amount = amount
|
|
newTransaction.related_acc_num = related_acc_num
|
|
transaction_collection.insert_one(newTransaction.__dict__)
|
|
|
|
|
|
class TransactionList(Resource):
|
|
# GET /transaction/list?username=ali&acc_num=1001
|
|
def get(self):
|
|
query = request.args
|
|
missing = missing_fields(query, ["username", "acc_num"])
|
|
if missing:
|
|
return {"message": f"Missing fields: {', '.join(missing)}"}, 400
|
|
username = query["username"]
|
|
acc_num = query["acc_num"]
|
|
|
|
if get_own_account(username, acc_num) is None:
|
|
return {"message": "you are not allowed"}, 403
|
|
|
|
transactions = []
|
|
for t in transaction_collection.find({"acc_num": acc_num}).sort("time", -1):
|
|
transactions.append({
|
|
"acc_num": t["acc_num"],
|
|
"transaction_type": t["transaction_type"],
|
|
"amount": t["amount"],
|
|
"related_acc_num": t.get("related_acc_num"),
|
|
"time": to_iso(t["time"]),
|
|
})
|
|
return transactions
|