From 708a45d43c63a0d7a11b51ed8a0312762e343615 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 29 Jan 2021 01:09:06 +0100 Subject: [PATCH] [Plugin] balance: Added function and route for list of transactions --- flaschengeist/plugins/balance/__init__.py | 42 +++++++++++++++++++ .../plugins/balance/balance_controller.py | 20 +++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 54a0202..71c0bcf 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -160,6 +160,48 @@ def get_balance(userid, current_session: Session): return {"credit": balance[0], "debit": balance[1], "balance": balance[2]} +@balance_bp.route("/users//balance/transactions", methods=["GET"]) +@login_required(permission=permissions.SHOW) +def get_transactions(userid, current_session: Session): + """Get transactions of user, optionally filtered + + Route: ``/users//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//balance", methods=["PUT"]) @login_required() def change_balance(userid, current_session: Session): diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index 3bbb820..e2732ce 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -36,7 +36,6 @@ def get_balance(user, start: datetime = None, end: datetime = None): credit = ( db.session.query(func.sum(Transaction.amount)) .filter(Transaction.receiver_ == user) - .filter(Transaction._reversal_id.is_(None)) # ignore reverted transactions .filter(start <= Transaction.time) .filter(Transaction.time <= end) .scalar() @@ -45,7 +44,6 @@ def get_balance(user, start: datetime = None, end: datetime = None): debit = ( db.session.query(func.sum(Transaction.amount)) .filter(Transaction.sender_ == user) - .filter(Transaction._reversal_id.is_(None)) # ignore reverted transactions .filter(start <= Transaction.time) .filter(Transaction.time <= end) .scalar() @@ -102,6 +100,20 @@ def get_transaction(transaction_id) -> 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): """Reverse a transaction @@ -109,10 +121,10 @@ def reverse_transaction(transaction: Transaction, author: User): transaction: Transaction to reverse author: User that wants the transaction to be reverted """ - if transaction.reversal: + if transaction.reversal_: raise Conflict reversal = send(transaction.receiver_, transaction.sender_, transaction.amount, author) - reversal.reversal = transaction + reversal.reversal_ = transaction transaction.reversal = reversal db.session.commit() return reversal