feat: republish to nostr
This commit is contained in:
parent
5694c4112f
commit
13150f6360
4 changed files with 65 additions and 36 deletions
21
services.py
21
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:
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
>
|
||||
</q-item-section>
|
||||
</q-item>
|
||||
<q-item @click="republishMerchantData" disable clickable v-close-popup>
|
||||
<q-item @click="republishMerchantData" clickable v-close-popup>
|
||||
<q-item-section>
|
||||
<q-item-label>Republish to Nostr</q-item-label>
|
||||
<q-item-label caption
|
||||
|
|
|
|||
|
|
@ -16,29 +16,22 @@ async function merchantDetails(path) {
|
|||
this.$emit('show-keys', this.showKeys)
|
||||
},
|
||||
|
||||
republishMerchantData: function () {
|
||||
LNbits.utils
|
||||
.confirmDialog(
|
||||
`You are about to republish to Nostr`
|
||||
)
|
||||
.onOk(async () => {
|
||||
republishMerchantData: async function () {
|
||||
try {
|
||||
await LNbits.api.request(
|
||||
'PATCH',
|
||||
'PUT',
|
||||
`/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`,
|
||||
this.adminkey
|
||||
)
|
||||
this.$emit('merchant-deleted', this.merchantId)
|
||||
this.$q.notify({
|
||||
type: 'positive',
|
||||
message: 'Merchant Deleted',
|
||||
message: 'Merchant data republished to Nostr',
|
||||
timeout: 5000
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn(error)
|
||||
LNbits.utils.notifyApiError(error)
|
||||
}
|
||||
})
|
||||
},
|
||||
deleteMerchantTables: function () {
|
||||
LNbits.utils
|
||||
|
|
|
|||
41
views_api.py
41
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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue