-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
241 lines (210 loc) · 7.4 KB
/
database.py
File metadata and controls
241 lines (210 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#db
import psycopg2
from psycopg2 import sql
import bcrypt
from flask import jsonify
from datetime import datetime
# Function to initialize the database
def init_db():
# Connect to the PostgreSQL database
conn = psycopg2.connect(
dbname="kaneah",
user="kaneah",
password="", # Assuming there's no password
host="localhost"
)
# Create a cursor object to execute SQL queries
cursor = conn.cursor()
# Create Users Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone_number VARCHAR(15)
);
""")
# Create Bank Accounts Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS bank_accounts (
account_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(user_id),
account_number VARCHAR(20) UNIQUE NOT NULL,
balance DECIMAL(10, 2) DEFAULT 0.0
);
""")
# Create Momo Accounts Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS momo_accounts (
momo_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(user_id),
phone_number VARCHAR(15) UNIQUE NOT NULL,
balance DECIMAL(10, 2) DEFAULT 0.0
);
""")
# Create Transactions Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS transactions (
transaction_id SERIAL PRIMARY KEY,
sender_id INTEGER REFERENCES users(user_id),
receiver_id INTEGER REFERENCES users(user_id),
amount DECIMAL(10, 2) NOT NULL,
transaction_type VARCHAR(50) NOT NULL,
transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
# Create Statements Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS statements (
statement_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(user_id),
transaction_id INTEGER REFERENCES transactions(transaction_id),
statement_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
# Create Charges Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS charges (
charge_id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(user_id),
amount DECIMAL(10, 2) NOT NULL,
charge_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
""")
# Create Admins Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS admins (
admin_id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
""")
# Commit the changes and close the cursor and connection
conn.commit()
cursor.close()
conn.close()
# Call the init_db function to initialize the database
init_db()
# Create a cursor object to execute SQL queries
conn = psycopg2.connect(
dbname="kaneah",
user="kaneah",
password="", # Assuming there's no password
host="localhost"
)
cursor = conn.cursor()
# Function to create a bank account
def create_bank_account(username, password, email, phone_number, account_number=None, initial_balance=1000.0):
try:
# Insert user data into the 'users' table
cursor.execute("""
INSERT INTO users (username, password, email, phone_number)
VALUES (%s, %s, %s, %s)
RETURNING user_id;
""", (username, password, email, phone_number))
user_id = cursor.fetchone()[0]
# If account_number is not provided, generate a unique account number
if account_number is None:
account_number = generate_account_number()
# Insert bank account data into the 'bank_accounts' table
cursor.execute("""
INSERT INTO bank_accounts (user_id, account_number, balance)
VALUES (%s, %s, %s)
RETURNING account_id;
""", (user_id, account_number, initial_balance))
account_result = cursor.fetchone()
if account_result:
account_id = account_result[0]
conn.commit()
return account_id
else:
# Handle the case where the account result is None
conn.rollback()
return None
except Exception as e:
# Handle any other exceptions and rollback the transaction
print(f"Error: {e}")
conn.rollback()
return None
# Function to get all accounts
def get_all_accounts():
cursor.execute("""
SELECT
u.user_id,
u.username,
u.email,
u.phone_number,
b.account_number,
b.balance
FROM users u
JOIN bank_accounts b ON u.user_id = b.user_id;
""")
accounts = cursor.fetchall()
return accounts
# Function to create a phone number
def create_phone_number(user_id, phone_number, initial_balance=1000.0):
cursor.execute("""
INSERT INTO momo_accounts (user_id, phone_number, balance)
VALUES (%s, %s, %s)
RETURNING momo_id;
""", (user_id, phone_number, initial_balance))
momo_id = cursor.fetchone()[0]
conn.commit()
return momo_id
# Function to transfer money to own momo
def transfer_to_own_momo(user_id, amount):
if amount > 0:
# Update momo_account balance
cursor.execute("""
UPDATE momo_accounts
SET balance = balance + %s
WHERE user_id = %s;
""", (amount, user_id))
# Insert the transaction record
cursor.execute("""
INSERT INTO transactions (sender_id, receiver_id, amount, transaction_type)
VALUES (%s, %s, %s, %s);
""", (user_id, user_id, amount, 'Bank to Momo'))
conn.commit()
return True, jsonify(message='Money transferred to own momo successfully') # Return a tuple with success flag
else:
return False, jsonify(error='Invalid amount. Amount must be greater than 0.'), 400
# Function to transfer money to third-party momo
def transfer_to_third_party_momo(sender_id, receiver_phone_number, amount):
cursor.execute("""
SELECT user_id FROM momo_accounts WHERE phone_number = %s;
""", (receiver_phone_number,))
receiver_id = cursor.fetchone()[0]
cursor.execute("""
INSERT INTO transactions (sender_id, receiver_id, amount, transaction_type)
VALUES (%s, %s, %s, %s);
""", (sender_id, receiver_id, amount, 'Bank to Third-Party Momo'))
conn.commit()
# Function to request statements
def request_statements(user_id):
cursor.execute("""
SELECT transaction_id, statement_date
FROM statements
WHERE user_id = %s;
""", (user_id,))
statements = cursor.fetchall()
return statements
# Function to check user data privacy
def check_user_data_privacy(user_id, requested_user_id):
return user_id == requested_user_id # Replace with your logic
# Function to save charge profit
def save_charge_profit(user_id, amount):
cursor.execute("""
INSERT INTO charges (user_id, amount, charge_date)
VALUES (%s, %s, %s);
""", (user_id, amount, datetime.now()))
conn.commit()
# Function for admin to get statements
def admin_get_statements(admin_id):
# Implement logic for admin access (if needed)
return []
# Close the cursor and connection
def close_db():
cursor.close()
conn.close()