feat: show customer names

This commit is contained in:
Vlad Stan 2023-03-27 17:36:08 +03:00
parent 89f46fff35
commit a719517b9c
5 changed files with 19 additions and 38 deletions

18
crud.py
View file

@ -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] 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: async def get_last_order_time(public_key: str) -> int:
row = await db.fetchone( 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 ######################################## ######################################## 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 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( rows = await db.fetchall(
"SELECT * FROM nostrmarket.customers WHERE merchant_id = ?", (merchant_id,) "SELECT * FROM nostrmarket.customers WHERE merchant_id = ?", (merchant_id,)
) )

View file

@ -253,6 +253,13 @@ async def _handle_nip04_message(merchant_public_key: str, event: NostrEvent):
async def _handle_incoming_dms( async def _handle_incoming_dms(
event: NostrEvent, merchant: Merchant, clear_text_msg: str 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( dm_reply = await _handle_dirrect_message(
merchant.id, merchant.id,
merchant.public_key, merchant.public_key,
@ -265,13 +272,6 @@ async def _handle_incoming_dms(
dm_event = merchant.build_dm_event(dm_reply, event.pubkey) dm_event = merchant.build_dm_event(dm_reply, event.pubkey)
await nostr_client.publish_nostr_event(dm_event) 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( async def _handle_outgoing_dms(
event: NostrEvent, merchant: Merchant, clear_text_msg: str event: NostrEvent, merchant: Merchant, clear_text_msg: str

View file

@ -9,7 +9,7 @@
<q-card-section> <q-card-section>
<q-select <q-select
v-model="activePublicKey" v-model="activePublicKey"
:options="customersPublicKeys.map(k => ({label: `${k.slice(0, 16)}...${k.slice(k.length - 16)}`, value: k}))" :options="customers.map(c => ({label: `${c.profile.name || 'unknown'} ${c.profile.about || ''} (${c.public_key.slice(0, 16)}...${c.public_key.slice(c.public_key.length - 16)})`, value: c.public_key}))"
label="Select Customer" label="Select Customer"
emit-value emit-value
@input="selectActiveCustomer()" @input="selectActiveCustomer()"

View file

@ -12,7 +12,7 @@ async function directMessages(path) {
}, },
data: function () { data: function () {
return { return {
customersPublicKeys: [], customers: [],
messages: [], messages: [],
newMessage: '' newMessage: ''
} }
@ -40,14 +40,15 @@ async function directMessages(path) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} }
}, },
getCustomersPublicKeys: async function () { getCustomers: async function () {
try { try {
const {data} = await LNbits.api.request( const {data} = await LNbits.api.request(
'GET', 'GET',
'/nostrmarket/api/v1/customers', '/nostrmarket/api/v1/customers',
this.inkey this.inkey
) )
this.customersPublicKeys = data this.customers = data
console.log('### customers', this.customers)
} catch (error) { } catch (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} }
@ -85,7 +86,7 @@ async function directMessages(path) {
} }
}, },
created: async function () { created: async function () {
await this.getCustomersPublicKeys() await this.getCustomers()
} }
}) })
} }

View file

@ -31,6 +31,7 @@ from .crud import (
delete_product, delete_product,
delete_stall, delete_stall,
delete_zone, delete_zone,
get_customers,
get_direct_messages, get_direct_messages,
get_merchant_by_pubkey, get_merchant_by_pubkey,
get_merchant_for_user, get_merchant_for_user,
@ -39,8 +40,6 @@ from .crud import (
get_orders_for_stall, get_orders_for_stall,
get_product, get_product,
get_products, get_products,
get_public_keys_for_direct_messages,
get_public_keys_for_orders,
get_stall, get_stall,
get_stalls, get_stalls,
get_zone, get_zone,
@ -52,6 +51,7 @@ from .crud import (
update_zone, update_zone,
) )
from .models import ( from .models import (
Customer,
DirectMessage, DirectMessage,
Merchant, Merchant,
Order, Order,
@ -785,17 +785,13 @@ async def api_create_message(
@nostrmarket_ext.get("/api/v1/customers") @nostrmarket_ext.get("/api/v1/customers")
async def api_create_message( async def api_get_customers(
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
) -> DirectMessage: ) -> List[Customer]:
try: try:
merchant = await get_merchant_for_user(wallet.wallet.user) merchant = await get_merchant_for_user(wallet.wallet.user)
assert merchant, f"Merchant cannot be found" assert merchant, f"Merchant cannot be found"
return await get_customers(merchant.id)
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))
except AssertionError as ex: except AssertionError as ex:
raise HTTPException( raise HTTPException(