From 05ecc65e5a5959f07875b7d8c788209753507c80 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 23 Mar 2023 11:14:34 +0200 Subject: [PATCH] feat: allow delete merchant from `nostr` --- crud.py | 14 ++++++ models.py | 46 ++++++++++++++++++- .../customer-stall/customer-stall.js | 5 +- .../merchant-details/merchant-details.html | 14 ++++-- .../merchant-details/merchant-details.js | 28 ++++++++++- static/js/index.js | 2 +- views_api.py | 43 ++++++++++++++++- 7 files changed, 141 insertions(+), 11 deletions(-) diff --git a/crud.py b/crud.py index a939f52..2029a2e 100644 --- a/crud.py +++ b/crud.py @@ -7,6 +7,7 @@ from . import db from .models import ( DirectMessage, Merchant, + MerchantConfig, Order, PartialDirectMessage, PartialMerchant, @@ -35,6 +36,19 @@ async def create_merchant(user_id: str, m: PartialMerchant) -> Merchant: return merchant +async def update_merchant( + user_id: str, merchant_id: str, config: MerchantConfig +) -> Optional[Merchant]: + await db.execute( + f""" + UPDATE nostrmarket.merchants SET meta = ? + WHERE id = ? AND user_id = ? + """, + (json.dumps(config.dict()), merchant_id, user_id), + ) + return await get_merchant(user_id, merchant_id) + + async def get_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]: row = await db.fetchone( """SELECT * FROM nostrmarket.merchants WHERE user_id = ? AND id = ?""", diff --git a/models.py b/models.py index 379d3f8..9c82306 100644 --- a/models.py +++ b/models.py @@ -30,8 +30,16 @@ class Nostrable: ######################################## MERCHANT ######################################## -class MerchantConfig(BaseModel): + + +class MerchantProfile(BaseModel): name: Optional[str] + about: Optional[str] + picture: Optional[str] + + +class MerchantConfig(MerchantProfile): + event_id: Optional[str] class PartialMerchant(BaseModel): @@ -40,7 +48,7 @@ class PartialMerchant(BaseModel): config: MerchantConfig = MerchantConfig() -class Merchant(PartialMerchant): +class Merchant(PartialMerchant, Nostrable): id: str def sign_hash(self, hash: bytes) -> str: @@ -74,6 +82,40 @@ class Merchant(PartialMerchant): merchant.config = MerchantConfig(**json.loads(row["meta"])) return merchant + def to_nostr_event(self, pubkey: str) -> NostrEvent: + content = { + "name": self.config.name, + "about": self.config.about, + "picture": self.config.picture, + } + event = NostrEvent( + pubkey=pubkey, + created_at=round(time.time()), + kind=0, + tags=[], + content=json.dumps(content, separators=(",", ":"), ensure_ascii=False), + ) + event.id = event.event_id + + return event + + def to_nostr_delete_event(self, pubkey: str) -> NostrEvent: + content = { + "name": f"{self.config.name} (deleted)", + "about": "Merchant Deleted", + "picture": "", + } + delete_event = NostrEvent( + pubkey=pubkey, + created_at=round(time.time()), + kind=0, + tags=[], + content=json.dumps(content, separators=(",", ":"), ensure_ascii=False), + ) + delete_event.id = delete_event.event_id + + return delete_event + ######################################## ZONES ######################################## class PartialZone(BaseModel): diff --git a/static/components/customer-stall/customer-stall.js b/static/components/customer-stall/customer-stall.js index 2b4090d..f9e82ea 100644 --- a/static/components/customer-stall/customer-stall.js +++ b/static/components/customer-stall/customer-stall.js @@ -354,8 +354,9 @@ async function customerStall(path) { this.qrCodeDialog.data.message = json.message return cb() } - let payment_request = json.payment_options.find(o => o.type == 'ln') - .link + let payment_request = json.payment_options.find( + o => o.type == 'ln' + ).link if (!payment_request) return this.loading = false this.qrCodeDialog.data.payment_request = payment_request diff --git a/static/components/merchant-details/merchant-details.html b/static/components/merchant-details/merchant-details.html index 0d8e1ec..2485447 100644 --- a/static/components/merchant-details/merchant-details.html +++ b/static/components/merchant-details/merchant-details.html @@ -24,11 +24,19 @@ > - + - Delete Merchant + Delete Merchant from DB Delete all stalls, products and ordersDelete all stalls, products and orders from database + + + + + Delete Merchant from Nostr + Delete all stalls, products and orders from Nostr diff --git a/static/components/merchant-details/merchant-details.js b/static/components/merchant-details/merchant-details.js index 1327ba5..297ce36 100644 --- a/static/components/merchant-details/merchant-details.js +++ b/static/components/merchant-details/merchant-details.js @@ -15,7 +15,7 @@ async function merchantDetails(path) { this.showKeys = !this.showKeys this.$emit('show-keys', this.showKeys) }, - deleteMerchant: function () { + deleteMerchantTables: function () { LNbits.utils .confirmDialog( ` @@ -30,7 +30,6 @@ async function merchantDetails(path) { '/nostrmarket/api/v1/merchant/' + this.merchantId, this.adminkey ) - // todo: refresh parent page this.$emit('merchant-deleted', this.merchantId) this.$q.notify({ type: 'positive', @@ -42,6 +41,31 @@ async function merchantDetails(path) { LNbits.utils.notifyApiError(error) } }) + }, + deleteMerchantFromNostr: function () { + LNbits.utils + .confirmDialog( + ` + Do you want to remove the merchant from Nostr? + ` + ) + .onOk(async () => { + try { + await LNbits.api.request( + 'DELETE', + `/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`, + this.adminkey + ) + this.$q.notify({ + type: 'positive', + message: 'Merchant Deleted from Nostr', + timeout: 5000 + }) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } + }) } }, created: async function () {} diff --git a/static/js/index.js b/static/js/index.js index 12c354e..58b2c5c 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -59,7 +59,7 @@ const merchant = async () => { toggleMerchantKeys: function (value) { this.showKeys = value }, - handleMerchantDeleted: function() { + handleMerchantDeleted: function () { this.merchant = null this.shippingZones = [] this.activeChatCustomer = '' diff --git a/views_api.py b/views_api.py index cefac0a..df436aa 100644 --- a/views_api.py +++ b/views_api.py @@ -45,6 +45,7 @@ from .crud import ( get_stalls, get_zone, get_zones, + update_merchant, update_order_shipped_status, update_product, update_stall, @@ -147,6 +148,44 @@ async def api_delete_merchant( ) +@nostrmarket_ext.delete("/api/v1/merchant/{merchant_id}/nostr") +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" + + 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 + 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", + ) + + ######################################## ZONES ######################################## @@ -566,7 +605,9 @@ async def api_delete_product( ) await delete_product(merchant.id, product_id) - await sign_and_send_to_nostr(merchant, product, True) + event = await sign_and_send_to_nostr(merchant, product, True) + product.config.event_id = event.id + await update_product(merchant.id, product) except AssertionError as ex: raise HTTPException(