Skip to content

Commit

Permalink
fix: delete charge
Browse files Browse the repository at this point in the history
motorina0 authored and dni committed Feb 21, 2023
1 parent b727456 commit 4139011
Showing 3 changed files with 21 additions and 8 deletions.
12 changes: 6 additions & 6 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import Optional
from typing import List, Optional

import httpx

from lnbits.core.crud import get_wallet
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:
9 changes: 9 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 6 additions & 2 deletions views_api.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4139011

Please sign in to comment.