lnbits-legend/lnbits/extensions/shop/views_api.py

509 lines
14 KiB
Python
Raw Normal View History

2022-01-27 14:58:58 +00:00
from base64 import urlsafe_b64encode
2022-01-27 12:24:38 +00:00
from http import HTTPStatus
from typing import List, Union
2022-01-27 14:58:58 +00:00
from uuid import uuid4
2022-01-27 12:24:38 +00:00
from fastapi import Request
from fastapi.param_functions import Body, Query
2022-01-27 12:24:38 +00:00
from fastapi.params import Depends
2022-08-18 10:49:04 +01:00
from loguru import logger
2022-09-28 11:11:52 +01:00
from secp256k1 import PrivateKey, PublicKey
2022-08-16 12:19:31 +01:00
from starlette.exceptions import HTTPException
2022-01-27 12:24:38 +00:00
from lnbits.core.crud import get_user
2022-01-27 14:58:58 +00:00
from lnbits.core.services import create_invoice
2022-08-19 11:19:45 +01:00
from lnbits.core.views.api import api_payment
2022-07-12 15:29:46 +01:00
from lnbits.decorators import (
WalletTypeInfo,
get_key_type,
require_admin_key,
require_invoice_key,
)
2022-01-27 12:24:38 +00:00
2022-08-18 10:49:04 +01:00
from ...helpers import urlsafe_short_hash
2022-12-20 08:21:48 +00:00
from . import db, shop_ext
2022-01-27 12:24:38 +00:00
from .crud import (
2022-12-20 08:21:48 +00:00
create_shop_market,
create_shop_market_stalls,
create_shop_order,
create_shop_order_details,
create_shop_product,
create_shop_stall,
create_shop_zone,
delete_shop_order,
delete_shop_product,
delete_shop_stall,
delete_shop_zone,
get_shop_chat_by_merchant,
get_shop_chat_messages,
get_shop_latest_chat_messages,
get_shop_market,
get_shop_market_stalls,
get_shop_markets,
get_shop_order,
get_shop_order_details,
get_shop_order_invoiceid,
get_shop_orders,
get_shop_product,
get_shop_products,
get_shop_stall,
get_shop_stalls,
get_shop_stalls_by_ids,
get_shop_zone,
get_shop_zones,
set_shop_order_pubkey,
update_shop_market,
update_shop_product,
update_shop_stall,
update_shop_zone,
2022-01-27 12:24:38 +00:00
)
2022-01-27 16:18:12 +00:00
from .models import (
2022-07-27 15:28:58 +01:00
CreateMarket,
2022-01-27 16:18:12 +00:00
Orders,
Products,
Stalls,
Zones,
2022-01-28 15:11:31 +00:00
createOrder,
2022-01-27 16:18:12 +00:00
createProduct,
createStalls,
createZones,
)
2022-01-27 12:24:38 +00:00
# from lnbits.db import open_ext_db
### Products
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/products")
async def api_shop_products(
2022-01-27 12:24:38 +00:00
wallet: WalletTypeInfo = Depends(get_key_type),
2022-07-12 15:25:25 +01:00
all_stalls: bool = Query(False),
2022-01-27 12:24:38 +00:00
):
wallet_ids = [wallet.wallet.id]
2022-01-27 14:58:58 +00:00
if all_stalls:
2022-01-27 12:24:38 +00:00
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
2022-12-20 08:21:48 +00:00
stalls = [stall.id for stall in await get_shop_stalls(wallet_ids)]
2022-07-15 10:22:13 +01:00
if not stalls:
return
2022-12-20 08:21:48 +00:00
return [product.dict() for product in await get_shop_products(stalls)]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.post("/api/v1/products")
@shop_ext.put("/api/v1/products/{product_id}")
async def api_shop_product_create(
2022-08-16 12:19:31 +01:00
data: createProduct, product_id=None, wallet: WalletTypeInfo = Depends(get_key_type)
2022-07-12 15:25:25 +01:00
):
2022-01-27 12:24:38 +00:00
if product_id:
2022-12-20 08:21:48 +00:00
product = await get_shop_product(product_id)
2022-01-27 12:24:38 +00:00
if not product:
2022-07-12 15:25:25 +01:00
return {"message": "Withdraw product does not exist."}
2022-08-16 12:19:31 +01:00
2022-12-20 08:21:48 +00:00
stall = await get_shop_stall(stall_id=product.stall)
2022-07-15 10:22:13 +01:00
if stall.wallet != wallet.wallet.id:
2022-07-12 15:25:25 +01:00
return {"message": "Not your withdraw product."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
product = await update_shop_product(product_id, **data.dict())
2022-01-27 12:24:38 +00:00
else:
2022-12-20 08:21:48 +00:00
product = await create_shop_product(data=data)
2022-08-16 12:19:31 +01:00
2022-01-27 14:58:58 +00:00
return product.dict()
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.delete("/api/v1/products/{product_id}")
async def api_shop_products_delete(
2022-07-12 15:25:25 +01:00
product_id, wallet: WalletTypeInfo = Depends(require_admin_key)
):
2022-12-20 08:21:48 +00:00
product = await get_shop_product(product_id)
2022-01-27 12:24:38 +00:00
if not product:
2022-07-12 15:25:25 +01:00
return {"message": "Product does not exist."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
stall = await get_shop_stall(product.stall)
2022-07-15 10:22:13 +01:00
if stall.wallet != wallet.wallet.id:
2022-12-20 08:21:48 +00:00
return {"message": "Not your Shop."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
await delete_shop_product(product_id)
2022-01-27 12:24:38 +00:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
# # # Shippingzones
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/zones")
async def api_shop_zones(wallet: WalletTypeInfo = Depends(get_key_type)):
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
return await get_shop_zones(wallet.wallet.user)
2022-01-27 12:24:38 +00:00
2022-07-12 15:25:25 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.post("/api/v1/zones")
async def api_shop_zone_create(
2022-07-12 15:25:25 +01:00
data: createZones, wallet: WalletTypeInfo = Depends(get_key_type)
):
2022-12-20 08:21:48 +00:00
zone = await create_shop_zone(user=wallet.wallet.user, data=data)
2022-01-27 15:26:55 +00:00
return zone.dict()
2022-01-27 12:24:38 +00:00
2022-07-12 15:25:25 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.post("/api/v1/zones/{zone_id}")
async def api_shop_zone_update(
2022-07-12 15:25:25 +01:00
data: createZones,
2022-07-15 10:22:13 +01:00
zone_id: str,
2022-07-12 15:25:25 +01:00
wallet: WalletTypeInfo = Depends(require_admin_key),
):
2022-12-20 08:21:48 +00:00
zone = await get_shop_zone(zone_id)
2022-02-04 13:05:48 +00:00
if not zone:
2022-07-12 15:25:25 +01:00
return {"message": "Zone does not exist."}
2022-02-04 13:05:48 +00:00
if zone.user != wallet.wallet.user:
2022-07-12 15:25:25 +01:00
return {"message": "Not your record."}
2022-12-20 08:21:48 +00:00
zone = await update_shop_zone(zone_id, **data.dict())
2022-02-04 13:05:48 +00:00
return zone
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.delete("/api/v1/zones/{zone_id}")
async def api_shop_zone_delete(
2022-07-12 15:25:25 +01:00
zone_id, wallet: WalletTypeInfo = Depends(require_admin_key)
):
2022-12-20 08:21:48 +00:00
zone = await get_shop_zone(zone_id)
2022-01-27 12:24:38 +00:00
if not zone:
2022-07-12 15:25:25 +01:00
return {"message": "zone does not exist."}
2022-01-27 12:24:38 +00:00
2022-02-04 13:05:48 +00:00
if zone.user != wallet.wallet.user:
2022-07-12 15:25:25 +01:00
return {"message": "Not your zone."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
await delete_shop_zone(zone_id)
2022-01-27 12:24:38 +00:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
# # # Stalls
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/stalls")
async def api_shop_stalls(
2022-07-12 15:25:25 +01:00
wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
2022-01-27 12:24:38 +00:00
wallet_ids = [wallet.wallet.id]
2022-01-27 16:18:12 +00:00
if all_wallets:
2022-01-27 12:24:38 +00:00
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
2022-12-20 08:21:48 +00:00
return [stall.dict() for stall in await get_shop_stalls(wallet_ids)]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.post("/api/v1/stalls")
@shop_ext.put("/api/v1/stalls/{stall_id}")
async def api_shop_stall_create(
2022-07-12 15:25:25 +01:00
data: createStalls,
2022-07-15 10:22:13 +01:00
stall_id: str = None,
2022-07-12 15:29:46 +01:00
wallet: WalletTypeInfo = Depends(require_invoice_key),
2022-07-12 15:25:25 +01:00
):
2022-01-27 12:24:38 +00:00
if stall_id:
2022-12-20 08:21:48 +00:00
stall = await get_shop_stall(stall_id)
2022-01-27 12:24:38 +00:00
if not stall:
2022-07-12 15:25:25 +01:00
return {"message": "Withdraw stall does not exist."}
2022-01-27 12:24:38 +00:00
if stall.wallet != wallet.wallet.id:
2022-07-12 15:25:25 +01:00
return {"message": "Not your withdraw stall."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
stall = await update_shop_stall(stall_id, **data.dict())
2022-01-27 12:24:38 +00:00
else:
2022-12-20 08:21:48 +00:00
stall = await create_shop_stall(data=data)
2022-01-27 12:24:38 +00:00
2022-01-27 16:18:12 +00:00
return stall.dict()
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.delete("/api/v1/stalls/{stall_id}")
async def api_shop_stall_delete(
2022-07-15 10:22:13 +01:00
stall_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
2022-07-12 15:25:25 +01:00
):
2022-12-20 08:21:48 +00:00
stall = await get_shop_stall(stall_id)
2022-01-27 12:24:38 +00:00
if not stall:
2022-07-12 15:25:25 +01:00
return {"message": "Stall does not exist."}
2022-01-27 12:24:38 +00:00
if stall.wallet != wallet.wallet.id:
2022-07-12 15:25:25 +01:00
return {"message": "Not your Stall."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
await delete_shop_stall(stall_id)
2022-01-27 12:24:38 +00:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
###Orders
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/orders")
async def api_shop_orders(
2022-07-12 15:25:25 +01:00
wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
2022-01-27 12:24:38 +00:00
wallet_ids = [wallet.wallet.id]
2022-01-28 15:11:31 +00:00
if all_wallets:
2022-01-27 12:24:38 +00:00
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
2022-12-20 08:21:48 +00:00
orders = await get_shop_orders(wallet_ids)
orders_with_details = []
for order in orders:
order = order.dict()
2022-12-20 08:21:48 +00:00
order["details"] = await get_shop_order_details(order["id"])
orders_with_details.append(order)
2022-01-27 12:24:38 +00:00
try:
2022-08-18 16:20:15 +01:00
return orders_with_details # [order for order in orders]
2022-12-20 08:21:48 +00:00
# return [order.dict() for order in await get_shop_orders(wallet_ids)]
2022-01-27 12:24:38 +00:00
except:
2022-07-12 15:25:25 +01:00
return {"message": "We could not retrieve the orders."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/orders/{order_id}")
async def api_shop_order_by_id(order_id: str):
order = (await get_shop_order(order_id)).dict()
order["details"] = await get_shop_order_details(order_id)
return order
2022-12-20 08:21:48 +00:00
@shop_ext.post("/api/v1/orders")
async def api_shop_order_create(data: createOrder):
2022-08-18 10:49:04 +01:00
ref = urlsafe_short_hash()
2022-08-18 16:20:15 +01:00
2022-08-18 10:49:04 +01:00
payment_hash, payment_request = await create_invoice(
wallet_id=data.wallet,
amount=data.total,
memo=f"New order on Diagon alley",
extra={
2022-12-20 08:21:48 +00:00
"tag": "shop",
2022-08-18 10:49:04 +01:00
"reference": ref,
2022-08-18 16:20:15 +01:00
},
2022-08-18 10:49:04 +01:00
)
2022-12-20 08:21:48 +00:00
order_id = await create_shop_order(invoiceid=payment_hash, data=data)
2022-08-18 10:49:04 +01:00
logger.debug(f"ORDER ID {order_id}")
logger.debug(f"PRODUCTS {data.products}")
2022-12-20 08:21:48 +00:00
await create_shop_order_details(order_id=order_id, data=data.products)
2022-08-18 16:20:15 +01:00
return {
"payment_hash": payment_hash,
"payment_request": payment_request,
"order_reference": ref,
}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/orders/payments/{payment_hash}")
async def api_shop_check_payment(payment_hash: str):
order = await get_shop_order_invoiceid(payment_hash)
2022-08-19 11:19:45 +01:00
if not order:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Order does not exist."
)
try:
status = await api_payment(payment_hash)
except Exception as exc:
logger.error(exc)
return {"paid": False}
return status
2022-09-12 17:01:41 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.delete("/api/v1/orders/{order_id}")
async def api_shop_order_delete(
2022-11-11 12:11:14 +00:00
order_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
2022-07-12 15:25:25 +01:00
):
2022-12-20 08:21:48 +00:00
order = await get_shop_order(order_id)
2022-01-27 12:24:38 +00:00
if not order:
2022-07-12 15:25:25 +01:00
return {"message": "Order does not exist."}
2022-01-27 12:24:38 +00:00
if order.wallet != wallet.wallet.id:
2022-07-12 15:25:25 +01:00
return {"message": "Not your Order."}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
await delete_shop_order(order_id)
2022-01-27 12:24:38 +00:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/orders/paid/{order_id}")
async def api_shop_order_paid(
2022-07-15 10:22:13 +01:00
order_id, wallet: WalletTypeInfo = Depends(require_admin_key)
2022-07-12 15:25:25 +01:00
):
2022-01-27 12:24:38 +00:00
await db.execute(
2022-12-20 08:21:48 +00:00
"UPDATE shop.orders SET paid = ? WHERE id = ?",
2022-01-27 12:24:38 +00:00
(
True,
order_id,
),
)
return "", HTTPStatus.OK
2022-12-21 17:45:44 +00:00
@shop_ext.get("/api/v1/order/pubkey/{payment_hash}/{pubkey}")
async def api_shop_order_pubkey(payment_hash: str, pubkey: str):
await set_shop_order_pubkey(payment_hash, pubkey)
return "", HTTPStatus.OK
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/orders/shipped/{order_id}")
async def api_shop_order_shipped(
2022-07-15 10:22:13 +01:00
order_id, wallet: WalletTypeInfo = Depends(get_key_type)
2022-07-12 15:25:25 +01:00
):
2022-01-27 12:24:38 +00:00
await db.execute(
2022-12-20 08:21:48 +00:00
"UPDATE shop.orders SET shipped = ? WHERE id = ?",
2022-01-27 12:24:38 +00:00
(
True,
order_id,
),
)
order = await db.fetchone("SELECT * FROM shop.orders WHERE id = ?", (order_id,))
2022-01-27 12:24:38 +00:00
2022-10-25 11:57:01 +01:00
return order
2022-01-27 12:24:38 +00:00
###List products based on stall id
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/stall/products/{stall_id}")
async def api_shop_stall_products(
2022-07-15 10:22:13 +01:00
stall_id, wallet: WalletTypeInfo = Depends(get_key_type)
2022-07-12 15:25:25 +01:00
):
2022-01-27 12:24:38 +00:00
rows = await db.fetchone("SELECT * FROM shop.stalls WHERE id = ?", (stall_id,))
2022-01-27 12:24:38 +00:00
if not rows:
2022-07-12 15:25:25 +01:00
return {"message": "Stall does not exist."}
2022-01-27 12:24:38 +00:00
products = db.fetchone("SELECT * FROM shop.products WHERE wallet = ?", (rows[1],))
2022-01-27 12:24:38 +00:00
if not products:
2022-07-12 15:25:25 +01:00
return {"message": "No products"}
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
return [products.dict() for products in await get_shop_products(rows[1])]
2022-01-27 12:24:38 +00:00
###Check a product has been shipped
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/stall/checkshipped/{checking_id}")
async def api_shop_stall_checkshipped(
2022-07-12 15:25:25 +01:00
checking_id, wallet: WalletTypeInfo = Depends(get_key_type)
):
2022-01-27 12:24:38 +00:00
rows = await db.fetchone(
2022-12-20 08:21:48 +00:00
"SELECT * FROM shop.orders WHERE invoiceid = ?", (checking_id,)
2022-01-27 12:24:38 +00:00
)
2022-07-12 15:25:25 +01:00
return {"shipped": rows["shipped"]}
2022-01-27 12:24:38 +00:00
###Place order
2022-12-20 08:21:48 +00:00
# @shop_ext.post("/api/v1/stall/order/{stall_id}")
# async def api_shop_stall_order(
2022-11-11 12:11:14 +00:00
# stall_id, data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)
# ):
2022-12-20 08:21:48 +00:00
# product = await get_shop_product(data.productid)
# shipping = await get_shop_stall(stall_id)
2022-11-11 12:11:14 +00:00
# if data.shippingzone == 1:
# shippingcost = shipping.zone1cost # missing in model
# else:
# shippingcost = shipping.zone2cost # missing in model
# checking_id, payment_request = await create_invoice(
# wallet_id=product.wallet,
# amount=shippingcost + (data.quantity * product.price),
# memo=shipping.wallet,
# )
# selling_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8")
# await db.execute(
# """
2022-12-20 08:21:48 +00:00
# INSERT INTO shop.orders (id, productid, wallet, product, quantity, shippingzone, address, email, invoiceid, paid, shipped)
2022-11-11 12:11:14 +00:00
# VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
# """,
# (
# selling_id,
# data.productid,
# product.wallet, # doesn't exist in model
# product.product,
# data.quantity,
# data.shippingzone,
# data.address,
# data.email,
# checking_id,
# False,
# False,
# ),
# )
# return {"checking_id": checking_id, "payment_request": payment_request}
2022-07-27 15:28:58 +01:00
##
# MARKETS
##
2022-08-16 12:19:31 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/markets")
async def api_shop_markets(wallet: WalletTypeInfo = Depends(get_key_type)):
# await get_shop_market_stalls(market_id="FzpWnMyHQMcRppiGVua4eY")
2022-07-27 15:28:58 +01:00
try:
return [market.dict() for market in await get_shop_markets(wallet.wallet.user)]
2022-07-27 15:28:58 +01:00
except:
return {"message": "We could not retrieve the markets."}
2022-08-16 12:19:31 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/markets/{market_id}/stalls")
async def api_shop_market_stalls(market_id: str):
stall_ids = await get_shop_market_stalls(market_id)
return stall_ids
2022-09-15 15:52:08 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.post("/api/v1/markets")
@shop_ext.put("/api/v1/markets/{market_id}")
async def api_shop_stall_create(
2022-07-27 15:28:58 +01:00
data: CreateMarket,
market_id: str = None,
wallet: WalletTypeInfo = Depends(require_invoice_key),
):
if market_id:
2022-12-20 08:21:48 +00:00
market = await get_shop_market(market_id)
2022-07-27 15:28:58 +01:00
if not market:
return {"message": "Market does not exist."}
if market.usr != wallet.wallet.user:
return {"message": "Not your market."}
2022-12-20 08:21:48 +00:00
market = await update_shop_market(market_id, **data.dict())
2022-07-27 15:28:58 +01:00
else:
2022-12-20 08:21:48 +00:00
market = await create_shop_market(data=data)
await create_shop_market_stalls(market_id=market.id, data=data.stalls)
2022-07-27 15:28:58 +01:00
return market.dict()
2022-09-28 11:11:52 +01:00
2022-09-28 11:11:52 +01:00
## KEYS
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/keys")
async def api_shop_generate_keys():
2022-09-28 11:11:52 +01:00
private_key = PrivateKey()
public_key = private_key.pubkey.serialize().hex()
while not public_key.startswith("02"):
private_key = PrivateKey()
public_key = private_key.pubkey.serialize().hex()
return {"privkey": private_key.serialize(), "pubkey": public_key[2:]}
2022-09-30 17:28:35 +01:00
## MESSAGES/CHAT
2022-09-30 17:29:35 +01:00
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/chat/messages/merchant")
async def api_get_merchant_messages(
orders: str = Query(...), wallet: WalletTypeInfo = Depends(require_admin_key)
):
return [msg.dict() for msg in await get_shop_chat_by_merchant(orders.split(","))]
2022-12-20 08:21:48 +00:00
@shop_ext.get("/api/v1/chat/messages/{room_name}")
2022-09-30 17:28:35 +01:00
async def api_get_latest_chat_msg(room_name: str, all_messages: bool = Query(False)):
2022-09-30 17:29:35 +01:00
if all_messages:
2022-12-20 08:21:48 +00:00
messages = await get_shop_chat_messages(room_name)
2022-09-30 17:28:35 +01:00
else:
2022-12-20 08:21:48 +00:00
messages = await get_shop_latest_chat_messages(room_name)
2022-09-30 17:28:35 +01:00
return messages