feat: add visibility filter to unbound permits API

This commit is contained in:
Codex Agent 2025-12-29 16:59:26 +08:00
parent c55170208b
commit fbc696b61c
2 changed files with 21 additions and 6 deletions

View File

@ -141,7 +141,8 @@ def lawrisk_regions():
def lawrisk_unbound_permits(): def lawrisk_unbound_permits():
"""Get list of permits that are not bound to any theme.""" """Get list of permits that are not bound to any theme."""
try: try:
permits = list_unbound_permits() visibility = request.args.get("visibility") or "visible"
permits = list_unbound_permits(visibility=visibility)
return jsonify({"success": True, "data": permits}) return jsonify({"success": True, "data": permits})
except Exception as exc: except Exception as exc:
print(f"lawrisk_unbound_permits error: {exc}") print(f"lawrisk_unbound_permits error: {exc}")

View File

@ -2589,23 +2589,37 @@ def list_all_themes() -> List[Dict[str, Any]]:
return items return items
def list_unbound_permits() -> List[Dict[str, Any]]: 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.""" """Return all permits that are in a region but not bound to any theme in that region.
sql = """
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 SELECT
r.id AS region_id, r.id AS region_id,
r.name AS region_name, r.name AS region_name,
p.id AS permit_id, p.id AS permit_id,
p.name AS permit_name, p.name AS permit_name,
rpd.unit_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 FROM region_permit_details rpd
JOIN regions r ON r.id = rpd.region_id JOIN regions r ON r.id = rpd.region_id
JOIN permits p ON p.id = rpd.permit_id JOIN permits p ON p.id = rpd.permit_id
LEFT JOIN region_theme_permits rtp LEFT JOIN region_theme_permits rtp
ON rtp.region_id = rpd.region_id ON rtp.region_id = rpd.region_id
AND rtp.permit_id = rpd.permit_id AND rtp.permit_id = rpd.permit_id
WHERE rtp.theme_id IS NULL WHERE {where_clause}
ORDER BY r.name, p.name ORDER BY r.name, p.name
""" """
items: List[Dict[str, Any]] = [] items: List[Dict[str, Any]] = []