From 2dc5c5479f6d6d5f2f1a614d0df697d637fb92a9 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 20 Sep 2023 12:06:36 +0300 Subject: [PATCH] feat: refresh merchant from nostr (#85) --- crud.py | 2 +- nostr/nostr_client.py | 4 +-- .../merchant-details/merchant-details.html | 9 +++++++ .../merchant-details/merchant-details.js | 17 ++++++++++++ static/components/order-list/order-list.html | 2 +- views_api.py | 26 ++++++++++++++++++- 6 files changed, 55 insertions(+), 5 deletions(-) diff --git a/crud.py b/crud.py index 339ccfe..986b7b5 100644 --- a/crud.py +++ b/crud.py @@ -205,7 +205,7 @@ async def create_stall(merchant_id: str, data: PartialStall) -> Stall: ) stall = await get_stall(merchant_id, stall_id) - assert stall, "Newly created stall couldn't be retrieved" + assert stall, f"Newly created stall couldn't be retrieved. Id: {stall_id}" return stall diff --git a/nostr/nostr_client.py b/nostr/nostr_client.py index 20e9bd5..b243c66 100644 --- a/nostr/nostr_client.py +++ b/nostr/nostr_client.py @@ -103,7 +103,7 @@ class NostrClient: f"Subscribed to events for: {len(public_keys)} keys. New subscription id: {self.subscription_id}" ) - async def merchant_temp_subscription(self, pk, duration=5): + async def merchant_temp_subscription(self, pk, duration=10): dm_filters = self._filters_for_direct_messages([pk], 0) stall_filters = self._filters_for_stall_events([pk], 0) product_filters = self._filters_for_product_events([pk], 0) @@ -125,7 +125,7 @@ class NostrClient: asyncio.create_task(unsubscribe_with_delay(subscription_id, duration)) - async def user_profile_temp_subscribe(self, public_key: str, duration=30) -> List: + async def user_profile_temp_subscribe(self, public_key: str, duration=5) -> List: try: profile_filter = [{"kinds": [0], "authors": [public_key]}] subscription_id = "profile-" + urlsafe_short_hash()[:32] diff --git a/static/components/merchant-details/merchant-details.html b/static/components/merchant-details/merchant-details.html index cc76cd6..35f9e31 100644 --- a/static/components/merchant-details/merchant-details.html +++ b/static/components/merchant-details/merchant-details.html @@ -24,6 +24,14 @@ > + + + Refresh from Nostr + Requery all stalls, products and orders from Nostr + + Republish to Nostr @@ -32,6 +40,7 @@ > + Delete from DB diff --git a/static/components/merchant-details/merchant-details.js b/static/components/merchant-details/merchant-details.js index 9a63d40..31dcfac 100644 --- a/static/components/merchant-details/merchant-details.js +++ b/static/components/merchant-details/merchant-details.js @@ -33,6 +33,23 @@ async function merchantDetails(path) { LNbits.utils.notifyApiError(error) } }, + requeryMerchantData: async function () { + try { + await LNbits.api.request( + 'GET', + `/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`, + this.adminkey + ) + this.$q.notify({ + type: 'positive', + message: 'Merchant data refreshed from Nostr', + timeout: 5000 + }) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } + }, deleteMerchantTables: function () { LNbits.utils .confirmDialog( diff --git a/static/components/order-list/order-list.html b/static/components/order-list/order-list.html index d3c7705..f166b03 100644 --- a/static/components/order-list/order-list.html +++ b/static/components/order-list/order-list.html @@ -15,7 +15,7 @@
+ class="q-pt-md float-right" :label="search.restoring ? 'Restoring Orders...' : 'Load Orders'"> diff --git a/views_api.py b/views_api.py index 457f659..4bb792f 100644 --- a/views_api.py +++ b/views_api.py @@ -219,7 +219,31 @@ async def api_republish_merchant( logger.warning(ex) raise HTTPException( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - detail="Cannot get merchant", + detail="Cannot republish to nostr", + ) + +@nostrmarket_ext.get("/api/v1/merchant/{merchant_id}/nostr") +async def api_refresh_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 nostr_client.merchant_temp_subscription(merchant.public_key) + + 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 refresh from nostr", )