-
Notifications
You must be signed in to change notification settings - Fork 4
/
crud.py
174 lines (138 loc) · 4.66 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
from datetime import datetime
from typing import Optional
from lnbits.core.services import create_invoice
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import (
Charge,
CreateCharge,
CreateSatsPayTheme,
SatspaySettings,
SatsPayTheme,
)
db = Database("ext_satspay")
async def create_charge(
user: str,
data: CreateCharge,
onchainaddress: Optional[str] = None,
) -> Charge:
if not data.amount or data.amount <= 0:
raise Exception("Amount must be greater than 0")
charge = Charge(
id=urlsafe_short_hash(),
timestamp=datetime.now(),
user=user,
onchainaddress=onchainaddress,
payment_request=None,
payment_hash=None,
balance=0,
pending=False,
paid=False,
name=data.name,
description=data.description,
onchainwallet=data.onchainwallet,
lnbitswallet=data.lnbitswallet,
webhook=data.webhook,
completelink=data.completelink,
completelinktext=data.completelinktext,
time=data.time,
amount=data.amount,
zeroconf=data.zeroconf,
fasttrack=data.fasttrack,
extra=data.extra,
custom_css=data.custom_css,
currency=data.currency,
currency_amount=data.currency_amount,
)
if data.onchainwallet:
if not onchainaddress:
raise Exception(f"Wallet '{data.onchainwallet}' can no longer be accessed.")
if data.lnbitswallet:
payment = await create_invoice(
wallet_id=data.lnbitswallet,
amount=data.amount,
memo=data.description,
extra={"tag": "charge", "charge": charge.id},
expiry=int(data.time * 60),
)
charge.payment_hash = payment.payment_hash
charge.payment_request = payment.bolt11
await db.insert("satspay.charges", charge)
return charge
async def update_charge(charge: Charge) -> Charge:
await db.update("satspay.charges", charge)
return charge
async def get_charge(charge_id: str) -> Optional[Charge]:
return await db.fetchone(
"SELECT * FROM satspay.charges WHERE id = :id",
{"id": charge_id},
Charge,
)
async def get_pending_charges() -> list[Charge]:
return await db.fetchall(
"SELECT * FROM satspay.charges WHERE paid = false",
model=Charge,
)
async def get_charge_by_onchain_address(onchain_address: str) -> Optional[Charge]:
return await db.fetchone(
"SELECT * FROM satspay.charges WHERE onchainaddress = :address",
{"address": onchain_address},
Charge,
)
async def get_charges(user: str) -> list[Charge]:
return await db.fetchall(
"""
SELECT * FROM satspay.charges WHERE "user" = :user ORDER BY "timestamp" DESC
""",
{"user": user},
Charge,
)
async def delete_charge(charge_id: str) -> None:
await db.execute("DELETE FROM satspay.charges WHERE id = :id", {"id": charge_id})
async def create_theme(data: CreateSatsPayTheme, user_id: str) -> SatsPayTheme:
theme = SatsPayTheme(
css_id=urlsafe_short_hash(),
title=data.title,
custom_css=data.custom_css,
user=user_id,
)
await db.insert("satspay.themes", theme)
return theme
async def update_theme(theme: SatsPayTheme) -> SatsPayTheme:
await db.update("satspay.themes", theme, "css_id = :css_id")
return theme
async def get_theme(css_id: str) -> Optional[SatsPayTheme]:
return await db.fetchone(
"SELECT * FROM satspay.themes WHERE css_id = :css_id",
{"css_id": css_id},
SatsPayTheme,
)
async def get_themes(user_id: str) -> list[SatsPayTheme]:
return await db.fetchall(
"""
SELECT * FROM satspay.themes WHERE "user" = :user ORDER BY title DESC
""",
{"user": user_id},
SatsPayTheme,
)
async def delete_theme(theme_id: str) -> None:
await db.execute(
"DELETE FROM satspay.themes WHERE css_id = :css_id", {"css_id": theme_id}
)
async def get_or_create_satspay_settings() -> SatspaySettings:
settings = await db.fetchone(
"SELECT * FROM satspay.settings LIMIT 1",
model=SatspaySettings,
)
if settings:
return settings
else:
settings = SatspaySettings()
await db.insert("satspay.settings", settings)
return settings
async def update_satspay_settings(settings: SatspaySettings) -> SatspaySettings:
# 3rd arguments `WHERE clause` is empty for settings
await db.update("satspay.settings", settings, "")
return settings
async def delete_satspay_settings() -> None:
await db.execute("DELETE FROM satspay.settings")