diff --git a/lnbits/extensions/diagonalley/views_api.py b/lnbits/extensions/diagonalley/views_api.py index 3c55286..7a59cd5 100644 --- a/lnbits/extensions/diagonalley/views_api.py +++ b/lnbits/extensions/diagonalley/views_api.py @@ -5,7 +5,7 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.helpers import Status from lnbits.extensions.diagonalley import diagonalley_ext -from .crud import create_diagonalleys_product,get_diagonalleys_product,get_diagonalleys_products,delete_diagonalleys_product,create_diagonalleys_indexer,get_diagonalleys_indexer,get_diagonalleys_indexers,delete_diagonalleys_indexer,create_diagonalleys_order,get_diagonalleys_order,get_diagonalleys_orders,delete_diagonalleys_order +from .crud import create_diagonalleys_product,get_diagonalleys_product,get_diagonalleys_products,delete_diagonalleys_product,create_diagonalleys_indexer,update_diagonalleys_indexer,get_diagonalleys_indexer,get_diagonalleys_indexers,delete_diagonalleys_indexer,create_diagonalleys_order,get_diagonalleys_order,get_diagonalleys_orders,delete_diagonalleys_order from lnbits.core.services import create_invoice from base64 import urlsafe_b64encode from uuid import uuid4 @@ -25,6 +25,7 @@ def api_diagonalley_products(): @diagonalley_ext.route("/api/v1/diagonalley/products", methods=["POST"]) +@diagonalley_ext.route("/api/v1/diagonalley/products", methods=["PUT"]) @api_check_wallet_key(key_type="invoice") @api_validate_post_request(schema={ "product": {"type": "string", "empty": False, "required": True}, @@ -34,10 +35,23 @@ def api_diagonalley_products(): "price": {"type": "integer", "min": 0, "required": True}, "quantity": {"type": "integer", "min": 0, "required": True} }) -def api_diagonalley_products_create(): - product = create_diagonalleys_product(wallet_id=g.wallet.id, **g.data) +def api_diagonalley_product_create(product_id=None): + + if product_id: + product = get_diagonalleys_indexer(product_id) + + if not product: + return jsonify({"message": "Withdraw product does not exist."}), Status.NOT_FOUND + + if product.wallet != g.wallet.id: + return jsonify({"message": "Not your withdraw product."}), Status.FORBIDDEN + + product = update_diagonalleys_product(product_id, **g.data) + else: + product = create_diagonalleys_product(wallet_id=g.wallet.id, **g.data) + + return jsonify(product._asdict()), Status.OK if product_id else Status.CREATED - return jsonify(product._asdict()), Status.CREATED @diagonalley_ext.route("/api/v1/diagonalley/products/", methods=["DELETE"]) @@ -71,6 +85,7 @@ def api_diagonalley_indexers(): @diagonalley_ext.route("/api/v1/diagonalley/indexers", methods=["POST"]) +@diagonalley_ext.route("/api/v1/diagonalley/indexers", methods=["PUT"]) @api_check_wallet_key(key_type="invoice") @api_validate_post_request(schema={ "shopname": {"type": "string", "empty": False, "required": True}, @@ -81,10 +96,22 @@ def api_diagonalley_indexers(): "zone1cost": {"type": "integer", "min": 0, "required": True}, "zone2cost": {"type": "integer", "min": 0, "required": True} }) -def api_diagonalley_indexer_create(): - indexer = create_diagonalleys_indexer(wallet_id=g.wallet.id, **g.data) +def api_diagonalley_indexer_create(indexer_id=None): - return jsonify(indexer._asdict()), Status.CREATED + if indexer_id: + indexer = get_diagonalleys_indexer(indexer_id) + + if not indexer: + return jsonify({"message": "Withdraw indexer does not exist."}), Status.NOT_FOUND + + if indexer.wallet != g.wallet.id: + return jsonify({"message": "Not your withdraw indexer."}), Status.FORBIDDEN + + indexer = update_diagonalleys_indexer(indexer_id, **g.data) + else: + indexer = create_diagonalleys_indexer(wallet_id=g.wallet.id, **g.data) + + return jsonify(indexer._asdict()), Status.OK if indexer_id else Status.CREATED @diagonalley_ext.route("/api/v1/diagonalley/indexers/", methods=["DELETE"]) @@ -184,7 +211,7 @@ def api_diagonalleys_stall_checkshipped(checking_id): ###Place order -@diagonalley_ext.route("/api/v1/diagonalley/stall/order/", methods=["POST"]) +@diagonalley_ext.route("/api/v1/diagonalley/stall/order/", methods=["POST"]) @api_validate_post_request(schema={ "id": {"type": "string", "empty": False, "required": True}, "email": {"type": "string", "empty": False, "required": True}, @@ -192,10 +219,16 @@ def api_diagonalleys_stall_checkshipped(checking_id): "quantity": {"type": "integer", "empty": False, "required": True}, "shippingzone": {"type": "integer", "empty": False, "required": True}, }) -def api_diagonalley_stall_order(wallet_id): +def api_diagonalley_stall_order(indexer_id): product = get_diagonalleys_product(g.data["id"]) + shipping = get_diagonalleys_indexer(indexer_id) + + if g.data["shippingzone"] == 1: + shippingcost = shipping.zone1cost + else: + shippingcost = shipping.zone2cost - checking_id, payment_request = create_invoice(wallet_id=wallet_id, amount=(g.data["quantity"] * product.price), memo=g.data["id"]) + checking_id, payment_request = create_invoice(wallet_id=product.wallet, amount=shippingcost + (g.data["quantity"] * product.price), memo=g.data["id"]) selling_id = urlsafe_b64encode(uuid4().bytes_le).decode('utf-8') with open_ext_db("diagonalley") as db: db.execute( @@ -203,7 +236,7 @@ def api_diagonalley_stall_order(wallet_id): INSERT INTO orders (id, productid, wallet, product, quantity, shippingzone, address, email, invoiceid, paid, shipped) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, - (selling_id ,g.data["id"] ,wallet_id, product.product, g.data["quantity"], g.data["shippingzone"], g.data["address"], g.data["email"], checking_id, False, False), + (selling_id ,g.data["id"] , product.wallet, product.product, g.data["quantity"], g.data["shippingzone"], g.data["address"], g.data["email"], checking_id, False, False), ) return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.OK