diff --git a/crud.py b/crud.py index f84fac7..72d3826 100644 --- a/crud.py +++ b/crud.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import List, Optional import httpx @@ -6,8 +6,6 @@ from lnbits.db import SQLITE from lnbits.helpers import urlsafe_short_hash -# todo: use the API, not direct import -from ..satspay.crud import delete_charge # type: ignore from . import db from .models import CreateService, Donation, Service @@ -228,15 +226,18 @@ async def service_add_token(service_id, token): return True -async def delete_service(service_id: int) -> None: +async def delete_service(service_id: int) -> List[str]: """Delete a Service and all corresponding Donations""" - await db.execute("DELETE FROM streamalerts.Services WHERE id = ?", (service_id,)) rows = await db.fetchall( "SELECT * FROM streamalerts.Donations WHERE service = ?", (service_id,) ) for row in rows: await delete_donation(row["id"]) + await db.execute("DELETE FROM streamalerts.Services WHERE id = ?", (service_id,)) + + return [row["id"] for row in rows] + async def get_donation(donation_id: str) -> Optional[Donation]: """Return a Donation""" @@ -257,7 +258,6 @@ async def get_donations(wallet_id: str) -> Optional[list]: async def delete_donation(donation_id: str) -> None: """Delete a Donation and its corresponding statspay charge""" await db.execute("DELETE FROM streamalerts.Donations WHERE id = ?", (donation_id,)) - await delete_charge(donation_id) async def update_donation(donation_id: str, **kwargs) -> Donation: diff --git a/helpers.py b/helpers.py index 122651f..f3282a2 100644 --- a/helpers.py +++ b/helpers.py @@ -26,3 +26,12 @@ async def get_charge_status(charge_id: str, api_key: str) -> ChargeStatus: ) r.raise_for_status() return ChargeStatus.parse_obj(r.json()) + +async def delete_charge(charge_id: str, api_key: str): + async with httpx.AsyncClient() as client: + headers = {"X-API-KEY": api_key} + r = await client.delete( + url=f"http://{settings.host}:{settings.port}/satspay/api/v1/charge/{charge_id}", + headers=headers, + ) + r.raise_for_status() \ No newline at end of file diff --git a/views_api.py b/views_api.py index 0ef79d7..d3505b6 100644 --- a/views_api.py +++ b/views_api.py @@ -26,7 +26,7 @@ update_donation, update_service, ) -from .helpers import create_charge, get_charge_status +from .helpers import create_charge, delete_charge, get_charge_status from .models import CreateDonation, CreateService, ValidateDonation @@ -256,6 +256,7 @@ async def api_delete_donation(donation_id, g: WalletTypeInfo = Depends(get_key_t detail="Not authorized to delete this donation!", ) await delete_donation(donation_id) + await delete_charge(donation_id, g.wallet.inkey) return "", HTTPStatus.NO_CONTENT @@ -272,5 +273,8 @@ async def api_delete_service(service_id, g: WalletTypeInfo = Depends(get_key_typ status_code=HTTPStatus.FORBIDDEN, detail="Not authorized to delete this service!", ) - await delete_service(service_id) + donations = await delete_service(service_id) + for d in donations: + await delete_charge(d, g.wallet.inkey) + return "", HTTPStatus.NO_CONTENT