-
Notifications
You must be signed in to change notification settings - Fork 3
/
crud.py
156 lines (123 loc) · 4.09 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
from typing import List, Optional, Union
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import (
CreateInvoiceData,
Invoice,
InvoiceItem,
InvoiceItemData,
Payment,
)
db = Database("ext_invoices")
async def get_invoice(invoice_id: str) -> Optional[Invoice]:
return await db.fetchone(
"SELECT * FROM invoices.invoices WHERE id = :id",
{"id": invoice_id},
Invoice,
)
async def get_invoice_items(invoice_id: str) -> List[InvoiceItem]:
return await db.fetchall(
"SELECT * FROM invoices.invoice_items WHERE invoice_id = :id",
{"id": invoice_id},
InvoiceItem,
)
async def get_invoice_item(item_id: str) -> Optional[InvoiceItem]:
return await db.fetchone(
"SELECT * FROM invoices.invoice_items WHERE id = :id",
{"id": item_id},
InvoiceItem,
)
async def get_invoice_total(items: List[InvoiceItem]) -> int:
return sum(item.amount for item in items)
async def get_invoices(wallet_ids: Union[str, List[str]]) -> List[Invoice]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM invoices.invoices WHERE wallet IN ({q})",
model=Invoice,
)
async def get_invoice_payments(invoice_id: str) -> List[Payment]:
return await db.fetchall(
"SELECT * FROM invoices.payments WHERE invoice_id = :id",
{"id": invoice_id},
Payment,
)
async def get_invoice_payment(payment_id: str) -> Optional[Payment]:
return await db.fetchone(
"SELECT * FROM invoices.payments WHERE id = :id",
{"id": payment_id},
Payment,
)
async def get_payments_total(payments: List[Payment]) -> int:
return sum(item.amount for item in payments)
async def create_invoice_internal(wallet_id: str, data: CreateInvoiceData) -> Invoice:
invoice_id = urlsafe_short_hash()
invoice = Invoice(
id=invoice_id,
wallet=wallet_id,
status=data.status,
currency=data.currency,
company_name=data.company_name,
first_name=data.first_name,
last_name=data.last_name,
email=data.email,
phone=data.phone,
address=data.address,
)
await db.insert("invoices.invoices", invoice)
return invoice
async def create_invoice_items(
invoice_id: str, data: List[InvoiceItemData]
) -> List[InvoiceItem]:
invoice_items = []
for item in data:
item_id = urlsafe_short_hash()
invoice_item = InvoiceItem(
id=item_id,
invoice_id=invoice_id,
description=item.description,
amount=int(item.amount * 100),
)
invoice_items.append(invoice_item)
await db.insert("invoices.invoice_items", invoice_item)
return invoice_items
async def update_invoice_internal(invoice: Invoice) -> Invoice:
await db.update("invoices.invoices", invoice)
return invoice
async def delete_invoice(
invoice_id: str,
) -> bool:
await db.execute(
"DELETE FROM invoices.payments WHERE invoice_id = :id",
{"id": invoice_id},
)
await db.execute(
"DELETE FROM invoices.invoice_items WHERE invoice_id = :id",
{"id": invoice_id},
)
await db.execute(
"DELETE FROM invoices.invoices WHERE id = :id",
{"id": invoice_id},
)
return True
async def delete_invoice_items(
invoice_id: str,
) -> bool:
await db.execute(
"DELETE FROM invoices.invoice_items WHERE invoice_id = :id",
{"id": invoice_id},
)
return True
async def create_invoice_payment(invoice_id: str, amount: int) -> Payment:
payment_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO invoices.payments (id, invoice_id, amount)
VALUES (:id, :invoice_id, :amount)
""",
{"id": payment_id, "invoice_id": invoice_id, "amount": amount},
)
payment = await get_invoice_payment(payment_id)
assert payment, "Newly created payment couldn't be retrieved"
return payment