From a719517b9cdf3805be327e55c417e17163433697 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Mon, 27 Mar 2023 17:36:08 +0300 Subject: [PATCH] feat: show customer names --- crud.py | 18 +----------------- services.py | 14 +++++++------- .../direct-messages/direct-messages.html | 2 +- .../direct-messages/direct-messages.js | 9 +++++---- views_api.py | 14 +++++--------- 5 files changed, 19 insertions(+), 38 deletions(-) diff --git a/crud.py b/crud.py index fb70c79..196a973 100644 --- a/crud.py +++ b/crud.py @@ -464,14 +464,6 @@ async def get_orders_for_stall(merchant_id: str, stall_id: str) -> List[Order]: return [Order.from_row(row) for row in rows] -async def get_public_keys_for_orders(merchant_id: str) -> List[str]: - rows = await db.fetchall( - "SELECT DISTINCT public_key FROM nostrmarket.orders WHERE merchant_id = ?", - (merchant_id,), - ) - return [row[0] for row in rows] - - async def get_last_order_time(public_key: str) -> int: row = await db.fetchone( """ @@ -598,14 +590,6 @@ async def delete_merchant_direct_messages(merchant_id: str) -> None: ) -async def get_public_keys_for_direct_messages(merchant_id: str) -> List[str]: - rows = await db.fetchall( - "SELECT DISTINCT public_key FROM nostrmarket.direct_messages WHERE merchant_id = ?", - (merchant_id), - ) - return [row[0] for row in rows] - - ######################################## CUSTOMERS ######################################## @@ -638,7 +622,7 @@ async def get_customer(merchant_id: str, public_key: str) -> Optional[Customer]: return Customer.from_row(row) if row else None -async def get_cusomers(merchant_id: str) -> List[Customer]: +async def get_customers(merchant_id: str) -> List[Customer]: rows = await db.fetchall( "SELECT * FROM nostrmarket.customers WHERE merchant_id = ?", (merchant_id,) ) diff --git a/services.py b/services.py index d653457..6c159c4 100644 --- a/services.py +++ b/services.py @@ -253,6 +253,13 @@ async def _handle_nip04_message(merchant_public_key: str, event: NostrEvent): async def _handle_incoming_dms( event: NostrEvent, merchant: Merchant, clear_text_msg: str ): + customer = await get_customer(merchant.id, event.pubkey) + if not customer: + await create_customer( + merchant.id, Customer(merchant_id=merchant.id, public_key=event.pubkey) + ) + await nostr_client.subscribe_to_user_profile(event.pubkey, 0) + dm_reply = await _handle_dirrect_message( merchant.id, merchant.public_key, @@ -265,13 +272,6 @@ async def _handle_incoming_dms( dm_event = merchant.build_dm_event(dm_reply, event.pubkey) await nostr_client.publish_nostr_event(dm_event) - customer = await get_customer(merchant.id, event.pubkey) - if not customer: - await create_customer( - merchant.id, Customer(merchant_id=merchant.id, public_key=event.pubkey) - ) - await nostr_client.subscribe_to_user_profile(event.pubkey, 0) - async def _handle_outgoing_dms( event: NostrEvent, merchant: Merchant, clear_text_msg: str diff --git a/static/components/direct-messages/direct-messages.html b/static/components/direct-messages/direct-messages.html index 43a81ac..5d4c678 100644 --- a/static/components/direct-messages/direct-messages.html +++ b/static/components/direct-messages/direct-messages.html @@ -9,7 +9,7 @@ DirectMessage: +) -> List[Customer]: try: merchant = await get_merchant_for_user(wallet.wallet.user) assert merchant, f"Merchant cannot be found" - - dm_pubkeys = await get_public_keys_for_direct_messages(merchant.id) - orders_pubkeys = await get_public_keys_for_orders(merchant.id) - - return list(dict.fromkeys(dm_pubkeys + orders_pubkeys)) + return await get_customers(merchant.id) except AssertionError as ex: raise HTTPException(