From 5b03c324f10f6fa51fb574360e5f6a38ce02e55d Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 29 Mar 2023 16:22:05 +0300 Subject: [PATCH] feat: filter orders --- crud.py | 31 +++++++--- static/components/order-list/order-list.html | 39 ++++++++++-- static/components/order-list/order-list.js | 64 ++++++++++++++++++-- templates/nostrmarket/index.html | 10 ++- views_api.py | 20 ++++-- 5 files changed, 138 insertions(+), 26 deletions(-) diff --git a/crud.py b/crud.py index 131bd2e..a4d5fb7 100644 --- a/crud.py +++ b/crud.py @@ -445,21 +445,34 @@ async def get_order_by_event_id(merchant_id: str, event_id: str) -> Optional[Ord return Order.from_row(row) if row else None -async def get_orders(merchant_id: str) -> List[Order]: +async def get_orders(merchant_id: str, **kwargs) -> List[Order]: + q = " AND ".join( + [f"{field[0]} = ?" for field in kwargs.items() if field[1] != None] + ) + values = () + if q: + q = f"AND {q}" + values = (v for v in kwargs.values() if v != None) rows = await db.fetchall( - "SELECT * FROM nostrmarket.orders WHERE merchant_id = ? ORDER BY time DESC", - (merchant_id,), + f"SELECT * FROM nostrmarket.orders WHERE merchant_id = ? {q} ORDER BY time DESC", + (merchant_id, *values), ) return [Order.from_row(row) for row in rows] -async def get_orders_for_stall(merchant_id: str, stall_id: str) -> List[Order]: +async def get_orders_for_stall( + merchant_id: str, stall_id: str, **kwargs +) -> List[Order]: + q = " AND ".join( + [f"{field[0]} = ?" for field in kwargs.items() if field[1] != None] + ) + values = () + if q: + q = f"AND {q}" + values = (v for v in kwargs.values() if v != None) rows = await db.fetchall( - "SELECT * FROM nostrmarket.orders WHERE merchant_id = ? AND stall_id = ? ORDER BY time DESC", - ( - merchant_id, - stall_id, - ), + f"SELECT * FROM nostrmarket.orders WHERE merchant_id = ? AND stall_id = ? {q} ORDER BY time DESC", + (merchant_id, stall_id, *values), ) return [Order.from_row(row) for row in rows] diff --git a/static/components/order-list/order-list.html b/static/components/order-list/order-list.html index 15465bd..1c665ff 100644 --- a/static/components/order-list/order-list.html +++ b/static/components/order-list/order-list.html @@ -1,18 +1,45 @@
-
+
+ + +
+
+ + +
+
+ + +
+
Refresh OrdersSearch Orders
-
+
({...s, expanded: false})) @@ -129,10 +160,35 @@ async function orderList(path) { }, customerSelected: function (customerPubkey) { this.$emit('customer-selected', customerPubkey) + }, + getCustomers: async function () { + try { + const {data} = await LNbits.api.request( + 'GET', + '/nostrmarket/api/v1/customers', + this.inkey + ) + this.customers = data + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, + buildCustomerLabel: function (c) { + let label = `${c.profile.name || 'unknown'} ${c.profile.about || ''}` + if (c.unread_messages) { + label += `[new: ${c.unread_messages}]` + } + label += ` (${c.public_key.slice(0, 16)}...${c.public_key.slice( + c.public_key.length - 16 + )}` + return label } }, created: async function () { - await this.getOrders() + if (this.stallId) { + await this.getOrders() + } + await this.getCustomers() } }) } diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index c2c8c23..051f524 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -62,9 +62,13 @@
-
-
-
+
+ +
diff --git a/views_api.py b/views_api.py index 3092bee..8b8a4ee 100644 --- a/views_api.py +++ b/views_api.py @@ -1,6 +1,6 @@ import json from http import HTTPStatus -from typing import List, Optional +from typing import List, Optional, Union from fastapi import Depends from fastapi.exceptions import HTTPException @@ -448,12 +448,17 @@ async def api_get_stall_products( @nostrmarket_ext.get("/api/v1/stall/order/{stall_id}") async def api_get_stall_orders( stall_id: str, + paid: Optional[bool] = None, + shipped: Optional[bool] = None, + pubkey: Optional[str] = None, wallet: WalletTypeInfo = Depends(require_invoice_key), ): try: merchant = await get_merchant_for_user(wallet.wallet.user) assert merchant, "Merchant cannot be found" - orders = await get_orders_for_stall(merchant.id, stall_id) + orders = await get_orders_for_stall( + merchant.id, stall_id, paid=paid, shipped=shipped, public_key=pubkey + ) return orders except AssertionError as ex: raise HTTPException( @@ -673,12 +678,19 @@ async def api_get_order(order_id: str, wallet: WalletTypeInfo = Depends(get_key_ @nostrmarket_ext.get("/api/v1/order") -async def api_get_orders(wallet: WalletTypeInfo = Depends(get_key_type)): +async def api_get_orders( + paid: Optional[bool] = None, + shipped: Optional[bool] = None, + pubkey: Optional[str] = None, + wallet: WalletTypeInfo = Depends(get_key_type), +): try: merchant = await get_merchant_for_user(wallet.wallet.user) assert merchant, "Merchant cannot be found" - orders = await get_orders(merchant.id) + orders = await get_orders( + merchant_id=merchant.id, paid=paid, shipped=shipped, public_key=pubkey + ) return orders except AssertionError as ex: raise HTTPException(