Ejemplo n.º 1
0
def loan_logs():
    # Only Admin Role Can access This
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 3 stands for Admin
    if token[0][4] != 3:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    connection = psycopg2.connect(POSTGRESQL_URI)
    #Fetching All logs
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "SELECT loan_id, cur_state, changeby, log_date, operation_done, loan_interest, loan_quantity, tenture FROM loan_logs"
            )
            result = cursor.fetchall()
    connection.close()
    finalresult = []
    # Prettifying Result
    for temp in result:
        temp = {
            'loan_id': temp[0],
            'cur_state': temp[1],
            'Agent': temp[2],
            'date': temp[3],
            'Operation': temp[4],
            'Interest': temp[5],
            'Amount': temp[6],
            'Tenture': temp[7]
        }
        finalresult.append(temp)
    return json.dumps(finalresult), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 2
0
def edit_roles():
    # Token Verification
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 3 stands for Admin
    if token[0][4] != 3:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    form_data = request.get_json()
    # Fetching and validaing new role
    try:
        user_role = form_data['user_role']
        public_id = form_data['public_id']
    except:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    if user_role < 1 or user_role > 3:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    connection = psycopg2.connect(POSTGRESQL_URI)
    # Updating new roles to database
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "UPDATE register SET userrole=%s WHERE public_id=%s",
                [user_role, public_id])
    connection.close()
    return json.dumps({"status": "success"}), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 3
0
def listuser():
    # Token Verification
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 2 stands for Agent, 3 stands for Admin
    if token[0][4] < 2 or token[0][4] > 3:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Fetching Data with users role
    connection = psycopg2.connect(POSTGRESQL_URI)
    # Fetching Users
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "SELECT public_id, name, email, mob_number, state, city FROM register WHERE userrole = 1"
            )
            result = cursor.fetchall()
    connection.close()
    finalresult = []
    # Prettifying Result
    for temp in result:
        token = {
            'public_id': temp[0],
            'name': temp[1],
            'email': temp[2],
            'mob_number': temp[3],
            'state': temp[4],
            'city': temp[5]
        }
        finalresult.append(token)
    return json.dumps(finalresult), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 4
0
def gen_new_pub_id():
    # Token Verification
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 2 stands for Agent , 3 for Admin
    if token[0][4] < 2 or token[0][4] > 3:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    form_data = request.get_json()
    try:
        public_id = form_data['public_id']
    except:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Generating new Public id
    new_public_id = str(uuid.uuid1())
    connection = psycopg2.connect(POSTGRESQL_URI)
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "UPDATE register SET public_id=%s WHERE public_id=%s",
                [new_public_id, public_id])
    connection.close()
    return json.dumps({"status": "success"}), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 5
0
def loan_approve():
    # Only Admin Role Can access This
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 3 stands for Admin
    if token[0][4] != 3:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    form_data = request.get_json()
    # Checking required fields
    try:
        loan_id = int(form_data['loan_id'])
        loan_state = int(form_data['loan_state'])
    except:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    if loan_id < 1 or loan_state < 1 or loan_state > 3:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    connection = psycopg2.connect(POSTGRESQL_URI)
    # Checking Loan exist if exist fetching details
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "SELECT loan_quantity, loan_interest, tenture FROM loan_request WHERE loan_id=%s LIMIT 1",
                [loan_id])
            result = cursor.fetchall()
    if len(result) < 1:
        connection.close()
        return json.dumps({"status": "invalid_loan"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Updating loan status
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "UPDATE loan_request SET loan_state=%s WHERE loan_id=%s",
                [loan_state, loan_id])
    datelog = datetime.datetime.now()
    if loan_state == 2:
        operation_done = "rejected"
    else:
        operation_done = "approve"
    # inserting this change in loan log table
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "INSERT INTO loan_logs (loan_id, cur_state, changeby, log_date, operation_done, loan_interest, loan_quantity, tenture) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                [
                    loan_id, loan_state, token[0][1], datelog, operation_done,
                    result[0][1], result[0][0], result[0][2]
                ])
    connection.close()
    return json.dumps({"status": "success"}), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 6
