From 4c7e4f9769af806aa046419c1266d201ed2186c0 Mon Sep 17 00:00:00 2001 From: chendelian <116870791@qq.com> Date: Mon, 11 May 2026 11:25:52 +0800 Subject: [PATCH] feat(db): add get_active_lease_by_token helper to accounts_repo Co-authored-by: Cursor --- scripts/db/accounts_repo.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scripts/db/accounts_repo.py b/scripts/db/accounts_repo.py index b6d171e..84c90be 100644 --- a/scripts/db/accounts_repo.py +++ b/scripts/db/accounts_repo.py @@ -733,6 +733,36 @@ def cleanup_expired_leases() -> int: conn.close() +def get_active_lease_by_token(conn, lease_token: str) -> Optional[dict[str, Any]]: + """Look up a lease by token: active and not expired. + + Returns dict with id, account_id, lease_token, holder, purpose, expires_at (Unix sec), + status; or None if not found / inactive / expired. + """ + cur = conn.cursor() + now = _now_unix() + cur.execute( + """ + SELECT id, account_id, lease_token, holder, purpose, expires_at, status + FROM account_leases + WHERE lease_token = ? AND status = 'active' AND expires_at > ? + """, + (lease_token, now), + ) + row = cur.fetchone() + if not row: + return None + return { + "id": row[0], + "account_id": row[1], + "lease_token": row[2], + "holder": row[3], + "purpose": row[4], + "expires_at": int(row[5]), + "status": row[6], + } + + # ============================================================================ # Internal helpers # ============================================================================