mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-26 15:42:30 +01:00
orders
This commit is contained in:
parent
d6d33c6a22
commit
cad896f909
3 changed files with 65 additions and 67 deletions
|
@ -16,6 +16,7 @@ from .models import (
|
|||
Products,
|
||||
Stalls,
|
||||
Zones,
|
||||
createOrder,
|
||||
createProduct,
|
||||
createStalls,
|
||||
createZones,
|
||||
|
@ -83,7 +84,7 @@ async def get_diagonalley_product(product_id: str) -> Optional[Products]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.products WHERE id = ?", (product_id,)
|
||||
)
|
||||
return Products.from_row(row) if row else None
|
||||
return Products(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_products(wallet_ids: Union[str, List[str]]) -> List[Products]:
|
||||
|
@ -98,7 +99,7 @@ async def get_diagonalley_products(wallet_ids: Union[str, List[str]]) -> List[Pr
|
|||
""",
|
||||
(*wallet_ids,),
|
||||
)
|
||||
return [Products.from_row(row) for row in rows]
|
||||
return [Products(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_product(product_id: str) -> None:
|
||||
|
@ -139,12 +140,12 @@ async def update_diagonalley_zone(zone_id: str, **kwargs) -> Optional[Zones]:
|
|||
(*kwargs.values(), zone_id),
|
||||
)
|
||||
row = await db.fetchone("SELECT * FROM diagonalley.zones WHERE id = ?", (zone_id,))
|
||||
return Zones.from_row(row) if row else None
|
||||
return Zones(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_zone(zone_id: str) -> Optional[Zones]:
|
||||
row = await db.fetchone("SELECT * FROM diagonalley.zones WHERE id = ?", (zone_id,))
|
||||
return Zones.from_row(row) if row else None
|
||||
return Zones(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_zones(wallet_ids: Union[str, List[str]]) -> List[Zones]:
|
||||
|
@ -182,7 +183,7 @@ async def get_diagonalley_zones(wallet_ids: Union[str, List[str]]) -> List[Zones
|
|||
rows = await db.fetchall(
|
||||
f"SELECT * FROM diagonalley.zones WHERE wallet IN ({q})", (*wallet_ids,)
|
||||
)
|
||||
return [Zones.from_row(row) for row in rows]
|
||||
return [Zones(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_zone(zone_id: str) -> None:
|
||||
|
@ -233,7 +234,7 @@ async def update_diagonalley_stall(stall_id: str, **kwargs) -> Optional[Stalls]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
|
||||
)
|
||||
return Stalls.from_row(row) if row else None
|
||||
return Stalls(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_stall(stall_id: str) -> Optional[Stalls]:
|
||||
|
@ -266,7 +267,7 @@ async def get_diagonalley_stall(stall_id: str) -> Optional[Stalls]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
|
||||
)
|
||||
return Stalls.from_row(row) if row else None
|
||||
return Stalls(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_stalls(wallet_ids: Union[str, List[str]]) -> List[Stalls]:
|
||||
|
@ -303,7 +304,7 @@ async def get_diagonalley_stalls(wallet_ids: Union[str, List[str]]) -> List[Stal
|
|||
rows = await db.fetchall(
|
||||
f"SELECT * FROM diagonalley.stalls WHERE wallet IN ({q})", (*wallet_ids,)
|
||||
)
|
||||
return [Stalls.from_row(row) for row in rows]
|
||||
return [Stalls(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_stall(stall_id: str) -> None:
|
||||
|
@ -314,47 +315,34 @@ async def delete_diagonalley_stall(stall_id: str) -> None:
|
|||
|
||||
|
||||
async def create_diagonalley_order(
|
||||
*,
|
||||
productid: str,
|
||||
wallet: str,
|
||||
product: str,
|
||||
quantity: int,
|
||||
shippingzone: str,
|
||||
address: str,
|
||||
email: str,
|
||||
invoiceid: str,
|
||||
paid: bool,
|
||||
shipped: bool,
|
||||
data: createOrder
|
||||
) -> Orders:
|
||||
returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||
method = db.execute if db.type == SQLITE else db.fetchone
|
||||
|
||||
order_id = urlsafe_short_hash()
|
||||
result = await (method)(
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO diagonalley.orders (id, productid, wallet, product,
|
||||
quantity, shippingzone, address, email, invoiceid, paid, shipped)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
{returning}
|
||||
""",
|
||||
(
|
||||
order_id,
|
||||
productid,
|
||||
wallet,
|
||||
product,
|
||||
quantity,
|
||||
shippingzone,
|
||||
address,
|
||||
email,
|
||||
invoiceid,
|
||||
data.productid,
|
||||
data.wallet,
|
||||
data.product,
|
||||
data.quantity,
|
||||
data.shippingzone,
|
||||
data.address,
|
||||
data.email,
|
||||
data.invoiceid,
|
||||
False,
|
||||
False,
|
||||
),
|
||||
)
|
||||
if db.type == SQLITE:
|
||||
order_id = result._result_proxy.lastrowid
|
||||
else:
|
||||
order_id = result[0]
|
||||
# if db.type == SQLITE:
|
||||
# order_id = result._result_proxy.lastrowid
|
||||
# else:
|
||||
# order_id = result[0]
|
||||
|
||||
link = await get_diagonalley_order(order_id)
|
||||
assert link, "Newly created link couldn't be retrieved"
|
||||
|
@ -365,7 +353,7 @@ async def get_diagonalley_order(order_id: str) -> Optional[Orders]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
|
||||
)
|
||||
return Orders.from_row(row) if row else None
|
||||
return Orders(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
|
||||
|
@ -377,7 +365,7 @@ async def get_diagonalley_orders(wallet_ids: Union[str, List[str]]) -> List[Orde
|
|||
f"SELECT * FROM diagonalley.orders WHERE wallet IN ({q})", (*wallet_ids,)
|
||||
)
|
||||
#
|
||||
return [Orders.from_row(row) for row in rows]
|
||||
return [Orders(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_order(order_id: str) -> None:
|
||||
|
|
|
@ -59,15 +59,25 @@ class Zones(BaseModel):
|
|||
countries: str
|
||||
|
||||
|
||||
class createOrder(BaseModel):
|
||||
productid: str = Query(...)
|
||||
stall: str = Query(...)
|
||||
product: str = Query(...)
|
||||
quantity: int = Query(..., ge=1)
|
||||
shippingzone: int = Query(...)
|
||||
address: str = Query(...)
|
||||
email: str = Query(...)
|
||||
invoiceid: str = Query(...)
|
||||
|
||||
class Orders(BaseModel):
|
||||
id: str = Query(None)
|
||||
productid: str = Query(None)
|
||||
stall: str = Query(None)
|
||||
product: str = Query(None)
|
||||
quantity: int = Query(0)
|
||||
shippingzone: int = Query(0)
|
||||
address: str = Query(None)
|
||||
email: str = Query(None)
|
||||
invoiceid: str = Query(None)
|
||||
id: str
|
||||
productid: str
|
||||
stall: str
|
||||
product: str
|
||||
quantity: int
|
||||
shippingzone: int
|
||||
address: str
|
||||
email: str
|
||||
invoiceid: str
|
||||
paid: bool
|
||||
shipped: bool
|
||||
|
|
|
@ -44,6 +44,7 @@ from .models import (
|
|||
Products,
|
||||
Stalls,
|
||||
Zones,
|
||||
createOrder,
|
||||
createProduct,
|
||||
createStalls,
|
||||
createZones,
|
||||
|
@ -87,8 +88,8 @@ async def api_diagonalley_products(
|
|||
@diagonalley_ext.post("/api/v1/products")
|
||||
@diagonalley_ext.put("/api/v1/products/{product_id}")
|
||||
async def api_diagonalley_product_create(
|
||||
product_id,
|
||||
data: createProduct,
|
||||
product_id: str = Query(None),
|
||||
wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
|
||||
|
@ -186,7 +187,7 @@ async def api_diagonalley_stalls(wallet: WalletTypeInfo = Depends(get_key_type),
|
|||
|
||||
@diagonalley_ext.post("/api/v1/stalls")
|
||||
@diagonalley_ext.put("/api/v1/stalls/{stall_id}")
|
||||
async def api_diagonalley_stall_create(data: createStalls, stall_id = None, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_stall_create(data: createStalls, stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
|
||||
if stall_id:
|
||||
stall = await get_diagonalley_stall(stall_id)
|
||||
|
@ -222,23 +223,22 @@ async def api_diagonalley_stall_delete(stall_id: str = Query(None), wallet: Wall
|
|||
|
||||
|
||||
@diagonalley_ext.get("/api/v1/orders")
|
||||
async def api_diagonalley_orders(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_orders(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
if all_wallets:
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
|
||||
try:
|
||||
return ([order._asdict() for order in await get_diagonalley_orders(wallet_ids)])
|
||||
return ([order.dict() for order in await get_diagonalley_orders(wallet_ids)])
|
||||
except:
|
||||
return ({"message": "We could not retrieve the orders."}))
|
||||
return ({"message": "We could not retrieve the orders."})
|
||||
|
||||
|
||||
@diagonalley_ext.post("/api/v1/orders")
|
||||
|
||||
async def api_diagonalley_order_create(data: createOrders, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
order = await create_diagonalley_order(wallet_id=wallet.wallet.id, data)
|
||||
return ({**order._asdict()})
|
||||
async def api_diagonalley_order_create(data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
order = await create_diagonalley_order(wallet_id=wallet.wallet.id, data=data)
|
||||
return order.dict()
|
||||
|
||||
|
||||
@diagonalley_ext.delete("/api/v1/orders/{order_id}")
|
||||
|
@ -281,7 +281,7 @@ async def api_diagonalley_order_shipped(order_id: str = Query(None), wallet: Wal
|
|||
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
|
||||
)
|
||||
|
||||
return ([order._asdict() for order in get_diagonalley_orders(order["wallet"])]))
|
||||
return ([order.dict() for order in get_diagonalley_orders(order["wallet"])])
|
||||
|
||||
|
||||
###List products based on stall id
|
||||
|
@ -303,14 +303,14 @@ async def api_diagonalley_stall_products(stall_id: str = Query(None), wallet: Wa
|
|||
if not products:
|
||||
return ({"message": "No products"})
|
||||
|
||||
return ([products._asdict() for products in await get_diagonalley_products(rows[1])])
|
||||
return ([products.dict() for products in await get_diagonalley_products(rows[1])])
|
||||
|
||||
|
||||
###Check a product has been shipped
|
||||
|
||||
|
||||
@diagonalley_ext.get("/api/v1/stall/checkshipped/{checking_id}")
|
||||
async def api_diagonalley_stall_checkshipped(checking_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_stall_checkshipped(checking_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
rows = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.orders WHERE invoiceid = ?", (checking_id,)
|
||||
)
|
||||
|
@ -321,19 +321,19 @@ async def api_diagonalley_stall_checkshipped(checking_id: str = Query(None), wal
|
|||
|
||||
|
||||
@diagonalley_ext.post("/api/v1/stall/order/{stall_id}")
|
||||
async def api_diagonalley_stall_order(data:createOrders, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
product = await get_diagonalley_product(data.id)
|
||||
async def api_diagonalley_stall_order(stall_id, data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
product = await get_diagonalley_product(data.productid)
|
||||
shipping = await get_diagonalley_stall(stall_id)
|
||||
|
||||
if data.shippingzone == 1:
|
||||
shippingcost = shipping.zone1cost
|
||||
shippingcost = shipping.zone1cost #missing in model
|
||||
else:
|
||||
shippingcost = shipping.zone2cost
|
||||
shippingcost = shipping.zone2cost #missing in model
|
||||
|
||||
checking_id, payment_request = await create_invoice(
|
||||
wallet_id=product.wallet,
|
||||
amount=shippingcost + (data.quantity * product.price),
|
||||
memo=data.id,
|
||||
memo=shipping.wallet,
|
||||
)
|
||||
selling_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8")
|
||||
await db.execute(
|
||||
|
@ -343,8 +343,8 @@ async def api_diagonalley_stall_order(data:createOrders, wallet: WalletTypeInfo
|
|||
""",
|
||||
(
|
||||
selling_id,
|
||||
data.id,
|
||||
product.wallet,
|
||||
data.productid,
|
||||
product.wallet, #doesn't exist in model
|
||||
product.product,
|
||||
data.quantity,
|
||||
data.shippingzone,
|
||||
|
@ -355,4 +355,4 @@ async def api_diagonalley_stall_order(data:createOrders, wallet: WalletTypeInfo
|
|||
False,
|
||||
),
|
||||
)
|
||||
return ({"checking_id": checking_id, "payment_request": payment_request}))
|
||||
return ({"checking_id": checking_id, "payment_request": payment_request})
|
||||
|
|
Loading…
Add table
Reference in a new issue