diff --git a/neo_dolfin/api/optimized_api/API_db_op.py b/neo_dolfin/api/optimized_api/API_db_op.py index 7c305429..fd22424a 100644 --- a/neo_dolfin/api/optimized_api/API_db_op.py +++ b/neo_dolfin/api/optimized_api/API_db_op.py @@ -13,13 +13,16 @@ data_instance = Data() access_token = core_instance.generate_auth_token() -## Operations specifically for interacting with the Dolfin Database - -# create "dolfin" database, that is capable of holding user data specific to the dolfin app that can then be passed on to the BASIQ API - not yet implemented in APP.PY +# Operations specifically for interacting with the Dolfin Database def init_dolfin_db(): + """ + Initialize the DolFin database and create user and transaction tables. + Connects to the SQLite database and sets up foreign key constraints. + Creates users and transactions tables if they don't exist. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: conn.execute("PRAGMA foreign_keys = ON;") cursor = conn.cursor() cursor.execute(''' @@ -57,8 +60,13 @@ class VARCHAR(50), def register_user(email, mobile, first_name, middle_name, last_name, password): + """ + Registers a new DolFin user. + Inserts user information into the users table. + Parameters include email, mobile number, first name, middle name, last name, and password. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: cursor = conn.cursor() cursor.execute('''INSERT INTO users (email, mobile, first_name, middle_name, last_name, password) VALUES (?, ?, ?, ?, ?, ?)''', @@ -68,10 +76,14 @@ def register_user(email, mobile, first_name, middle_name, last_name, password): except sqlite3.Error as e: return "An error occurred: " + str(e) -# retrive basiq_id (that will be need for most user-specific calls to the API) based on the user ID (for the dolfin app) that has been passed. + def get_basiq_id(user_id): + """ + Retrieves the basiq ID for a specific DolFin user. + Queries the users table for the basiq ID based on the user ID. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: cursor = conn.cursor() cursor.execute("SELECT basiq_id FROM users WHERE u_id = ?", (user_id,)) result = cursor.fetchone() @@ -84,8 +96,12 @@ def get_basiq_id(user_id): def get_user_info(user_id): + """ + Retrieves information of a DolFin user. + Queries for basic information of a user by user ID, including email, mobile, first name, middle name, and last name. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: cursor = conn.cursor() cursor.execute("SELECT email, mobile, first_name, middle_name, last_name FROM users WHERE u_id = ?", (user_id,)) @@ -104,11 +120,15 @@ def get_user_info(user_id): except sqlite3.Error as e: return "An error occurred: " + str(e) -# add basiq + def register_basiq_id(user_id): + """ + Registers a basiq ID for a DolFin user. + Generates a basiq ID based on user information and updates the basiq ID field in the users table. + """ try: new_basiq_id = json.loads(core_instance.create_user_by_dict(get_user_info(user_id), access_token)).get('id') - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: cursor = conn.cursor() cursor.execute("UPDATE users SET basiq_id = ? WHERE u_id = ?", (new_basiq_id, user_id)) if cursor.rowcount == 0: @@ -118,10 +138,14 @@ def register_basiq_id(user_id): except sqlite3.Error as e: return "An error occurred: " + str(e) -# Creates authorisation link to user - can be presented as a popup or sent as an email -def create_link_bank_account(user_id): + +def link_bank_account(user_id): + """ + Link a DolFin user to their bank accounts. + Generates an authorization link based on the user's basiq ID and opens it in a web browser. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: cursor = conn.cursor() cursor.execute("SELECT basiq_id FROM users WHERE u_id = ?", (user_id,)) result = cursor.fetchone() @@ -133,9 +157,14 @@ def create_link_bank_account(user_id): except sqlite3.Error as e: return "An error occurred: " + str(e) -# returns dataframe with the transactions -def request_transactions_df(user_id): - tran_data = json.loads(data_instance.get_transaction_list(get_basiq_id(user_id), access_token)) + +def request_transactions_df(user_id, limit_para=500, filter_para=''): + """ + Requests and returns user transaction data in DataFrame format. + Fetches the transaction list based on the user's basiq ID and converts it into a DataFrame. + """ + tran_data = json.loads( + data_instance.get_transaction_list(get_basiq_id(user_id), limit_para, filter_para, access_token)) transaction_list = tran_data['data'] transactions = [] for transaction in transaction_list: @@ -160,8 +189,12 @@ def request_transactions_df(user_id): def cache_transactions(user_id, tran_data): + """ + Caches transaction data for a Dolfin user. + Inserts transaction data into the transactions table, including transaction ID, type, status, description, etc. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: conn.execute("PRAGMA foreign_keys = ON;") cursor = conn.cursor() insert_statement = ''' @@ -181,8 +214,12 @@ def cache_transactions(user_id, tran_data): def fetch_transactions_by_user(user_id): + """ + Fetches cached transaction data based on the user ID. + Queries the transactions table for all transaction information for a specific user. + """ try: - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: query = "SELECT * FROM transactions WHERE trans_u_id = ?" return pd.read_sql_query(query, conn, params=(user_id,)) except sqlite3.Error as e: @@ -190,12 +227,16 @@ def fetch_transactions_by_user(user_id): def clear_transactions(): + """ + Clears all cached data from the transactions table. + Deletes all records from the transactions table. + """ try: # Database connection - with sqlite3.connect("../../db/dolfin_db.db") as conn: + with sqlite3.connect("dolfin_db.db") as conn: cursor = conn.cursor() # SQL statement to delete all data from the transactions table cursor.execute("DELETE FROM transactions;") return "Transactions table cleared successfully." except sqlite3.Error as e: - return "An error occurred: " + str(e) \ No newline at end of file + return "An error occurred: " + str(e) diff --git a/neo_dolfin/db/dolfin_db.db b/neo_dolfin/api/optimized_api/dolfin_db.db similarity index 100% rename from neo_dolfin/db/dolfin_db.db rename to neo_dolfin/api/optimized_api/dolfin_db.db diff --git a/neo_dolfin/api/optimized_api/optimized_API.py b/neo_dolfin/api/optimized_api/optimized_API.py index 381a05b9..bd2bcf1d 100644 --- a/neo_dolfin/api/optimized_api/optimized_API.py +++ b/neo_dolfin/api/optimized_api/optimized_API.py @@ -171,7 +171,7 @@ def get_account(user_id, account_id, access_token): return response.text @staticmethod - def get_transaction_list(user_id, access_token, limit_para=500, filter_para=''): + def get_transaction_list(user_id, limit_para, filter_para, access_token): url = f"https://au-api.basiq.io/users/{user_id}/transactions?limit={limit_para}&filter={filter_para}" headers = {