starpost: contain prestaged cargo buyers
This commit is contained in:
1 parent
b28069ecfc
commit
b5bbafc664
4 files changed
+52
-16
No files matched your search
+37
-13
@@ -13,7 +13,7 @@ from flask import Flask, jsonify, request, send_file, session
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from contracts import CONTRACT_DIR, create_contract
|
||||
from database import connect as db, migrate
|
||||
from database import CARGO_PURCHASE_WINDOW_SECONDS, connect as db, migrate
|
||||
from warehouse_client import CALLBACK_TOKEN as WAREHOUSE_CALLBACK_TOKEN
|
||||
from warehouse_client import get_health as get_warehouse_health
|
||||
from warehouse_client import submit_jobs as submit_warehouse_jobs
|
||||
@@ -29,7 +29,6 @@ app.config.update(
|
||||
)
|
||||
|
||||
CONTRACT_FILENAME_RE = re.compile(r"contract-([0-9]+)-[0-9a-f]{16}\.pdf")
|
||||
CARGO_PURCHASE_WINDOW_SECONDS = 2
|
||||
|
||||
|
||||
migrate()
|
||||
@@ -523,9 +522,28 @@ def update_cart_quantity(product_id):
|
||||
return jsonify(ok=True, quantity=quantity)
|
||||
|
||||
|
||||
def create_order(cur, buyer_id, product_id, price, tracking, buyer_created_at, product_created_at):
|
||||
cur.execute(
|
||||
"INSERT INTO orders(buyer_id,product_id,price,status,tracking_code,cargo_entitled) "
|
||||
"VALUES(%s,%s,%s,'pending',%s,%s >= %s AND "
|
||||
"CURRENT_TIMESTAMP <= %s + (%s * interval '1 second')) RETURNING id",
|
||||
(
|
||||
buyer_id,
|
||||
product_id,
|
||||
price,
|
||||
tracking,
|
||||
buyer_created_at,
|
||||
product_created_at,
|
||||
product_created_at,
|
||||
CARGO_PURCHASE_WINDOW_SECONDS,
|
||||
),
|
||||
)
|
||||
return cur.fetchone()["id"]
|
||||
|
||||
|
||||
def order_product(product_id):
|
||||
with db() as conn, conn.cursor() as cur:
|
||||
cur.execute("SELECT p.*,u.balance FROM products p CROSS JOIN users u WHERE p.id=%s AND p.is_active=true AND u.id=%s FOR UPDATE OF u", (product_id, session["user_id"]))
|
||||
cur.execute("SELECT p.*,u.balance,u.created_at buyer_created_at FROM products p CROSS JOIN users u WHERE p.id=%s AND p.is_active=true AND u.id=%s FOR UPDATE OF u", (product_id, session["user_id"]))
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None, error("product not found", 404)
|
||||
@@ -536,8 +554,8 @@ def order_product(product_id):
|
||||
cur.execute("UPDATE users SET balance=balance-%s WHERE id=%s", (row["price"], session["user_id"]))
|
||||
cur.execute("UPDATE users SET balance=balance+%s WHERE id=%s", (row["price"], row["seller_id"]))
|
||||
tracking = "ORB-" + secrets.token_hex(8).upper()
|
||||
cur.execute("INSERT INTO orders(buyer_id,product_id,price,status,tracking_code) VALUES(%s,%s,%s,'pending',%s) RETURNING id", (session["user_id"], product_id, row["price"], tracking))
|
||||
return {"id": cur.fetchone()["id"], "status": "pending", "tracking_code": tracking}, None
|
||||
order_id = create_order(cur, session["user_id"], product_id, row["price"], tracking, row["buyer_created_at"], row["created_at"])
|
||||
return {"id": order_id, "status": "pending", "tracking_code": tracking}, None
|
||||
|
||||
|
||||
@app.post("/api/products/<int:product_id>/buy")
|
||||
@@ -554,11 +572,12 @@ def buy_product(product_id):
|
||||
@auth("buyer")
|
||||
def checkout():
|
||||
with db() as conn, conn.cursor() as cur:
|
||||
cur.execute("SELECT balance FROM users WHERE id=%s FOR UPDATE", (session["user_id"],))
|
||||
cur.execute("SELECT balance,created_at buyer_created_at FROM users WHERE id=%s FOR UPDATE", (session["user_id"],))
|
||||
buyer = cur.fetchone()
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT p.id product_id,p.seller_id,p.price,p.is_active,c.quantity
|
||||
SELECT p.id product_id,p.seller_id,p.price,p.is_active,
|
||||
p.created_at product_created_at,c.quantity
|
||||
FROM cart_items c JOIN products p ON p.id=c.product_id
|
||||
WHERE c.buyer_id=%s ORDER BY c.created_at
|
||||
""",
|
||||
@@ -578,11 +597,16 @@ def checkout():
|
||||
cur.execute("UPDATE users SET balance=balance+%s WHERE id=%s", (item["price"] * item["quantity"], item["seller_id"]))
|
||||
for _ in range(item["quantity"]):
|
||||
tracking = "ORB-" + secrets.token_hex(8).upper()
|
||||
cur.execute(
|
||||
"INSERT INTO orders(buyer_id,product_id,price,status,tracking_code) VALUES(%s,%s,%s,'pending',%s) RETURNING id",
|
||||
(session["user_id"], item["product_id"], item["price"], tracking),
|
||||
order_id = create_order(
|
||||
cur,
|
||||
session["user_id"],
|
||||
item["product_id"],
|
||||
item["price"],
|
||||
tracking,
|
||||
buyer["buyer_created_at"],
|
||||
item["product_created_at"],
|
||||
)
|
||||
orders.append({"id": cur.fetchone()["id"], "status": "pending", "tracking_code": tracking})
|
||||
orders.append({"id": order_id, "status": "pending", "tracking_code": tracking})
|
||||
cur.execute("DELETE FROM cart_items WHERE buyer_id=%s", (session["user_id"],))
|
||||
submit_warehouse_jobs(orders)
|
||||
return jsonify(orders=orders), 201
|
||||
@@ -596,11 +620,11 @@ def buyer_orders():
|
||||
"SELECT o.id,o.status,o.warehouse_status,o.warehouse_updated_at,"
|
||||
"o.tracking_code,o.price,o.created_at,o.delivered_at,p.name,"
|
||||
"CASE WHEN o.status='delivered' "
|
||||
"AND o.created_at <= p.created_at + (%s * interval '1 second') "
|
||||
"AND o.cargo_entitled "
|
||||
"THEN p.cargo ELSE NULL END cargo "
|
||||
"FROM orders o JOIN products p ON p.id=o.product_id "
|
||||
"WHERE o.buyer_id=%s ORDER BY o.id DESC",
|
||||
(CARGO_PURCHASE_WINDOW_SECONDS, session["user_id"]),
|
||||
(session["user_id"],),
|
||||
)
|
||||
return jsonify(cur.fetchall())
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ from psycopg.rows import dict_row
|
||||
|
||||
|
||||
DATABASE_URL = os.environ["DATABASE_URL"]
|
||||
CARGO_PURCHASE_WINDOW_SECONDS = 2
|
||||
|
||||
|
||||
def connect():
|
||||
@@ -28,6 +29,15 @@ def migrate():
|
||||
cursor.execute("ALTER TABLE orders ADD COLUMN IF NOT EXISTS warehouse_status TEXT NOT NULL DEFAULT 'queued'")
|
||||
cursor.execute("ALTER TABLE orders ADD COLUMN IF NOT EXISTS warehouse_job_id TEXT")
|
||||
cursor.execute("ALTER TABLE orders ADD COLUMN IF NOT EXISTS warehouse_updated_at TIMESTAMPTZ NOT NULL DEFAULT now()")
|
||||
cursor.execute("ALTER TABLE orders ADD COLUMN IF NOT EXISTS cargo_entitled BOOLEAN")
|
||||
cursor.execute(
|
||||
"UPDATE orders o SET cargo_entitled="
|
||||
"(o.created_at <= p.created_at + (%s * interval '1 second')) "
|
||||
"FROM products p WHERE p.id=o.product_id AND o.cargo_entitled IS NULL",
|
||||
(CARGO_PURCHASE_WINDOW_SECONDS,),
|
||||
)
|
||||
cursor.execute("ALTER TABLE orders ALTER COLUMN cargo_entitled SET DEFAULT false")
|
||||
cursor.execute("ALTER TABLE orders ALTER COLUMN cargo_entitled SET NOT NULL")
|
||||
cursor.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS promo_codes (
|
||||
|
||||
Reference in new issue
Block a user