refactor: do not go over http to create order

This commit is contained in:
Vlad Stan 2023-03-08 15:36:50 +02:00
parent 7f3438d07f
commit bf670c3545
4 changed files with 11 additions and 35 deletions

View file

@ -320,6 +320,7 @@ async def create_order(user_id: str, o: Order) -> Order:
f""" f"""
INSERT INTO nostrmarket.orders (user_id, id, event_id, event_created_at, pubkey, address, contact_data, extra_data, order_items, stall_id, invoice_id, total) INSERT INTO nostrmarket.orders (user_id, id, event_id, event_created_at, pubkey, address, contact_data, extra_data, order_items, stall_id, invoice_id, total)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(event_id) DO NOTHING
""", """,
( (
user_id, user_id,
@ -421,6 +422,7 @@ async def create_direct_message(
f""" f"""
INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, event_created_at, message, public_key, incoming) INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, event_created_at, message, public_key, incoming)
VALUES (?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(event_id) DO NOTHING
""", """,
( (
merchant_id, merchant_id,

View file

@ -89,7 +89,8 @@ async def m001_initial(db):
invoice_id TEXT NOT NULL, invoice_id TEXT NOT NULL,
paid BOOLEAN NOT NULL DEFAULT false, paid BOOLEAN NOT NULL DEFAULT false,
shipped BOOLEAN NOT NULL DEFAULT false, shipped BOOLEAN NOT NULL DEFAULT false,
time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now} time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
UNIQUE(event_id)
); );
""" """
) )
@ -107,7 +108,8 @@ async def m001_initial(db):
message TEXT NOT NULL, message TEXT NOT NULL,
public_key TEXT NOT NULL, public_key TEXT NOT NULL,
incoming BOOLEAN NOT NULL DEFAULT false, incoming BOOLEAN NOT NULL DEFAULT false,
time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now} time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
UNIQUE(event_id)
); );
""" """
) )

View file

@ -1,10 +1,9 @@
import json import json
from typing import Optional from typing import Optional
import httpx
from loguru import logger from loguru import logger
from lnbits.core import create_invoice, get_wallet, url_for from lnbits.core import create_invoice, get_wallet
from .crud import ( from .crud import (
create_direct_message, create_direct_message,
@ -204,18 +203,8 @@ async def _handle_new_order(order: PartialOrder) -> Optional[str]:
wallet = await get_wallet(wallet_id) wallet = await get_wallet(wallet_id)
assert wallet, f"Cannot find wallet for product id: {first_product_id}" assert wallet, f"Cannot find wallet for product id: {first_product_id}"
market_url = url_for(f"/nostrmarket/api/v1/order", external=True) new_order = await create_new_order(wallet.user, order)
async with httpx.AsyncClient() as client: if new_order:
resp = await client.post( return json.dumps(new_order.dict(), separators=(",", ":"), ensure_ascii=False)
url=market_url,
headers={
"X-Api-Key": wallet.adminkey,
},
json=order.dict(),
)
resp.raise_for_status()
data = resp.json()
if data:
return json.dumps(data, separators=(",", ":"), ensure_ascii=False)
return None return None

View file

@ -48,17 +48,15 @@ from .models import (
OrderStatusUpdate, OrderStatusUpdate,
PartialDirectMessage, PartialDirectMessage,
PartialMerchant, PartialMerchant,
PartialOrder,
PartialProduct, PartialProduct,
PartialStall, PartialStall,
PartialZone, PartialZone,
PaymentRequest,
Product, Product,
Stall, Stall,
Zone, Zone,
) )
from .nostr.nostr_client import publish_nostr_event from .nostr.nostr_client import publish_nostr_event
from .services import create_new_order, sign_and_send_to_nostr from .services import sign_and_send_to_nostr
######################################## MERCHANT ######################################## ######################################## MERCHANT ########################################
@ -449,21 +447,6 @@ async def api_delete_product(
######################################## ORDERS ######################################## ######################################## ORDERS ########################################
@nostrmarket_ext.post("/api/v1/order")
async def api_create_order(
data: PartialOrder, wallet: WalletTypeInfo = Depends(require_admin_key)
) -> Optional[PaymentRequest]:
try:
# print("### new order: ", json.dumps(data.dict()))
return await create_new_order(wallet.wallet.user, data)
except Exception as ex:
logger.warning(ex)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot create order",
)
nostrmarket_ext.get("/api/v1/order/{order_id}") nostrmarket_ext.get("/api/v1/order/{order_id}")