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

446 lines
12 KiB
Python
Raw Normal View History

2022-01-27 12:24:38 +00:00
from base64 import urlsafe_b64encode
from typing import List, Optional, Union
2022-01-27 15:26:55 +00:00
from uuid import uuid4
2022-01-27 12:24:38 +00:00
# from lnbits.db import open_ext_db
from lnbits.db import SQLITE
from lnbits.helpers import urlsafe_short_hash
2022-01-27 15:26:55 +00:00
from lnbits.settings import WALLET
from . import db
2022-01-27 16:18:12 +00:00
from .models import (
2022-09-30 17:28:35 +01:00
ChatMessage,
2022-09-30 10:16:34 +01:00
CreateChatMessage,
2022-09-12 16:58:56 +01:00
CreateMarket,
CreateMarketStalls,
2022-07-27 15:28:58 +01:00
Market,
2022-08-16 12:08:27 +01:00
OrderDetail,
2022-01-27 16:18:12 +00:00
Orders,
Products,
Stalls,
Zones,
2022-01-28 15:11:31 +00:00
createOrder,
2022-08-16 12:08:27 +01:00
createOrderDetails,
2022-01-27 16:18:12 +00:00
createProduct,
createStalls,
createZones,
)
2022-01-27 12:24:38 +00:00
###Products
2022-07-12 15:25:25 +01:00
2022-12-20 08:21:48 +00:00
async def create_shop_product(data: createProduct) -> Products:
2022-01-27 12:24:38 +00:00
product_id = urlsafe_short_hash()
2022-01-27 15:26:55 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.products (id, stall, product, categories, description, image, price, quantity)
2022-01-27 12:24:38 +00:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
product_id,
2022-01-27 15:26:55 +00:00
data.stall,
data.product,
data.categories,
data.description,
data.image,
data.price,
data.quantity,
2022-01-27 12:24:38 +00:00
),
)
2022-12-20 08:21:48 +00:00
product = await get_shop_product(product_id)
2022-01-27 12:24:38 +00:00
assert product, "Newly created product couldn't be retrieved"
return product
2022-12-22 12:23:22 +00:00
async def update_shop_product(product_id: str, **kwargs) -> Optional[Products]:
2022-01-27 12:24:38 +00:00
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
2022-12-20 08:21:48 +00:00
f"UPDATE shop.products SET {q} WHERE id = ?",
2022-01-27 12:24:38 +00:00
(*kwargs.values(), product_id),
)
row = await db.fetchone("SELECT * FROM shop.products WHERE id = ?", (product_id,))
2022-01-27 12:24:38 +00:00
2022-07-15 10:22:13 +01:00
return Products(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def get_shop_product(product_id: str) -> Optional[Products]:
row = await db.fetchone("SELECT * FROM shop.products WHERE id = ?", (product_id,))
2022-01-28 15:11:31 +00:00
return Products(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def get_shop_products(stall_ids: Union[str, List[str]]) -> List[Products]:
2022-07-12 17:22:03 +01:00
if isinstance(stall_ids, str):
stall_ids = [stall_ids]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
# with open_ext_db("shop") as db:
2022-07-12 17:22:03 +01:00
q = ",".join(["?"] * len(stall_ids))
2022-01-27 12:24:38 +00:00
rows = await db.fetchall(
f"""
2022-12-20 08:21:48 +00:00
SELECT * FROM shop.products WHERE stall IN ({q})
2022-01-27 12:24:38 +00:00
""",
2022-07-12 17:22:03 +01:00
(*stall_ids,),
2022-01-27 12:24:38 +00:00
)
2022-01-28 15:11:31 +00:00
return [Products(**row) for row in rows]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def delete_shop_product(product_id: str) -> None:
await db.execute("DELETE FROM shop.products WHERE id = ?", (product_id,))
2022-01-27 12:24:38 +00:00
###zones
2022-12-20 08:21:48 +00:00
async def create_shop_zone(user, data: createZones) -> Zones:
2022-01-27 12:24:38 +00:00
zone_id = urlsafe_short_hash()
2022-01-27 15:26:55 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.zones (
2022-01-27 12:24:38 +00:00
id,
"user",
2022-01-27 12:24:38 +00:00
cost,
countries
)
VALUES (?, ?, ?, ?)
""",
2022-02-04 13:05:48 +00:00
(zone_id, user, data.cost, data.countries.lower()),
2022-01-27 12:24:38 +00:00
)
2022-12-20 08:21:48 +00:00
zone = await get_shop_zone(zone_id)
2022-01-27 12:24:38 +00:00
assert zone, "Newly created zone couldn't be retrieved"
return zone
2022-12-20 08:21:48 +00:00
async def update_shop_zone(zone_id: str, **kwargs) -> Optional[Zones]:
2022-01-27 12:24:38 +00:00
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
2022-12-20 08:21:48 +00:00
f"UPDATE shop.zones SET {q} WHERE id = ?",
2022-01-27 12:24:38 +00:00
(*kwargs.values(), zone_id),
)
2022-12-20 08:21:48 +00:00
row = await db.fetchone("SELECT * FROM shop.zones WHERE id = ?", (zone_id,))
2022-01-28 15:11:31 +00:00
return Zones(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def get_shop_zone(zone_id: str) -> Optional[Zones]:
row = await db.fetchone("SELECT * FROM shop.zones WHERE id = ?", (zone_id,))
2022-01-28 15:11:31 +00:00
return Zones(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def get_shop_zones(user: str) -> List[Zones]:
rows = await db.fetchall('SELECT * FROM shop.zones WHERE "user" = ?', (user,))
2022-01-28 15:11:31 +00:00
return [Zones(**row) for row in rows]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def delete_shop_zone(zone_id: str) -> None:
await db.execute("DELETE FROM shop.zones WHERE id = ?", (zone_id,))
2022-01-27 12:24:38 +00:00
###Stalls
2022-12-20 08:21:48 +00:00
async def create_shop_stall(data: createStalls) -> Stalls:
2022-01-27 12:24:38 +00:00
stall_id = urlsafe_short_hash()
2022-01-27 16:18:12 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.stalls (
2022-01-27 12:24:38 +00:00
id,
wallet,
name,
publickey,
relays,
shippingzones
)
2022-12-21 17:44:37 +00:00
VALUES (?, ?, ?, ?, ?, ?)
2022-01-27 12:24:38 +00:00
""",
2022-01-27 16:18:12 +00:00
(
stall_id,
data.wallet,
data.name,
data.publickey,
data.relays,
2022-07-12 15:25:25 +01:00
data.shippingzones,
),
2022-01-27 12:24:38 +00:00
)
2022-12-20 08:21:48 +00:00
stall = await get_shop_stall(stall_id)
2022-01-27 12:24:38 +00:00
assert stall, "Newly created stall couldn't be retrieved"
return stall
2022-12-20 08:21:48 +00:00
async def update_shop_stall(stall_id: str, **kwargs) -> Optional[Stalls]:
2022-01-27 12:24:38 +00:00
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
2022-12-20 08:21:48 +00:00
f"UPDATE shop.stalls SET {q} WHERE id = ?",
2022-01-27 12:24:38 +00:00
(*kwargs.values(), stall_id),
)
row = await db.fetchone("SELECT * FROM shop.stalls WHERE id = ?", (stall_id,))
2022-01-28 15:11:31 +00:00
return Stalls(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def get_shop_stall(stall_id: str) -> Optional[Stalls]:
row = await db.fetchone("SELECT * FROM shop.stalls WHERE id = ?", (stall_id,))
2022-01-28 15:11:31 +00:00
return Stalls(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def get_shop_stalls(wallet_ids: Union[str, List[str]]) -> List[Stalls]:
2022-01-27 12:24:38 +00:00
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
f"SELECT * FROM shop.stalls WHERE wallet IN ({q})", (*wallet_ids,)
2022-01-27 12:24:38 +00:00
)
2022-01-28 15:11:31 +00:00
return [Stalls(**row) for row in rows]
2022-01-27 12:24:38 +00:00
2022-09-15 15:52:08 +01:00
async def get_shop_stalls_by_ids(stall_ids: Union[str, List[str]]) -> List[Stalls]:
q = ",".join(["?"] * len(stall_ids))
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
f"SELECT * FROM shop.stalls WHERE id IN ({q})", (*stall_ids,)
)
2022-09-15 15:52:08 +01:00
return [Stalls(**row) for row in rows]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def delete_shop_stall(stall_id: str) -> None:
await db.execute("DELETE FROM shop.stalls WHERE id = ?", (stall_id,))
2022-01-27 12:24:38 +00:00
###Orders
2022-12-20 08:21:48 +00:00
async def create_shop_order(data: createOrder, invoiceid: str) -> Orders:
2022-08-18 10:49:04 +01:00
returning = "" if db.type == SQLITE else "RETURNING ID"
method = db.execute if db.type == SQLITE else db.fetchone
2022-01-27 12:24:38 +00:00
2022-08-18 10:49:04 +01:00
result = await (method)(
2022-01-27 12:24:38 +00:00
f"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.orders (wallet, shippingzone, address, email, total, invoiceid, paid, shipped)
2022-08-18 10:49:04 +01:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
{returning}
2022-01-27 12:24:38 +00:00
""",
(
2022-08-18 10:49:04 +01:00
data.wallet,
2022-01-28 15:11:31 +00:00
data.shippingzone,
data.address,
data.email,
2022-08-16 12:08:27 +01:00
data.total,
2022-08-18 10:49:04 +01:00
invoiceid,
2022-01-27 12:24:38 +00:00
False,
False,
),
)
2022-08-18 10:49:04 +01:00
if db.type == SQLITE:
return result._result_proxy.lastrowid
else:
return result[0]
2022-12-20 08:21:48 +00:00
# link = await get_shop_order(link.id)
2022-08-18 10:49:04 +01:00
# assert link, "Newly created link couldn't be retrieved"
# return link
2022-01-27 12:24:38 +00:00
2022-08-16 12:19:31 +01:00
async def create_shop_order_details(order_id: str, data: List[createOrderDetails]):
2022-08-16 12:08:27 +01:00
for item in data:
item_id = urlsafe_short_hash()
await db.execute(
"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.order_details (id, order_id, product_id, quantity)
2022-08-16 12:08:27 +01:00
VALUES (?, ?, ?, ?)
""",
(
item_id,
order_id,
item.product_id,
item.quantity,
),
)
2022-12-20 08:21:48 +00:00
order_details = await get_shop_order_details(order_id)
2022-08-16 12:08:27 +01:00
return order_details
2022-08-16 12:19:31 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_order_details(order_id: str) -> List[OrderDetail]:
2022-08-16 12:08:27 +01:00
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
f"SELECT * FROM shop.order_details WHERE order_id = ?", (order_id,)
2022-08-16 12:08:27 +01:00
)
return [OrderDetail(**row) for row in rows]
2022-01-27 12:24:38 +00:00
2022-08-16 12:19:31 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_order(order_id: str) -> Optional[Orders]:
row = await db.fetchone("SELECT * FROM shop.orders WHERE id = ?", (order_id,))
2022-01-28 15:11:31 +00:00
return Orders(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-09-12 17:01:41 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_order_invoiceid(invoice_id: str) -> Optional[Orders]:
2022-08-19 11:19:45 +01:00
row = await db.fetchone(
2022-12-20 08:21:48 +00:00
"SELECT * FROM shop.orders WHERE invoiceid = ?", (invoice_id,)
2022-08-19 11:19:45 +01:00
)
return Orders(**row) if row else None
2022-09-12 17:01:41 +01:00
2022-12-20 08:21:48 +00:00
async def set_shop_order_paid(payment_hash: str) -> Orders:
2022-08-19 11:19:45 +01:00
await db.execute(
2022-09-12 17:01:41 +01:00
"""
2022-12-20 08:21:48 +00:00
UPDATE shop.orders
2022-08-19 11:19:45 +01:00
SET paid = true
WHERE invoiceid = ?
""",
2022-09-12 17:01:41 +01:00
(payment_hash,),
)
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def set_shop_order_pubkey(payment_hash: str, pubkey: str):
2022-11-11 12:11:14 +00:00
await db.execute(
"""
2022-12-20 08:21:48 +00:00
UPDATE shop.orders
2022-11-11 12:11:14 +00:00
SET pubkey = ?
WHERE invoiceid = ?
""",
(
pubkey,
payment_hash,
),
)
2022-12-20 08:21:48 +00:00
async def update_shop_product_stock(products):
2022-09-12 17:01:41 +01:00
q = "\n".join(
[f"""WHEN id='{p.product_id}' THEN quantity - {p.quantity}""" for p in products]
)
2022-08-19 13:24:18 +01:00
v = ",".join(["?"] * len(products))
2022-09-12 17:01:41 +01:00
2022-08-19 13:24:18 +01:00
await db.execute(
f"""
2022-12-20 08:21:48 +00:00
UPDATE shop.products
2022-08-19 13:24:18 +01:00
SET quantity=(CASE
{q}
END)
WHERE id IN ({v});
""",
2022-09-12 17:01:41 +01:00
(*[p.product_id for p in products],),
2022-08-19 13:24:18 +01:00
)
2022-09-12 17:01:41 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
2022-01-27 12:24:38 +00:00
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
f"SELECT * FROM shop.orders WHERE wallet IN ({q})", (*wallet_ids,)
2022-01-27 12:24:38 +00:00
)
#
2022-01-28 15:11:31 +00:00
return [Orders(**row) for row in rows]
2022-01-27 12:24:38 +00:00
2022-12-20 08:21:48 +00:00
async def delete_shop_order(order_id: str) -> None:
await db.execute("DELETE FROM shop.orders WHERE id = ?", (order_id,))
2022-07-27 15:28:58 +01:00
2022-08-16 12:19:31 +01:00
2022-07-27 15:28:58 +01:00
### Market/Marketplace
2022-08-16 12:19:31 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_markets(user: str) -> List[Market]:
rows = await db.fetchall("SELECT * FROM shop.markets WHERE usr = ?", (user,))
2022-07-27 15:28:58 +01:00
return [Market(**row) for row in rows]
2022-12-20 08:21:48 +00:00
async def get_shop_market(market_id: str) -> Optional[Market]:
row = await db.fetchone("SELECT * FROM shop.markets WHERE id = ?", (market_id,))
2022-09-12 16:58:56 +01:00
return Market(**row) if row else None
2022-07-27 15:28:58 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_market_stalls(market_id: str):
2022-07-27 15:28:58 +01:00
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
"SELECT * FROM shop.market_stalls WHERE marketid = ?", (market_id,)
2022-09-15 15:52:08 +01:00
)
ids = [row["stallid"] for row in rows]
2022-09-12 16:58:56 +01:00
2022-12-20 08:21:48 +00:00
return await get_shop_stalls_by_ids(ids)
2022-09-12 16:58:56 +01:00
2022-09-12 17:01:41 +01:00
2022-12-20 08:21:48 +00:00
async def create_shop_market(data: CreateMarket):
2022-09-12 16:58:56 +01:00
market_id = urlsafe_short_hash()
await db.execute(
2022-09-12 17:01:41 +01:00
"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.markets (id, usr, name)
2022-09-12 16:58:56 +01:00
VALUES (?, ?, ?)
""",
2022-09-12 17:01:41 +01:00
(
market_id,
data.usr,
data.name,
),
)
2022-12-20 08:21:48 +00:00
market = await get_shop_market(market_id)
2022-09-12 16:58:56 +01:00
assert market, "Newly created market couldn't be retrieved"
return market
async def create_shop_market_stalls(market_id: str, data: List[CreateMarketStalls]):
2022-09-12 16:58:56 +01:00
for stallid in data:
id = urlsafe_short_hash()
await db.execute(
"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.market_stalls (id, marketid, stallid)
2022-09-12 16:58:56 +01:00
VALUES (?, ?, ?)
""",
(
id,
market_id,
stallid,
),
)
2022-12-20 08:21:48 +00:00
market_stalls = await get_shop_market_stalls(market_id)
2022-09-12 16:58:56 +01:00
return market_stalls
2022-12-20 08:21:48 +00:00
async def update_shop_market(market_id):
2022-09-12 16:58:56 +01:00
pass
2022-09-30 10:16:34 +01:00
2022-10-04 10:00:42 +01:00
### CHAT / MESSAGES
2022-09-30 17:29:35 +01:00
2022-09-30 10:16:34 +01:00
async def create_chat_message(data: CreateChatMessage):
await db.execute(
"""
2022-12-20 08:21:48 +00:00
INSERT INTO shop.messages (msg, pubkey, id_conversation)
2022-09-30 10:16:34 +01:00
VALUES (?, ?, ?)
""",
(
data.msg,
data.pubkey,
data.room_name,
),
)
2022-09-30 17:28:35 +01:00
2022-09-30 17:29:35 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_latest_chat_messages(room_name: str):
2022-09-30 17:28:35 +01:00
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
"SELECT * FROM shop.messages WHERE id_conversation = ? ORDER BY timestamp DESC LIMIT 20",
2022-09-30 17:29:35 +01:00
(room_name,),
2022-09-30 17:28:35 +01:00
)
return [ChatMessage(**row) for row in rows]
2022-09-30 17:29:35 +01:00
2022-12-20 08:21:48 +00:00
async def get_shop_chat_messages(room_name: str):
2022-09-30 17:28:35 +01:00
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
"SELECT * FROM shop.messages WHERE id_conversation = ? ORDER BY timestamp DESC",
2022-09-30 17:29:35 +01:00
(room_name,),
2022-09-30 17:28:35 +01:00
)
return [ChatMessage(**row) for row in rows]
2022-12-20 08:21:48 +00:00
async def get_shop_chat_by_merchant(ids: List[str]) -> List[ChatMessage]:
q = ",".join(["?"] * len(ids))
rows = await db.fetchall(
2022-12-20 08:21:48 +00:00
f"SELECT * FROM shop.messages WHERE id_conversation IN ({q})",
(*ids,),
)
return [ChatMessage(**row) for row in rows]