From 13150f6360276d0f6ea648a8432a51acbdfe059f Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 24 Mar 2023 14:52:34 +0200 Subject: [PATCH] feat: republish to nostr --- services.py | 21 ++++++++++ .../merchant-details/merchant-details.html | 2 +- .../merchant-details/merchant-details.js | 37 +++++++---------- views_api.py | 41 +++++++++++++------ 4 files changed, 65 insertions(+), 36 deletions(-) diff --git a/services.py b/services.py index 2bf3f01..63e715a 100644 --- a/services.py +++ b/services.py @@ -12,11 +12,14 @@ from .crud import ( get_merchant_by_pubkey, get_order, get_order_by_event_id, + get_products, get_products_by_ids, + get_stalls, get_wallet_for_product, update_order_paid_status, update_product, update_product_quantity, + update_stall, ) from .helpers import order_from_json from .models import ( @@ -88,6 +91,24 @@ async def create_new_order( ) +async def update_merchant_to_nostr( + merchant: Merchant, delete_merchant=False +) -> Merchant: + stalls = await get_stalls(merchant.id) + for stall in stalls: + products = await get_products(merchant.id, stall.id) + for product in products: + event = await sign_and_send_to_nostr(merchant, product, delete_merchant) + product.config.event_id = event.id + await update_product(merchant.id, product) + event = await sign_and_send_to_nostr(merchant, stall, delete_merchant) + stall.config.event_id = event.id + await update_stall(merchant.id, stall) + event = await sign_and_send_to_nostr(merchant, merchant, delete_merchant) + merchant.config.event_id = event.id + return merchant + + async def sign_and_send_to_nostr( merchant: Merchant, n: Nostrable, delete=False ) -> NostrEvent: diff --git a/static/components/merchant-details/merchant-details.html b/static/components/merchant-details/merchant-details.html index 802a31f..cc76cd6 100644 --- a/static/components/merchant-details/merchant-details.html +++ b/static/components/merchant-details/merchant-details.html @@ -24,7 +24,7 @@ > - + Republish to Nostr { - try { - await LNbits.api.request( - 'PATCH', - `/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`, - this.adminkey - ) - this.$emit('merchant-deleted', this.merchantId) - this.$q.notify({ - type: 'positive', - message: 'Merchant Deleted', - timeout: 5000 - }) - } catch (error) { - console.warn(error) - LNbits.utils.notifyApiError(error) - } + this.$q.notify({ + type: 'positive', + message: 'Merchant data republished to Nostr', + timeout: 5000 }) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } }, deleteMerchantTables: function () { LNbits.utils diff --git a/views_api.py b/views_api.py index 2e209df..e7bc07c 100644 --- a/views_api.py +++ b/views_api.py @@ -65,7 +65,7 @@ from .models import ( Stall, Zone, ) -from .services import sign_and_send_to_nostr +from .services import sign_and_send_to_nostr, update_merchant_to_nostr ######################################## MERCHANT ######################################## @@ -148,6 +148,32 @@ async def api_delete_merchant( ) +@nostrmarket_ext.put("/api/v1/merchant/{merchant_id}/nostr") +async def api_republish_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" + + merchant = await update_merchant_to_nostr(merchant) + await update_merchant(wallet.wallet.user, merchant.id, merchant.config) + + except AssertionError as ex: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail=str(ex), + ) + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot get merchant", + ) + + @nostrmarket_ext.delete("/api/v1/merchant/{merchant_id}/nostr") async def api_delete_merchant( merchant_id: str, @@ -158,18 +184,7 @@ async def api_delete_merchant( assert merchant, "Merchant cannot be found" assert merchant.id == merchant_id, "Wrong merchant ID" - stalls = await get_stalls(merchant.id) - for stall in stalls: - products = await get_products(merchant.id, stall.id) - for product in products: - event = await sign_and_send_to_nostr(merchant, product, True) - product.config.event_id = event.id - await update_product(merchant.id, product) - event = await sign_and_send_to_nostr(merchant, stall, True) - stall.config.event_id = event.id - await update_stall(merchant.id, stall) - event = await sign_and_send_to_nostr(merchant, merchant, True) - merchant.config.event_id = event.id + merchant = await update_merchant_to_nostr(merchant, True) await update_merchant(wallet.wallet.user, merchant.id, merchant.config) except AssertionError as ex: