feat: allow delete merchant from nostr
This commit is contained in:
parent
260bea9ccf
commit
05ecc65e5a
7 changed files with 141 additions and 11 deletions
14
crud.py
14
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 = ?""",
|
||||
|
|
|
|||
46
models.py
46
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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -24,11 +24,19 @@
|
|||
>
|
||||
</q-item-section>
|
||||
</q-item>
|
||||
<q-item @click="deleteMerchant" clickable v-close-popup>
|
||||
<q-item @click="deleteMerchantTables" clickable v-close-popup>
|
||||
<q-item-section>
|
||||
<q-item-label>Delete Merchant</q-item-label>
|
||||
<q-item-label>Delete Merchant from DB</q-item-label>
|
||||
<q-item-label caption
|
||||
>Delete all stalls, products and orders</q-item-label
|
||||
>Delete all stalls, products and orders from database</q-item-label
|
||||
>
|
||||
</q-item-section>
|
||||
</q-item>
|
||||
<q-item @click="deleteMerchantFromNostr" clickable v-close-popup>
|
||||
<q-item-section>
|
||||
<q-item-label>Delete Merchant from Nostr</q-item-label>
|
||||
<q-item-label caption
|
||||
>Delete all stalls, products and orders from Nostr</q-item-label
|
||||
>
|
||||
</q-item-section>
|
||||
</q-item>
|
||||
|
|
|
|||
|
|
@ -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 () {}
|
||||
|
|
|
|||
43
views_api.py
43
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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue