feat: republish to nostr

This commit is contained in:
Vlad Stan 2023-03-24 14:52:34 +02:00
parent 5694c4112f
commit 13150f6360
4 changed files with 65 additions and 36 deletions

View file

@ -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:

View file

@ -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

View file

@ -15,30 +15,23 @@ async function merchantDetails(path) {
this.showKeys = !this.showKeys
this.$emit('show-keys', this.showKeys)
},
republishMerchantData: function () {
LNbits.utils
.confirmDialog(
`You are about to republish to Nostr`
republishMerchantData: async function () {
try {
await LNbits.api.request(
'PUT',
`/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`,
this.adminkey
)
.onOk(async () => {
try {
await LNbits.api.request(
'PATCH',
`/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`,
this.adminkey
)
this.$emit('merchant-deleted', this.merchantId)
this.$q.notify({
type: 'positive',
message: 'Merchant Deleted',
timeout: 5000
})
} catch (error) {
console.warn(error)
LNbits.utils.notifyApiError(error)
}
this.$q.notify({
type: 'positive',
message: 'Merchant data republished to Nostr',
timeout: 5000
})
} catch (error) {
console.warn(error)
LNbits.utils.notifyApiError(error)
}
},
deleteMerchantTables: function () {
LNbits.utils

View file

@ -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: