fix: use merchant id instead of user id

This commit is contained in:
Vlad Stan 2023-03-15 11:25:52 +02:00
parent 40a52e9134
commit 856ad53b7e
3 changed files with 18 additions and 17 deletions

View file

@ -35,7 +35,7 @@ async def create_new_order(
merchant_public_key: str, data: PartialOrder merchant_public_key: str, data: PartialOrder
) -> Optional[PaymentRequest]: ) -> Optional[PaymentRequest]:
merchant = await get_merchant_by_pubkey(merchant_public_key) merchant = await get_merchant_by_pubkey(merchant_public_key)
assert merchant, "Cannot find merchant!" assert merchant, "Cannot find merchant for order!"
if await get_order(merchant.id, data.id): if await get_order(merchant.id, data.id):
return None return None
@ -116,29 +116,31 @@ async def process_nostr_message(msg: str):
type, *rest = json.loads(msg) type, *rest = json.loads(msg)
if type.upper() == "EVENT": if type.upper() == "EVENT":
subscription_id, event = rest subscription_id, event = rest
subscription_name, merchant_public_key = subscription_id.split(":") _, merchant_public_key = subscription_id.split(":")
event = NostrEvent(**event) event = NostrEvent(**event)
if event.kind == 4: if event.kind == 4:
await _handle_nip04_message( await _handle_nip04_message(merchant_public_key, event)
subscription_name, merchant_public_key, event
)
return return
except Exception as ex: except Exception as ex:
print("####### bad message", msg)
logger.warning(ex) logger.warning(ex)
async def _handle_nip04_message( async def _handle_nip04_message(merchant_public_key: str, event: NostrEvent):
subscription_name: str, merchant_public_key: str, event: NostrEvent
):
merchant = await get_merchant_by_pubkey(merchant_public_key) merchant = await get_merchant_by_pubkey(merchant_public_key)
assert merchant, f"Merchant not found for public key '{merchant_public_key}'" assert merchant, f"Merchant not found for public key '{merchant_public_key}'"
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
# print("### clear_text_msg", subscription_name, clear_text_msg) # print("### clear_text_msg", subscription_name, clear_text_msg)
if subscription_name == "direct-messages-in": if event.pubkey == merchant_public_key:
assert len(event.tag_values("p")) != 0, "Outgong message has no 'p' tag"
clear_text_msg = merchant.decrypt_message(event.content, event.tag_values("p")[0])
await _handle_outgoing_dms(event, merchant, clear_text_msg)
elif event.has_tag_value("p", merchant_public_key):
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
await _handle_incoming_dms(event, merchant, clear_text_msg) await _handle_incoming_dms(event, merchant, clear_text_msg)
else: else:
await _handle_outgoing_dms(event, merchant, clear_text_msg) logger.warning(f"Bad NIP04 event: '{event.id}'")
async def _handle_incoming_dms( async def _handle_incoming_dms(
@ -166,8 +168,7 @@ async def _handle_outgoing_dms(
event_id=event.id, event_id=event.id,
event_created_at=event.created_at, event_created_at=event.created_at,
message=clear_text_msg, # exclude if json message=clear_text_msg, # exclude if json
public_key=sent_to[0], public_key=sent_to[0]
incoming=True,
) )
await create_direct_message(merchant.id, dm) await create_direct_message(merchant.id, dm)
@ -216,7 +217,7 @@ async def _handle_new_order(order: PartialOrder) -> Optional[str]:
wallet = await get_wallet(wallet_id) wallet = await get_wallet(wallet_id)
assert wallet, f"Cannot find wallet for product id: {first_product_id}" assert wallet, f"Cannot find wallet for product id: {first_product_id}"
new_order = await create_new_order(wallet.user, order) new_order = await create_new_order(order.merchant_public_key, order)
if new_order: if new_order:
return json.dumps(new_order.dict(), separators=(",", ":"), ensure_ascii=False) return json.dumps(new_order.dict(), separators=(",", ":"), ensure_ascii=False)

View file

@ -8,7 +8,7 @@ async function directMessages(path) {
data: function () { data: function () {
return { return {
activePublicKey: activePublicKey:
'83d07a79496f4cbdc50ca585741a79a2df1fd938cfa449f0fccb0ab7352115dd', '186895a32209c3a92f0efaa7c65c3f8da690c75b952b815718c0d55d3eed821e',
messages: [], messages: [],
newMessage: '' newMessage: ''
} }

View file

@ -175,7 +175,7 @@ async def api_update_zone(
status_code=HTTPStatus.NOT_FOUND, status_code=HTTPStatus.NOT_FOUND,
detail="Zone does not exist.", detail="Zone does not exist.",
) )
zone = await update_zone(wallet.wallet.user, data) zone = await update_zone(merchant.id, data)
assert zone, "Cannot find updated zone" assert zone, "Cannot find updated zone"
return zone return zone
except HTTPException as ex: except HTTPException as ex:
@ -201,7 +201,7 @@ async def api_delete_zone(zone_id, wallet: WalletTypeInfo = Depends(require_admi
detail="Zone does not exist.", detail="Zone does not exist.",
) )
await delete_zone(wallet.wallet.user, zone_id) await delete_zone(merchant.id, zone_id)
except Exception as ex: except Exception as ex:
logger.warning(ex) logger.warning(ex)