From b45927a15dda3ad779414ed7e9d46da85cba5b2e Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 16 Mar 2023 18:34:56 +0200 Subject: [PATCH] feat: show DMs for customers --- crud.py | 16 ++++++++++ .../direct-messages/direct-messages.html | 13 ++++---- .../direct-messages/direct-messages.js | 30 ++++++++++++++---- views_api.py | 31 +++++++++++++++++++ 4 files changed, 78 insertions(+), 12 deletions(-) diff --git a/crud.py b/crud.py index 3917270..a939f52 100644 --- a/crud.py +++ b/crud.py @@ -448,6 +448,14 @@ 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( """ @@ -572,3 +580,11 @@ async def delete_merchant_direct_messages(merchant_id: str) -> None: "DELETE FROM nostrmarket.direct_messages WHERE merchant_id = ?", (merchant_id,), ) + + +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] diff --git a/static/components/direct-messages/direct-messages.html b/static/components/direct-messages/direct-messages.html index 17cf401..43a81ac 100644 --- a/static/components/direct-messages/direct-messages.html +++ b/static/components/direct-messages/direct-messages.html @@ -7,13 +7,14 @@ - + @input="selectActiveCustomer()" + > +
diff --git a/static/components/direct-messages/direct-messages.js b/static/components/direct-messages/direct-messages.js index 9020cad..a9c58ef 100644 --- a/static/components/direct-messages/direct-messages.js +++ b/static/components/direct-messages/direct-messages.js @@ -7,22 +7,23 @@ async function directMessages(path) { data: function () { return { - activePublicKey: - '186895a32209c3a92f0efaa7c65c3f8da690c75b952b815718c0d55d3eed821e', + customersPublicKeys: [], + activePublicKey: '', messages: [], newMessage: '' } }, methods: { sendMessage: async function () {}, - getDirectMessages: async function () { - if (!this.activePublicKey) { + getDirectMessages: async function (pubkey) { + if (!pubkey) { + this.messages = [] return } try { const {data} = await LNbits.api.request( 'GET', - '/nostrmarket/api/v1/message/' + this.activePublicKey, + '/nostrmarket/api/v1/message/' + pubkey, this.inkey ) this.messages = data @@ -35,6 +36,19 @@ async function directMessages(path) { LNbits.utils.notifyApiError(error) } }, + getCustomersPublicKeys: async function () { + try { + const {data} = await LNbits.api.request( + 'GET', + '/nostrmarket/api/v1/customers', + this.inkey + ) + this.customersPublicKeys = data + console.log('### this.customersPublicKeys', this.customersPublicKeys) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, sendDirectMesage: async function () { try { const {data} = await LNbits.api.request( @@ -54,6 +68,10 @@ async function directMessages(path) { LNbits.utils.notifyApiError(error) } }, + selectActiveCustomer: async function () { + console.log('### selectActiveCustomer', this.activePublicKey) + await this.getDirectMessages(this.activePublicKey) + }, focusOnChatBox: function (index) { setTimeout(() => { const lastChatBox = document.getElementsByClassName( @@ -66,7 +84,7 @@ async function directMessages(path) { } }, created: async function () { - await this.getDirectMessages() + await this.getCustomersPublicKeys() } }) } diff --git a/views_api.py b/views_api.py index 1934b42..bc3a9e3 100644 --- a/views_api.py +++ b/views_api.py @@ -39,6 +39,8 @@ from .crud import ( get_orders_for_stall, get_product, get_products, + get_public_keys_for_direct_messages, + get_public_keys_for_orders, get_stall, get_stalls, get_zone, @@ -727,6 +729,35 @@ async def api_create_message( ) +######################################## CUSTOMERS ######################################## + + +@nostrmarket_ext.get("/api/v1/customers") +async def api_create_message( + wallet: WalletTypeInfo = Depends(get_key_type), +) -> DirectMessage: + 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)) + + 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 create message", + ) + + ######################################## OTHER ########################################