[Plugin] balance: Added function and route for list of transactions

This commit is contained in:
Ferdinand Thiessen 2021-01-29 01:09:06 +01:00
parent 7ccae7e888
commit 708a45d43c
2 changed files with 58 additions and 4 deletions

View File

@ -160,6 +160,48 @@ def get_balance(userid, current_session: Session):
return {"credit": balance[0], "debit": balance[1], "balance": balance[2]} return {"credit": balance[0], "debit": balance[1], "balance": balance[2]}
@balance_bp.route("/users/<userid>/balance/transactions", methods=["GET"])
@login_required(permission=permissions.SHOW)
def get_transactions(userid, current_session: Session):
"""Get transactions of user, optionally filtered
Route: ``/users/<userid>/balance/transactions`` | Method: ``GET``
GET-parameters: ```{from?: string, to?: string, limit?: int, offset?: int}```
Args:
userid: Userid of user to get transactions from
current_session: Session sent with Authorization Header
Returns:
JSON encoded list of transactions or HTTP error
"""
if userid != current_session._user.userid and not current_session._user.has_permission(permissions.SHOW_OTHER):
raise Forbidden
# Might raise NotFound
user = userController.get_user(userid)
start = request.args.get("from")
if start:
start = from_iso_format(start)
end = request.args.get("to")
if end:
end = from_iso_format(end)
limit = request.args.get("limit")
offset = request.args.get("offset")
try:
if limit:
limit = int(limit)
if offset:
offset = int(offset)
except ValueError:
raise BadRequest
transactions = balance_controller.get_transactions(user, start, end, limit, offset)
return jsonify(transactions)
@balance_bp.route("/users/<userid>/balance", methods=["PUT"]) @balance_bp.route("/users/<userid>/balance", methods=["PUT"])
@login_required() @login_required()
def change_balance(userid, current_session: Session): def change_balance(userid, current_session: Session):

View File

@ -36,7 +36,6 @@ def get_balance(user, start: datetime = None, end: datetime = None):
credit = ( credit = (
db.session.query(func.sum(Transaction.amount)) db.session.query(func.sum(Transaction.amount))
.filter(Transaction.receiver_ == user) .filter(Transaction.receiver_ == user)
.filter(Transaction._reversal_id.is_(None)) # ignore reverted transactions
.filter(start <= Transaction.time) .filter(start <= Transaction.time)
.filter(Transaction.time <= end) .filter(Transaction.time <= end)
.scalar() .scalar()
@ -45,7 +44,6 @@ def get_balance(user, start: datetime = None, end: datetime = None):
debit = ( debit = (
db.session.query(func.sum(Transaction.amount)) db.session.query(func.sum(Transaction.amount))
.filter(Transaction.sender_ == user) .filter(Transaction.sender_ == user)
.filter(Transaction._reversal_id.is_(None)) # ignore reverted transactions
.filter(start <= Transaction.time) .filter(start <= Transaction.time)
.filter(Transaction.time <= end) .filter(Transaction.time <= end)
.scalar() .scalar()
@ -102,6 +100,20 @@ def get_transaction(transaction_id) -> Transaction:
return transaction return transaction
def get_transactions(user, start=None, end=None, limit=None, offset=None):
query = Transaction.query.filter((Transaction.sender_ == user) | (Transaction.receiver_ == user))
if start:
query = query.filter(start <= Transaction.time)
if end:
query = query.filter(Transaction.time >= end)
query = query.order_by(Transaction.time.desc())
if offset is not None:
query = query.offset(offset)
if limit is not None:
query = query.limit(limit)
return query.all()
def reverse_transaction(transaction: Transaction, author: User): def reverse_transaction(transaction: Transaction, author: User):
"""Reverse a transaction """Reverse a transaction
@ -109,10 +121,10 @@ def reverse_transaction(transaction: Transaction, author: User):
transaction: Transaction to reverse transaction: Transaction to reverse
author: User that wants the transaction to be reverted author: User that wants the transaction to be reverted
""" """
if transaction.reversal: if transaction.reversal_:
raise Conflict raise Conflict
reversal = send(transaction.receiver_, transaction.sender_, transaction.amount, author) reversal = send(transaction.receiver_, transaction.sender_, transaction.amount, author)
reversal.reversal = transaction reversal.reversal_ = transaction
transaction.reversal = reversal transaction.reversal = reversal
db.session.commit() db.session.commit()
return reversal return reversal