-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
298 lines (242 loc) · 8.27 KB
/
database.py
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import sqlite3 as sq
import json
db = sq.connect("bank_users.db")
cur = db.cursor()
def db_init():
cur.execute(
"CREATE TABLE IF NOT EXISTS bank_users ("
"telegram_id INTEGER PRIMARY KEY, "
"auth_link TEXT, "
"requisition_id TEXT, "
"bank_account_id TEXT, "
"is_authorized INTEGER DEFAULT 0, "
"tx_notify INTEGER DEFAULT 0, "
"last_tx TEXT"
");"
)
db.commit()
def insert_user(
telegram_id,
auth_link=None,
requisition_id=None,
bank_account_id=None,
is_authorized=False,
tx_notify=False,
last_tx=None,
):
try:
cur.execute(
"INSERT INTO bank_users (telegram_id, auth_link, requisition_id, bank_account_id, is_authorized, tx_notify, last_tx) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
(
telegram_id,
auth_link,
requisition_id,
bank_account_id,
is_authorized,
tx_notify,
last_tx,
),
)
db.commit()
print("User inserted successfully.")
except sq.Error as e:
print("Error inserting user:", e)
def user_exists(telegram_id):
try:
cur.execute(
"SELECT COUNT(*) FROM bank_users WHERE telegram_id = ?", (telegram_id,)
)
count = cur.fetchone()[0]
return count > 0 # Return True if the user exists, False otherwise
except sq.Error as e:
print("Error checking user existence:", e)
return False # Return False on error
def is_authorized(telegram_id):
try:
cur.execute(
"SELECT is_authorized FROM bank_users WHERE telegram_id = ?", (telegram_id,)
)
result = cur.fetchone()
if result is not None:
return bool(
result[0]
) # Return True if the user is authorized, False otherwise
else:
return False # User not found, return False
except sq.Error as e:
print("Error checking authorization:", e)
return False # Return False on error
def check_user_authorization(telegram_id):
if user_exists(telegram_id):
return (
True,
is_authorized(telegram_id),
) # Return a tuple (True, True) or (True, False) based on existence and authorization
else:
return (False, False)
def get_auth_link(telegram_id):
try:
cur.execute(
"SELECT auth_link FROM bank_users WHERE telegram_id = ?", (telegram_id,)
)
result = cur.fetchone()
if result is not None:
return result[0] # Return the auth_link if the user is found
else:
return None # User not found, return None
except sq.Error as e:
print("Error retrieving auth link:", e)
return None # Return None on error
def get_requisition_id(telegram_id):
try:
cur.execute(
"SELECT requisition_id FROM bank_users WHERE telegram_id = ?",
(telegram_id,),
)
result = cur.fetchone()
if result is not None:
return result[0] # Return the requisition_id if the user is found
else:
return None # User not found, return None
except sq.Error as e:
print("Error retrieving requisition ID:", e)
return None # Return None on error
def get_account_id(telegram_id):
try:
cur.execute(
"SELECT bank_account_id FROM bank_users WHERE telegram_id = ?",
(telegram_id,),
)
result = cur.fetchone()
if result is not None:
return result[0] # Return the bank_account_id if the user is found
else:
return None # User not found, return None
except sq.Error as e:
print("Error retrieving bank account ID:", e)
return None # Return None on error
def insert_auth_link(telegram_id, auth_link):
try:
cur.execute(
"UPDATE bank_users SET auth_link = ? WHERE telegram_id = ?",
(auth_link, telegram_id),
)
db.commit()
print(f"Auth Link updated for user {telegram_id}")
except sq.Error as e:
print("Error updating auth link:", e)
def insert_requisition_id(telegram_id, requisition_id):
try:
cur.execute(
"UPDATE bank_users SET requisition_id = ? WHERE telegram_id = ?",
(requisition_id, telegram_id),
)
db.commit()
print(f"Requisition ID updated for user {telegram_id}")
except sq.Error as e:
print("Error updating requisition ID:", e)
def insert_account_id(telegram_id, bank_account_id):
try:
cur.execute(
"UPDATE bank_users SET bank_account_id = ? WHERE telegram_id = ?",
(bank_account_id, telegram_id),
)
db.commit()
print(f"Bank Account ID updated for user {telegram_id}")
except sq.Error as e:
print("Error updating bank account ID:", e)
def insert_is_authorized(telegram_id, is_authorized):
try:
cur.execute(
"UPDATE bank_users SET is_authorized = ? WHERE telegram_id = ?",
(is_authorized, telegram_id),
)
db.commit()
print(f"is_authorized updated for user {telegram_id} to {is_authorized}")
except sq.Error as e:
print("Error updating is_authorized:", e)
def get_tx_notify(telegram_id):
try:
# Select tx_notify for the specified telegram_id
cur.execute(
"SELECT tx_notify FROM bank_users WHERE telegram_id = ?", (telegram_id,)
)
result = cur.fetchone()
if result:
return result
else:
print(f"User with telegram_id {telegram_id} not found.")
return None
except sq.Error as e:
print("Error getting tx_notify:", e)
return None
def set_tx_notify(telegram_id: int, value: bool) -> None:
try:
# Update the tx_notify field for the specified telegram_id
cur.execute(
"UPDATE bank_users SET tx_notify = ? WHERE telegram_id = ?",
(value, telegram_id),
)
db.commit()
print(f"tx_notify updated for user {telegram_id} to {value}")
except sq.Error as e:
print("Error updating tx_notify:", e)
def get_telegram_ids() -> list:
ids_list = []
try:
# Select all users' telegram_id values
cur.execute("SELECT telegram_id FROM bank_users")
telegram_ids = cur.fetchall()
for telegram_id in telegram_ids:
ids_list.append(telegram_id[0])
return ids_list
except sq.Error as e:
print("Error fetching telegram_ids:", e)
def get_users_to_notify():
try:
# Select all users' telegram_id and tx_notify
cur.execute("SELECT telegram_id, tx_notify FROM bank_users")
user_records = cur.fetchall()
user_info_dict = {}
for record in user_records:
telegram_id, tx_notify = record
user_info_dict[telegram_id] = {"tx_notify": bool(tx_notify)}
return user_info_dict
except sq.Error as e:
print("Error fetching user info:", e)
return {}
def get_last_tx(telegram_id):
try:
# Select the last_tx for the specified telegram_id
cur.execute(
"SELECT last_tx FROM bank_users WHERE telegram_id = ?", (telegram_id,)
)
result = cur.fetchone()
if result:
(last_tx,) = result
return result
else:
print(f"User with telegram_id {telegram_id} not found.")
return None
except sq.Error as e:
print("Error getting last_tx:", e)
return None
import sqlite3 as sq
import json
def set_last_tx(telegram_id, last_tx):
try:
# Update the last_tx for the specified telegram_id
cur.execute(
"UPDATE bank_users SET last_tx = ? WHERE telegram_id = ?",
(last_tx, telegram_id),
)
db.commit()
print(f"Last transaction set for user with telegram_id {telegram_id}")
except sq.Error as e:
print("Error setting last_tx:", e)
# Usage example
# Replace 'telegram_id' and 'last_tx_data' with your actual values
telegram_id = "12345"
last_tx_data = {"transaction_id": 1, "amount": 100.0, "description": "Payment"}
set_last_tx(telegram_id, last_tx_data)