feat: delete merchant from the local DB

This commit is contained in:
Vlad Stan 2023-03-14 16:26:04 +02:00
parent 9931a08566
commit 92c0833991
4 changed files with 52 additions and 4 deletions

37
crud.py
View file

@ -73,6 +73,13 @@ async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
return Merchant.from_row(row) if row else None
async def delete_merchants(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.merchants WHERE id = ?",
(merchant_id,),
)
######################################## ZONES ########################################
@ -220,6 +227,13 @@ async def delete_stall(merchant_id: str, stall_id: str) -> None:
)
async def delete_merchant_stalls(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.stalls WHERE merchant_id = ?",
(merchant_id,),
)
######################################## PRODUCTS ########################################
@ -326,6 +340,13 @@ async def delete_product(merchant_id: str, product_id: str) -> None:
)
async def delete_merchant_products(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.products WHERE merchant_id = ?",
(merchant_id,),
)
######################################## ORDERS ########################################
@ -451,6 +472,13 @@ async def update_order_shipped_status(
return Order.from_row(row) if row else None
async def delete_merchant_orders(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.orders WHERE merchant_id = ?",
(merchant_id,),
)
######################################## MESSAGES ########################################L
@ -520,6 +548,13 @@ async def get_last_direct_messages_time(public_key: str) -> int:
SELECT event_created_at FROM nostrmarket.direct_messages
WHERE public_key = ? ORDER BY event_created_at DESC LIMIT 1
""",
(public_key),
(public_key,),
)
return row[0] if row else 0
async def delete_merchant_direct_messages(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.direct_messages WHERE merchant_id = ?",
(merchant_id,),
)

View file

@ -11,7 +11,7 @@ from .event import NostrEvent
async def publish_nostr_event(e: NostrEvent):
print('### publish_nostr_event', e.dict())
print("### publish_nostr_event", e.dict())
await send_req_queue.put(["EVENT", e.dict()])

View file

@ -8,6 +8,7 @@
<div class="row">
<div class="col-2">
<merchant-details
:merchant-id="merchant.id"
:inkey="g.user.wallets[0].inkey"
:adminkey="g.user.wallets[0].adminkey"
@show-keys="toggleMerchantKeys"

View file

@ -22,7 +22,12 @@ from .crud import (
create_product,
create_stall,
create_zone,
delete_merchant_direct_messages,
delete_merchant_orders,
delete_merchant_products,
delete_merchant_stalls,
delete_merchant_zones,
delete_merchants,
delete_product,
delete_stall,
delete_zone,
@ -96,16 +101,23 @@ async def api_get_merchant(
)
@nostrmarket_ext.delete("/api/v1/merchant")
@nostrmarket_ext.delete("/api/v1/merchant/{merchant_id}")
async def api_delete_merchant(
merchant_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
try:
merchant = await get_merchant_for_user(wallet.wallet.user)
assert merchant, "Merchant cannot be found"
assert merchant.id == merchant_id, "Wrong merchant ID"
await delete_merchant_zones(wallet.wallet.user)
await delete_merchant_orders(merchant.id)
await delete_merchant_products(merchant.id)
await delete_merchant_stalls(merchant.id)
await delete_merchant_direct_messages(merchant.id)
await delete_merchant_zones(merchant.id)
await delete_merchants(merchant.id)
except Exception as ex:
logger.warning(ex)
raise HTTPException(