Example #1
0
    def get(self):
        args = by_id.parse_args()
        succ, username = user_client.get_username_from_token(
            request.headers.get("Authorization"))

        transaction = coll_trans.find({
            "user": username,
            "_id": ObjectId(args["id"])
        })
        transaction = stringify_ids(list(transaction))
        return transaction[0], 200
Example #2
0
    def get(self):
        args = between_dates.parse_args()
        succ, username = user_client.get_username_from_token(
            request.headers.get("Authorization"))
        find_query = {
            "user": username,
            "date_trans": {
                "$gte": int(args["startDate"]),
                "$lte": int(args["endDate"])
            }
        }

        if args["toAccount"]:
            find_query["to_account"] = int(args["toAccount"])
        if args["fromAccount"]:
            find_query["from_account"] = int(args["fromAccount"])

        transactions = coll_trans.find(find_query)
        transactions = stringify_ids(list(transactions))
        return transactions, 200
Example #3
0
    def resolve_transactions(self,
                             info,
                             start_date=0,
                             end_date=(2**53 - 1),
                             amount=None,
                             desc=None,
                             from_account=None,
                             to_account=None):
        token = get_token_from_info(info, require_token=not dev_user
                                    ) or user_client.get_valid_token(dev_user)

        succ, username = user_client.get_username_from_token(token)
        if not succ:
            raise Exception(f"User {username} does not exist")

        if user_client.token_has_privileges(token,
                                            ["transactions", "economy"]):
            query = {
                "user": username,
                "date_trans": {
                    "$gte": start_date,
                    "$lte": end_date
                }
            }
            if amount:
                query["amount"] = amount
            if desc:
                query["desc"] = {"$regex": ".*" + desc + ".*"}
            if from_account:
                query["from_account"] = from_account.number
            if to_account:
                query["to_account"] = to_account.number
            return [
                Transaction(**t) for t in list(
                    coll_trans.find(query, {"_id": 0},
                                    sort=[("date_trans", 1)]))
            ]

        raise Exception(
            "User must have privileges 'transactions', 'accounts' and 'economy'"
        )
Example #4
0
    def resolve_from_to(self, info, from_account=None, to_account=None):
        token = get_token_from_info(info, require_token=not dev_user
                                    ) or user_client.get_valid_token(dev_user)

        succ, username = user_client.get_username_from_token(token)
        if not succ:
            raise Exception(f"User {username} does not exist")

        query = {
            "user": username,
        }

        if from_account:
            query["from_account"] = from_account.number
        if to_account:
            query["to_account"] = to_account.number

        transactions = list(coll_trans.find(query, {"_id": 0}))

        return FromToCounter(amount=sum([t["amount"] for t in transactions]),
                             amount_of_transactions=len(transactions))
Example #5
0
def get_economy_transactions():
    sample_args_1 = {
        "startDate": {
            "required": True,
            "allowed_types": [str]
        },
        "endDate": {
            "required": True,
            "allowed_types": [str]
        },
        "toAccount": {
            "required": False,
            "allowed_types": [str]
        },
        "fromAccount": {
            "required": False,
            "allowed_types": [str]
        },
        "username": {
            "required": True,
            "allowed_types": [str]
        }
    }
    sample_args_2 = {
        "id": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        },
        "username": {
            "required": True,
            "allowed_types": [list],
            "list_element": {
                "allowed_types": [str]
            }
        }
    }
    args1 = request.args.to_dict(flat=True)
    succ1, errors1 = check(sample_args_1, args1)
    args2 = request.args.to_dict(flat=False)
    succ2, errors2 = check(sample_args_2, args2)
    if not succ1 and not succ2:
        return make_response(jsonify(errors1 + errors2), 400)
    elif succ2 and not succ1:
        username = args2["username"][0]
        l = [x for x in args2["id"] if len(x) == 24]
        transactions = coll_trans.find({
            "user": username,
            "_id": {
                "$in": [ObjectId(i) for i in l]
            }
        })
        transactions = stringify_ids(list(transactions))
        return make_response(jsonify(transactions), 200)
    elif succ1 and not succ2:
        username = args1["username"]
        find_query = {
            "user": username,
            "date_trans": {
                "$gte": int(args1["startDate"]),
                "$lte": int(args1["endDate"])
            }
        }

        if "toAccount" in args1:
            find_query["to_account"] = int(args1["toAccount"])
        if "fromAccount" in args1:
            find_query["from_account"] = int(args1["fromAccount"])

        transactions = coll_trans.find(find_query)
        transactions = stringify_ids(list(transactions))
        return make_response(jsonify(transactions), 200)
    elif succ1 and succ2:
        return make_response(
            jsonify([
                "ERROR: You may not specify a transaction id if retrieving using dates and accounts."
            ]), 400)
    else:
        return make_response(jsonify(errors1 + errors2), 400)