42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import json
|
|
from typing import Optional
|
|
|
|
from lnbits.helpers import urlsafe_short_hash
|
|
|
|
from . import db
|
|
from .models import Merchant, PartialMerchant
|
|
|
|
|
|
async def create_merchant(user_id: str, m: PartialMerchant) -> Merchant:
|
|
merchant_id = urlsafe_short_hash()
|
|
await db.execute(
|
|
"""
|
|
INSERT INTO nostrmarket.merchants (user_id, id, private_key, public_key, meta)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
""",
|
|
(user_id, merchant_id, m.private_key, m.public_key, json.dumps(dict(m.config))),
|
|
)
|
|
merchant = await get_merchant(user_id, merchant_id)
|
|
assert merchant, "Created merchant cannot be retrieved"
|
|
return merchant
|
|
|
|
|
|
async def get_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]:
|
|
row = await db.fetchone(
|
|
"""SELECT * FROM nostrmarket.merchants WHERE user_id = ? AND id = ?""",
|
|
(
|
|
user_id,
|
|
merchant_id,
|
|
),
|
|
)
|
|
|
|
return Merchant.from_row(row) if row else None
|
|
|
|
|
|
async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
|
|
row = await db.fetchone(
|
|
"""SELECT * FROM nostrmarket.merchants WHERE user_id = ? """,
|
|
(user_id,),
|
|
)
|
|
|
|
return Merchant.from_row(row) if row else None
|