fix: notification for orders

This commit is contained in:
Vlad Stan 2023-07-03 16:30:04 +03:00
parent 21a77d643f
commit 2b84ebc83b
5 changed files with 54 additions and 38 deletions

View file

@ -109,17 +109,6 @@ async def create_new_order(
extra=extra, extra=extra,
) )
await create_order(merchant.id, order) await create_order(merchant.id, order)
await websocketUpdater(
merchant.id,
json.dumps(
{
"type": "new-order",
"stallId": products[0].stall_id,
"customerPubkey": data.public_key,
"orderId": order.id,
}
),
)
return PaymentRequest( return PaymentRequest(
id=data.id, payment_options=[PaymentOption(type="ln", link=invoice)] id=data.id, payment_options=[PaymentOption(type="ln", link=invoice)]
@ -174,15 +163,7 @@ async def handle_order_paid(order_id: str, merchant_pubkey: str):
success, message = await update_products_for_order(merchant, order) success, message = await update_products_for_order(merchant, order)
await notify_client_of_order_status(order, merchant, success, message) await notify_client_of_order_status(order, merchant, success, message)
await websocketUpdater(
merchant.id,
json.dumps(
{
"type": "order-paid",
"orderId": order_id,
}
),
)
except Exception as ex: except Exception as ex:
logger.warning(ex) logger.warning(ex)
@ -217,10 +198,15 @@ async def notify_client_of_order_status(
if success if success
else DirectMessageType.PLAIN_TEXT.value, else DirectMessageType.PLAIN_TEXT.value,
) )
await create_direct_message(merchant.id, dm) dm_reply = await create_direct_message(merchant.id, dm)
await nostr_client.publish_nostr_event(dm_event) await nostr_client.publish_nostr_event(dm_event)
await websocketUpdater(
merchant.id,
json.dumps({ "type": f"dm:{dm.type}", "customerPubkey": order.public_key, "dm": dm_reply.dict() }),
)
async def update_products_for_order( async def update_products_for_order(
merchant: Merchant, order: Order merchant: Merchant, order: Order
@ -375,9 +361,9 @@ async def _handle_incoming_dms(
new_dm = await _persist_dm(merchant.id, dm_type.value, event.pubkey, event.id, event.created_at, clear_text_msg) new_dm = await _persist_dm(merchant.id, dm_type.value, event.pubkey, event.id, event.created_at, clear_text_msg)
if json_data: if json_data:
dm_reply = await _handle_incoming_structured_dm(merchant, new_dm, json_data) reply_type, dm_reply = await _handle_incoming_structured_dm(merchant, new_dm, json_data)
if dm_reply: if dm_reply:
await _reply_to_structured_dm(merchant, event, dm_reply) await _reply_to_structured_dm(merchant, event, reply_type.value, dm_reply)
async def _handle_outgoing_dms( async def _handle_outgoing_dms(
@ -396,14 +382,16 @@ async def _handle_outgoing_dms(
await create_direct_message(merchant.id, dm) await create_direct_message(merchant.id, dm)
async def _handle_incoming_structured_dm(merchant: Merchant, dm: DirectMessage, json_data) -> Optional[str]: async def _handle_incoming_structured_dm(merchant: Merchant, dm: DirectMessage, json_data) -> Tuple[DirectMessageType, Optional[str]]:
try: try:
if dm.type == DirectMessageType.CUSTOMER_ORDER.value and merchant.config.active: if dm.type == DirectMessageType.CUSTOMER_ORDER.value and merchant.config.active:
json_data["public_key"] = dm.public_key json_data["public_key"] = dm.public_key
json_data["merchant_public_key"] = merchant.public_key json_data["merchant_public_key"] = merchant.public_key
json_data["event_id"] = dm.event_id json_data["event_id"] = dm.event_id
json_data["event_created_at"] = dm.event_created_at json_data["event_created_at"] = dm.event_created_at
return await _handle_new_order(PartialOrder(**json_data))
json_data = await _handle_new_order(PartialOrder(**json_data))
return DirectMessageType.PAYMENT_REQUEST, json_data
return None return None
except Exception as ex: except Exception as ex:
@ -411,7 +399,7 @@ async def _handle_incoming_structured_dm(merchant: Merchant, dm: DirectMessage,
return None return None
async def _persist_dm(merchant_id: str, dm_type: str, from_pubkey:str, event_id:str, event_created_at: int, msg: str) -> DirectMessage: async def _persist_dm(merchant_id: str, dm_type: int, from_pubkey:str, event_id:str, event_created_at: int, msg: str) -> DirectMessage:
dm = PartialDirectMessage( dm = PartialDirectMessage(
event_id=event_id, event_id=event_id,
event_created_at=event_created_at, event_created_at=event_created_at,
@ -424,21 +412,26 @@ async def _persist_dm(merchant_id: str, dm_type: str, from_pubkey:str, event_id:
await websocketUpdater( await websocketUpdater(
merchant_id, merchant_id,
json.dumps({"type": "new-direct-message", "customerPubkey": from_pubkey, "data": new_dm.dict()}), json.dumps({"type": f"dm:{dm_type}", "customerPubkey": from_pubkey, "dm": new_dm.dict()}),
) )
return new_dm return new_dm
async def _reply_to_structured_dm(merchant: Merchant, event: NostrEvent, dm_reply: str): async def _reply_to_structured_dm(merchant: Merchant, event: NostrEvent, dm_type: int, dm_reply: str):
dm_event = merchant.build_dm_event(dm_reply, event.pubkey) dm_event = merchant.build_dm_event(dm_reply, event.pubkey)
dm = PartialDirectMessage( dm = PartialDirectMessage(
event_id=dm_event.id, event_id=dm_event.id,
event_created_at=dm_event.created_at, event_created_at=dm_event.created_at,
message=dm_reply, message=dm_reply,
public_key=event.pubkey, public_key=event.pubkey,
type=DirectMessageType.PAYMENT_REQUEST.value, type=dm_type,
) )
await create_direct_message(merchant.id, dm) await create_direct_message(merchant.id, dm)
await nostr_client.publish_nostr_event(dm_event) await nostr_client.publish_nostr_event(dm_event)
print("### _reply_to_structured_dm", json.dumps({ "type": f"dm:{dm_type}", "customerPubkey": dm.public_key, "dm": dm.dict() }))
await websocketUpdater(
merchant.id,
json.dumps({ "type": f"dm:{dm_type}", "customerPubkey": dm.public_key, "dm": dm.dict() }),
)
@ -455,6 +448,7 @@ async def _handle_new_order(order: PartialOrder) -> Optional[str]:
payment_req = await create_new_order(order.merchant_public_key, order) payment_req = await create_new_order(order.merchant_public_key, order)
print("### payment_req", payment_req)
except Exception as e: except Exception as e:
payment_req = PaymentRequest(id=order.id, message=str(e), payment_options=[]) payment_req = PaymentRequest(id=order.id, message=str(e), payment_options=[])

View file

@ -107,9 +107,9 @@ async function directMessages(path) {
this.showAddPublicKey = false this.showAddPublicKey = false
} }
}, },
handleNewMessage: async function (dm) { handleNewMessage: async function (data) {
if (dm.customerPubkey === this.activePublicKey) { if (data.customerPubkey === this.activePublicKey) {
this.messages.push(dm.data) this.messages.push(data.dm)
this.focusOnChatBox(this.messages.length - 1) this.focusOnChatBox(this.messages.length - 1)
// focus back on input box // focus back on input box
} }

View file

@ -233,7 +233,8 @@ async function orderList(path) {
!this.search.publicKey || !this.search.publicKey ||
this.search.publicKey === data.customerPubkey this.search.publicKey === data.customerPubkey
) { ) {
const order = await this.getOrder(data.orderId) const orderData = JSON.parse(data.dm.message)
const order = await this.getOrder(orderData.id)
this.orders.unshift(order) this.orders.unshift(order)
} }
}, },

View file

@ -162,21 +162,37 @@ const merchant = async () => {
this.wsConnection = new WebSocket(wsUrl) this.wsConnection = new WebSocket(wsUrl)
this.wsConnection.onmessage = async e => { this.wsConnection.onmessage = async e => {
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
if (data.type === 'new-order') { console.log('#### onmessage', data)
if (data.type === 'dm:0') {
this.$q.notify({ this.$q.notify({
timeout: 5000, timeout: 5000,
type: 'positive', type: 'positive',
message: 'New Order' message: 'New Order'
}) })
await this.$refs.directMessagesRef.handleNewMessage(data)
return
}
if (data.type === 'dm:1') {
await this.$refs.directMessagesRef.handleNewMessage(data)
await this.$refs.orderListRef.addOrder(data) await this.$refs.orderListRef.addOrder(data)
} else if (data.type === 'order-paid') { return
}
if (data.type === 'dm:2') {
const orderStatus = JSON.parse(data.dm.message)
console.log('### orderStatus', orderStatus)
this.$q.notify({ this.$q.notify({
timeout: 5000, timeout: 5000,
type: 'positive', type: 'positive',
message: 'Order Paid' message: orderStatus.message
}) })
await this.$refs.orderListRef.addOrder(data) if (orderStatus.paid) {
} else if (data.type === 'new-direct-message') { await this.$refs.orderListRef.orderPaid(orderStatus.id)
}
await this.$refs.directMessagesRef.handleNewMessage(data)
return
}
if (data.type === 'dm:-1') {
await this.$refs.directMessagesRef.handleNewMessage(data) await this.$refs.directMessagesRef.handleNewMessage(data)
} }
// order paid // order paid

View file

@ -5,6 +5,7 @@ from typing import List, Optional
from fastapi import Depends from fastapi import Depends
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from loguru import logger from loguru import logger
from lnbits.core.services import websocketUpdater
from lnbits.decorators import ( from lnbits.decorators import (
WalletTypeInfo, WalletTypeInfo,
@ -799,6 +800,10 @@ async def api_update_order_status(
await create_direct_message(merchant.id, dm) await create_direct_message(merchant.id, dm)
await nostr_client.publish_nostr_event(dm_event) await nostr_client.publish_nostr_event(dm_event)
await websocketUpdater(
merchant.id,
json.dumps({ "type": f"dm:{dm.type}", "customerPubkey": order.public_key, "dm": dm.dict() }),
)
return order return order