fix: shipping cost; update UI on new order

This commit is contained in:
Vlad Stan 2023-04-03 11:57:55 +03:00
parent f80a75e90c
commit 547c477ce6
11 changed files with 57 additions and 23 deletions

View file

@ -20,6 +20,7 @@ from .crud import (
get_products_by_ids,
get_stalls,
get_wallet_for_product,
get_zone,
increment_customer_unread_messages,
update_customer_profile,
update_order_paid_status,
@ -60,8 +61,12 @@ async def create_new_order(
merchant.id, [p.product_id for p in data.items]
)
data.validate_order_items(products)
shipping_zone = await get_zone(merchant.id, data.shipping_id)
assert shipping_zone, f"Shipping zone not found for order '{data.id}'"
total_amount = await data.total_sats(products)
product_cost_sat, shipping_cost_sat = await data.costs_in_sats(
products, shipping_zone.cost
)
wallet_id = await get_wallet_for_product(data.items[0].product_id)
assert wallet_id, "Missing wallet for order `{data.id}`"
@ -75,7 +80,7 @@ async def create_new_order(
payment_hash, invoice = await create_invoice(
wallet_id=wallet_id,
amount=round(total_amount),
amount=round(product_cost_sat + shipping_cost_sat),
memo=f"Order '{data.id}' for pubkey '{data.public_key}'",
extra={
"tag": "nostrmarket",
@ -84,12 +89,16 @@ async def create_new_order(
},
)
extra = await OrderExtra.from_products(products)
extra.shipping_cost_sat = shipping_cost_sat
extra.shipping_cost = shipping_zone.cost
order = Order(
**data.dict(),
stall_id=products[0].stall_id,
invoice_id=payment_hash,
total=total_amount,
extra=await OrderExtra.from_products(products),
total=product_cost_sat + shipping_cost_sat,
extra=extra,
)
await create_order(merchant.id, order)
await websocketUpdater(
@ -99,6 +108,7 @@ async def create_new_order(
"type": "new-order",
"stallId": products[0].stall_id,
"customerPubkey": data.public_key,
"orderId": order.id,
}
),
)
@ -312,6 +322,11 @@ async def _handle_dirrect_message(
order["event_created_at"] = event_created_at
return await _handle_new_order(PartialOrder(**order))
await websocketUpdater(
merchant_id,
json.dumps({"type": "new-direct-message", "customerPubkey": from_pubkey}),
)
return None
except Exception as ex:
logger.warning(ex)