feat: user merchant_id instead of user_id

This commit is contained in:
Vlad Stan 2023-03-14 12:36:42 +02:00
parent 152fe5baab
commit 3e0b480f0a
10 changed files with 286 additions and 187 deletions

174
crud.py
View file

@ -76,16 +76,16 @@ async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
######################################## ZONES ########################################
async def create_zone(user_id: str, data: PartialZone) -> Zone:
async def create_zone(merchant_id: str, data: PartialZone) -> Zone:
zone_id = urlsafe_short_hash()
await db.execute(
f"""
INSERT INTO nostrmarket.zones (id, user_id, name, currency, cost, regions)
INSERT INTO nostrmarket.zones (id, merchant_id, name, currency, cost, regions)
VALUES (?, ?, ?, ?, ?, ?)
""",
(
zone_id,
user_id,
merchant_id,
data.name,
data.currency,
data.cost,
@ -93,55 +93,67 @@ async def create_zone(user_id: str, data: PartialZone) -> Zone:
),
)
zone = await get_zone(user_id, zone_id)
zone = await get_zone(merchant_id, zone_id)
assert zone, "Newly created zone couldn't be retrieved"
return zone
async def update_zone(user_id: str, z: Zone) -> Optional[Zone]:
async def update_zone(merchant_id: str, z: Zone) -> Optional[Zone]:
await db.execute(
f"UPDATE nostrmarket.zones SET name = ?, cost = ?, regions = ? WHERE id = ? AND user_id = ?",
(z.name, z.cost, json.dumps(z.countries), z.id, user_id),
f"UPDATE nostrmarket.zones SET name = ?, cost = ?, regions = ? WHERE id = ? AND merchant_id = ?",
(z.name, z.cost, json.dumps(z.countries), z.id, merchant_id),
)
return await get_zone(user_id, z.id)
return await get_zone(merchant_id, z.id)
async def get_zone(user_id: str, zone_id: str) -> Optional[Zone]:
async def get_zone(merchant_id: str, zone_id: str) -> Optional[Zone]:
row = await db.fetchone(
"SELECT * FROM nostrmarket.zones WHERE user_id = ? AND id = ?",
"SELECT * FROM nostrmarket.zones WHERE merchant_id = ? AND id = ?",
(
user_id,
merchant_id,
zone_id,
),
)
return Zone.from_row(row) if row else None
async def get_zones(user_id: str) -> List[Zone]:
async def get_zones(merchant_id: str) -> List[Zone]:
rows = await db.fetchall(
"SELECT * FROM nostrmarket.zones WHERE user_id = ?", (user_id,)
"SELECT * FROM nostrmarket.zones WHERE merchant_id = ?", (merchant_id,)
)
return [Zone.from_row(row) for row in rows]
async def delete_zone(zone_id: str) -> None:
# todo: add user_id
await db.execute("DELETE FROM nostrmarket.zones WHERE id = ?", (zone_id,))
async def delete_zone(merchant_id: str, zone_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.zones WHERE merchant_id = ? AND id = ?",
(
merchant_id,
zone_id,
),
)
async def delete_merchant_zones(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.zones WHERE merchant_id = ?", (merchant_id,)
)
######################################## STALL ########################################
async def create_stall(user_id: str, data: PartialStall) -> Stall:
async def create_stall(merchant_id: str, data: PartialStall) -> Stall:
stall_id = urlsafe_short_hash()
await db.execute(
f"""
INSERT INTO nostrmarket.stalls (user_id, id, wallet, name, currency, zones, meta)
INSERT INTO nostrmarket.stalls (merchant_id, id, wallet, name, currency, zones, meta)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
user_id,
merchant_id,
stall_id,
data.wallet,
data.name,
@ -153,35 +165,35 @@ async def create_stall(user_id: str, data: PartialStall) -> Stall:
),
)
stall = await get_stall(user_id, stall_id)
stall = await get_stall(merchant_id, stall_id)
assert stall, "Newly created stall couldn't be retrieved"
return stall
async def get_stall(user_id: str, stall_id: str) -> Optional[Stall]:
async def get_stall(merchant_id: str, stall_id: str) -> Optional[Stall]:
row = await db.fetchone(
"SELECT * FROM nostrmarket.stalls WHERE user_id = ? AND id = ?",
"SELECT * FROM nostrmarket.stalls WHERE merchant_id = ? AND id = ?",
(
user_id,
merchant_id,
stall_id,
),
)
return Stall.from_row(row) if row else None
async def get_stalls(user_id: str) -> List[Stall]:
async def get_stalls(merchant_id: str) -> List[Stall]:
rows = await db.fetchall(
"SELECT * FROM nostrmarket.stalls WHERE user_id = ?",
(user_id,),
"SELECT * FROM nostrmarket.stalls WHERE merchant_id = ?",
(merchant_id,),
)
return [Stall.from_row(row) for row in rows]
async def update_stall(user_id: str, stall: Stall) -> Optional[Stall]:
async def update_stall(merchant_id: str, stall: Stall) -> Optional[Stall]:
await db.execute(
f"""
UPDATE nostrmarket.stalls SET wallet = ?, name = ?, currency = ?, zones = ?, meta = ?
WHERE user_id = ? AND id = ?
WHERE merchant_id = ? AND id = ?
""",
(
stall.wallet,
@ -191,18 +203,18 @@ async def update_stall(user_id: str, stall: Stall) -> Optional[Stall]:
[z.dict() for z in stall.shipping_zones]
), # todo: cost is float. should be int for sats
json.dumps(stall.config.dict()),
user_id,
merchant_id,
stall.id,
),
)
return await get_stall(user_id, stall.id)
return await get_stall(merchant_id, stall.id)
async def delete_stall(user_id: str, stall_id: str) -> None:
async def delete_stall(merchant_id: str, stall_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.stalls WHERE user_id =? AND id = ?",
"DELETE FROM nostrmarket.stalls WHERE merchant_id =? AND id = ?",
(
user_id,
merchant_id,
stall_id,
),
)
@ -211,16 +223,16 @@ async def delete_stall(user_id: str, stall_id: str) -> None:
######################################## PRODUCTS ########################################
async def create_product(user_id: str, data: PartialProduct) -> Product:
async def create_product(merchant_id: str, data: PartialProduct) -> Product:
product_id = urlsafe_short_hash()
await db.execute(
f"""
INSERT INTO nostrmarket.products (user_id, id, stall_id, name, image, price, quantity, category_list, meta)
INSERT INTO nostrmarket.products (merchant_id, id, stall_id, name, image, price, quantity, category_list, meta)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
user_id,
merchant_id,
product_id,
data.stall_id,
data.name,
@ -231,18 +243,18 @@ async def create_product(user_id: str, data: PartialProduct) -> Product:
json.dumps(data.config.dict()),
),
)
product = await get_product(user_id, product_id)
product = await get_product(merchant_id, product_id)
assert product, "Newly created product couldn't be retrieved"
return product
async def update_product(user_id: str, product: Product) -> Product:
async def update_product(merchant_id: str, product: Product) -> Product:
await db.execute(
f"""
UPDATE nostrmarket.products set name = ?, image = ?, price = ?, quantity = ?, category_list = ?, meta = ?
WHERE user_id = ? AND id = ?
WHERE merchant_id = ? AND id = ?
""",
(
product.name,
@ -251,40 +263,42 @@ async def update_product(user_id: str, product: Product) -> Product:
product.quantity,
json.dumps(product.categories),
json.dumps(product.config.dict()),
user_id,
merchant_id,
product.id,
),
)
updated_product = await get_product(user_id, product.id)
updated_product = await get_product(merchant_id, product.id)
assert updated_product, "Updated product couldn't be retrieved"
return updated_product
async def get_product(user_id: str, product_id: str) -> Optional[Product]:
async def get_product(merchant_id: str, product_id: str) -> Optional[Product]:
row = await db.fetchone(
"SELECT * FROM nostrmarket.products WHERE user_id =? AND id = ?",
"SELECT * FROM nostrmarket.products WHERE merchant_id =? AND id = ?",
(
user_id,
merchant_id,
product_id,
),
)
return Product.from_row(row) if row else None
async def get_products(user_id: str, stall_id: str) -> List[Product]:
async def get_products(merchant_id: str, stall_id: str) -> List[Product]:
rows = await db.fetchall(
"SELECT * FROM nostrmarket.products WHERE user_id = ? AND stall_id = ?",
(user_id, stall_id),
"SELECT * FROM nostrmarket.products WHERE merchant_id = ? AND stall_id = ?",
(merchant_id, stall_id),
)
return [Product.from_row(row) for row in rows]
async def get_products_by_ids(user_id: str, product_ids: List[str]) -> List[Product]:
async def get_products_by_ids(
merchant_id: str, product_ids: List[str]
) -> List[Product]:
q = ",".join(["?"] * len(product_ids))
rows = await db.fetchall(
f"SELECT id, stall_id, name, price, quantity, category_list, meta FROM nostrmarket.products WHERE user_id = ? AND id IN ({q})",
(user_id, *product_ids),
f"SELECT id, stall_id, name, price, quantity, category_list, meta FROM nostrmarket.products WHERE merchant_id = ? AND id IN ({q})",
(merchant_id, *product_ids),
)
return [Product.from_row(row) for row in rows]
@ -302,11 +316,11 @@ async def get_wallet_for_product(product_id: str) -> Optional[str]:
return row[0] if row else None
async def delete_product(user_id: str, product_id: str) -> None:
async def delete_product(merchant_id: str, product_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.products WHERE user_id =? AND id = ?",
"DELETE FROM nostrmarket.products WHERE merchant_id =? AND id = ?",
(
user_id,
merchant_id,
product_id,
),
)
@ -315,11 +329,11 @@ async def delete_product(user_id: str, product_id: str) -> None:
######################################## ORDERS ########################################
async def create_order(user_id: str, o: Order) -> Order:
async def create_order(merchant_id: str, o: Order) -> Order:
await db.execute(
f"""
INSERT INTO nostrmarket.orders (
user_id,
merchant_id,
id,
event_id,
event_created_at,
@ -337,7 +351,7 @@ async def create_order(user_id: str, o: Order) -> Order:
ON CONFLICT(event_id) DO NOTHING
""",
(
user_id,
merchant_id,
o.id,
o.event_id,
o.event_created_at,
@ -352,47 +366,47 @@ async def create_order(user_id: str, o: Order) -> Order:
o.total,
),
)
order = await get_order(user_id, o.id)
order = await get_order(merchant_id, o.id)
assert order, "Newly created order couldn't be retrieved"
return order
async def get_order(user_id: str, order_id: str) -> Optional[Order]:
async def get_order(merchant_id: str, order_id: str) -> Optional[Order]:
row = await db.fetchone(
"SELECT * FROM nostrmarket.orders WHERE user_id =? AND id = ?",
"SELECT * FROM nostrmarket.orders WHERE merchant_id =? AND id = ?",
(
user_id,
merchant_id,
order_id,
),
)
return Order.from_row(row) if row else None
async def get_order_by_event_id(user_id: str, event_id: str) -> Optional[Order]:
async def get_order_by_event_id(merchant_id: str, event_id: str) -> Optional[Order]:
row = await db.fetchone(
"SELECT * FROM nostrmarket.orders WHERE user_id =? AND event_id =?",
"SELECT * FROM nostrmarket.orders WHERE merchant_id =? AND event_id =?",
(
user_id,
merchant_id,
event_id,
),
)
return Order.from_row(row) if row else None
async def get_orders(user_id: str) -> List[Order]:
async def get_orders(merchant_id: str) -> List[Order]:
rows = await db.fetchall(
"SELECT * FROM nostrmarket.orders WHERE user_id = ? ORDER BY time DESC",
(user_id,),
"SELECT * FROM nostrmarket.orders WHERE merchant_id = ? ORDER BY time DESC",
(merchant_id,),
)
return [Order.from_row(row) for row in rows]
async def get_orders_for_stall(user_id: str, stall_id: str) -> List[Order]:
async def get_orders_for_stall(merchant_id: str, stall_id: str) -> List[Order]:
rows = await db.fetchall(
"SELECT * FROM nostrmarket.orders WHERE user_id = ? AND stall_id = ? ORDER BY time DESC",
"SELECT * FROM nostrmarket.orders WHERE merchant_id = ? AND stall_id = ? ORDER BY time DESC",
(
user_id,
merchant_id,
stall_id,
),
)
@ -423,11 +437,11 @@ async def update_order_paid_status(order_id: str, paid: bool) -> Optional[Order]
async def update_order_shipped_status(
user_id: str, order_id: str, shipped: bool
merchant_id: str, order_id: str, shipped: bool
) -> Optional[Order]:
await db.execute(
f"UPDATE nostrmarket.orders SET shipped = ? WHERE user_id = ? AND id = ?",
(shipped, user_id, order_id),
f"UPDATE nostrmarket.orders SET shipped = ? WHERE merchant_id = ? AND id = ?",
(shipped, merchant_id, order_id),
)
row = await db.fetchone(
@ -460,8 +474,10 @@ async def create_direct_message(
dm.incoming,
),
)
msg = await get_direct_message(merchant_id, dm_id)
if dm.event_id:
msg = await get_direct_message_by_event_id(merchant_id, dm.event_id)
else:
msg = await get_direct_message(merchant_id, dm_id)
assert msg, "Newly created dm couldn't be retrieved"
return msg
@ -476,6 +492,16 @@ async def get_direct_message(merchant_id: str, dm_id: str) -> Optional[DirectMes
)
return DirectMessage.from_row(row) if row else None
async def get_direct_message_by_event_id(merchant_id: str, event_id: str) -> Optional[DirectMessage]:
row = await db.fetchone(
"SELECT * FROM nostrmarket.direct_messages WHERE merchant_id = ? AND event_id = ?",
(
merchant_id,
event_id,
),
)
return DirectMessage.from_row(row) if row else None
async def get_direct_messages(merchant_id: str, public_key: str) -> List[DirectMessage]:
rows = await db.fetchall(