From fbc696b61c38cade813fe84c5f67c167a1572462 Mon Sep 17 00:00:00 2001 From: Codex Agent Date: Mon, 29 Dec 2025 16:59:26 +0800 Subject: [PATCH] feat: add visibility filter to unbound permits API --- lawrisk/api/v2.py | 3 ++- lawrisk/services/licensing_repo.py | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lawrisk/api/v2.py b/lawrisk/api/v2.py index 52e6695..e5f4ea7 100644 --- a/lawrisk/api/v2.py +++ b/lawrisk/api/v2.py @@ -141,7 +141,8 @@ def lawrisk_regions(): def lawrisk_unbound_permits(): """Get list of permits that are not bound to any theme.""" try: - permits = list_unbound_permits() + visibility = request.args.get("visibility") or "visible" + permits = list_unbound_permits(visibility=visibility) return jsonify({"success": True, "data": permits}) except Exception as exc: print(f"lawrisk_unbound_permits error: {exc}") diff --git a/lawrisk/services/licensing_repo.py b/lawrisk/services/licensing_repo.py index 9a5a001..0988004 100644 --- a/lawrisk/services/licensing_repo.py +++ b/lawrisk/services/licensing_repo.py @@ -2589,23 +2589,37 @@ def list_all_themes() -> List[Dict[str, Any]]: return items -def list_unbound_permits() -> List[Dict[str, Any]]: - """Return all permits that are in a region but not bound to any theme in that region.""" - sql = """ +def list_unbound_permits(visibility: Optional[str] = None) -> List[Dict[str, Any]]: + """Return all permits that are in a region but not bound to any theme in that region. + + Args: + visibility: 'visible', 'hidden', or None (all) + """ + filters = ["rtp.theme_id IS NULL"] + + if visibility == 'visible': + filters.append("(rpd.is_v2_visible IS TRUE OR rpd.is_v2_visible IS NULL)") + elif visibility == 'hidden': + filters.append("rpd.is_v2_visible IS FALSE") + + where_clause = " AND ".join(filters) + + sql = f""" SELECT r.id AS region_id, r.name AS region_name, p.id AS permit_id, p.name AS permit_name, rpd.unit_name, - rpd.updated_at + rpd.updated_at, + COALESCE(rpd.is_v2_visible, true) AS is_v2_visible FROM region_permit_details rpd JOIN regions r ON r.id = rpd.region_id JOIN permits p ON p.id = rpd.permit_id LEFT JOIN region_theme_permits rtp ON rtp.region_id = rpd.region_id AND rtp.permit_id = rpd.permit_id - WHERE rtp.theme_id IS NULL + WHERE {where_clause} ORDER BY r.name, p.name """ items: List[Dict[str, Any]] = []