diff --git a/crud.py b/crud.py index 2d89175..a4d513f 100644 --- a/crud.py +++ b/crud.py @@ -320,6 +320,7 @@ async def create_order(user_id: str, o: Order) -> Order: 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) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(event_id) DO NOTHING """, ( user_id, @@ -421,6 +422,7 @@ async def create_direct_message( f""" INSERT INTO nostrmarket.direct_messages (merchant_id, id, event_id, event_created_at, message, public_key, incoming) VALUES (?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(event_id) DO NOTHING """, ( merchant_id, diff --git a/migrations.py b/migrations.py index 6b3c3c5..c52dea3 100644 --- a/migrations.py +++ b/migrations.py @@ -89,7 +89,8 @@ async def m001_initial(db): invoice_id TEXT NOT NULL, paid 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, public_key TEXT NOT NULL, 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) ); """ ) diff --git a/services.py b/services.py index b499900..93de86d 100644 --- a/services.py +++ b/services.py @@ -1,10 +1,9 @@ import json from typing import Optional -import httpx 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 ( create_direct_message, @@ -204,18 +203,8 @@ async def _handle_new_order(order: PartialOrder) -> Optional[str]: wallet = await get_wallet(wallet_id) assert wallet, f"Cannot find wallet for product id: {first_product_id}" - market_url = url_for(f"/nostrmarket/api/v1/order", external=True) - async with httpx.AsyncClient() as client: - resp = await client.post( - 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) + new_order = await create_new_order(wallet.user, order) + if new_order: + return json.dumps(new_order.dict(), separators=(",", ":"), ensure_ascii=False) return None diff --git a/views_api.py b/views_api.py index 05f077f..b654e0c 100644 --- a/views_api.py +++ b/views_api.py @@ -48,17 +48,15 @@ from .models import ( OrderStatusUpdate, PartialDirectMessage, PartialMerchant, - PartialOrder, PartialProduct, PartialStall, PartialZone, - PaymentRequest, Product, Stall, Zone, ) 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 ######################################## @@ -449,21 +447,6 @@ async def api_delete_product( ######################################## 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}")