Files
ServerKit/backend/app/api/search.py
T
Juan DenisandClaude Opus 4.8 2dfe0d7d54 feat(search): unified entity omnisearch endpoint for the palette
GET /api/v1/search?q= (JWT, 2-char minimum) fans a term out across the core
entity types — services, servers, domains, databases, WordPress sites, cron
jobs (admin-only), installed extensions, and vaults (names only, never secret
values) — reusing each domain's existing access helpers so a member only sees
what their grants allow. Per-type cap of 5; each entity block is isolated so one
failing type degrades gracefully instead of 500-ing the search.

Wires the frontend api.search() async provider (200ms debounce) into the
palette's entity group, replacing the fetch-everything-on-open prefetch. Adds
tests/test_search.py covering shape, min-length, cap, member-vs-admin filtering,
cron admin-only, and the vaults names-only rule.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 00:26:40 -04:00

29 lines
1.1 KiB
Python

"""Unified entity omnisearch API (plan 41, Phase 4).
A single authz-aware endpoint the command palette calls to search across the
core entity types (services, servers, domains, databases, WordPress sites, cron
jobs, extensions, vaults). Business logic lives in SearchService; this blueprint
just validates the term, resolves the user, and shapes the JSON.
"""
from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
from app.models.user import User
from app.services.search_service import SearchService
search_bp = Blueprint('search', __name__)
@search_bp.route('', methods=['GET'])
@jwt_required()
def search():
"""Search entities by name. ?q=<term> (>= 2 chars). Returns {'results': [...]}."""
q = request.args.get('q', '').strip()
if len(q) < 2:
return jsonify({'results': []}), 200
user = User.query.get(get_jwt_identity())
workspace = request.headers.get('X-Workspace-Id') or request.args.get('workspace_id')
rows = SearchService.search(user, q, workspace)
return jsonify({'results': rows}), 200