From 21a77d643f81d01958a308e26364fbed1355ffc9 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Mon, 3 Jul 2023 12:40:42 +0300 Subject: [PATCH] feat: activate/deactivate merchant --- services.py | 3 +- static/js/index.js | 47 ++++++++++++++++++++++++++++++-- templates/nostrmarket/index.html | 14 ++++++++-- views_api.py | 29 +++++++++++++++++++- 4 files changed, 86 insertions(+), 7 deletions(-) diff --git a/services.py b/services.py index 8a877d1..47a8c5f 100644 --- a/services.py +++ b/services.py @@ -398,8 +398,7 @@ async def _handle_outgoing_dms( async def _handle_incoming_structured_dm(merchant: Merchant, dm: DirectMessage, json_data) -> Optional[str]: try: - if dm.type == DirectMessageType.CUSTOMER_ORDER.value: - # and merchant.config.active: + if dm.type == DirectMessageType.CUSTOMER_ORDER.value and merchant.config.active: json_data["public_key"] = dm.public_key json_data["merchant_public_key"] = merchant.public_key json_data["event_id"] = dm.event_id diff --git a/static/js/index.js b/static/js/index.js index e2db4d7..ec53a6e 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -61,6 +61,48 @@ const merchant = async () => { toggleMerchantKeys: function (value) { this.showKeys = value }, + toggleMerchantState: async function () { + const merchant = await this.getMerchant() + if (!merchant) { + this.$q.notify({ + timeout: 5000, + type: 'warning', + message: "Cannot fetch merchant!" + }) + return + } + const message = merchant.config.active ? + 'New orders will not be processed. Are you sure you want to deactivate?' : + merchant.config.restore_in_progress ? + 'Merchant restore from nostr in progress. Please wait!! ' + + 'Activating now can lead to duplicate order processing. Click "OK" if you want to activate anyway?' : + 'Are you sure you want activate this merchant?' + + LNbits.utils + .confirmDialog(message) + .onOk(async () => { + await this.toggleMerchant() + }) + }, + toggleMerchant: async function () { + try { + const { data } = await LNbits.api.request( + 'PUT', + `/nostrmarket/api/v1/merchant/${this.merchant.id}/toggle`, + this.g.user.wallets[0].adminkey, + ) + const state = data.config.active ? 'activated' : 'disabled' + this.merchant = data + this.$q.notify({ + type: 'positive', + message: `'Merchant ${state}`, + timeout: 5000 + }) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } + }, handleMerchantDeleted: function () { this.merchant = null this.shippingZones = [] @@ -75,7 +117,7 @@ const merchant = async () => { public_key: pubkey, config: {} } - const {data} = await LNbits.api.request( + const { data } = await LNbits.api.request( 'POST', '/nostrmarket/api/v1/merchant', this.g.user.wallets[0].adminkey, @@ -93,12 +135,13 @@ const merchant = async () => { }, getMerchant: async function () { try { - const {data} = await LNbits.api.request( + const { data } = await LNbits.api.request( 'GET', '/nostrmarket/api/v1/merchant', this.g.user.wallets[0].inkey ) this.merchant = data + return data } catch (error) { LNbits.utils.notifyApiError(error) } diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index 603cf6b..7e5a097 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -6,7 +6,7 @@
-
+
-
+
+ +
+
Merchant: + 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.config.active = not merchant.config.active + await update_merchant(wallet.wallet.user, merchant.id, merchant.config) + + return merchant + 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(