From c07f40b28ff22d2ffbd0ba5ebde4bec843363080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IT=E7=8B=97?= Date: Tue, 21 Jul 2026 12:49:07 +0800 Subject: [PATCH] Add /api/admin/reset/{section} endpoint for Danger Zone reset --- backend/main.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/main.py b/backend/main.py index 304fabb..035cf04 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1928,6 +1928,28 @@ async def restore_backup( shutil.copy2(fp, src) return {"restored": filename, "auto_backup": auto_dst.name if src.exists() else None} + +@app.post("/api/admin/reset/{section}") +async def reset_section( + section: str, + current_user: User = Depends(get_current_admin), +): + """Reset (truncate) a specific data section. Creates auto-backup first.""" + valid = {"attendance": AttendanceRecord, "holidays": Holiday, "leaves": LeaveRecord} + if section not in valid: + raise HTTPException(status_code=400, detail=f"Unknown section: {section}") + # Auto-backup before reset + src = Path(DB_PATH) + if src.exists(): + auto_dst = _backup_path(f"auto_pre_{section}_reset") + shutil.copy2(src, auto_dst) + # Truncate table + model = valid[section] + db.execute(model.__table__.delete()) + db.commit() + return {"reset": section, "auto_backup": auto_dst.name} + + @app.get("/api/version") async def get_version( current_user: User = Depends(get_current_user),