feat: show DMs for customers

This commit is contained in:
Vlad Stan 2023-03-16 18:34:56 +02:00
parent 482961a2ef
commit b45927a15d
4 changed files with 78 additions and 12 deletions

16
crud.py
View file

@ -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]

View file

@ -7,13 +7,14 @@
<q-separator></q-separator>
</q-card-section>
<q-card-section>
<!-- <q-select
v-model="customerKey"
:options="Object.keys(messages).map(k => ({label: `${k.slice(0, 25)}...`, value: k}))"
label="Customers"
@input="chatRoom(customerKey)"
<q-select
v-model="activePublicKey"
:options="customersPublicKeys.map(k => ({label: `${k.slice(0, 16)}...${k.slice(k.length - 16)}`, value: k}))"
label="Select Customer"
emit-value
></q-select> -->
@input="selectActiveCustomer()"
>
</q-select>
</q-card-section>
<q-card-section>
<div class="chat-container" ref="chatCard">

View file

@ -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()
}
})
}

View file

@ -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 ########################################