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_merchant_by_pubkey,
|
||||||
get_order,
|
get_order,
|
||||||
get_order_by_event_id,
|
get_order_by_event_id,
|
||||||
|
get_products,
|
||||||
get_products_by_ids,
|
get_products_by_ids,
|
||||||
|
get_stalls,
|
||||||
get_wallet_for_product,
|
get_wallet_for_product,
|
||||||
update_order_paid_status,
|
update_order_paid_status,
|
||||||
update_product,
|
update_product,
|
||||||
update_product_quantity,
|
update_product_quantity,
|
||||||
|
update_stall,
|
||||||
)
|
)
|
||||||
from .helpers import order_from_json
|
from .helpers import order_from_json
|
||||||
from .models import (
|
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(
|
async def sign_and_send_to_nostr(
|
||||||
merchant: Merchant, n: Nostrable, delete=False
|
merchant: Merchant, n: Nostrable, delete=False
|
||||||
) -> NostrEvent:
|
) -> NostrEvent:
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
>
|
>
|
||||||
</q-item-section>
|
</q-item-section>
|
||||||
</q-item>
|
</q-item>
|
||||||
<q-item @click="republishMerchantData" disable clickable v-close-popup>
|
<q-item @click="republishMerchantData" clickable v-close-popup>
|
||||||
<q-item-section>
|
<q-item-section>
|
||||||
<q-item-label>Republish to Nostr</q-item-label>
|
<q-item-label>Republish to Nostr</q-item-label>
|
||||||
<q-item-label caption
|
<q-item-label caption
|
||||||
|
|
|
||||||
|
|
@ -15,30 +15,23 @@ async function merchantDetails(path) {
|
||||||
this.showKeys = !this.showKeys
|
this.showKeys = !this.showKeys
|
||||||
this.$emit('show-keys', this.showKeys)
|
this.$emit('show-keys', this.showKeys)
|
||||||
},
|
},
|
||||||
|
|
||||||
republishMerchantData: function () {
|
republishMerchantData: async function () {
|
||||||
LNbits.utils
|
try {
|
||||||
.confirmDialog(
|
await LNbits.api.request(
|
||||||
`You are about to republish to Nostr`
|
'PUT',
|
||||||
|
`/nostrmarket/api/v1/merchant/${this.merchantId}/nostr`,
|
||||||
|
this.adminkey
|
||||||
)
|
)
|
||||||
.onOk(async () => {
|
this.$q.notify({
|
||||||
try {
|
type: 'positive',
|
||||||
await LNbits.api.request(
|
message: 'Merchant data republished to Nostr',
|
||||||
'PATCH',
|
timeout: 5000
|
||||||
`/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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(error)
|
||||||
|
LNbits.utils.notifyApiError(error)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
deleteMerchantTables: function () {
|
deleteMerchantTables: function () {
|
||||||
LNbits.utils
|
LNbits.utils
|
||||||
|
|
|
||||||
41
views_api.py
41
views_api.py
|
|
@ -65,7 +65,7 @@ from .models import (
|
||||||
Stall,
|
Stall,
|
||||||
Zone,
|
Zone,
|
||||||
)
|
)
|
||||||
from .services import sign_and_send_to_nostr
|
from .services import sign_and_send_to_nostr, update_merchant_to_nostr
|
||||||
|
|
||||||
######################################## MERCHANT ########################################
|
######################################## 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")
|
@nostrmarket_ext.delete("/api/v1/merchant/{merchant_id}/nostr")
|
||||||
async def api_delete_merchant(
|
async def api_delete_merchant(
|
||||||
merchant_id: str,
|
merchant_id: str,
|
||||||
|
|
@ -158,18 +184,7 @@ async def api_delete_merchant(
|
||||||
assert merchant, "Merchant cannot be found"
|
assert merchant, "Merchant cannot be found"
|
||||||
assert merchant.id == merchant_id, "Wrong merchant ID"
|
assert merchant.id == merchant_id, "Wrong merchant ID"
|
||||||
|
|
||||||
stalls = await get_stalls(merchant.id)
|
merchant = await update_merchant_to_nostr(merchant, True)
|
||||||
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)
|
await update_merchant(wallet.wallet.user, merchant.id, merchant.config)
|
||||||
|
|
||||||
except AssertionError as ex:
|
except AssertionError as ex:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue