-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcrud.py
258 lines (214 loc) · 6.72 KB
/
crud.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
import secrets
from datetime import datetime
from typing import Optional
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import Card, CreateCardData, Hit, Refund
db = Database("ext_boltcards")
async def create_card(data: CreateCardData, wallet_id: str) -> Card:
card_id = urlsafe_short_hash().upper()
extenal_id = urlsafe_short_hash().lower()
await db.execute(
"""
INSERT INTO boltcards.cards (
id,
uid,
external_id,
wallet,
card_name,
counter,
tx_limit,
daily_limit,
enable,
k0,
k1,
k2,
otp
)
VALUES (
:id, :uid, :external_id, :wallet, :card_name, :counter,
:tx_limit, :daily_limit, :enable, :k0, :k1, :k2, :otp
)
""",
{
"id": card_id,
"uid": data.uid.upper(),
"external_id": extenal_id,
"wallet": wallet_id,
"card_name": data.card_name,
"counter": data.counter,
"tx_limit": data.tx_limit,
"daily_limit": data.daily_limit,
"enable": True,
"k0": data.k0,
"k1": data.k1,
"k2": data.k2,
"otp": secrets.token_hex(16),
},
)
card = await get_card(card_id)
assert card, "Newly created card couldn't be retrieved"
return card
async def update_card(card_id: str, data: CreateCardData) -> Card:
card = Card(
id=card_id,
**data.dict(),
)
await db.update("boltcards.cards", card)
return card
async def get_cards(wallet_ids: list[str]) -> list[Card]:
if len(wallet_ids) == 0:
return []
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM boltcards.cards WHERE wallet IN ({q})",
model=Card,
)
async def get_card(card_id: str) -> Optional[Card]:
return await db.fetchone(
"SELECT * FROM boltcards.cards WHERE id = :id",
{"id": card_id},
Card,
)
async def get_card_by_uid(card_uid: str) -> Optional[Card]:
return await db.fetchone(
"SELECT * FROM boltcards.cards WHERE uid = :uid",
{"uid": card_uid.upper()},
Card,
)
async def get_card_by_external_id(external_id: str) -> Optional[Card]:
return await db.fetchone(
"SELECT * FROM boltcards.cards WHERE external_id = :ext_id",
{"ext_id": external_id.lower()},
Card,
)
async def get_card_by_otp(otp: str) -> Optional[Card]:
return await db.fetchone(
"SELECT * FROM boltcards.cards WHERE otp = :otp",
{"otp": otp},
Card,
)
async def delete_card(card_id: str) -> None:
# Delete cards
await db.execute("DELETE FROM boltcards.cards WHERE id = :id", {"id": card_id})
# Delete hits
hits = await get_hits([card_id])
for hit in hits:
await db.execute("DELETE FROM boltcards.hits WHERE id = :id", {"id": hit.id})
# Delete refunds
refunds = await get_refunds([hit.id])
for refund in refunds:
await db.execute(
"DELETE FROM boltcards.refunds WHERE id = :id", {"id": refund.id}
)
async def update_card_counter(counter: int, card_id: str):
await db.execute(
"UPDATE boltcards.cards SET counter = :counter WHERE id = :id",
{"counter": counter, "id": card_id},
)
async def enable_disable_card(enable: bool, card_id: str) -> Optional[Card]:
await db.execute(
"UPDATE boltcards.cards SET enable = :enable WHERE id = :id",
{"enable": enable, "id": card_id},
)
return await get_card(card_id)
async def update_card_otp(otp: str, card_id: str):
await db.execute(
"UPDATE boltcards.cards SET otp = :otp WHERE id = :id",
{"otp": otp, "id": card_id},
)
async def get_hit(hit_id: str) -> Optional[Hit]:
return await db.fetchone(
"SELECT * FROM boltcards.hits WHERE id = :id",
{"id": hit_id},
Hit,
)
async def get_hits(cards_ids: list[str]) -> list[Hit]:
if len(cards_ids) == 0:
return []
q = ",".join([f"'{card_id}'" for card_id in cards_ids])
return await db.fetchall(
f"SELECT * FROM boltcards.hits WHERE card_id IN ({q})",
model=Hit,
)
async def get_hits_today(card_id: str) -> list[Hit]:
rows = await db.fetchall(
"SELECT * FROM boltcards.hits WHERE card_id = :id",
{"id": card_id},
Hit,
)
updatedrow = []
for hit in rows:
if datetime.now().date() == hit.time.date():
updatedrow.append(hit)
return updatedrow
async def spend_hit(card_id: str, amount: int):
await db.execute(
"UPDATE boltcards.hits SET spent = :spent, amount = :amount WHERE id = :id",
{"spent": True, "amount": amount, "id": card_id},
)
return await get_hit(card_id)
async def create_hit(card_id, ip, useragent, old_ctr, new_ctr) -> Hit:
hit_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO boltcards.hits (
id,
card_id,
ip,
spent,
useragent,
old_ctr,
new_ctr,
amount
)
VALUES (:id, :card_id, :ip, :spent, :useragent, :old_ctr, :new_ctr, :amount)
""",
{
"id": hit_id,
"card_id": card_id,
"ip": ip,
"spent": False,
"useragent": useragent,
"old_ctr": old_ctr,
"new_ctr": new_ctr,
"amount": 0,
},
)
hit = await get_hit(hit_id)
assert hit, "Newly recorded hit couldn't be retrieved"
return hit
async def create_refund(hit_id, refund_amount) -> Refund:
refund_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO boltcards.refunds (
id,
hit_id,
refund_amount
)
VALUES (:id, :hit_id, :refund_amount)
""",
{
"id": refund_id,
"hit_id": hit_id,
"refund_amount": refund_amount,
},
)
refund = await get_refund(refund_id)
assert refund, "Newly recorded hit couldn't be retrieved"
return refund
async def get_refund(refund_id: str) -> Optional[Refund]:
return await db.fetchone(
"SELECT * FROM boltcards.refunds WHERE id = :id",
{"id": refund_id},
Refund,
)
async def get_refunds(hits_ids: list[str]) -> list[Refund]:
if len(hits_ids) == 0:
return []
q = ",".join([f"'{hit_id}'" for hit_id in hits_ids])
return await db.fetchall(
f"SELECT * FROM boltcards.refunds WHERE hit_id IN ({q})",
model=Refund,
)