From 76af65c148b2e548d8cf1e44be2c80bb00f78f22 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Tue, 7 Mar 2023 12:00:43 +0200 Subject: [PATCH] feat: show more details about the order --- crud.py | 5 +- migrations.py | 1 + models.py | 28 +++++++++-- static/components/order-list/order-list.html | 52 ++++++++++++-------- static/components/order-list/order-list.js | 7 +++ tasks.py | 4 +- views_api.py | 2 + 7 files changed, 72 insertions(+), 27 deletions(-) diff --git a/crud.py b/crud.py index 62e862f..4297f7a 100644 --- a/crud.py +++ b/crud.py @@ -318,8 +318,8 @@ async def delete_product(user_id: str, product_id: str) -> None: async def create_order(user_id: str, o: Order) -> Order: await db.execute( f""" - INSERT INTO nostrmarket.orders (user_id, id, event_id, pubkey, address, contact_data, order_items, stall_id, invoice_id, total) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO nostrmarket.orders (user_id, id, event_id, pubkey, address, contact_data, extra_data, order_items, stall_id, invoice_id, total) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( user_id, @@ -328,6 +328,7 @@ async def create_order(user_id: str, o: Order) -> Order: o.pubkey, o.address, json.dumps(o.contact.dict() if o.contact else {}), + json.dumps(o.extra.dict()), json.dumps([i.dict() for i in o.items]), o.stall_id, o.invoice_id, diff --git a/migrations.py b/migrations.py index 7c50264..f006d36 100644 --- a/migrations.py +++ b/migrations.py @@ -80,6 +80,7 @@ async def m001_initial(db): event_id TEXT, pubkey TEXT NOT NULL, contact_data TEXT NOT NULL DEFAULT '{empty_object}', + extra_data TEXT NOT NULL DEFAULT '{empty_object}', order_items TEXT NOT NULL, address TEXT, total REAL NOT NULL, diff --git a/models.py b/models.py index a8415e1..3c1111f 100644 --- a/models.py +++ b/models.py @@ -6,7 +6,7 @@ from typing import List, Optional from pydantic import BaseModel -from lnbits.utils.exchange_rates import fiat_amount_as_satoshis +from lnbits.utils.exchange_rates import btc_price, fiat_amount_as_satoshis from .helpers import ( decrypt_message, @@ -244,6 +244,12 @@ class Product(PartialProduct, Nostrable): return product +class ProductOverview(BaseModel): + id: str + name: str + price: float + + ######################################## ORDERS ######################################## @@ -258,6 +264,20 @@ class OrderContact(BaseModel): email: Optional[str] +class OrderExtra(BaseModel): + products: List[ProductOverview] + currency: str + btc_price: str + + @classmethod + async def from_products(cls, products: List[Product]): + currency = products[0].config.currency + exchange_rate = ( + (await btc_price(currency)) if currency and currency != "sat" else 1 + ) + return OrderExtra(products=products, currency=currency, btc_price=exchange_rate) + + class PartialOrder(BaseModel): id: str event_id: Optional[str] @@ -311,13 +331,15 @@ class Order(PartialOrder): total: float paid: bool = False shipped: bool = False - time: int + time: Optional[int] + extra: OrderExtra @classmethod def from_row(cls, row: Row) -> "Order": contact = OrderContact(**json.loads(row["contact_data"])) + extra = OrderExtra(**json.loads(row["extra_data"])) items = [OrderItem(**z) for z in json.loads(row["order_items"])] - order = cls(**dict(row), contact=contact, items=items) + order = cls(**dict(row), contact=contact, items=items, extra=extra) return order diff --git a/static/components/order-list/order-list.html b/static/components/order-list/order-list.html index f1ebbb3..a0968e7 100644 --- a/static/components/order-list/order-list.html +++ b/static/components/order-list/order-list.html @@ -63,20 +63,7 @@
-
-
Customer Public Key:
-
- -
-
-
+
Address:
@@ -91,23 +78,48 @@
-
-
Nostr Contact Pubkey:
+
+
Products:
+
+
+
Quantity
+
+
Name
+
+
+
+
+
+
+
+
+
{{item.quantity}}
+
x
+
+ {{productOverview(props.row, item.product_id)}} +
+
+
+
+
+
+
Customer Public Key:
+
p.id === productId) + if (product) { + return `${product.name} (${product.price} ${order.extra.currency})` + } + return '' + }, getOrders: async function () { try { const ordersPath = this.stallId diff --git a/tasks.py b/tasks.py index f4b9477..69df5a3 100644 --- a/tasks.py +++ b/tasks.py @@ -36,7 +36,7 @@ async def on_invoice_paid(payment: Payment) -> None: if payment.extra.get("tag") != "nostrmarket": return - print("### on_invoice_paid") + print("### on_invoice_paid", json.dumps(payment)) async def subscribe_to_nostr_client(recieve_event_queue: Queue, send_req_queue: Queue): @@ -124,7 +124,7 @@ async def handle_dirrect_message( async def handle_new_order(order: PartialOrder): - ### check that event_id not parsed already + ### todo: check that event_id not parsed already order.validate_order() diff --git a/views_api.py b/views_api.py index e21193d..48935aa 100644 --- a/views_api.py +++ b/views_api.py @@ -46,6 +46,7 @@ from .models import ( Merchant, Nostrable, Order, + OrderExtra, PartialMerchant, PartialOrder, PartialProduct, @@ -487,6 +488,7 @@ async def api_create_order( stall_id=products[0].stall_id, invoice_id=payment_hash, total=total_amount, + extra=await OrderExtra.from_products(products), ) await create_order(wallet.wallet.user, order)