fix: create invoice for fresh merchant

This commit is contained in:
Vlad Stan 2023-03-15 23:08:37 +02:00
parent eb01b3ab34
commit 6498f1aa96
6 changed files with 42 additions and 19 deletions

View file

@ -49,7 +49,7 @@ def nostrmarket_start():
async def _wait_for_nostr_events():
# wait for this extension to initialize
await asyncio.sleep(5)
await wait_for_nostr_events(recieve_event_queue, send_req_queue)
await wait_for_nostr_events(recieve_event_queue)
loop = asyncio.get_event_loop()
task1 = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))

View file

@ -73,7 +73,7 @@ async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
return Merchant.from_row(row) if row else None
async def delete_merchants(merchant_id: str) -> None:
async def delete_merchant(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.merchants WHERE id = ?",
(merchant_id,),

View file

@ -34,3 +34,25 @@ async def connect_to_nostrclient_ws(
wst.start()
return ws
async def subscribe_to_direct_messages(public_key: str, since: int):
in_messages_filter = {"kind": 4, "#p": [public_key]}
out_messages_filter = {"kind": 4, "authors": [public_key]}
if since != 0:
in_messages_filter["since"] = since
out_messages_filter["since"] = since
print("### in_messages_filter", in_messages_filter)
print("### out_messages_filter", out_messages_filter)
await send_req_queue.put(
["REQ", f"direct-messages-in:{public_key}", in_messages_filter]
)
await send_req_queue.put(
["REQ", f"direct-messages-out:{public_key}", out_messages_filter]
)
async def unsubscribe_from_direct_messages(public_key: str):
await send_req_queue.put(["CLOSE", f"direct-messages-in:{public_key}"])
await send_req_queue.put(["CLOSE", f"direct-messages-out:{public_key}"])

View file

@ -30,6 +30,7 @@ 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',

View file

@ -14,7 +14,7 @@ from .crud import (
get_last_order_time,
get_public_keys_for_merchants,
)
from .nostr.nostr_client import connect_to_nostrclient_ws
from .nostr.nostr_client import connect_to_nostrclient_ws, subscribe_to_direct_messages
from .services import handle_order_paid, process_nostr_message
@ -65,25 +65,14 @@ async def subscribe_to_nostr_client(recieve_event_queue: Queue, send_req_queue:
await asyncio.sleep(5)
async def wait_for_nostr_events(recieve_event_queue: Queue, send_req_queue: Queue):
async def wait_for_nostr_events(recieve_event_queue: Queue):
public_keys = await get_public_keys_for_merchants()
for p in public_keys:
last_order_time = await get_last_order_time(p)
last_dm_time = await get_last_direct_messages_time(p)
since = max(last_order_time, last_dm_time)
in_messages_filter = {"kind": 4, "#p": [p]}
out_messages_filter = {"kind": 4, "authors": [p]}
if since != 0:
in_messages_filter["since"] = since
# out_messages_filter["since"] = since
print("### in_messages_filter", in_messages_filter)
print("### out_messages_filter", out_messages_filter)
await send_req_queue.put(["REQ", f"direct-messages-in:{p}", in_messages_filter])
await send_req_queue.put(
["REQ", f"direct-messages-out:{p}", out_messages_filter]
)
await subscribe_to_direct_messages(p, since)
while True:
message = await recieve_event_queue.get()

View file

@ -22,12 +22,12 @@ from .crud import (
create_product,
create_stall,
create_zone,
delete_merchant,
delete_merchant_direct_messages,
delete_merchant_orders,
delete_merchant_products,
delete_merchant_stalls,
delete_merchant_zones,
delete_merchants,
delete_product,
delete_stall,
delete_zone,
@ -61,7 +61,11 @@ from .models import (
Stall,
Zone,
)
from .nostr.nostr_client import publish_nostr_event
from .nostr.nostr_client import (
publish_nostr_event,
subscribe_to_direct_messages,
unsubscribe_from_direct_messages,
)
from .services import sign_and_send_to_nostr
######################################## MERCHANT ########################################
@ -74,7 +78,11 @@ async def api_create_merchant(
) -> Merchant:
try:
merchant = await get_merchant_for_user(wallet.wallet.user)
assert merchant == None, "A merchant already exists for this user"
merchant = await create_merchant(wallet.wallet.user, data)
await subscribe_to_direct_messages(data.public_key, 0)
return merchant
except Exception as ex:
@ -117,7 +125,9 @@ async def api_delete_merchant(
await delete_merchant_stalls(merchant.id)
await delete_merchant_direct_messages(merchant.id)
await delete_merchant_zones(merchant.id)
await delete_merchants(merchant.id)
await unsubscribe_from_direct_messages(merchant.public_key)
await delete_merchant(merchant.id)
except Exception as ex:
logger.warning(ex)
raise HTTPException(
@ -646,4 +656,5 @@ async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)):
except Exception as ex:
logger.warning(ex)
# todo: close websocket
return {"success": True}