0
def cus_viewloan():
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 1 stands for Customer
    if token[0][4] != 1:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Filters
    # 0 stands for ASC 1 for Des
    sortin = request.args.get("sortin")
    loan_state = request.args.get("loan_state")
    if sortin is None:
        sortin = 1
    else:
        sortin = int(sortin)
    # Loan state 1 for created 2. Rejected 3. Approve 4. Any
    if loan_state is None:
        loan_state = 4
    else:
        loan_state = int(loan_state)
    connection = psycopg2.connect(POSTGRESQL_URI)
    # Filter in Action
    with connection:
        with connection.cursor() as cursor:
            if loan_state == 4:
                if sortin == 1:
                    cursor.execute(
                        "SELECT loan_quantity, loan_interest, tenture, loan_state, date_request FROM loan_request WHERE ofuser=%s ORDER BY loan_id DESC",
                        [token[0][1]])
                else:
                    cursor.execute(
                        "SELECT loan_quantity, loan_interest, tenture, loan_state, date_request FROM loan_request WHERE ofuser=%s",
                        [token[0][1]])
            else:
                if sortin == 1:
                    cursor.execute(
                        "SELECT loan_quantity, loan_interest, tenture, loan_state, date_request FROM loan_request WHERE ofuser=%s AND loan_state=%s  ORDER BY loan_id DESC",
                        [token[0][1], loan_state])
                else:
                    cursor.execute(
                        "SELECT loan_quantity, loan_interest, tenture, loan_state, date_request FROM loan_request WHERE ofuser=%s AND loan_state=%s",
                        [token[0][1], loan_state])
            result = cursor.fetchall()
    connection.close()
    finalresult = []
    # Prettifying Result
    for temp in result:
        if temp[3] == 1:
            token = "Queue"
        elif temp[3] == 2:
            token = "Rejected"
        else:
            token = "Approved"
        temp = {
            'Amount': temp[0],
            'State': token,
            'Interest': str(temp[1]),
            'Tenture': temp[2],
            'date': str(temp[4])[:-7],
        }
        finalresult.append(temp)
    return json.dumps(finalresult), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 7
0
def edit_loan():
    # Only Agent Role Can access This
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 2 stands for Agent
    if token[0][4] != 2:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    form_data = request.get_json()
    try:
        loan_quantity = int(form_data['loan_quantity'])
        loan_interest = float(form_data['loan_interest'])
        tenture = int(form_data['tenture'])
        loan_id = int(form_data['loan_id'])
    except:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    if not validate_edit_loanreq(loan_quantity, loan_interest, tenture,
                                 loan_id):
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    connection = psycopg2.connect(POSTGRESQL_URI)
    # Checking Loan Exisr
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "SELECT loan_state FROM loan_request WHERE loan_id=%s AND byagent=%s LIMIT 1",
                [loan_id, token[0][1]])
            result = cursor.fetchall()
    if len(result) < 1:
        connection.close()
        return json.dumps({"status": "invalid_loan"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Approve loan cannot be edited | 1. New , 2. Rejected, 3. Approved
    if result[0][0] == 3:
        connection.close()
        return json.dumps({"status": "approved"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "UPDATE loan_request SET loan_quantity = %s, loan_interest = %s, tenture = %s WHERE loan_id = %s AND byagent = %s",
                [loan_quantity, loan_interest, tenture, loan_id, token[0][1]])

    # Updated Dates for Logs
    datelog = datetime.datetime.now()
    operation_done = "agentupdate"
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "INSERT INTO loan_logs (loan_id, cur_state, changeby, log_date, operation_done, loan_interest, loan_quantity, tenture) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                [
                    loan_id, result[0][0], token[0][1], datelog,
                    operation_done, loan_interest, loan_quantity, tenture
                ])
    connection.close()
    return json.dumps({"status": "success"}), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
Ejemplo n.º 8
0
def gen_loan():
    # Only Agent Role Can access This
    token = None
    token = verify_token(token)
    if token is None:
        return json.dumps({"status": "Token Missing"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    # Userrole 2 stands for Agent
    if token[0][4] != 2:
        return json.dumps({"status": "Restrict"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    form_data = request.get_json()
    try:
        loan_quantity = int(form_data['loan_quantity'])
        loan_interest = float(form_data['loan_interest'])
        tenture = int(form_data['tenture'])
        ofuser = form_data['ofuser']
    except:
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    if not validate_loanreq(loan_quantity, loan_interest, tenture):
        return json.dumps({"status": "field_empty"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    connection = psycopg2.connect(POSTGRESQL_URI)
    # Validating User
    with connection:
        with connection.cursor() as cursor:
            cursor.execute("SELECT 1 FROM register WHERE public_id=%s LIMIT 1",
                           [ofuser])
            result = cursor.fetchall()
    if len(result) < 1:
        return json.dumps({"status": "invalid_user"}), 200, {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
    datelog = datetime.datetime.now()
    # 1 Means new  2 Means Rejected 3 Means Approve
    loan_state = 1
    # Raising loan request
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "INSERT INTO loan_request (loan_quantity, loan_interest, tenture, loan_state, byagent, ofuser, date_request) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING loan_id;",
                [
                    loan_quantity, loan_interest, tenture, loan_state,
                    token[0][1], ofuser, datelog
                ])
            result_id = cursor.fetchone()[0]
    operation_done = "created"
    # Adding loan request to loan logs
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(
                "INSERT INTO loan_logs (loan_id, cur_state, changeby, log_date, operation_done, loan_interest, loan_quantity, tenture) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                [
                    result_id, loan_state, token[0][1], datelog,
                    operation_done, loan_interest, loan_quantity, tenture
                ])
    connection.close()
    return json.dumps({"status": "success"}), 200, {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }