2022-03-07 05:03:32 +00:00
|
|
|
from http import HTTPStatus
|
2022-03-12 14:18:09 +00:00
|
|
|
|
|
|
|
from fastapi import Body, Depends, Request
|
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
|
2022-03-07 05:03:32 +00:00
|
|
|
from lnbits.core.crud import get_wallet
|
2022-03-12 14:18:09 +00:00
|
|
|
from lnbits.decorators import WalletTypeInfo, require_admin_key
|
|
|
|
from lnbits.extensions.admin import admin_ext
|
|
|
|
from lnbits.extensions.admin.models import Admin, UpdateAdminSettings
|
|
|
|
|
|
|
|
from .crud import get_admin, update_admin, update_wallet_balance
|
|
|
|
|
2022-03-07 05:03:32 +00:00
|
|
|
|
2022-03-12 14:18:09 +00:00
|
|
|
@admin_ext.get("/api/v1/admin/{wallet_id}/{topup_amount}", status_code=HTTPStatus.OK)
|
2022-03-22 10:28:44 +00:00
|
|
|
async def api_update_balance(wallet_id, topup_amount: int, g: WalletTypeInfo = Depends(require_admin_key)):
|
2022-03-07 05:03:32 +00:00
|
|
|
try:
|
|
|
|
wallet = await get_wallet(wallet_id)
|
|
|
|
except:
|
2022-03-12 14:18:09 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.FORBIDDEN, detail="Not allowed: not an admin"
|
|
|
|
)
|
2022-03-22 10:28:44 +00:00
|
|
|
|
|
|
|
await update_wallet_balance(wallet_id=wallet_id, amount=int(topup_amount))
|
|
|
|
|
2022-03-12 14:18:09 +00:00
|
|
|
return {"status": "Success"}
|
2022-03-07 05:03:32 +00:00
|
|
|
|
|
|
|
|
2022-03-12 14:18:09 +00:00
|
|
|
@admin_ext.post("/api/v1/admin/", status_code=HTTPStatus.OK)
|
|
|
|
async def api_update_admin(
|
|
|
|
request: Request,
|
|
|
|
data: UpdateAdminSettings = Body(...),
|
|
|
|
g: WalletTypeInfo = Depends(require_admin_key)
|
|
|
|
):
|
2022-03-07 05:03:32 +00:00
|
|
|
admin = await get_admin()
|
2022-03-12 14:18:09 +00:00
|
|
|
print(data)
|
|
|
|
if not admin.user == g.wallet.user:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.FORBIDDEN, detail="Not allowed: not an admin"
|
|
|
|
)
|
|
|
|
updated = await update_admin(user=g.wallet.user, **data.dict())
|
2022-03-07 05:03:32 +00:00
|
|
|
print(updated)
|
2022-03-12 14:18:09 +00:00
|
|
|
return {"status": "Success"}
|