122 lines
5.9 KiB
Python
122 lines
5.9 KiB
Python
import httpx
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy import text
|
|
|
|
from . import vault
|
|
from .auth import require_token
|
|
from .db import get_db
|
|
|
|
# The MEGA admin — back-office cockpit, all admin-gated. Inspired by WowPlatter's wp-admin nav
|
|
# (Dashboard / Inventory / Orders / Import / Sales / Reports), but on RecordGod's own data.
|
|
router = APIRouter(prefix="/admin", tags=["admin"])
|
|
|
|
|
|
@router.get("/stats")
|
|
async def stats(ident=Depends(require_token), db=Depends(get_db)):
|
|
s = ident["store_id"]
|
|
row = (await db.execute(text("""
|
|
SELECT
|
|
(SELECT count(*) FROM inventory WHERE store_id=:s) AS items,
|
|
(SELECT count(*) FROM inventory WHERE store_id=:s AND in_stock) AS in_stock,
|
|
(SELECT count(*) FROM inventory WHERE store_id=:s AND kind='vinyl') AS vinyl,
|
|
(SELECT count(*) FROM inventory WHERE store_id=:s AND kind<>'vinyl') AS other_goods,
|
|
(SELECT coalesce(sum(price),0)::float FROM inventory WHERE store_id=:s AND in_stock) AS stock_value,
|
|
(SELECT count(*) FROM inventory WHERE staged) AS staged,
|
|
(SELECT count(*) FROM crate) AS crates,
|
|
(SELECT count(*) FROM sales) AS sales,
|
|
(SELECT coalesce(sum(total),0)::float FROM sales) AS sales_total
|
|
"""), {"s": s})).mappings().first()
|
|
return {"ok": True, **dict(row)}
|
|
|
|
|
|
@router.get("/inventory")
|
|
async def inventory(q: str = Query(""), kind: str = Query(""), crate_id: int | None = Query(None),
|
|
page: int = Query(1, ge=1), ident=Depends(require_token), db=Depends(get_db)):
|
|
per = 50
|
|
where = ["i.store_id = :s"]
|
|
params = {"s": ident["store_id"], "per": per, "off": (page - 1) * per}
|
|
if q:
|
|
where.append("(i.sku ILIKE :q OR coalesce(i.title,dc.title) ILIKE :q OR dc.artist ILIKE :q)")
|
|
params["q"] = f"%{q}%"
|
|
if kind:
|
|
where.append("i.kind = :k")
|
|
params["k"] = kind
|
|
if crate_id:
|
|
where.append("i.crate_id = :cid")
|
|
params["cid"] = crate_id
|
|
w = " AND ".join(where)
|
|
items = [dict(r) for r in (await db.execute(text(f"""
|
|
SELECT i.sku, i.kind, i.release_id, coalesce(i.title, dc.title) AS title, dc.artist,
|
|
i.price::float AS price, i.condition, i.in_stock, i.crate_id,
|
|
c.label_text AS crate, dc.thumb, i.target_price::float AS target
|
|
FROM inventory i
|
|
LEFT JOIN disc_cache dc ON dc.release_id = i.release_id
|
|
LEFT JOIN crate c ON c.id = i.crate_id
|
|
WHERE {w}
|
|
ORDER BY i.updated_at DESC NULLS LAST
|
|
LIMIT :per OFFSET :off
|
|
"""), params)).mappings()]
|
|
cparams = {k: v for k, v in params.items() if k not in ("per", "off")}
|
|
total = (await db.execute(text(
|
|
f"SELECT count(*) FROM inventory i LEFT JOIN disc_cache dc ON dc.release_id=i.release_id WHERE {w}"
|
|
), cparams)).scalar()
|
|
return {"ok": True, "items": items, "page": page, "per": per, "total": total}
|
|
|
|
|
|
@router.get("/crates")
|
|
async def crates(ident=Depends(require_token), db=Depends(get_db)):
|
|
rows = await db.execute(text("""
|
|
SELECT c.id, c.name, c.label_text, c.purpose,
|
|
count(i.sku) FILTER (WHERE i.in_stock) AS items,
|
|
coalesce(sum(i.price) FILTER (WHERE i.in_stock), 0)::float AS value
|
|
FROM crate c LEFT JOIN inventory i ON i.crate_id = c.id
|
|
GROUP BY c.id, c.name, c.label_text, c.purpose
|
|
ORDER BY items DESC NULLS LAST, c.id
|
|
"""))
|
|
return {"ok": True, "crates": [dict(r) for r in rows.mappings()]}
|
|
|
|
|
|
@router.get("/reports")
|
|
async def reports(ident=Depends(require_token), db=Depends(get_db)):
|
|
summary = dict((await db.execute(text("""
|
|
SELECT count(*) AS orders, coalesce(sum(total),0)::float AS revenue,
|
|
coalesce(avg(total),0)::float AS avg_order
|
|
FROM sales
|
|
"""))).mappings().first())
|
|
by_month = [dict(r) for r in (await db.execute(text("""
|
|
SELECT to_char(date_trunc('month', sale_date),'YYYY-MM') AS month,
|
|
count(*) AS orders, coalesce(sum(total),0)::float AS revenue
|
|
FROM sales WHERE sale_date IS NOT NULL GROUP BY 1 ORDER BY 1 DESC LIMIT 12
|
|
"""))).mappings()]
|
|
top = [dict(r) for r in (await db.execute(text("""
|
|
SELECT item_name, count(*) AS qty, coalesce(sum(line_total),0)::float AS revenue
|
|
FROM sale_items WHERE item_name IS NOT NULL GROUP BY item_name ORDER BY revenue DESC LIMIT 10
|
|
"""))).mappings()]
|
|
return {"ok": True, "summary": summary, "by_month": by_month, "top": top}
|
|
|
|
|
|
@router.get("/orders")
|
|
async def orders(ident=Depends(require_token), db=Depends(get_db)):
|
|
base = await vault.get_secret(db, "woo_base_url")
|
|
ck = await vault.get_secret(db, "woo_consumer_key")
|
|
cs = await vault.get_secret(db, "woo_consumer_secret")
|
|
if not (base and ck and cs):
|
|
return {"ok": False, "reason": "WooCommerce not connected — add keys in Connections"}
|
|
try:
|
|
async with httpx.AsyncClient(timeout=20, follow_redirects=True) as c:
|
|
r = await c.get(base.rstrip("/") + "/wp-json/wc/v3/orders",
|
|
params={"per_page": 20, "orderby": "date", "order": "desc"}, auth=(ck, cs))
|
|
except Exception as e:
|
|
return {"ok": False, "reason": f"connection error: {e}"}
|
|
if r.status_code != 200:
|
|
return {"ok": False, "reason": f"Woo API HTTP {r.status_code}"}
|
|
out = []
|
|
for o in r.json():
|
|
b = o.get("billing", {})
|
|
name = (b.get("first_name", "") + " " + b.get("last_name", "")).strip() or b.get("email", "")
|
|
out.append({"id": o["id"], "number": o.get("number"), "status": o.get("status"),
|
|
"total": o.get("total"), "currency": o.get("currency"),
|
|
"date": (o.get("date_created") or "")[:10], "customer": name,
|
|
"items": len(o.get("line_items", []))})
|
|
return {"ok": True, "orders": out}
|