-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews_api.py
219 lines (182 loc) · 6.99 KB
/
views_api.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
from http import HTTPStatus
from fastapi import APIRouter, Depends, Query
from fastapi.exceptions import HTTPException
from lnbits.core.crud import get_standalone_payment, get_user
from lnbits.core.models import WalletTypeInfo
from lnbits.core.services import create_invoice
from lnbits.decorators import (
require_admin_key,
require_invoice_key,
)
from .crud import (
create_appointment,
create_schedule,
create_unavailable_time,
delete_schedule,
delete_unavailable_time,
get_appointments,
get_appointments_wallets,
get_schedule,
get_schedules,
get_unavailable_times,
purge_appointments,
set_appointment_paid,
update_schedule,
)
from .models import CreateAppointment, CreateSchedule, CreateUnavailableTime
lncalendar_api_router = APIRouter()
@lncalendar_api_router.get("/api/v1/schedule")
async def api_schedules(
wallet: WalletTypeInfo = Depends(require_invoice_key),
all_wallets: bool = Query(False),
):
wallet_ids = [wallet.wallet.id]
if all_wallets:
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
return [
{**schedule.dict(), "available_days": schedule.availabe_days}
for schedule in await get_schedules(wallet_ids)
]
@lncalendar_api_router.post("/api/v1/schedule")
async def api_schedule_create(
data: CreateSchedule, wallet: WalletTypeInfo = Depends(require_admin_key)
):
schedule = await create_schedule(wallet_id=wallet.wallet.id, data=data)
return schedule.dict()
@lncalendar_api_router.put("/api/v1/schedule/{schedule_id}")
async def api_schedule_update(
schedule_id: str,
data: CreateSchedule,
wallet: WalletTypeInfo = Depends(require_invoice_key),
):
schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
if schedule.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your schedule."
)
for k, v in data.dict().items():
if v is not None:
setattr(schedule, k, v)
schedule = await update_schedule(schedule)
return {**schedule.dict(), "available_days": schedule.availabe_days}
@lncalendar_api_router.delete("/api/v1/schedule/{schedule_id}")
async def api_schedule_delete(
schedule_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
):
schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
if schedule.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your schedule."
)
await delete_schedule(schedule_id)
return "", HTTPStatus.NO_CONTENT
## Appointment API
@lncalendar_api_router.post("/api/v1/appointment")
async def api_appointment_create(data: CreateAppointment):
schedule = await get_schedule(data.schedule)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
try:
payment = await create_invoice(
wallet_id=schedule.wallet,
amount=schedule.amount, # type: ignore
memo=f"{schedule.name}",
extra={"tag": "lncalendar", "name": data.name, "email": data.email},
)
await create_appointment(
schedule_id=data.schedule, payment_hash=payment.payment_hash, data=data
)
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc)
) from exc
return {"payment_hash": payment.payment_hash, "payment_request": payment.bolt11}
@lncalendar_api_router.get("/api/v1/appointment/purge/{schedule_id}")
async def api_purge_appointments(schedule_id: str):
schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
return await purge_appointments(schedule_id)
@lncalendar_api_router.get("/api/v1/appointment/{schedule_id}/{payment_hash}")
async def api_appointment_check_invoice(schedule_id: str, payment_hash: str):
schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
payment = await get_standalone_payment(payment_hash, incoming=True)
if not payment:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Payment does not exist"
)
if payment.success:
await set_appointment_paid(payment_hash)
return {"paid": payment.success}
@lncalendar_api_router.get("/api/v1/appointment/{schedule_id}")
async def api_get_appointments_schedule(schedule_id: str):
appointments = await get_appointments(schedule_id)
if not appointments:
return []
return appointments
@lncalendar_api_router.get("/api/v1/appointment")
async def api_get_all_appointments(
wallet: WalletTypeInfo = Depends(require_invoice_key),
):
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
return await get_appointments_wallets(wallet_ids)
## Unavailable Time API
@lncalendar_api_router.post("/api/v1/unavailable")
async def api_unavailable_create(
data: CreateUnavailableTime, wallet: WalletTypeInfo = Depends(require_admin_key)
):
schedule = await get_schedule(data.schedule)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
if schedule.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your schedule."
)
unavailable = await create_unavailable_time(data=data)
assert unavailable, "Newly created unavailable time couldn't be retrieved"
return unavailable
@lncalendar_api_router.get("/api/v1/unavailable/{schedule_id}")
async def api_unavailable_get(schedule_id: str):
schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
return await get_unavailable_times(schedule_id)
@lncalendar_api_router.delete("/api/v1/unavailable/{schedule_id}/{unavailable_id}")
async def api_unavailable_delete(
schedule_id: str,
unavailable_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
if schedule.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your schedule."
)
await delete_unavailable_time(unavailable_id)
return "", HTTPStatus.NO_CONTENT