176 lines
7.2 KiB
Python
176 lines
7.2 KiB
Python
from flask import request
|
|
from flask_restful import Resource
|
|
from pymongo import ReturnDocument
|
|
from models import Account, VALID_ACC_TYPES
|
|
from config import account_collection
|
|
from modules.helpers import (
|
|
get_body, missing_fields, parse_amount, get_active_user, get_own_account,
|
|
)
|
|
from modules.transactionModuls import create_transaction
|
|
|
|
|
|
class CreateAccount(Resource):
|
|
# POST /account/create {"username","acc_num","acc_type"}
|
|
def post(self):
|
|
body = get_body(request)
|
|
missing = missing_fields(body, ["username", "acc_num", "acc_type"])
|
|
if missing:
|
|
return {"message": f"Missing fields: {', '.join(missing)}"}, 400
|
|
username = body["username"]
|
|
acc_num = str(body["acc_num"])
|
|
acc_type = body["acc_type"]
|
|
|
|
if get_active_user(username) is None:
|
|
return {"message": "user not found"}, 404
|
|
if acc_type not in VALID_ACC_TYPES:
|
|
return {"message": f"acc_type must be one of {VALID_ACC_TYPES}"}, 400
|
|
if account_collection.find_one({"acc_num": acc_num}) is not None:
|
|
return {"message": "This account number already exists"}, 409
|
|
|
|
newAccount = Account()
|
|
newAccount.username = username
|
|
newAccount.acc_num = acc_num
|
|
newAccount.acc_type = acc_type
|
|
account_collection.insert_one(newAccount.__dict__)
|
|
return {"message": "account created successfully"}, 201
|
|
|
|
|
|
class AccountList(Resource):
|
|
# GET /account/list?username=ali
|
|
def get(self):
|
|
username = request.args.get("username")
|
|
if not username:
|
|
return {"message": "Missing fields: username"}, 400
|
|
if get_active_user(username) is None:
|
|
return {"message": "user not found"}, 404
|
|
|
|
accounts = []
|
|
for account in account_collection.find({"username": username}):
|
|
accounts.append({
|
|
"acc_type": account["acc_type"],
|
|
"acc_num": account["acc_num"],
|
|
"balance": account["balance"],
|
|
})
|
|
return accounts
|
|
|
|
|
|
class IncreaseBalance(Resource):
|
|
# POST /account/increase {"username","acc_num","amount"}
|
|
def post(self):
|
|
body = get_body(request)
|
|
missing = missing_fields(body, ["username", "acc_num", "amount"])
|
|
if missing:
|
|
return {"message": f"Missing fields: {', '.join(missing)}"}, 400
|
|
username = body["username"]
|
|
acc_num = str(body["acc_num"])
|
|
|
|
amount = parse_amount(body["amount"])
|
|
if amount is None:
|
|
return {"message": "Amount must be a positive integer"}, 400
|
|
|
|
# $inc اتمیکـه: خوندن + نوشتن توی یک عملیات انجام میشه
|
|
updated = account_collection.find_one_and_update(
|
|
{"acc_num": acc_num, "username": username},
|
|
{"$inc": {"balance": amount}},
|
|
return_document=ReturnDocument.AFTER,
|
|
)
|
|
if updated is None:
|
|
return {"message": "you are not allowed"}, 403
|
|
|
|
create_transaction(acc_num, "increase", amount)
|
|
return {"message": f"{amount} dollars increased to {acc_num}",
|
|
"balance": updated["balance"]}
|
|
|
|
|
|
class DecreaseBalance(Resource):
|
|
# POST /account/decrease {"username","acc_num","amount"}
|
|
def post(self):
|
|
body = get_body(request)
|
|
missing = missing_fields(body, ["username", "acc_num", "amount"])
|
|
if missing:
|
|
return {"message": f"Missing fields: {', '.join(missing)}"}, 400
|
|
username = body["username"]
|
|
acc_num = str(body["acc_num"])
|
|
|
|
amount = parse_amount(body["amount"])
|
|
if amount is None:
|
|
return {"message": "Amount must be a positive integer"}, 400
|
|
|
|
if get_own_account(username, acc_num) is None:
|
|
return {"message": "you are not allowed"}, 403
|
|
|
|
# شرط balance >= amount توی خود query هست، پس منفی شدن غیرممکنه
|
|
updated = account_collection.find_one_and_update(
|
|
{"acc_num": acc_num, "username": username, "balance": {"$gte": amount}},
|
|
{"$inc": {"balance": -amount}},
|
|
return_document=ReturnDocument.AFTER,
|
|
)
|
|
if updated is None:
|
|
return {"message": "You dont have enough money in your account"}, 400
|
|
|
|
create_transaction(acc_num, "decrease", amount)
|
|
return {"message": f"{amount} dollars decreased from {acc_num}",
|
|
"balance": updated["balance"]}
|
|
|
|
|
|
class TransferMoney(Resource):
|
|
# POST /account/transfer {"username","source_acc_num","destination_acc_num","amount"}
|
|
def post(self):
|
|
body = get_body(request)
|
|
missing = missing_fields(
|
|
body, ["username", "source_acc_num", "destination_acc_num", "amount"])
|
|
if missing:
|
|
return {"message": f"Missing fields: {', '.join(missing)}"}, 400
|
|
username = body["username"]
|
|
source = str(body["source_acc_num"])
|
|
destination = str(body["destination_acc_num"])
|
|
|
|
amount = parse_amount(body["amount"])
|
|
if amount is None:
|
|
return {"message": "Amount must be a positive integer"}, 400
|
|
|
|
if get_own_account(username, source) is None:
|
|
return {"message": "you are not allowed"}, 403
|
|
if source == destination:
|
|
return {"message": "Source and destination can't be the same"}, 400
|
|
if account_collection.find_one({"acc_num": destination}) is None:
|
|
return {"message": "destination account not found"}, 404
|
|
|
|
# 1) از مبدا کم کن (فقط اگه موجودی کافی باشه)
|
|
updated_source = account_collection.find_one_and_update(
|
|
{"acc_num": source, "username": username, "balance": {"$gte": amount}},
|
|
{"$inc": {"balance": -amount}},
|
|
return_document=ReturnDocument.AFTER,
|
|
)
|
|
if updated_source is None:
|
|
return {"message": "You dont have enough money in your account"}, 400
|
|
|
|
# 2) به مقصد اضافه کن
|
|
result = account_collection.update_one(
|
|
{"acc_num": destination}, {"$inc": {"balance": amount}})
|
|
if result.matched_count == 0:
|
|
# اگه مقصد وسط کار از بین رفت، پول مبدا رو برگردون
|
|
account_collection.update_one(
|
|
{"acc_num": source}, {"$inc": {"balance": amount}})
|
|
return {"message": "transfer failed"}, 500
|
|
|
|
# 3) دو تا تراکنش ثبت کن
|
|
create_transaction(source, "decrease", amount, related_acc_num=destination)
|
|
create_transaction(destination, "increase", amount, related_acc_num=source)
|
|
return {"message": f"{amount} dollars transferred from {source} to {destination}",
|
|
"balance": updated_source["balance"]}
|
|
|
|
|
|
class GetBalance(Resource):
|
|
# GET /account/balance?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
|
|
|
|
account = get_own_account(query["username"], query["acc_num"])
|
|
if account is None:
|
|
return {"message": "you are not allowed"}, 403
|
|
return {"acc_num": account["acc_num"], "balance": account["balance"]}
|