From 612d31eae07dc4998e300b700c47a43427e46cd1 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Tue, 7 Mar 2023 14:30:58 +0200 Subject: [PATCH] feat: add ship/unship functionality --- crud.py | 8 +- static/components/order-list/order-list.html | 84 +++++++++++++------- static/components/order-list/order-list.js | 37 ++++++++- tasks.py | 3 +- views_api.py | 47 +++++++---- 5 files changed, 129 insertions(+), 50 deletions(-) diff --git a/crud.py b/crud.py index b8ee099..2e6013b 100644 --- a/crud.py +++ b/crud.py @@ -392,10 +392,12 @@ async def update_order_paid_status(order_id: str, paid: bool) -> Optional[Order] return Order.from_row(row) if row else None -async def update_order_shipped_status(order_id: str, shipped: bool) -> Optional[Order]: +async def update_order_shipped_status( + user_id: str, order_id: str, shipped: bool +) -> Optional[Order]: await db.execute( - f"UPDATE nostrmarket.orders SET shipped = ? WHERE id = ?", - (order_id, shipped), + f"UPDATE nostrmarket.orders SET shipped = ? WHERE user_id = ? AND id = ?", + (shipped, user_id, order_id), ) row = await db.fetchone( diff --git a/static/components/order-list/order-list.html b/static/components/order-list/order-list.html index a0968e7..e629b1f 100644 --- a/static/components/order-list/order-list.html +++ b/static/components/order-list/order-list.html @@ -37,6 +37,7 @@ @@ -49,6 +50,33 @@ +
+
Products:
+
+
+
Quantity
+
+
Name
+
+
+
+
+
+
+
+
+
{{item.quantity}}
+
x
+
+ {{productOverview(props.row, item.product_id)}} +
+
+
+
+
Order ID:
@@ -78,33 +106,7 @@
-
-
Products:
-
-
-
Quantity
-
-
Name
-
-
-
-
-
-
-
-
-
{{item.quantity}}
-
x
-
- {{productOverview(props.row, item.product_id)}} -
-
-
-
-
+
Customer Public Key:
@@ -172,4 +174,32 @@ + + + + + + +
+ + + Cancel +
+
+
+
diff --git a/static/components/order-list/order-list.js b/static/components/order-list/order-list.js index bb4f335..482cc8a 100644 --- a/static/components/order-list/order-list.js +++ b/static/components/order-list/order-list.js @@ -8,7 +8,9 @@ async function orderList(path) { data: function () { return { orders: [], - + selectedOrder: null, + shippingMessage: '', + showShipDialog: false, filter: '', ordersTable: { columns: [ @@ -93,6 +95,39 @@ async function orderList(path) { } catch (error) { LNbits.utils.notifyApiError(error) } + }, + updateOrderShipped: async function () { + console.log('### order', this.selectedOrder) + this.selectedOrder.shipped = !this.selectedOrder.shipped + try { + await LNbits.api.request( + 'PATCH', + `/nostrmarket/api/v1/order/${this.selectedOrder.id}`, + this.adminkey, + { + id: this.selectedOrder.id, + message: this.shippingMessage, + shipped: this.selectedOrder.shipped + } + ) + this.$q.notify({ + type: 'positive', + message: 'Order updated!' + }) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + this.showShipDialog = false + }, + showShipOrderDialog: function (order) { + this.selectedOrder = order + this.shippingMessage = order.shipped + ? `The order has been shipped! Order ID: '${order.id}' ` + : `The order has NOT yet been shipped! Order ID: '${order.id}'` + + // do not change the status yet + this.selectedOrder.shipped = !order.shipped + this.showShipDialog = true } }, created: async function () { diff --git a/tasks.py b/tasks.py index f5afec3..c97c2b2 100644 --- a/tasks.py +++ b/tasks.py @@ -45,7 +45,6 @@ async def on_invoice_paid(payment: Payment) -> None: await handle_order_paid(order_id, merchant_pubkey) - async def handle_order_paid(order_id: str, merchant_pubkey: str): try: order = await update_order_paid_status(order_id, True) @@ -55,7 +54,7 @@ async def handle_order_paid(order_id: str, merchant_pubkey: str): ) merchant = await get_merchant_by_pubkey(merchant_pubkey) - assert merchant, f"Merchant cannot be foud for order {order_id}" + assert merchant, f"Merchant cannot be found for order {order_id}" dm_content = json.dumps( order_status.dict(), separators=(",", ":"), ensure_ascii=False ) diff --git a/views_api.py b/views_api.py index 32696e4..29f6cd3 100644 --- a/views_api.py +++ b/views_api.py @@ -38,6 +38,7 @@ from .crud import ( get_wallet_for_product, get_zone, get_zones, + update_order_shipped_status, update_product, update_stall, update_zone, @@ -47,6 +48,7 @@ from .models import ( Nostrable, Order, OrderExtra, + OrderStatusUpdate, PartialMerchant, PartialOrder, PartialProduct, @@ -542,24 +544,35 @@ async def api_get_orders(wallet: WalletTypeInfo = Depends(get_key_type)): ) -# @nostrmarket_ext.patch("/api/v1/order/{order_id}") -# async def api_update_order( -# data: OrderStatusUpdate, -# wallet: WalletTypeInfo = Depends(require_admin_key), -# ) -> Zone: -# try: +@nostrmarket_ext.patch("/api/v1/order/{order_id}") +async def api_update_order_status( + data: OrderStatusUpdate, + wallet: WalletTypeInfo = Depends(require_admin_key), +) -> Order: + try: + assert data.shipped != None, "Shipped value is required for order" + order = await update_order_shipped_status( + wallet.wallet.user, data.id, data.shipped + ) + assert order, "Cannot find updated order" -# zone = await update_order(wallet.wallet.user, data) -# assert zone, "Cannot find updated zone" -# return zone -# except HTTPException as ex: -# raise ex -# except Exception as ex: -# logger.warning(ex) -# raise HTTPException( -# status_code=HTTPStatus.INTERNAL_SERVER_ERROR, -# detail="Cannot update order", -# ) + merchant = await get_merchant_for_user(wallet.wallet.user) + assert merchant, f"Merchant cannot be found for order {data.id}" + + data.paid = order.paid + dm_content = json.dumps(data.dict(), separators=(",", ":"), ensure_ascii=False) + + dm_event = merchant.build_dm_event(dm_content, order.pubkey) + await publish_nostr_event(dm_event) + + return order + + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot update order", + ) ######################################## OTHER ########################################