예제 #1
0
def delete_transaction(transaction_id):  # noqa: E501
    """removes a transaction from an account

     # noqa: E501

    :param transaction_id: transaction item unique identifier
    :type transaction_id: int

    :rtype: None
    """

    trans_to_delete = db_session.query(Transaction).filter(
        Transaction.transaction_id == transaction_id).one_or_none()

    # Make sure the item to delete exists
    if trans_to_delete is not None:
        # If item exists, delete it
        db_session.delete(trans_to_delete)
        db_session.commit()
    else:
        # If item doesn't exist, indicate so and return 404
        return jsonify({"message": "item does not exist"}), 404

    # Verify that the delete occurred
    if db_session.query(Transaction).filter(
            Transaction.transaction_id == transaction_id).count():
        return jsonify({"message": "delete did not work"}), 500

    return jsonify({'message': "success"}), 200
예제 #2
0
def delete_user(user_id):  # noqa: E501
    """delete a user from the app

     # noqa: E501

    :param user_id: unique user_id
    :type user_id: int

    :rtype: None
    """
    # Delete the user
    db_session.query(User).filter(User.user_id == user_id).delete()
    db_session.commit()

    return jsonify({"message": "user successfully deleted"}), 200
예제 #3
0
def user_all_accounts(user_id):  # noqa: E501
    """Get all accounts associated with a given user

     # noqa: E501

    :param user_id: unique user_id
    :type user_id: int

    :rtype: object
    """

    # Custom filter for getting all accounts where user is a part of
    filter_user = text(":x = ANY(account_users::int[])")
    # Query on accounts using custom filter where the param is user_id, order by last_updated desc for ordering
    q = db_session.query(Account).filter(filter_user).params(x=user_id).order_by(Account.last_updated.desc()).all()
    accounts_list = [account.dump() for account in q]
    # Get the balance for each account
    for account in accounts_list:
        account['acc_balance'] = get_user_account_balance(user_id, account['account_id'])
        account['real_names'] = []
        for uid in account['account_users']:
            if uid == user_id:
                continue
            # Get the first name, last initial for the other users in the account
            account['real_names'].append(get_user_first_last_name(uid))

    return jsonify(accounts_list), 200
예제 #4
0
def set_account_last_updated(account_id, timestamp):

    q = db_session.query(Account).filter(Account.account_id == account_id).one()

    q.last_updated = timestamp

    db_session.commit()
예제 #5
0
def get_user_account_balance(user_id, account_id):
    """
    will retrive the balance of an account for a user
    """
    q = db_session.query(func.public.calc_account_balance(user_id, account_id)).one()
    acc_bal = q[0]
    return acc_bal
예제 #6
0
def remove_account(account_id):  # noqa: E501
    """deletes an account given the account id

     # noqa: E501

    :param account_id: unique account identifier
    :type account_id: int

    :rtype: None
    """

    # First delete all transactions associated with the account
    db_session.query(Transaction).filter(Transaction.account_id == account_id).delete()
    # Once the account is empty, remove it
    acc_q = db_session.query(Account).filter(Account.account_id == account_id).delete()
    # Commit the changes
    db_session.commit()

    return jsonify({"message": "account emptied and deleted"}), 200
예제 #7
0
def query_all_users():  # noqa: E501
    """get all registered users

     # noqa: E501


    :rtype: None
    """
    # set instance of database management
    q = db_session.query(User)
    return jsonify([u.dump() for u in q])
예제 #8
0
def get_user_id(email):
    """
    will retrieve a user_id from the database given a user's email.

    returns the user_id if it is found, None otherwise
    """
    # set instance of database management
    q = db_session.query(User).filter_by(email=email).one_or_none()
    if q is not None:
        return q.user_id
    else:
        return -1
예제 #9
0
def user_net_balance(user_id):  # noqa: E501
    """retrieve net balance for a user

     # noqa: E501

    :param user_id: unique user identifier
    :type user_id: int

    :rtype: None
    """
    q = db_session.query(
        User.net_balance).filter(User.user_id == user_id).one()
    return jsonify({"netBalance": q[0]})
예제 #10
0
def get_user_first_last_name(user_id):
    """
    Returns the first name and last initial as a string for a given user
    """

    try:
        q = db_session.query(User).filter(User.user_id == user_id).one()
        # First name, last initial
        first_last_name = q.first_name + " " + q.last_name[0] + "."
    except:
        first_last_name = "unknown"


    return first_last_name
예제 #11
0
def get_all_account_users_names(account_id, user_id):
    """
    Returns a list of users who are in the account (not the current user)
    """

    q = db_session.query(Account).filter(Account.account_id == account_id).one()

    account = q.dump()
    account['account_user_names'] = []

    for user in account['account_users']:
        if user != user_id:
            account['account_user_names'].append(get_user_first_last_name(user))

    return account['account_user_names']
예제 #12
0
def find_all_transactions(account_id, user_id):  # noqa: E501
    """get all transactions within an account

     # noqa: E501

    :param account_id:
    :type account_id: int

    :rtype: None
    """

    query_filter = ""

    # query the database
    q = db_session.query(Transaction).filter(
        Transaction.account_id == account_id).order_by(
            Transaction.transaction_timestamp.desc()).all()
    transactions_list = [transaction.dump() for transaction in q]
    # find number of users in the account
    account_size = get_account_size(account_id)
    # Iterate through transactions list, dividing non-user transactions by account_size-1
    # also make them negative, because they OWE someone else money
    for transaction in transactions_list:
        if transaction['user_owed'] != user_id:
            transaction['amount'] = -transaction['amount'] / (account_size - 1)
            transaction['user_owed_name'] = get_user_first_last_name(
                transaction['user_owed'])
        else:
            transaction['user_owed_name'] = "You"
    return_object = {
        "account_balance": get_user_account_balance(user_id, account_id),
        "transactions_list": transactions_list,
        "account_members": get_all_account_users_names(account_id, user_id)
    }

    return jsonify(return_object), 200
예제 #13
0
def get_account_size(account_id):

    q = db_session.query(Account).filter(Account.account_id == account_id).one()

    return len(q.account_users)