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