add archive column + endpoint for orders

This commit is contained in:
root
2026-06-20 18:11:18 +00:00
parent 044ab67073
commit 8da70dbdb8
+35 -5
View File
@@ -110,6 +110,11 @@ def init_schema(db):
db.execute("ALTER TABLE submissions ADD COLUMN status TEXT NOT NULL DEFAULT 'received'")
except Exception:
pass
# Migration: add archived column if missing
try:
db.execute("ALTER TABLE submissions ADD COLUMN archived INTEGER NOT NULL DEFAULT 0")
except Exception:
pass
def get_setting(db, key, default=""):
row = db.execute("SELECT value FROM settings WHERE key = ?", (key,)).fetchone()
@@ -403,6 +408,23 @@ def update_status(body: StatusUpdate, request: Request):
db.close()
return {"ok": True}
# ---- Admin: Archive / Unarchive ----
@app.post("/admin/archive", status_code=200)
def archive_order(body: dict, request: Request):
check_auth(request)
submission_id = body.get("submission_id")
archived = body.get("archived", True)
if not submission_id:
raise HTTPException(400, "submission_id is required")
db = get_db()
db.execute("UPDATE submissions SET archived = ? WHERE id = ?", (1 if archived else 0, submission_id))
db.commit()
db.close()
return {"ok": True, "id": submission_id, "archived": archived}
# ---- Admin: Add Note ----
class NoteBody(BaseModel):
@@ -437,18 +459,25 @@ def admin_submissions(
request: Request,
form_id: Optional[int] = None,
page: int = 1,
per_page: int = 50
per_page: int = 50,
include_archived: bool = False
):
check_auth(request)
db = get_db()
offset = (page - 1) * per_page
query = "SELECT s.*, f.name as form_name FROM submissions s JOIN forms f ON s.form_id = f.id"
count_query = "SELECT COUNT(*) as c FROM submissions"
count_query = "SELECT COUNT(*) as c FROM submissions s"
params = []
wheres = []
if not include_archived:
wheres.append("s.archived IS NULL OR s.archived = 0")
if form_id:
query += " WHERE s.form_id = ?"
count_query += " WHERE form_id = ?"
wheres.append("s.form_id = ?")
params.append(form_id)
if wheres:
where = " WHERE " + " AND ".join(wheres)
query += where
count_query += where
query += " ORDER BY s.id DESC LIMIT ? OFFSET ?"
total = db.execute(count_query, params).fetchone()["c"]
rows = db.execute(query, params + [per_page, offset]).fetchall()
@@ -468,7 +497,8 @@ def admin_submissions(
"created_at": r["created_at"], "status": r["status"],
"data": [dict(d) for d in data],
"tags": [t["tag"] for t in tags],
"reply": dict(reply) if reply else None
"reply": dict(reply) if reply else None,
"archived": bool(r["archived"]) if "archived" in r.keys() else False
})
db.close()
return {