Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete charge #3

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Expand Down Expand Up @@ -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"""
Expand All @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand All @@ -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